22 #ifndef fixed_var_general_matrix_h 23 #define fixed_var_general_matrix_h 43 static constexpr
bool isVector {
false};
45 typedef AT value_type;
75 Matrix<General,Fixed<M>,Var,AT>& operator=(Matrix<General,Fixed<M>,Var,AT> &&src) {
76 FMATVEC_ASSERT(N == src.cols(), AT);
93 explicit Matrix(
int n, Noinit) : N(n), ele(new AT[M*N]) { }
94 explicit Matrix(
int n, Init ini=INIT,
const AT &a=AT()) : N(n), ele(new AT[M*N]) { init(a); }
95 explicit Matrix(
int n, Eye ini,
const AT &a=1) : N(n), ele(new AT[M*N]) { init(ini,a); }
96 explicit Matrix(
int m,
int n, Noinit) : N(n), ele(new AT[M*N]) { FMATVEC_ASSERT(m==M, AT); }
97 explicit Matrix(
int m,
int n, Init ini=INIT,
const AT &a=AT()) : N(n), ele(new AT[M*N]) { FMATVEC_ASSERT(m==M, AT); init(a); }
98 explicit Matrix(
int m,
int n, Eye ini,
const AT &a=1) : N(n), ele(new AT[M*N]) { FMATVEC_ASSERT(m==M, AT); init(ini,a); }
114 template<
class Row,
class Col>
116 FMATVEC_ASSERT(A.
rows() == M, AT);
125 template<
class Type,
class Row,
class Col>
127 FMATVEC_ASSERT(A.
rows() == M, AT);
143 Matrix(
const std::string &strs);
146 typedef AT* iterator;
147 typedef const AT* const_iterator;
148 iterator begin() {
return &ele[0]; }
149 iterator end() {
return &ele[M*N]; }
150 const_iterator begin()
const {
return &ele[0]; }
151 const_iterator end()
const {
return &ele[M*N]; }
166 Matrix<General,Fixed<M>,Var,AT>& resize(
int n, Init ini=INIT,
const AT &a=AT()) {
return resize(n,Noinit()).init(a); }
168 Matrix<General,Fixed<M>,Var,AT>& resize(
int n, Eye ini,
const AT &a=1) {
return resize(n,Noinit()).init(ini,a); }
174 throw std::runtime_error(
"A fixed-var matrix can only be resized in the second dimension.");
185 FMATVEC_ASSERT(N == A.
cols(), AT);
195 template <
class Type,
class Row,
class Col>
197 FMATVEC_ASSERT(M == A.
rows(), AT);
198 FMATVEC_ASSERT(N == A.
cols(), AT);
208 template<
class Type,
class Row,
class Col>
210 FMATVEC_ASSERT(M == A.
rows(), AT);
211 if(N!=A.
cols()) resize(A.
cols(),NONINIT);
215 template<
class Type,
class Row,
class Col>
217 FMATVEC_ASSERT(M == src.rows(), AT);
236 FMATVEC_ASSERT(i>=0, AT);
237 FMATVEC_ASSERT(j>=0, AT);
238 FMATVEC_ASSERT(i<M, AT);
239 FMATVEC_ASSERT(j<N, AT);
249 FMATVEC_ASSERT(i>=0, AT);
250 FMATVEC_ASSERT(j>=0, AT);
251 FMATVEC_ASSERT(i<M, AT);
252 FMATVEC_ASSERT(j<N, AT);
257 AT& e(
int i,
int j) {
265 const AT&
e(
int i,
int j)
const {
277 const AT&
e(
int i)
const {
298 constexpr
int rows()
const {
return M;}
304 constexpr
int cols()
const {
return N;}
330 return CblasRowMajor;
367 template <
int M1,
int M2>
382 inline Matrix<General,Fixed<M>,Var,AT>& init(Eye,
const AT &val=1);
383 inline Matrix<General,Fixed<M>,Var,AT>& init(Noinit,
const AT &a=AT()) {
return *
this; }
389 explicit inline operator std::vector<std::vector<AT>>()
const;
396 explicit inline Matrix(
const std::vector<std::vector<AT>> &m);
417 inline const Matrix<General,Var,Fixed<M>,AT> T()
const;
419 template<
class Row>
inline void set(
int j,
const Vector<Row,AT> &x);
421 template<
class Col>
inline void set(
int i,
const RowVector<Col,AT> &x);
423 template<
class Type,
class Row,
class Col>
inline void set(
const Range<Var,Var> &I,
const Range<Var,Var> &J,
const Matrix<Type,Row,Col,AT> &A);
425 template<
class Row>
inline void add(
int j,
const Vector<Row,AT> &x);
427 template<
class Col>
inline void add(
int i,
const RowVector<Col,AT> &x);
429 template<
class Type,
class Row,
class Col>
inline void add(
const Range<Var,Var> &I,
const Range<Var,Var> &J,
const Matrix<Type,Row,Col,AT> &A);
432 template <
int M,
class AT>
434 std::istringstream iss(strs);
440 throw std::runtime_error(
"Input not fully read.");
445 template <
int M,
class AT>
447 for(
int i=0; i<M*N; i++)
452 template <
int M,
class AT>
454 for(
int i=0; i<M; i++)
455 for(
int j=0; j<N; j++)
456 e(i,j) = (i==j) ? val : 0;
460 template <
int M,
class AT>
template <
int M1,
int M2>
461 inline const Matrix<General,Fixed<M2-M1+1>,Var,AT> Matrix<General,Fixed<M>,Var,AT>::operator()(
const Range<Fixed<M1>,Fixed<M2>> &I,
const Range<Var,Var> &J)
const {
462 FMATVEC_ASSERT(M2<M, AT);
463 FMATVEC_ASSERT(J.end()<N, AT);
464 Matrix<General,Fixed<M2-M1+1>,Var,AT> A(J.end()-J.start()+1,NONINIT);
466 for(
int i=0; i<A.
rows(); i++)
467 for(
int j=0; j<A.
cols(); j++)
468 A.e(i,j) = e(M1+i,J.start()+j);
473 template <
int M,
class AT>
475 FMATVEC_ASSERT(I.end()<M, AT);
476 FMATVEC_ASSERT(J.end()<N, AT);
479 for(
int i=0; i<A.
rows(); i++)
480 for(
int j=0; j<A.
cols(); j++)
481 A.e(i,j) = e(I.start()+i,J.start()+j);
486 template <
int M,
class AT>
489 FMATVEC_ASSERT(i>=0, AT);
490 FMATVEC_ASSERT(i<M, AT);
494 for(
int j=0; j<N; j++)
501 template <
int M,
class AT>
502 inline const Vector<Fixed<M>,AT> Matrix<General,Fixed<M>,Var,AT>::col(
int j)
const {
504 FMATVEC_ASSERT(j>=0, AT);
505 FMATVEC_ASSERT(j<N, AT);
507 Vector<Fixed<M>,AT> x(NONINIT);
509 for(
int i=0; i<M; i++)
516 template <
int M,
class AT>
519 for(
int i=0; i<N; i++)
520 for(
int j=0; j<M; j++)
525 template <
int M,
class AT>
template <
class Row>
527 FMATVEC_ASSERT(j<cols(), AT);
528 FMATVEC_ASSERT(rows()==x.size(), AT);
529 for(
int i=0; i<rows(); i++)
533 template <
int M,
class AT>
template <
class Col>
534 inline void Matrix<General,Fixed<M>,Var,AT>::set(
int i,
const RowVector<Col,AT> &x) {
535 FMATVEC_ASSERT(i<rows(), AT);
536 FMATVEC_ASSERT(cols()==x.size(), AT);
537 for(
int j=0; j<cols(); j++)
541 template <
int M,
class AT>
template<
class Type,
class Row,
class Col>
542 inline void Matrix<General,Fixed<M>,Var,AT>::set(
const Range<Var,Var> &I,
const Range<Var,Var> &J,
const Matrix<Type,Row,Col,AT> &A) {
544 FMATVEC_ASSERT(I.end()<rows(), AT);
545 FMATVEC_ASSERT(J.end()<cols(), AT);
546 FMATVEC_ASSERT(I.size()==A.rows(), AT);
547 FMATVEC_ASSERT(J.size()==A.cols(), AT);
549 for(
int i=I.start(), ii=0; i<=I.end(); i++, ii++)
550 for(
int j=J.start(), jj=0; j<=J.end(); j++, jj++)
554 template <
int M,
class AT>
template <
class Row>
555 inline void Matrix<General,Fixed<M>,Var,AT>::add(
int j,
const Vector<Row,AT> &x) {
556 FMATVEC_ASSERT(j<cols(), AT);
557 FMATVEC_ASSERT(rows()==x.size(), AT);
558 for(
int i=0; i<rows(); i++)
562 template <
int M,
class AT>
template <
class Col>
563 inline void Matrix<General,Fixed<M>,Var,AT>::add(
int i,
const RowVector<Col,AT> &x) {
564 FMATVEC_ASSERT(i<rows(), AT);
565 FMATVEC_ASSERT(cols()==x.size(), AT);
566 for(
int j=0; j<cols(); j++)
570 template <
int M,
class AT>
template<
class Type,
class Row,
class Col>
571 inline void Matrix<General,Fixed<M>,Var,AT>::add(
const Range<Var,Var> &I,
const Range<Var,Var> &J,
const Matrix<Type,Row,Col,AT> &A) {
573 FMATVEC_ASSERT(I.end()<rows(), AT);
574 FMATVEC_ASSERT(J.end()<cols(), AT);
575 FMATVEC_ASSERT(I.size()==A.rows(), AT);
576 FMATVEC_ASSERT(J.size()==A.cols(), AT);
578 for(
int i=I.start(), ii=0; i<=I.end(); i++, ii++)
579 for(
int j=J.start(), jj=0; j<=J.end(); j++, jj++)
580 e(i,j) += A.e(ii,jj);
583 template <
int M,
class AT>
585 std::vector<std::vector<AT>> ret(rows());
586 for(
int r=0; r<rows(); r++) {
587 ret[r].resize(cols());
588 for(
int c=0; c<cols(); c++)
594 template <
int M,
class AT>
597 throw std::runtime_error(
"The input has "+std::to_string(m.size())+
" rows but "+std::to_string(M)+
" rows are required.");
598 for(
int r=0; r<
rows(); r++) {
599 if(static_cast<int>(m[r].size())!=
cols())
600 throw std::runtime_error(
"The rows of the input have different length.");
601 for(
int c=0; c<
cols(); c++)
608 template <
int M,
class AT>
template <
class Type,
class Row,
class Col>
610 for(
int i=0; i<M; i++)
611 for(
int j=0; j<N; j++)
const AT & operator()(int i, int j) const
Element operator.
Definition: fixed_var_general_matrix.h:248
void resize(int m, int n)
Definition: fixed_var_general_matrix.h:172
AT & operator()(int i, int j)
Element operator.
Definition: fixed_var_general_matrix.h:235
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
Matrix()
Standard constructor.
Definition: fixed_var_general_matrix.h:66
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:84
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: fixed_var_general_matrix.h:318
const AT & e(int i) const
Element operator.
Definition: fixed_var_general_matrix.h:277
const AT * operator()() const
Pointer operator.
Definition: fixed_var_general_matrix.h:292
AT * operator()()
Pointer operator.
Definition: fixed_var_general_matrix.h:286
constexpr int rows() const
Number of rows.
Definition: fixed_var_general_matrix.h:298
int ldim() const
Leading dimension.
Definition: fixed_var_general_matrix.h:310
int cols() const
Number of columns.
This is an index class for creating submatrices.
Definition: range.h:35
const AT & e(int i, int j) const
Element operator.
Definition: fixed_var_general_matrix.h:265
Namespace fmatvec.
Definition: _memory.cc:28
~Matrix()
Destructor.
Definition: fixed_var_general_matrix.h:155
Matrix(const Matrix< General, Row, Col, AT > &A)
Copy Constructor.
Definition: fixed_var_general_matrix.h:115
Matrix(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: fixed_var_general_matrix.h:126
Basic shape class for matrices.
Definition: types.h:136
Matrix< General, Fixed< M >, Var, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: fixed_var_general_matrix.h:196
int rows() const
Number of rows.
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: fixed_var_general_matrix.h:329
constexpr int cols() const
Number of columns.
Definition: fixed_var_general_matrix.h:304
This is an index class for creating submatrices.
Definition: range.h:44
Matrix(const Matrix< General, Fixed< M >, Var, AT > &A)
Copy Constructor.
Definition: fixed_var_general_matrix.h:105
Matrix< General, Fixed< M >, Var, AT > & operator=(const Matrix< General, Fixed< M >, Var, AT > &A)
Assignment operator.
Definition: fixed_var_general_matrix.h:184
This is a vector class of general shape in dense storage format.
Definition: var_row_vector.h:39