22 #ifndef fixed_var_general_matrix_h 23 #define fixed_var_general_matrix_h 44 typedef AT value_type;
75 explicit Matrix(
int n,
Noinit) : N(n), ele(new AT[M*N]) { }
76 explicit Matrix(
int n, Init ini=INIT,
const AT &a=AT()) : N(n), ele(new AT[M*N]) { init(a); }
77 explicit Matrix(
int n, Eye ini,
const AT &a=1) : N(n), ele(new AT[M*N]) { init(ini,a); }
78 explicit Matrix(
int m,
int n, Noinit) : N(n), ele(new AT[M*N]) { FMATVEC_ASSERT(m==M, AT); }
79 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); }
80 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); }
96 template<
class Row,
class Col>
98 FMATVEC_ASSERT(A.
rows() == M, AT);
107 template<
class Type,
class Row,
class Col>
109 FMATVEC_ASSERT(A.
rows() == M, AT);
125 Matrix(
const std::string &strs);
141 Matrix<General,Fixed<M>,Var,AT>& resize(
int n, Init ini=INIT,
const AT &a=AT()) {
return resize(n,Noinit()).init(a); }
143 Matrix<General,Fixed<M>,Var,AT>& resize(
int n, Eye ini,
const AT &a=1) {
return resize(n,Noinit()).init(ini,a); }
149 throw std::runtime_error(
"A fixed-var matrix can only be resized in the second dimension.");
160 FMATVEC_ASSERT(N == A.
cols(), AT);
170 template <
class Type,
class Row,
class Col>
172 FMATVEC_ASSERT(M == A.
rows(), AT);
173 FMATVEC_ASSERT(N == A.
cols(), AT);
183 template<
class Type,
class Row,
class Col>
185 FMATVEC_ASSERT(M == A.
rows(), AT);
186 if(N!=A.
cols()) resize(A.
cols(),NONINIT);
200 FMATVEC_ASSERT(i>=0, AT);
201 FMATVEC_ASSERT(j>=0, AT);
202 FMATVEC_ASSERT(i<M, AT);
203 FMATVEC_ASSERT(j<N, AT);
213 FMATVEC_ASSERT(i>=0, AT);
214 FMATVEC_ASSERT(j>=0, AT);
215 FMATVEC_ASSERT(i<M, AT);
216 FMATVEC_ASSERT(j<N, AT);
221 AT& e(
int i,
int j) {
229 const AT&
e(
int i,
int j)
const {
241 const AT&
e(
int i)
const {
294 return CblasRowMajor;
331 template <
int M1,
int M2>
346 inline Matrix<General,Fixed<M>,Var,AT>& init(Eye,
const AT &val=1);
347 inline Matrix<General,Fixed<M>,Var,AT>& init(Noinit,
const AT &a=AT()) {
return *
this; }
353 explicit inline operator std::vector<std::vector<AT>>()
const;
360 explicit inline Matrix(
const std::vector<std::vector<AT>> &m);
381 inline const Matrix<General,Var,Fixed<M>,AT> T()
const;
383 template<
class Row>
inline void set(
int j,
const Vector<Row,AT> &x);
385 template<
class Col>
inline void set(
int i,
const RowVector<Col,AT> &x);
387 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);
389 template<
class Row>
inline void add(
int j,
const Vector<Row,AT> &x);
391 template<
class Col>
inline void add(
int i,
const RowVector<Col,AT> &x);
393 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);
396 template <
int M,
class AT>
398 std::istringstream iss(strs);
404 throw std::runtime_error(
"Input not fully read.");
409 template <
int M,
class AT>
411 for(
int i=0; i<M*N; i++)
416 template <
int M,
class AT>
418 for(
int i=0; i<M; i++)
419 for(
int j=0; j<N; j++)
420 e(i,j) = (i==j) ? val : 0;
424 template <
int M,
class AT>
template <
int M1,
int M2>
425 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 {
426 FMATVEC_ASSERT(M2<M, AT);
427 FMATVEC_ASSERT(J.end()<N, AT);
428 Matrix<General,Fixed<M2-M1+1>,Var,AT> A(J.end()-J.start()+1,NONINIT);
430 for(
int i=0; i<A.
rows(); i++)
431 for(
int j=0; j<A.
cols(); j++)
432 A.e(i,j) = e(M1+i,J.start()+j);
437 template <
int M,
class AT>
439 FMATVEC_ASSERT(I.end()<M, AT);
440 FMATVEC_ASSERT(J.end()<N, AT);
443 for(
int i=0; i<A.
rows(); i++)
444 for(
int j=0; j<A.
cols(); j++)
445 A.e(i,j) = e(I.start()+i,J.start()+j);
450 template <
int M,
class AT>
453 FMATVEC_ASSERT(i>=0, AT);
454 FMATVEC_ASSERT(i<M, AT);
458 for(
int j=0; j<N; j++)
465 template <
int M,
class AT>
466 inline const Vector<Fixed<M>,AT> Matrix<General,Fixed<M>,Var,AT>::col(
int j)
const {
468 FMATVEC_ASSERT(j>=0, AT);
469 FMATVEC_ASSERT(j<N, AT);
471 Vector<Fixed<M>,AT> x(NONINIT);
473 for(
int i=0; i<M; i++)
480 template <
int M,
class AT>
483 for(
int i=0; i<N; i++)
484 for(
int j=0; j<M; j++)
489 template <
int M,
class AT>
template <
class Row>
491 FMATVEC_ASSERT(j<cols(), AT);
492 FMATVEC_ASSERT(rows()==x.size(), AT);
493 for(
int i=0; i<rows(); i++)
497 template <
int M,
class AT>
template <
class Col>
498 inline void Matrix<General,Fixed<M>,Var,AT>::set(
int i,
const RowVector<Col,AT> &x) {
499 FMATVEC_ASSERT(i<rows(), AT);
500 FMATVEC_ASSERT(cols()==x.size(), AT);
501 for(
int j=0; j<cols(); j++)
505 template <
int M,
class AT>
template<
class Type,
class Row,
class Col>
506 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) {
508 FMATVEC_ASSERT(I.end()<rows(), AT);
509 FMATVEC_ASSERT(J.end()<cols(), AT);
510 FMATVEC_ASSERT(I.size()==A.rows(), AT);
511 FMATVEC_ASSERT(J.size()==A.cols(), AT);
513 for(
int i=I.start(), ii=0; i<=I.end(); i++, ii++)
514 for(
int j=J.start(), jj=0; j<=J.end(); j++, jj++)
518 template <
int M,
class AT>
template <
class Row>
519 inline void Matrix<General,Fixed<M>,Var,AT>::add(
int j,
const Vector<Row,AT> &x) {
520 FMATVEC_ASSERT(j<cols(), AT);
521 FMATVEC_ASSERT(rows()==x.size(), AT);
522 for(
int i=0; i<rows(); i++)
526 template <
int M,
class AT>
template <
class Col>
527 inline void Matrix<General,Fixed<M>,Var,AT>::add(
int i,
const RowVector<Col,AT> &x) {
528 FMATVEC_ASSERT(i<rows(), AT);
529 FMATVEC_ASSERT(cols()==x.size(), AT);
530 for(
int j=0; j<cols(); j++)
534 template <
int M,
class AT>
template<
class Type,
class Row,
class Col>
535 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) {
537 FMATVEC_ASSERT(I.end()<rows(), AT);
538 FMATVEC_ASSERT(J.end()<cols(), AT);
539 FMATVEC_ASSERT(I.size()==A.rows(), AT);
540 FMATVEC_ASSERT(J.size()==A.cols(), AT);
542 for(
int i=I.start(), ii=0; i<=I.end(); i++, ii++)
543 for(
int j=J.start(), jj=0; j<=J.end(); j++, jj++)
544 e(i,j) += A.e(ii,jj);
547 template <
int M,
class AT>
549 std::vector<std::vector<AT>> ret(rows());
550 for(
int r=0; r<rows(); r++) {
551 ret[r].resize(cols());
552 for(
int c=0; c<cols(); c++)
558 template <
int M,
class AT>
561 throw std::runtime_error(
"The input has "+std::to_string(m.size())+
" rows but "+std::to_string(M)+
" rows are required.");
562 for(
int r=0; r<
rows(); r++) {
563 if(static_cast<int>(m[r].size())!=
cols())
564 throw std::runtime_error(
"The rows of the input have different length.");
565 for(
int c=0; c<
cols(); c++)
572 template <
int M,
class AT>
template <
class Type,
class Row,
class Col>
574 for(
int i=0; i<M; i++)
575 for(
int j=0; j<N; j++)
const AT & operator()(int i, int j) const
Element operator.
Definition: fixed_var_general_matrix.h:212
void resize(int m, int n)
Definition: fixed_var_general_matrix.h:147
AT & operator()(int i, int j)
Element operator.
Definition: fixed_var_general_matrix.h:199
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
Matrix()
Standard constructor.
Definition: fixed_var_general_matrix.h:64
int cols() const
Number of columns.
Definition: fixed_var_general_matrix.h:268
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:82
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: fixed_var_general_matrix.h:282
const AT & e(int i) const
Element operator.
Definition: fixed_var_general_matrix.h:241
const AT * operator()() const
Pointer operator.
Definition: fixed_var_general_matrix.h:256
AT * operator()()
Pointer operator.
Definition: fixed_var_general_matrix.h:250
int ldim() const
Leading dimension.
Definition: fixed_var_general_matrix.h:274
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:229
Namespace fmatvec.
Definition: _memory.cc:28
~Matrix()
Destructor.
Definition: fixed_var_general_matrix.h:130
Matrix(const Matrix< General, Row, Col, AT > &A)
Copy Constructor.
Definition: fixed_var_general_matrix.h:97
Matrix(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: fixed_var_general_matrix.h:108
Basic shape class for matrices.
Definition: types.h:132
int rows() const
Number of rows.
Definition: fixed_var_general_matrix.h:262
Matrix< General, Fixed< M >, Var, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: fixed_var_general_matrix.h:171
int rows() const
Number of rows.
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: fixed_var_general_matrix.h:293
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:87
Matrix< General, Fixed< M >, Var, AT > & operator=(const Matrix< General, Fixed< M >, Var, AT > &A)
Assignment operator.
Definition: fixed_var_general_matrix.h:159
This is a vector class of general shape in dense storage format.
Definition: var_row_vector.h:39