22 #ifndef fixed_var_general_matrix_h 23 #define fixed_var_general_matrix_h 44 static constexpr
bool isVector {
false};
46 using value_type = AT;
76 Matrix<General,Fixed<M>,Var,AT>& operator=(Matrix<General,Fixed<M>,Var,AT> &&src) noexcept {
77 FMATVEC_ASSERT(N == src.cols(), AT);
94 explicit Matrix(
int n, Noinit) : N(n), ele(new AT[M*N]) { }
95 explicit Matrix(
int n, Init ini=INIT,
const AT &a=AT()) : N(n), ele(new AT[M*N]) { init(a); }
96 explicit Matrix(
int n, Eye ini,
const AT &a=1) : N(n), ele(new AT[M*N]) { init(ini,a); }
97 explicit Matrix(
int m,
int n, Noinit) : N(n), ele(new AT[M*N]) { FMATVEC_ASSERT(m==M, AT); }
98 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); }
99 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); }
115 template<
class Row,
class Col>
117 FMATVEC_ASSERT(A.
rows() == M, AT);
126 template<
class Type,
class Row,
class Col>
128 FMATVEC_ASSERT(A.
rows() == M, AT);
144 Matrix(
const std::string &strs);
147 using iterator = AT *;
148 using const_iterator =
const AT *;
149 iterator begin() {
return &ele[0]; }
150 iterator end() {
return &ele[M*N]; }
151 const_iterator begin()
const {
return &ele[0]; }
152 const_iterator end()
const {
return &ele[M*N]; }
167 Matrix<General,Fixed<M>,Var,AT>& resize(
int n, Init ini=INIT,
const AT &a=AT()) {
return resize(n,Noinit()).init(a); }
169 Matrix<General,Fixed<M>,Var,AT>& resize(
int n, Eye ini,
const AT &a=1) {
return resize(n,Noinit()).init(ini,a); }
175 throw std::runtime_error(
"A fixed-var matrix can only be resized in the second dimension.");
186 FMATVEC_ASSERT(N == A.
cols(), AT);
196 template <
class Type,
class Row,
class Col>
198 FMATVEC_ASSERT(M == A.
rows(), AT);
199 FMATVEC_ASSERT(N == A.
cols(), AT);
209 template<
class Type,
class Row,
class Col>
211 FMATVEC_ASSERT(M == A.
rows(), AT);
212 if(N!=A.
cols()) resize(A.
cols(),NONINIT);
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);
431 inline const Matrix<General,Var,Var,AT>
operator()(
const Indices &I,
const Indices &J)
const;
434 template <
int M,
class AT>
436 std::istringstream iss(strs);
442 throw std::runtime_error(
"Input not fully read.");
447 template <
int M,
class AT>
449 for(
int i=0; i<M*N; i++)
454 template <
int M,
class AT>
456 for(
int i=0; i<M; i++)
457 for(
int j=0; j<N; j++)
458 e(i,j) = (i==j) ? val : 0;
462 template <
int M,
class AT>
template <
int M1,
int M2>
463 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 {
464 FMATVEC_ASSERT(M2<M, AT);
465 FMATVEC_ASSERT(J.end()<N, AT);
466 Matrix<General,Fixed<M2-M1+1>,Var,AT> A(J.end()-J.start()+1,NONINIT);
468 for(
int i=0; i<A.
rows(); i++)
469 for(
int j=0; j<A.
cols(); j++)
470 A.e(i,j) = e(M1+i,J.start()+j);
475 template <
int M,
class AT>
477 FMATVEC_ASSERT(I.end()<M, AT);
478 FMATVEC_ASSERT(J.end()<N, AT);
481 for(
int i=0; i<A.
rows(); i++)
482 for(
int j=0; j<A.
cols(); j++)
483 A.e(i,j) = e(I.start()+i,J.start()+j);
488 template <
int M,
class AT>
491 FMATVEC_ASSERT(i>=0, AT);
492 FMATVEC_ASSERT(i<M, AT);
496 for(
int j=0; j<N; j++)
503 template <
int M,
class AT>
504 inline const Vector<Fixed<M>,AT> Matrix<General,Fixed<M>,Var,AT>::col(
int j)
const {
506 FMATVEC_ASSERT(j>=0, AT);
507 FMATVEC_ASSERT(j<N, AT);
509 Vector<Fixed<M>,AT> x(NONINIT);
511 for(
int i=0; i<M; i++)
518 template <
int M,
class AT>
521 for(
int i=0; i<N; i++)
522 for(
int j=0; j<M; j++)
527 template <
int M,
class AT>
template <
class Row>
529 FMATVEC_ASSERT(j<cols(), AT);
530 FMATVEC_ASSERT(rows()==x.size(), AT);
531 for(
int i=0; i<rows(); i++)
535 template <
int M,
class AT>
template <
class Col>
536 inline void Matrix<General,Fixed<M>,Var,AT>::set(
int i,
const RowVector<Col,AT> &x) {
537 FMATVEC_ASSERT(i<rows(), AT);
538 FMATVEC_ASSERT(cols()==x.size(), AT);
539 for(
int j=0; j<cols(); j++)
543 template <
int M,
class AT>
template<
class Type,
class Row,
class Col>
544 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) {
546 FMATVEC_ASSERT(I.end()<rows(), AT);
547 FMATVEC_ASSERT(J.end()<cols(), AT);
548 FMATVEC_ASSERT(I.size()==A.rows(), AT);
549 FMATVEC_ASSERT(J.size()==A.cols(), AT);
551 for(
int i=I.start(), ii=0; i<=I.end(); i++, ii++)
552 for(
int j=J.start(), jj=0; j<=J.end(); j++, jj++)
556 template <
int M,
class AT>
template <
class Row>
557 inline void Matrix<General,Fixed<M>,Var,AT>::add(
int j,
const Vector<Row,AT> &x) {
558 FMATVEC_ASSERT(j<cols(), AT);
559 FMATVEC_ASSERT(rows()==x.size(), AT);
560 for(
int i=0; i<rows(); i++)
564 template <
int M,
class AT>
template <
class Col>
565 inline void Matrix<General,Fixed<M>,Var,AT>::add(
int i,
const RowVector<Col,AT> &x) {
566 FMATVEC_ASSERT(i<rows(), AT);
567 FMATVEC_ASSERT(cols()==x.size(), AT);
568 for(
int j=0; j<cols(); j++)
572 template <
int M,
class AT>
template<
class Type,
class Row,
class Col>
573 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) {
575 FMATVEC_ASSERT(I.end()<rows(), AT);
576 FMATVEC_ASSERT(J.end()<cols(), AT);
577 FMATVEC_ASSERT(I.size()==A.rows(), AT);
578 FMATVEC_ASSERT(J.size()==A.cols(), AT);
580 for(
int i=I.start(), ii=0; i<=I.end(); i++, ii++)
581 for(
int j=J.start(), jj=0; j<=J.end(); j++, jj++)
582 e(i,j) += A.e(ii,jj);
585 template <
int M,
class AT>
586 inline const Matrix<General,Var,Var,AT> Matrix<General,Fixed<M>,Var,AT>::operator()(
const Indices &I,
const Indices &J)
const {
587 FMATVEC_ASSERT(I.max()<rows(), AT);
588 FMATVEC_ASSERT(J.max()<cols(), AT);
590 Matrix<General,Var,Var,AT> A(I.size(),J.size(),NONINIT);
592 for(
int i=0; i<A.rows(); i++)
593 for(
int j=0; j<A.cols(); j++)
594 A.e(i,j) = e(I[i],J[j]);
599 template <
int M,
class AT>
601 std::vector<std::vector<AT>> ret(rows(),std::vector<AT>(cols()));
602 for(
int r=0; r<rows(); r++) {
603 for(
int c=0; c<cols(); c++)
609 template <
int M,
class AT>
612 throw std::runtime_error(
"The input has "+std::to_string(m.size())+
" rows but "+std::to_string(M)+
" rows are required.");
613 for(
int r=0; r<
rows(); r++) {
614 if(static_cast<int>(m[r].size())!=
cols())
615 throw std::runtime_error(
"The rows of the input have different length.");
616 for(
int c=0; c<
cols(); c++)
623 template <
int M,
class AT>
template <
class Type,
class Row,
class Col>
625 for(
int i=0; i<M; i++)
626 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:173
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:42
Matrix()
Standard constructor.
Definition: fixed_var_general_matrix.h:67
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:36
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:156
Matrix(const Matrix< General, Row, Col, AT > &A)
Copy Constructor.
Definition: fixed_var_general_matrix.h:116
Matrix(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: fixed_var_general_matrix.h:127
Shape class for general matrices.
Definition: types.h:112
Matrix< General, Fixed< M >, Var, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: fixed_var_general_matrix.h:197
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:45
Matrix(const Matrix< General, Fixed< M >, Var, AT > &A)
Copy Constructor.
Definition: fixed_var_general_matrix.h:106
Matrix< General, Fixed< M >, Var, AT > & operator=(const Matrix< General, Fixed< M >, Var, AT > &A)
Assignment operator.
Definition: fixed_var_general_matrix.h:185
This is a vector class of general shape in dense storage format.
Definition: var_row_vector.h:39