22 #ifndef var_general_matrix_h 23 #define var_general_matrix_h 44 static constexpr
bool isVector {
false};
45 using value_type = AT;
75 explicit Matrix(
int m,
int n,
Noinit) : M(m), N(n), ele(new AT[M*N]) { }
76 explicit Matrix(
int m,
int n, Init ini=INIT,
const AT &a=AT()) : M(m), N(n), ele(new AT[M*N]) { init(a); }
77 explicit Matrix(
int m,
int n, Eye ini,
const AT &a=1) : M(m), N(n), ele(new AT[M*N]) { init(ini,a); }
80 Matrix(Matrix<General,Var,Var,AT> &&src) noexcept {
88 Matrix<General,Var,Var,AT>& operator=(Matrix<General,Var,Var,AT> &&src) noexcept {
89 FMATVEC_ASSERT(M == src.rows(), AT);
90 FMATVEC_ASSERT(N == src.cols(), AT);
113 template<
class Row,
class Col>
123 template<
class Type,
class Row,
class Col>
140 Matrix(
const std::string &strs);
149 using iterator = AT *;
150 using const_iterator =
const AT *;
151 iterator begin() {
return &ele[0]; }
152 iterator end() {
return &ele[M*N]; }
153 const_iterator begin()
const {
return &ele[0]; }
154 const_iterator end()
const {
return &ele[M*N]; }
156 Matrix<General,Var,Var,AT>& resize(
int m,
int n, Noinit) {
164 Matrix<General,Var,Var,AT>& resize(
int m,
int n, Init ini=INIT,
const AT &a=AT()) {
return resize(m,n,Noinit()).init(a); }
166 Matrix<General,Var,Var,AT>& resize(
int m,
int n, Eye ini,
const AT &a=1) {
return resize(m,n,Noinit()).init(ini,a); }
175 FMATVEC_ASSERT(M == A.
rows(), AT);
176 FMATVEC_ASSERT(N == A.
cols(), AT);
186 template <
class Type,
class Row,
class Col>
188 FMATVEC_ASSERT(M == A.
rows(), AT);
189 FMATVEC_ASSERT(N == A.
cols(), AT);
199 template<
class Type,
class Row,
class Col>
217 operator Matrix<General,Var,Var,AT2>()
const {
218 Matrix<General,Var,Var,AT2> ret(
rows(),
cols());
219 for(
size_t r=0; r<
rows(); ++r)
220 for(
size_t c=0; c<
cols(); ++c)
221 ret(r,c) = (*this)(r,c);
235 FMATVEC_ASSERT(i>=0, AT);
236 FMATVEC_ASSERT(j>=0, AT);
237 FMATVEC_ASSERT(i<M, AT);
238 FMATVEC_ASSERT(j<N, AT);
248 FMATVEC_ASSERT(i>=0, AT);
249 FMATVEC_ASSERT(j>=0, AT);
250 FMATVEC_ASSERT(i<M, AT);
251 FMATVEC_ASSERT(j<N, AT);
256 AT& e(
int i,
int j) {
264 const AT&
e(
int i,
int j)
const {
276 const AT&
e(
int i)
const {
297 constexpr
int rows()
const {
return M;}
303 constexpr
int cols()
const {
return N;}
329 return CblasRowMajor;
338 template <
int M1,
int M2,
int N1,
int N2>
341 template <
int M1,
int M2>
344 template <
int N1,
int N2>
359 inline Matrix<General,Var,Var,AT>& init(Eye eye,
const AT &val=1);
360 inline Matrix<General,Var,Var,AT>& init(Noinit,
const AT &a=AT()) {
return *
this; }
366 explicit inline operator std::vector<std::vector<AT>>()
const;
373 explicit inline Matrix(
const std::vector<std::vector<AT>> &m);
391 inline const Matrix<General,Var,Var,AT> T()
const;
393 template<
class Row>
inline void set(
int j,
const Vector<Row,AT> &x);
395 template<
class Col>
inline void set(
int i,
const RowVector<Col,AT> &x);
399 template<
class Row>
inline void add(
int j,
const Vector<Row,AT> &x);
401 template<
class Col>
inline void add(
int i,
const RowVector<Col,AT> &x);
405 inline const Matrix<General,Var,Var,AT>
operator()(
const Indices &I,
const Indices &J)
const;
407 template<
class Type,
class Row,
class Col>
inline void set(
const Indices &I,
const Indices &J,
const Matrix<Type,Row,Col,AT> &A);
409 template<
class Row>
inline void set(
const Indices &I,
int j,
const Vector<Row,AT> &x);
413 Matrix<General,Var,Var,AT>::Matrix(
const std::string &strs) : ele(nullptr) {
414 std::istringstream iss(strs);
420 throw std::runtime_error(
"Input not fully read.");
422 template <
class AT> Matrix<General,Var,Var,AT>::Matrix(
const char *strs) : Matrix<General,Var,Var,AT>::Matrix(std::string(strs)) {}
425 inline Matrix<General,Var,Var,AT>& Matrix<General,Var,Var,AT>::init(
const AT &val) {
426 for(
int i=0; i<M*N; i++)
432 inline Matrix<General,Var,Var,AT>& Matrix<General,Var,Var,AT>::init(Eye eye,
const AT &val) {
433 for(
int i=0; i<M; i++)
434 for(
int j=0; j<N; j++)
435 e(i,j) = (i==j) ? val : 0;
441 FMATVEC_ASSERT(I.
end()<M, AT);
442 FMATVEC_ASSERT(J.
end()<N, AT);
443 Matrix<General,Var,Var,AT> A(I.
end()-I.
start()+1,J.
end()-J.
start()+1,NONINIT);
445 for(
int i=0; i<A.
rows(); i++)
446 for(
int j=0; j<A.
cols(); j++)
452 template <
class AT>
template <
int M1,
int M2,
int N1,
int N2>
454 FMATVEC_ASSERT(M2<M, AT);
455 FMATVEC_ASSERT(N2<N, AT);
456 Matrix<General,Fixed<M2-M1+1>,Fixed<N2-N1+1>,AT> A(NONINIT);
458 for(
int i=0; i<A.
rows(); i++)
459 for(
int j=0; j<A.
cols(); j++)
465 template <
class AT>
template <
int M1,
int M2>
467 FMATVEC_ASSERT(M2<M, AT);
468 FMATVEC_ASSERT(J.
end()<N, AT);
469 Matrix<General,Fixed<M2-M1+1>,Var,AT> A(J.
end()-J.
start()+1,NONINIT);
471 for(
int i=0; i<A.
rows(); i++)
472 for(
int j=0; j<A.
cols(); j++)
478 template <
class AT>
template <
int N1,
int N2>
480 FMATVEC_ASSERT(I.
end()<M, AT);
481 FMATVEC_ASSERT(N2<N, AT);
482 Matrix<General,Var,Fixed<N2-N1+1>,AT> A(I.
end()-I.
start()+1,NONINIT);
484 for(
int i=0; i<A.
rows(); i++)
485 for(
int j=0; j<A.
cols(); j++)
492 inline const RowVector<Var,AT> Matrix<General,Var,Var,AT>::row(
int i)
const {
494 FMATVEC_ASSERT(i>=0, AT);
495 FMATVEC_ASSERT(i<M, AT);
497 RowVector<Var,AT> x(N,NONINIT);
499 for(
int j=0; j<N; j++)
506 inline const Vector<Var,AT> Matrix<General,Var,Var,AT>::col(
int j)
const {
508 FMATVEC_ASSERT(j>=0, AT);
509 FMATVEC_ASSERT(j<N, AT);
511 Vector<Var,AT> x(M,NONINIT);
513 for(
int i=0; i<M; i++)
520 inline const Matrix<General,Var,Var,AT> Matrix<General,Var,Var,AT>::T()
const {
521 Matrix<General,Var,Var,AT> A(N,M,NONINIT);
522 for(
int i=0; i<N; i++)
523 for(
int j=0; j<M; j++)
528 template <
class AT>
template <
class Row>
529 inline void Matrix<General,Var,Var,AT>::set(
int j,
const Vector<Row,AT> &x) {
530 FMATVEC_ASSERT(j<cols(), AT);
531 FMATVEC_ASSERT(rows()==x.size(), AT);
532 for(
int i=0; i<rows(); i++)
536 template <
class AT>
template <
class Col>
537 inline void Matrix<General,Var,Var,AT>::set(
int i,
const RowVector<Col,AT> &x) {
538 FMATVEC_ASSERT(i<rows(), AT);
539 FMATVEC_ASSERT(cols()==x.size(), AT);
540 for(
int j=0; j<cols(); j++)
544 template <
class AT>
template<
class Type,
class Row,
class Col>
545 inline void Matrix<General,Var,Var,AT>::set(
const fmatvec::Range<Var,Var> &I,
const fmatvec::Range<Var,Var> &J,
const Matrix<Type,Row,Col,AT> &A) {
547 FMATVEC_ASSERT(I.
end()<rows(), AT);
548 FMATVEC_ASSERT(J.
end()<cols(), AT);
549 FMATVEC_ASSERT(I.
size()==A.rows(), AT);
550 FMATVEC_ASSERT(J.
size()==A.cols(), AT);
552 for(
int i=I.
start(), ii=0; i<=I.
end(); i++, ii++)
553 for(
int j=J.
start(), jj=0; j<=J.
end(); j++, jj++)
557 template <
class AT>
template <
class Row>
558 inline void Matrix<General,Var,Var,AT>::add(
int j,
const Vector<Row,AT> &x) {
559 FMATVEC_ASSERT(j<cols(), AT);
560 FMATVEC_ASSERT(rows()==x.size(), AT);
561 for(
int i=0; i<rows(); i++)
565 template <
class AT>
template <
class Col>
566 inline void Matrix<General,Var,Var,AT>::add(
int i,
const RowVector<Col,AT> &x) {
567 FMATVEC_ASSERT(i<rows(), AT);
568 FMATVEC_ASSERT(cols()==x.size(), AT);
569 for(
int j=0; j<cols(); j++)
573 template <
class AT>
template<
class Type,
class Row,
class Col>
574 inline void Matrix<General,Var,Var,AT>::add(
const fmatvec::Range<Var,Var> &I,
const fmatvec::Range<Var,Var> &J,
const Matrix<Type,Row,Col,AT> &A) {
576 FMATVEC_ASSERT(I.
end()<rows(), AT);
577 FMATVEC_ASSERT(J.
end()<cols(), AT);
578 FMATVEC_ASSERT(I.
size()==A.rows(), AT);
579 FMATVEC_ASSERT(J.
size()==A.cols(), AT);
581 for(
int i=I.
start(), ii=0; i<=I.
end(); i++, ii++)
582 for(
int j=J.
start(), jj=0; j<=J.
end(); j++, jj++)
583 e(i,j) += A.e(ii,jj);
588 FMATVEC_ASSERT(I.max()<rows(), AT);
589 FMATVEC_ASSERT(J.max()<cols(), AT);
591 Matrix<General,Var,Var,AT> A(I.size(),J.size(),NONINIT);
593 for(
int i=0; i<A.rows(); i++)
594 for(
int j=0; j<A.cols(); j++)
595 A.e(i,j) = e(I[i],J[j]);
600 template <
class AT>
template <
class Type,
class Row,
class Col>
601 inline void Matrix<General,Var,Var,AT>::set(
const Indices &I,
const Indices &J,
const Matrix<Type,Row,Col,AT> &A) {
602 FMATVEC_ASSERT(I.max()<rows(), AT);
603 FMATVEC_ASSERT(J.max()<cols(), AT);
604 FMATVEC_ASSERT(I.size()==A.rows(), AT);
605 FMATVEC_ASSERT(J.size()==A.cols(), AT);
606 for(
int i=0; i<I.size(); i++)
607 for(
int j=0; j<J.size(); j++)
608 e(I[i],J[j]) = A.e(i,j);
611 template <
class AT>
template<
class Row>
612 inline void Matrix<General,Var,Var,AT>::set(
const Indices &I,
int j,
const Vector<Row,AT> &x) {
613 FMATVEC_ASSERT(I.max()<rows(), AT);
614 FMATVEC_ASSERT(j<cols(), AT);
615 FMATVEC_ASSERT(I.size()==x.size(), AT);
616 for(
int i=0; i<I.size(); i++)
621 inline Matrix<General,Var,Var,AT>::operator std::vector<std::vector<AT>>()
const {
622 std::vector<std::vector<AT>> ret(rows(),std::vector<AT>(cols()));
623 for(
int r=0; r<rows(); r++) {
624 for(
int c=0; c<cols(); c++)
631 inline Matrix<General,Var,Var,AT>::Matrix(
const std::vector<std::vector<AT>> &m) : M(static_cast<int>(m.size())), N(static_cast<int>(m[0].size())), ele(new AT[M*N]) {
632 for(
int r=0; r<
rows(); r++) {
633 if(static_cast<int>(m[r].size())!=
cols())
634 throw std::runtime_error(
"The rows of the input have different length.");
635 for(
int c=0; c<
cols(); c++)
642 template <
class AT>
template <
class Type,
class Row,
class Col>
643 inline Matrix<General,Var,Var,AT>& Matrix<General,Var,Var,AT>::copy(
const Matrix<Type,Row,Col,AT> &A) {
644 for(
int i=0; i<M; i++)
645 for(
int j=0; j<N; j++)
int end() const
Last element.
Definition: range.h:92
constexpr int cols() const
Number of columns.
Definition: var_general_matrix.h:303
Matrix< General, Var, Var, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: var_general_matrix.h:187
constexpr int rows() const
Number of rows.
Definition: var_general_matrix.h:297
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:42
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:84
const AT & operator()(int i, int j) const
Element operator.
Definition: var_general_matrix.h:247
int ldim() const
Leading dimension.
Definition: var_general_matrix.h:309
Matrix< General, Var, Var, AT > & operator=(const Matrix< General, Var, Var, AT > &A)
Assignment operator.
Definition: var_general_matrix.h:174
Matrix(const Matrix< General, Var, Var, AT > &A)
Copy Constructor.
Definition: var_general_matrix.h:104
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: var_general_matrix.h:317
int start() const
First element.
Definition: range.h:86
AT * operator()()
Pointer operator.
Definition: var_general_matrix.h:285
Matrix(const Matrix< General, Row, Col, AT > &A)
Copy Constructor.
Definition: var_general_matrix.h:114
~Matrix()
Destructor.
Definition: var_general_matrix.h:145
const AT & e(int i) const
Element operator.
Definition: var_general_matrix.h:276
int cols() const
Number of columns.
Matrix(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: var_general_matrix.h:124
This is an index class for creating submatrices.
Definition: range.h:36
Namespace fmatvec.
Definition: _memory.cc:28
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: var_general_matrix.h:328
int size() const
Size.
Definition: range.h:98
Matrix()
Standard constructor.
Definition: var_general_matrix.h:67
const AT & e(int i, int j) const
Element operator.
Definition: var_general_matrix.h:264
Shape class for general matrices.
Definition: types.h:112
AT & operator()(int i, int j)
Element operator.
Definition: var_general_matrix.h:234
const AT * operator()() const
Pointer operator.
Definition: var_general_matrix.h:291
int rows() const
Number of rows.
This is an index class for creating submatrices.
Definition: range.h:45
This is a vector class of general shape in dense storage format.
Definition: var_row_vector.h:39