22 #ifndef general_matrix_h 23 #define general_matrix_h 44 static constexpr
bool isVector {
false};
46 typedef AT value_type;
62 Iterator(std::conditional_t<Const, const Mat, Mat> &mat_,
int row_,
int col_) : mat(mat_), row(row_), col(col_) {}
63 bool operator!=(
const Iterator& x)
const {
return row!=x.row || col!=x.col; }
64 Iterator& operator++() { ++row;
if(row>=mat.m) { row=0; ++col; }
return *
this; }
65 std::conditional_t<Const, const AT, AT>& operator*() {
return mat.e(row,col); }
66 std::conditional_t<Const, const AT, AT>& operator->() {
return mat.e(row,col); }
67 const AT& operator*()
const {
return mat.e(row,col); }
68 const AT& operator->()
const {
return mat.e(row,col); }
70 std::conditional_t<Const, const Mat, Mat> &mat;
83 const AT* elePtr(
int i,
int j)
const {
87 AT* elePtr(
int i,
int j) {
99 explicit Matrix() : memory(), ele(nullptr) { }
107 explicit Matrix(
int m_,
int n_,
Noinit) : memory(m_*n_), ele((AT*)memory.get()), m(m_), n(n_), lda(m_) { }
108 explicit Matrix(
int m_,
int n_, Init ini=INIT,
const AT &a=AT()) : memory(m_*n_), ele((AT*)memory.get()), m(m_), n(n_), lda(m_) { init0(a); }
109 explicit Matrix(
int m_,
int n_, Eye ini,
const AT &a=1) : memory(m_*n_), ele((AT*)memory.get()), m(m_), n(n_), lda(m_) { init(ini,a); }
125 template<
class Row,
class Col>
135 template<
class Type,
class Row,
class Col>
147 explicit Matrix(
int m_,
int n_, AT* ele_) : memory(), ele(ele_), m(m_), n(n_), lda(m_) {
162 Matrix(
const std::string &strs);
169 typedef Iterator<false> iterator;
170 typedef Iterator<true> const_iterator;
171 iterator begin() {
return iterator(*
this,0,0); }
172 iterator end() {
return iterator(*
this,0,n); }
173 const_iterator begin()
const {
return const_iterator(*
this,0,0); }
174 const_iterator end()
const {
return const_iterator(*
this,0,n); }
176 Matrix<General,Ref,Ref,AT>& resize(
int m_,
int n_, Noinit) {
177 m = m_; n = n_; lda = m;
179 ele = (AT*)memory.get();
183 Matrix<General,Ref,Ref,AT>& resize(
int m,
int n, Init ini=INIT,
const AT &a=AT()) {
return resize(m,n,Noinit()).init0(a); }
185 Matrix<General,Ref,Ref,AT>& resize(
int m,
int n, Eye ini,
const AT &a=1) {
return resize(m,n,Noinit()).init(ini,a); }
194 FMATVEC_ASSERT(m == A.
rows(), AT);
195 FMATVEC_ASSERT(n == A.
cols(), AT);
205 template<
class Type,
class Row,
class Col>
207 FMATVEC_ASSERT(m == A.
rows(), AT);
208 FMATVEC_ASSERT(n == A.
cols(), AT);
233 template<
class Type,
class Row,
class Col>
242 for(
size_t r=0; r<
rows(); ++r)
243 for(
size_t c=0; c<
cols(); ++c)
244 ret(r,c) = (*this)(r,c);
258 FMATVEC_ASSERT(i>=0, AT);
259 FMATVEC_ASSERT(j>=0, AT);
260 FMATVEC_ASSERT(i<m, AT);
261 FMATVEC_ASSERT(j<n, AT);
271 FMATVEC_ASSERT(i>=0, AT);
272 FMATVEC_ASSERT(j>=0, AT);
273 FMATVEC_ASSERT(i<m, AT);
274 FMATVEC_ASSERT(j<n, AT);
279 AT& e(
int i,
int j) {
283 const AT& e(
int i,
int j)
const {
336 return CblasColMajor;
393 inline Matrix<General,Ref,Ref,AT>& init(Eye,
const AT &val=1);
394 inline Matrix<General,Ref,Ref,AT>& init(Noinit,
const AT &a=AT()) {
return *
this; }
395 inline Matrix<General,Ref,Ref,AT>& init0(
const AT &val=AT());
396 inline Matrix<General,Ref,Ref,AT>& init0(Init,
const AT &a=AT()) {
return init0(a); }
402 explicit inline operator std::vector<std::vector<AT>>()
const;
409 explicit inline Matrix(
const std::vector<std::vector<AT>> &m);
427 const Matrix<General,Ref,Ref,AT> T()
const;
429 template<
class Row>
inline void set(
int j,
const Vector<Row,AT> &x);
431 template<
class Col>
inline void set(
int i,
const RowVector<Col,AT> &x);
435 template<
class Row>
inline void add(
int j,
const Vector<Row,AT> &x);
437 template<
class Col>
inline void add(
int i,
const RowVector<Col,AT> &x);
453 int countElements()
const;
458 std::istringstream iss(strs);
464 throw std::runtime_error(
"Input not fully read.");
469 inline Matrix<General,Ref,Ref,AT>& Matrix<General,Ref,Ref,AT>::init0(
const AT &val) {
470 for(
int i=0; i<m*n; i++) ele[i]=val;
476 for(
int i=0; i<
rows(); i++)
477 for(
int j=0; j<
cols(); j++)
484 for(
int i=0; i<m; i++)
485 for(
int j=0; j<n; j++)
486 e(i,j) = (i==j) ? val : 0;
492 FMATVEC_ASSERT(I.
end()<m, AT);
493 FMATVEC_ASSERT(J.
end()<n, AT);
496 for(
int i=0; i<A.
rows(); i++)
497 for(
int j=0; j<A.
cols(); j++)
505 FMATVEC_ASSERT(I.
end()<m, AT);
508 for(
int i=0; i<A.
rows(); i++)
509 for(
int j=0; j<A.
cols(); j++)
518 FMATVEC_ASSERT(i>=0, AT);
519 FMATVEC_ASSERT(i<m, AT);
523 for(
int j=0; j<n; j++)
532 FMATVEC_ASSERT(j>=0, AT);
533 FMATVEC_ASSERT(j<n, AT);
537 for(
int i=0; i<m; i++)
545 std::vector<std::vector<AT>> ret(rows());
546 for(
int r=0; r<rows(); r++) {
547 ret[r].resize(cols());
548 for(
int c=0; c<cols(); c++)
555 inline Matrix<General,Ref,Ref,AT>::Matrix(
const std::vector<std::vector<AT>> &m) : memory(m.size()*m[0].size()), ele((AT*)memory.get()), m(static_cast<int>(m.size())), n(static_cast<int>(m[0].size())), lda(static_cast<int>(m.size())) {
556 for(
int r=0; r<
rows(); r++) {
557 if(static_cast<int>(m[r].size())!=
cols())
558 throw std::runtime_error(
"The rows of the input have different length.");
559 for(
int c=0; c<
cols(); c++)
566 template <
class AT>
template <
class Type,
class Row,
class Col>
568 for(
int i=0; i<m; i++)
569 for(
int j=0; j<n; j++)
576 Matrix<General,Ref,Ref,AT> A(n,m,NONINIT);
577 for(
int i=0; i<n; i++)
578 for(
int j=0; j<m; j++)
583 template <
class AT>
template <
class Row>
584 inline void Matrix<General,Ref,Ref,AT>::set(
int j,
const Vector<Row,AT> &x) {
585 FMATVEC_ASSERT(j<
cols(), AT);
586 FMATVEC_ASSERT(
rows()==x.size(), AT);
587 for(
int i=0; i<
rows(); i++)
591 template <
class AT>
template <
class Col>
592 inline void Matrix<General,Ref,Ref,AT>::set(
int i,
const RowVector<Col,AT> &x) {
593 FMATVEC_ASSERT(i<
rows(), AT);
594 FMATVEC_ASSERT(
cols()==x.size(), AT);
595 for(
int j=0; j<
cols(); j++)
599 template <
class AT>
template<
class Type,
class Row,
class Col>
600 inline void Matrix<General,Ref,Ref,AT>::set(
const fmatvec::Range<Var,Var> &I,
const fmatvec::Range<Var,Var> &J,
const Matrix<Type,Row,Col,AT> &A) {
602 FMATVEC_ASSERT(I.
end()<
rows(), AT);
603 FMATVEC_ASSERT(J.
end()<
cols(), AT);
604 FMATVEC_ASSERT(I.
size()==A.rows(), AT);
605 FMATVEC_ASSERT(J.
size()==A.cols(), AT);
607 for(
int i=I.
start(), ii=0; i<=I.
end(); i++, ii++)
608 for(
int j=J.
start(), jj=0; j<=J.
end(); j++, jj++)
612 template <
class AT>
template <
class Row>
613 inline void Matrix<General,Ref,Ref,AT>::add(
int j,
const Vector<Row,AT> &x) {
614 FMATVEC_ASSERT(j<
cols(), AT);
615 FMATVEC_ASSERT(
rows()==x.size(), AT);
616 for(
int i=0; i<
rows(); i++)
620 template <
class AT>
template <
class Col>
621 inline void Matrix<General,Ref,Ref,AT>::add(
int i,
const RowVector<Col,AT> &x) {
622 FMATVEC_ASSERT(i<
rows(), AT);
623 FMATVEC_ASSERT(
cols()==x.size(), AT);
624 for(
int j=0; j<
cols(); j++)
628 template <
class AT>
template<
class Type,
class Row,
class Col>
629 inline void Matrix<General,Ref,Ref,AT>::add(
const fmatvec::Range<Var,Var> &I,
const fmatvec::Range<Var,Var> &J,
const Matrix<Type,Row,Col,AT> &A) {
631 FMATVEC_ASSERT(I.
end()<
rows(), AT);
632 FMATVEC_ASSERT(J.
end()<
cols(), AT);
633 FMATVEC_ASSERT(I.
size()==A.rows(), AT);
634 FMATVEC_ASSERT(J.
size()==A.cols(), AT);
636 for(
int i=I.
start(), ii=0; i<=I.
end(); i++, ii++)
637 for(
int j=J.
start(), jj=0; j<=J.
end(); j++, jj++)
638 e(i,j) += A.e(ii,jj);
641 template <
class AT>
template<
class Type,
class Row,
class Col>
642 inline void Matrix<General,Ref,Ref,AT>::sub(
const fmatvec::Range<Var,Var> &I,
const fmatvec::Range<Var,Var> &J,
const Matrix<Type,Row,Col,AT> &A) {
644 FMATVEC_ASSERT(I.
end()<
rows(), AT);
645 FMATVEC_ASSERT(J.
end()<
cols(), AT);
646 FMATVEC_ASSERT(I.
size()==A.rows(), AT);
647 FMATVEC_ASSERT(J.
size()==A.cols(), AT);
649 for(
int i=I.
start(), ii=0; i<=I.
end(); i++, ii++)
650 for(
int j=J.
start(), jj=0; j<=J.
end(); j++, jj++)
651 e(i,j) -= A.e(ii,jj);
655 inline void Matrix<General,Ref,Ref,AT>::ref(Matrix<General,Ref,Ref,AT> &A,
const fmatvec::Range<Var,Var> &I,
const fmatvec::Range<Var,Var> &J) {
656 FMATVEC_ASSERT(I.
end()<A.rows(), AT);
657 FMATVEC_ASSERT(J.
end()<A.cols(), AT);
666 inline void Matrix<General,Ref,Ref,AT>::ref(Matrix<Symmetric,Ref,Ref,AT> &A,
const fmatvec::Range<Var,Var> &I,
const fmatvec::Range<Var,Var> &J) {
667 FMATVEC_ASSERT(I.
end()<A.size(), AT);
668 FMATVEC_ASSERT(J.
end()<A.size(), AT);
670 FMATVEC_ASSERT(J.
end()<=I.
start(), AT);
681 for (
int i = 0; i < m; i++) {
682 for (
int j = 0; j < n; j++) {
683 if (fabs(e(i, j)) > 1e-16 || i==j) {
int end() const
Last element.
Definition: range.h:91
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: general_matrix.h:335
Matrix(const Matrix< General, Row, Col, AT > &A)
Copy Constructor.
Definition: general_matrix.h:126
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
Shape class for symmetric matrices.
Definition: types.h:148
int rows() const
Number of rows.
Definition: general_matrix.h:304
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:84
AT & operator()(int i, int j)
Element operator.
Definition: general_matrix.h:257
Matrix(const Matrix< General, Ref, Ref, AT > &A)
Copy Constructor.
Definition: general_matrix.h:116
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: general_matrix.h:324
const Matrix< General, Ref, Ref, AT > T() const
Cast to AT.
This is a rowvector class of general shape in dense storage format.
Definition: row_vector.h:36
int start() const
First element.
Definition: range.h:85
int countElements() const
Count nonzero elements.
Matrix< General, Ref, Ref, double > Mat
General matrix.
Definition: fmatvec.h:50
const AT * operator()() const
Pointer operator.
Definition: general_matrix.h:298
int cols() const
Number of columns.
This is a matrix class for general matrices.
Definition: general_matrix.h:41
Namespace fmatvec.
Definition: _memory.cc:28
int cols() const
Number of columns.
Definition: general_matrix.h:310
bool operator!=(const Matrix< Type1, Row1, Col1, AT > &A, const Matrix< Type2, Row2, Col2, AT > &B)
Matrix-matrix comparison.
Definition: linear_algebra.h:1534
int size() const
Size.
Definition: range.h:97
Matrix< General, Ref, Ref, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: general_matrix.h:206
AT * operator()()
Pointer operator.
Definition: general_matrix.h:292
Matrix< General, Ref, Ref, AT > & operator&=(Matrix< General, Ref, Ref, AT > &A)
Reference operator.
Definition: general_matrix.h:218
int ldim() const
Leading dimension.
Definition: general_matrix.h:316
Matrix(int m_, int n_, AT *ele_)
Regular Constructor.
Definition: general_matrix.h:147
Basic shape class for matrices.
Definition: types.h:132
const AT & operator()(int i, int j) const
Element operator.
Definition: general_matrix.h:270
This is a vector class of general shape in dense storage format.
Definition: vector.h:39
This is a matrix class of general quadratic matrices.
Definition: square_matrix.h:39
Matrix()
Standard constructor.
Definition: general_matrix.h:99
Matrix< General, Ref, Ref, AT > & operator=(const Matrix< General, Ref, Ref, AT > &A)
Assignment operator.
Definition: general_matrix.h:193
int rows() const
Number of rows.
Matrix(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: general_matrix.h:136
This is an index class for creating submatrices.
Definition: range.h:44