22 #ifndef fixed_var_general_matrix_h
23 #define fixed_var_general_matrix_h
44 typedef AT value_type;
76 Matrix(
int n, Init ini=INIT,
const AT &a=0) : N(n), ele(new AT[M*N]) { init(a); }
77 Matrix(
int n, Eye ini,
const AT &a=1) : N(n), ele(new AT[M*N]) { init(ini,a); }
78 Matrix(
int m,
int n, Noinit) : N(n), ele(new AT[M*N]) { }
79 Matrix(
int m,
int n, Init ini=INIT,
const AT &a=0) : N(n), ele(new AT[M*N]) { init(a); }
80 Matrix(
int m,
int n, Eye ini,
const AT &a=1) : N(n), ele(new AT[M*N]) { init(ini,a); }
93 template<
class Row,
class Col>
99 template<
class Type,
class Row,
class Col>
100 explicit Matrix(
const Matrix<Type,Row,Col,AT> &A) : N(A.
cols()), ele(new AT[M*N]) {
102 #ifndef FMATVEC_NO_SIZE_CHECK
103 assert(A.rows() == M);
121 Matrix(
const std::string &strs);
122 Matrix(
const char *strs);
137 Matrix<General,Fixed<M>,Var,AT>& resize(
int n, Noinit) {
144 Matrix<General,Fixed<M>,Var,AT>& resize(
int n, Init ini=INIT,
const AT &a=0) {
return resize(n,Noinit()).init(a); }
146 Matrix<General,Fixed<M>,Var,AT>& resize(
int n, Eye ini,
const AT &a=1) {
return resize(n,Noinit()).init(ini,a); }
152 throw std::runtime_error(
"A fixed-var matrix can only be resized in the second dimension.");
164 template <
class Type,
class Row,
class Col>
167 template<
class Type,
class Row,
class Col>
182 #ifndef FMATVEC_NO_BOUNDS_CHECK
197 #ifndef FMATVEC_NO_BOUNDS_CHECK
207 AT& e(
int i,
int j) {
215 const AT&
e(
int i,
int j)
const {
227 const AT&
e(
int i)
const {
285 return CblasColMajor;
322 template <
int M1,
int M2>
337 inline Matrix<General,Fixed<M>,Var,AT>& init(Eye,
const AT &val=1);
338 inline Matrix<General,Fixed<M>,Var,AT>& init(Noinit,
const AT &a=0) {
return *
this; }
344 inline operator std::vector<std::vector<AT> >()
const;
351 inline Matrix(std::vector<std::vector<AT> > m);
353 inline const Matrix<General,Var,Fixed<M>,AT> T()
const;
355 template<
class Row>
inline void set(
int j,
const Vector<Row,AT> &x);
357 template<
class Col>
inline void set(
int i,
const RowVector<Col,AT> &x);
359 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);
361 template<
class Row>
inline void add(
int j,
const Vector<Row,AT> &x);
363 template<
class Col>
inline void add(
int i,
const RowVector<Col,AT> &x);
365 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);
369 template <
int M,
class AT>
371 std::istringstream iss(strs);
377 throw std::runtime_error(
"Input not fully read.");
382 template <
int M,
class AT>
template<
class Type,
class Row,
class Col>
383 inline Matrix<General,Fixed<M>,Var,AT>& Matrix<General,Fixed<M>,Var,AT>::operator=(
const Matrix<Type,Row,Col,AT> &A) {
385 #ifndef FMATVEC_NO_SIZE_CHECK
386 assert(M == A.rows());
393 #ifndef FMATVEC_NO_SIZE_CHECK
394 assert(N == A.cols());
403 template <
int M,
class AT>
411 #ifndef FMATVEC_NO_SIZE_CHECK
412 assert(N == A.cols());
421 template <
int M,
class AT>
template<
class Type,
class Row,
class Col>
424 #ifndef FMATVEC_NO_SIZE_CHECK
425 assert(M == A.rows());
438 template <
int M,
class AT>
440 for(
int i=0; i<M*N; i++)
445 template <
int M,
class AT>
447 for(
int i=0; i<M; i++)
448 for(
int j=0; j<N; j++)
449 e(i,j) = (i==j) ? val : 0;
453 template <
int M,
class AT>
template <
int M1,
int M2>
454 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 {
455 #ifndef FMATVEC_NO_BOUNDS_CHECK
459 Matrix<General,Fixed<M2-M1+1>,Var,AT> A(J.end()-J.start()+1,NONINIT);
461 for(
int i=0; i<A.rows(); i++)
462 for(
int j=0; j<A.cols(); j++)
463 A.e(i,j) = e(M1+i,J.start()+j);
468 template <
int M,
class AT>
470 #ifndef FMATVEC_NO_BOUNDS_CHECK
476 for(
int i=0; i<A.rows(); i++)
477 for(
int j=0; j<A.cols(); j++)
478 A.e(i,j) = e(I.start()+i,J.start()+j);
483 template <
int M,
class AT>
486 #ifndef FMATVEC_NO_BOUNDS_CHECK
493 for(
int j=0; j<N; j++)
500 template <
int M,
class AT>
501 inline const Vector<Fixed<M>,AT> Matrix<General,Fixed<M>,Var,AT>::col(
int j)
const {
503 #ifndef FMATVEC_NO_BOUNDS_CHECK
508 Vector<Fixed<M>,AT> x(NONINIT);
510 for(
int i=0; i<M; i++)
517 template <
int M,
class AT>
518 inline const Matrix<General,Var,Fixed<M>,AT> Matrix<General,Fixed<M>,Var,AT>::T()
const {
519 Matrix<General,Var,Fixed<M>,AT> A(cols(),NONINIT);
520 for(
int i=0; i<N; i++)
521 for(
int j=0; j<M; j++)
526 template <
int M,
class AT>
template <
class Row>
527 inline void Matrix<General,Fixed<M>,Var,AT>::set(
int j,
const Vector<Row,AT> &x) {
528 #ifndef FMATVEC_NO_BOUNDS_CHECK
530 assert(rows()==x.size());
532 for(
int i=0; i<rows(); i++)
536 template <
int M,
class AT>
template <
class Col>
537 inline void Matrix<General,Fixed<M>,Var,AT>::set(
int i,
const RowVector<Col,AT> &x) {
538 #ifndef FMATVEC_NO_BOUNDS_CHECK
540 assert(cols()==x.size());
542 for(
int j=0; j<cols(); j++)
546 template <
int M,
class AT>
template<
class Type,
class Row,
class Col>
547 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) {
549 #ifndef FMATVEC_NO_BOUNDS_CHECK
550 assert(I.end()<rows());
551 assert(J.end()<cols());
552 assert(I.size()==A.rows());
553 assert(J.size()==A.cols());
556 for(
int i=I.start(), ii=0; i<=I.end(); i++, ii++)
557 for(
int j=J.start(), jj=0; j<=J.end(); j++, jj++)
561 template <
int M,
class AT>
template <
class Row>
562 inline void Matrix<General,Fixed<M>,Var,AT>::add(
int j,
const Vector<Row,AT> &x) {
563 #ifndef FMATVEC_NO_BOUNDS_CHECK
565 assert(rows()==x.size());
567 for(
int i=0; i<rows(); i++)
571 template <
int M,
class AT>
template <
class Col>
572 inline void Matrix<General,Fixed<M>,Var,AT>::add(
int i,
const RowVector<Col,AT> &x) {
573 #ifndef FMATVEC_NO_BOUNDS_CHECK
575 assert(cols()==x.size());
577 for(
int j=0; j<cols(); j++)
581 template <
int M,
class AT>
template<
class Type,
class Row,
class Col>
582 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) {
584 #ifndef FMATVEC_NO_BOUNDS_CHECK
585 assert(I.end()<rows());
586 assert(J.end()<cols());
587 assert(I.size()==A.rows());
588 assert(J.size()==A.cols());
591 for(
int i=I.start(), ii=0; i<=I.end(); i++, ii++)
592 for(
int j=J.start(), jj=0; j<=J.end(); j++, jj++)
593 e(i,j) += A.e(ii,jj);
596 template <
int M,
class AT>
598 std::vector<std::vector<AT> > ret(rows());
599 for(
int r=0; r<rows(); r++) {
600 ret[r].resize(cols());
601 for(
int c=0; c<cols(); c++)
607 template <
int M,
class AT>
609 Matrix<General,
Fixed<M>,Var,AT>(!m.empty() ? m[0].size() : 0) {
611 throw std::runtime_error(
"The input has "+std::to_string(m.size())+
" rows but "+std::to_string(M)+
" rows are required.");
612 if(static_cast<int>(m[0].size()) != N)
613 throw std::runtime_error(
"The input has "+std::to_string(m[0].size())+
" columns but "+std::to_string(N)+
" columns are required.");
614 for(
int r=0; r<
rows(); r++) {
615 if(static_cast<int>(m[r].size())!=
cols())
616 throw std::runtime_error(
"The rows of the input have different length.");
617 for(
int c=0; c<
cols(); c++)
624 template <
int M,
class AT>
template <
class Type,
class Row,
class Col>
626 for(
int i=0; i<M; i++)
627 for(
int j=0; j<N; j++)
void resize(int m, int n)
Definition: fixed_var_general_matrix.h:150
AT & operator()(int i, int j)
Element operator.
Definition: fixed_var_general_matrix.h:181
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
Matrix()
Standard constructor.
Definition: fixed_var_general_matrix.h:64
This is a matrix class for general matrices.
Definition: var_general_matrix.h:39
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:90
int rows() const
Number of rows.
Definition: fixed_var_general_matrix.h:248
const AT & operator()(int i, int j) const
Element operator.
Definition: fixed_var_general_matrix.h:196
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: fixed_var_general_matrix.h:284
const AT & e(int i) const
Element operator.
Definition: fixed_var_general_matrix.h:227
AT * operator()()
Pointer operator.
Definition: fixed_var_general_matrix.h:236
int rows() const
Number of rows.
const AT & e(int i, int j) const
Element operator.
Definition: fixed_var_general_matrix.h:215
int cols() const
Number of columns.
This is an index class for creating submatrices.
Definition: range.h:35
int cols() const
Number of columns.
Definition: fixed_var_general_matrix.h:254
~Matrix()
Destructor.
Definition: fixed_var_general_matrix.h:126
bool transposed() const
The storage format for a fixed-var matrix is fortran-storage order -> transposed is always false...
Definition: fixed_var_general_matrix.h:263
int ldim() const
Leading dimension.
Definition: fixed_var_general_matrix.h:260
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: fixed_var_general_matrix.h:273
const AT * operator()() const
Pointer operator.
Definition: fixed_var_general_matrix.h:242
Basic shape class for matrices.
Definition: types.h:100
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
This is a vector class of general shape in dense storage format.
Definition: var_row_vector.h:39