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;
77 explicit Matrix(
int n,
Noinit) : N(n), ele(new AT[M*N]) { }
78 explicit Matrix(
int n, Init ini=INIT,
const AT &a=AT()) : N(n), ele(new AT[M*N]) { init(a); }
79 explicit Matrix(
int n, Eye ini,
const AT &a=1) : N(n), ele(new AT[M*N]) { init(ini,a); }
80 explicit Matrix(
int m,
int n, Noinit) : N(n), ele(new AT[M*N]) { FMATVEC_ASSERT(m==M, AT); }
81 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); }
82 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); }
98 template<
class Row,
class Col>
100 FMATVEC_ASSERT(A.
rows() == M, AT);
109 template<
class Type,
class Row,
class Col>
111 FMATVEC_ASSERT(A.
rows() == M, AT);
127 Matrix(
const std::string &strs);
130 typedef AT* iterator;
131 typedef const AT* const_iterator;
132 iterator begin() {
return &ele[0]; }
133 iterator end() {
return &ele[M*N]; }
134 const_iterator begin()
const {
return &ele[0]; }
135 const_iterator end()
const {
return &ele[M*N]; }
150 Matrix<General,Fixed<M>,Var,AT>& resize(
int n, Init ini=INIT,
const AT &a=AT()) {
return resize(n,Noinit()).init(a); }
152 Matrix<General,Fixed<M>,Var,AT>& resize(
int n, Eye ini,
const AT &a=1) {
return resize(n,Noinit()).init(ini,a); }
158 throw std::runtime_error(
"A fixed-var matrix can only be resized in the second dimension.");
169 FMATVEC_ASSERT(N == A.
cols(), AT);
179 template <
class Type,
class Row,
class Col>
181 FMATVEC_ASSERT(M == A.
rows(), AT);
182 FMATVEC_ASSERT(N == A.
cols(), AT);
192 template<
class Type,
class Row,
class Col>
194 FMATVEC_ASSERT(M == A.
rows(), AT);
195 if(N!=A.
cols()) resize(A.
cols(),NONINIT);
209 FMATVEC_ASSERT(i>=0, AT);
210 FMATVEC_ASSERT(j>=0, AT);
211 FMATVEC_ASSERT(i<M, AT);
212 FMATVEC_ASSERT(j<N, AT);
222 FMATVEC_ASSERT(i>=0, AT);
223 FMATVEC_ASSERT(j>=0, AT);
224 FMATVEC_ASSERT(i<M, AT);
225 FMATVEC_ASSERT(j<N, AT);
230 AT& e(
int i,
int j) {
238 const AT&
e(
int i,
int j)
const {
250 const AT&
e(
int i)
const {
271 constexpr
int rows()
const {
return M;}
277 constexpr
int cols()
const {
return N;}
303 return CblasRowMajor;
340 template <
int M1,
int M2>
355 inline Matrix<General,Fixed<M>,Var,AT>& init(Eye,
const AT &val=1);
356 inline Matrix<General,Fixed<M>,Var,AT>& init(Noinit,
const AT &a=AT()) {
return *
this; }
362 explicit inline operator std::vector<std::vector<AT>>()
const;
369 explicit inline Matrix(
const std::vector<std::vector<AT>> &m);
390 inline const Matrix<General,Var,Fixed<M>,AT> T()
const;
392 template<
class Row>
inline void set(
int j,
const Vector<Row,AT> &x);
394 template<
class Col>
inline void set(
int i,
const RowVector<Col,AT> &x);
396 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);
398 template<
class Row>
inline void add(
int j,
const Vector<Row,AT> &x);
400 template<
class Col>
inline void add(
int i,
const RowVector<Col,AT> &x);
402 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);
405 template <
int M,
class AT>
407 std::istringstream iss(strs);
413 throw std::runtime_error(
"Input not fully read.");
418 template <
int M,
class AT>
420 for(
int i=0; i<M*N; i++)
425 template <
int M,
class AT>
427 for(
int i=0; i<M; i++)
428 for(
int j=0; j<N; j++)
429 e(i,j) = (i==j) ? val : 0;
433 template <
int M,
class AT>
template <
int M1,
int M2>
434 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 {
435 FMATVEC_ASSERT(M2<M, AT);
436 FMATVEC_ASSERT(J.end()<N, AT);
437 Matrix<General,Fixed<M2-M1+1>,Var,AT> A(J.end()-J.start()+1,NONINIT);
439 for(
int i=0; i<A.
rows(); i++)
440 for(
int j=0; j<A.
cols(); j++)
441 A.e(i,j) = e(M1+i,J.start()+j);
446 template <
int M,
class AT>
448 FMATVEC_ASSERT(I.end()<M, AT);
449 FMATVEC_ASSERT(J.end()<N, AT);
452 for(
int i=0; i<A.
rows(); i++)
453 for(
int j=0; j<A.
cols(); j++)
454 A.e(i,j) = e(I.start()+i,J.start()+j);
459 template <
int M,
class AT>
462 FMATVEC_ASSERT(i>=0, AT);
463 FMATVEC_ASSERT(i<M, AT);
467 for(
int j=0; j<N; j++)
474 template <
int M,
class AT>
475 inline const Vector<Fixed<M>,AT> Matrix<General,Fixed<M>,Var,AT>::col(
int j)
const {
477 FMATVEC_ASSERT(j>=0, AT);
478 FMATVEC_ASSERT(j<N, AT);
480 Vector<Fixed<M>,AT> x(NONINIT);
482 for(
int i=0; i<M; i++)
489 template <
int M,
class AT>
492 for(
int i=0; i<N; i++)
493 for(
int j=0; j<M; j++)
498 template <
int M,
class AT>
template <
class Row>
500 FMATVEC_ASSERT(j<cols(), AT);
501 FMATVEC_ASSERT(rows()==x.size(), AT);
502 for(
int i=0; i<rows(); i++)
506 template <
int M,
class AT>
template <
class Col>
507 inline void Matrix<General,Fixed<M>,Var,AT>::set(
int i,
const RowVector<Col,AT> &x) {
508 FMATVEC_ASSERT(i<rows(), AT);
509 FMATVEC_ASSERT(cols()==x.size(), AT);
510 for(
int j=0; j<cols(); j++)
514 template <
int M,
class AT>
template<
class Type,
class Row,
class Col>
515 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) {
517 FMATVEC_ASSERT(I.end()<rows(), AT);
518 FMATVEC_ASSERT(J.end()<cols(), AT);
519 FMATVEC_ASSERT(I.size()==A.rows(), AT);
520 FMATVEC_ASSERT(J.size()==A.cols(), AT);
522 for(
int i=I.start(), ii=0; i<=I.end(); i++, ii++)
523 for(
int j=J.start(), jj=0; j<=J.end(); j++, jj++)
527 template <
int M,
class AT>
template <
class Row>
528 inline void Matrix<General,Fixed<M>,Var,AT>::add(
int j,
const Vector<Row,AT> &x) {
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>::add(
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>::add(
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++)
553 e(i,j) += A.e(ii,jj);
556 template <
int M,
class AT>
558 std::vector<std::vector<AT>> ret(rows());
559 for(
int r=0; r<rows(); r++) {
560 ret[r].resize(cols());
561 for(
int c=0; c<cols(); c++)
567 template <
int M,
class AT>
570 throw std::runtime_error(
"The input has "+std::to_string(m.size())+
" rows but "+std::to_string(M)+
" rows are required.");
571 for(
int r=0; r<
rows(); r++) {
572 if(static_cast<int>(m[r].size())!=
cols())
573 throw std::runtime_error(
"The rows of the input have different length.");
574 for(
int c=0; c<
cols(); c++)
581 template <
int M,
class AT>
template <
class Type,
class Row,
class Col>
583 for(
int i=0; i<M; i++)
584 for(
int j=0; j<N; j++)
const AT & operator()(int i, int j) const
Element operator.
Definition: fixed_var_general_matrix.h:221
void resize(int m, int n)
Definition: fixed_var_general_matrix.h:156
AT & operator()(int i, int j)
Element operator.
Definition: fixed_var_general_matrix.h:208
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:291
const AT & e(int i) const
Element operator.
Definition: fixed_var_general_matrix.h:250
const AT * operator()() const
Pointer operator.
Definition: fixed_var_general_matrix.h:265
AT * operator()()
Pointer operator.
Definition: fixed_var_general_matrix.h:259
constexpr int rows() const
Number of rows.
Definition: fixed_var_general_matrix.h:271
int ldim() const
Leading dimension.
Definition: fixed_var_general_matrix.h:283
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:238
Namespace fmatvec.
Definition: _memory.cc:28
~Matrix()
Destructor.
Definition: fixed_var_general_matrix.h:139
Matrix(const Matrix< General, Row, Col, AT > &A)
Copy Constructor.
Definition: fixed_var_general_matrix.h:99
Matrix(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: fixed_var_general_matrix.h:110
Basic shape class for matrices.
Definition: types.h:132
Matrix< General, Fixed< M >, Var, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: fixed_var_general_matrix.h:180
int rows() const
Number of rows.
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: fixed_var_general_matrix.h:302
constexpr int cols() const
Number of columns.
Definition: fixed_var_general_matrix.h:277
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:89
Matrix< General, Fixed< M >, Var, AT > & operator=(const Matrix< General, Fixed< M >, Var, AT > &A)
Assignment operator.
Definition: fixed_var_general_matrix.h:168
This is a vector class of general shape in dense storage format.
Definition: var_row_vector.h:39