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);
216 inline Matrix<General,Fixed<M>,Var,AT>& operator<<=(Matrix<General,Fixed<M>,Var,AT> &&src) {
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;
433 inline const Matrix<General,Var,Var,AT>
operator()(
const Range<Var,Var> &I,
const Indices &J)
const;
435 inline const Matrix<General,Var,Var,AT>
operator()(
const Indices &I,
const Range<Var,Var> &J)
const;
438 template <
int M,
class AT>
440 std::istringstream iss(strs);
446 throw std::runtime_error(
"Input not fully read.");
451 template <
int M,
class AT>
453 for(
int i=0; i<M*N; i++)
458 template <
int M,
class AT>
460 for(
int i=0; i<M; i++)
461 for(
int j=0; j<N; j++)
462 e(i,j) = (i==j) ? val : 0;
466 template <
int M,
class AT>
template <
int M1,
int M2>
467 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 {
468 FMATVEC_ASSERT(M2<M, AT);
469 FMATVEC_ASSERT(J.end()<N, AT);
470 Matrix<General,Fixed<M2-M1+1>,Var,AT> A(J.size(),NONINIT);
472 for(
int i=0; i<A.
rows(); i++)
473 for(
int j=0; j<A.
cols(); j++)
474 A.e(i,j) = e(M1+i,J.start()+j);
479 template <
int M,
class AT>
481 FMATVEC_ASSERT(I.end()<M, AT);
482 FMATVEC_ASSERT(J.end()<N, AT);
485 for(
int i=0; i<A.
rows(); i++)
486 for(
int j=0; j<A.
cols(); j++)
487 A.e(i,j) = e(I.start()+i,J.start()+j);
492 template <
int M,
class AT>
495 FMATVEC_ASSERT(i>=0, AT);
496 FMATVEC_ASSERT(i<M, AT);
500 for(
int j=0; j<N; j++)
507 template <
int M,
class AT>
508 inline const Vector<Fixed<M>,AT> Matrix<General,Fixed<M>,Var,AT>::col(
int j)
const {
510 FMATVEC_ASSERT(j>=0, AT);
511 FMATVEC_ASSERT(j<N, AT);
513 Vector<Fixed<M>,AT> x(NONINIT);
515 for(
int i=0; i<M; i++)
522 template <
int M,
class AT>
523 inline const Matrix<General,Var,Fixed<M>,AT> Matrix<General,Fixed<M>,Var,AT>::T()
const {
524 Matrix<General,Var,Fixed<M>,AT> A(cols(),NONINIT);
525 for(
int i=0; i<N; i++)
526 for(
int j=0; j<M; j++)
531 template <
int M,
class AT>
template <
class Row>
532 inline void Matrix<General,Fixed<M>,Var,AT>::set(
int j,
const Vector<Row,AT> &x) {
533 FMATVEC_ASSERT(j<cols(), AT);
534 FMATVEC_ASSERT(rows()==x.size(), AT);
535 for(
int i=0; i<rows(); i++)
539 template <
int M,
class AT>
template <
class Col>
540 inline void Matrix<General,Fixed<M>,Var,AT>::set(
int i,
const RowVector<Col,AT> &x) {
541 FMATVEC_ASSERT(i<rows(), AT);
542 FMATVEC_ASSERT(cols()==x.size(), AT);
543 for(
int j=0; j<cols(); j++)
547 template <
int M,
class AT>
template<
class Type,
class Row,
class Col>
548 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) {
550 FMATVEC_ASSERT(I.end()<rows(), AT);
551 FMATVEC_ASSERT(J.end()<cols(), AT);
552 FMATVEC_ASSERT(I.size()==A.rows(), AT);
553 FMATVEC_ASSERT(J.size()==A.cols(), AT);
555 for(
int i=I.start(), ii=0; i<=I.end(); i++, ii++)
556 for(
int j=J.start(), jj=0; j<=J.end(); j++, jj++)
560 template <
int M,
class AT>
template <
class Row>
561 inline void Matrix<General,Fixed<M>,Var,AT>::add(
int j,
const Vector<Row,AT> &x) {
562 FMATVEC_ASSERT(j<cols(), AT);
563 FMATVEC_ASSERT(rows()==x.size(), AT);
564 for(
int i=0; i<rows(); i++)
568 template <
int M,
class AT>
template <
class Col>
569 inline void Matrix<General,Fixed<M>,Var,AT>::add(
int i,
const RowVector<Col,AT> &x) {
570 FMATVEC_ASSERT(i<rows(), AT);
571 FMATVEC_ASSERT(cols()==x.size(), AT);
572 for(
int j=0; j<cols(); j++)
576 template <
int M,
class AT>
template<
class Type,
class Row,
class Col>
577 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) {
579 FMATVEC_ASSERT(I.end()<rows(), AT);
580 FMATVEC_ASSERT(J.end()<cols(), AT);
581 FMATVEC_ASSERT(I.size()==A.rows(), AT);
582 FMATVEC_ASSERT(J.size()==A.cols(), AT);
584 for(
int i=I.start(), ii=0; i<=I.end(); i++, ii++)
585 for(
int j=J.start(), jj=0; j<=J.end(); j++, jj++)
586 e(i,j) += A.e(ii,jj);
589 template <
int M,
class AT>
590 inline const Matrix<General,Var,Var,AT> Matrix<General,Fixed<M>,Var,AT>::operator()(
const Indices &I,
const Indices &J)
const {
591 FMATVEC_ASSERT(I.max()<rows(), AT);
592 FMATVEC_ASSERT(J.max()<cols(), AT);
594 Matrix<General,Var,Var,AT> A(I.size(),J.size(),NONINIT);
596 for(
int i=0; i<A.rows(); i++)
597 for(
int j=0; j<A.cols(); j++)
598 A.e(i,j) = e(I[i],J[j]);
603 template <
int M,
class AT>
604 inline const Matrix<General,Var,Var,AT> Matrix<General,Fixed<M>,Var,AT>::operator()(
const Range<Var,Var> &I,
const Indices &J)
const {
605 FMATVEC_ASSERT(I.end()<M, AT);
606 FMATVEC_ASSERT(J.max()<cols(), AT);
608 Matrix<General,Var,Var,AT> A(I.size(),J.size(),NONINIT);
610 for(
int i=0; i<A.rows(); i++)
611 for(
int j=0; j<A.cols(); j++)
612 A.e(i,j) = e(I.start()+i,J[j]);
617 template <
int M,
class AT>
618 inline const Matrix<General,Var,Var,AT> Matrix<General,Fixed<M>,Var,AT>::operator()(
const Indices &I,
const Range<Var,Var> &J)
const {
619 FMATVEC_ASSERT(I.max()<rows(), AT);
620 FMATVEC_ASSERT(J.end()<N, AT);
622 Matrix<General,Var,Var,AT> A(I.size(),J.size(),NONINIT);
624 for(
int i=0; i<A.rows(); i++)
625 for(
int j=0; j<A.cols(); j++)
626 A.e(i,j) = e(I[i],J.start()+j);
631 template <
int M,
class AT>
633 std::vector<std::vector<AT>> ret(rows(),std::vector<AT>(cols()));
634 for(
int r=0; r<rows(); r++) {
635 for(
int c=0; c<cols(); c++)
641 template <
int M,
class AT>
642 inline Matrix<General,Fixed<M>,
Var,AT>::Matrix(
const std::vector<std::vector<AT>> &m) : N(!m.empty()? static_cast<int>(m.empty()?0:m[0].size()):0), ele(new AT[M*N]) {
644 throw std::runtime_error(
"The input has "+std::to_string(m.size())+
" rows but "+std::to_string(M)+
" rows are required.");
645 for(
int r=0; r<
rows(); r++) {
646 if(
static_cast<int>(m[r].size())!=
cols())
647 throw std::runtime_error(
"The rows of the input have different length.");
648 for(
int c=0; c<
cols(); c++)
655 template <
int M,
class AT>
template <
class Type,
class Row,
class Col>
657 for(
int i=0; i<M; i++)
658 for(
int j=0; j<N; j++)
Shape class for general matrices.
Definition: types.h:116
Matrix< General, Fixed< M >, Var, AT > & operator=(const Matrix< General, Fixed< M >, Var, AT > &A)
Assignment operator.
Definition: fixed_var_general_matrix.h:185
const AT & operator()(int i, int j) const
Element operator.
Definition: fixed_var_general_matrix.h:248
Matrix(const Matrix< General, Row, Col, AT > &A)
Copy Constructor.
Definition: fixed_var_general_matrix.h:116
const AT & e(int i, int j) const
Element operator.
Definition: fixed_var_general_matrix.h:265
Matrix()
Standard constructor.
Definition: fixed_var_general_matrix.h:67
constexpr int cols() const
Number of columns.
Definition: fixed_var_general_matrix.h:304
AT & operator()(int i, int j)
Element operator.
Definition: fixed_var_general_matrix.h:235
constexpr int rows() const
Number of rows.
Definition: fixed_var_general_matrix.h:298
const AT * operator()() const
Pointer operator.
Definition: fixed_var_general_matrix.h:292
Matrix(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: fixed_var_general_matrix.h:127
Matrix(const Matrix< General, Fixed< M >, Var, AT > &A)
Copy Constructor.
Definition: fixed_var_general_matrix.h:106
~Matrix()
Destructor.
Definition: fixed_var_general_matrix.h:156
CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: fixed_var_general_matrix.h:318
CBLAS_ORDER blasOrder() const
Storage convention.
Definition: fixed_var_general_matrix.h:329
AT * operator()()
Pointer operator.
Definition: fixed_var_general_matrix.h:286
void resize(int m, int n)
Definition: fixed_var_general_matrix.h:173
int ldim() const
Leading dimension.
Definition: fixed_var_general_matrix.h:310
const AT & e(int i) const
Element operator.
Definition: fixed_var_general_matrix.h:277
This is a matrix class for general matrices.
Definition: var_general_matrix.h:41
constexpr int cols() const
Number of columns.
Definition: var_general_matrix.h:303
constexpr int rows() const
Number of rows.
Definition: var_general_matrix.h:297
This is the basic matrix class for arbitrary matrices.
Definition: matrix.h:52
int rows() const
Number of rows.
int cols() const
Number of columns.
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:84
This is an index class for creating submatrices.
Definition: range.h:44
This is an index class for creating submatrices.
Definition: range.h:35
This is a vector class of general shape in dense storage format.
Definition: var_row_vector.h:39
Namespace fmatvec.
Definition: _memory.cc:28