22 #ifndef fixed_var_general_matrix_h
23 #define fixed_var_general_matrix_h
43 typedef AT AtomicType;
75 Matrix(
int n, Init ini=INIT,
const AT &a=0) : N(n), ele(new AT[M*N]) { init(a); }
76 Matrix(
int n, Eye ini,
const AT &a=1) : N(n), ele(new AT[M*N]) { init(ini,a); }
77 Matrix(
int m,
int n, Noinit) : N(n), ele(new AT[M*N]) { }
78 Matrix(
int m,
int n, Init ini=INIT,
const AT &a=0) : N(n), ele(new AT[M*N]) { init(a); }
79 Matrix(
int m,
int n, Eye ini,
const AT &a=1) : N(n), ele(new AT[M*N]) { init(ini,a); }
92 template<
class Row,
class Col>
98 template<
class Type,
class Row,
class Col>
99 explicit Matrix(
const Matrix<Type,Row,Col,AT> &A) : N(A.
cols()), ele(new AT[M*N]) {
101 #ifndef FMATVEC_NO_SIZE_CHECK
102 assert(A.rows() == M);
120 Matrix(
const char *str);
135 Matrix<General,Fixed<M>,Var,AT>& resize(
int n, Noinit) {
142 Matrix<General,Fixed<M>,Var,AT>& resize(
int n, Init ini=INIT,
const AT &a=0) {
return resize(n,Noinit()).init(a); }
144 Matrix<General,Fixed<M>,Var,AT>& resize(
int n, Eye ini,
const AT &a=1) {
return resize(n,Noinit()).init(ini,a); }
150 throw std::runtime_error(
"A fixed-var matrix can only be resized in the second dimension.");
162 template <
class Type,
class Row,
class Col>
165 template<
class Type,
class Row,
class Col>
180 #ifndef FMATVEC_NO_BOUNDS_CHECK
195 #ifndef FMATVEC_NO_BOUNDS_CHECK
205 AT& e(
int i,
int j) {
213 const AT&
e(
int i,
int j)
const {
225 const AT&
e(
int i)
const {
283 return CblasColMajor;
320 template <
int M1,
int M2>
335 inline Matrix<General,Fixed<M>,Var,AT>& init(Eye,
const AT &a=1);
336 inline Matrix<General,Fixed<M>,Var,AT>& init(Noinit,
const AT &a=0) {
return *
this; }
342 inline operator std::vector<std::vector<AT> >();
349 inline Matrix(std::vector<std::vector<AT> > m);
351 inline const Matrix<General,Var,Fixed<M>,AT> T()
const;
353 template<
class Row>
inline void set(
int j,
const Vector<Row,AT> &x);
355 template<
class Col>
inline void set(
int i,
const RowVector<Col,AT> &x);
357 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);
359 template<
class Row>
inline void add(
int j,
const Vector<Row,AT> &x);
361 template<
class Col>
inline void add(
int i,
const RowVector<Col,AT> &x);
363 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);
367 template <
int M,
class AT>
372 std::istringstream iss(strs);
375 if(c==
'[') iss.str(strs);
376 else iss.str(std::string(
"[")+strs+
"]");
406 for(
int i=0; i<M; i++)
407 for(
int j=0; j<N; j++) {
415 template <
int M,
class AT>
template<
class Type,
class Row,
class Col>
418 #ifndef FMATVEC_NO_SIZE_CHECK
419 assert(M == A.rows());
426 #ifndef FMATVEC_NO_SIZE_CHECK
427 assert(N == A.cols());
436 template <
int M,
class AT>
444 #ifndef FMATVEC_NO_SIZE_CHECK
445 assert(N == A.cols());
454 template <
int M,
class AT>
template<
class Type,
class Row,
class Col>
457 #ifndef FMATVEC_NO_SIZE_CHECK
458 assert(M == A.rows());
471 template <
int M,
class AT>
473 for(
int i=0; i<M*N; i++)
478 template <
int M,
class AT>
480 for(
int i=0; i<M; i++)
481 for(
int j=0; j<N; j++)
482 e(i,j) = (i==j) ? val : 0;
486 template <
int M,
class AT>
template <
int M1,
int M2>
487 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 {
488 #ifndef FMATVEC_NO_BOUNDS_CHECK
492 Matrix<General,Fixed<M2-M1+1>,Var,AT> A(J.end()-J.start()+1,NONINIT);
494 for(
int i=0; i<A.rows(); i++)
495 for(
int j=0; j<A.cols(); j++)
496 A.e(i,j) = e(M1+i,J.start()+j);
501 template <
int M,
class AT>
503 #ifndef FMATVEC_NO_BOUNDS_CHECK
509 for(
int i=0; i<A.rows(); i++)
510 for(
int j=0; j<A.cols(); j++)
511 A.e(i,j) = e(I.start()+i,J.start()+j);
516 template <
int M,
class AT>
519 #ifndef FMATVEC_NO_BOUNDS_CHECK
526 for(
int j=0; j<N; j++)
533 template <
int M,
class AT>
534 inline const Vector<Fixed<M>,AT> Matrix<General,Fixed<M>,Var,AT>::col(
int j)
const {
536 #ifndef FMATVEC_NO_BOUNDS_CHECK
541 Vector<Fixed<M>,AT> x(NONINIT);
543 for(
int i=0; i<M; i++)
550 template <
int M,
class AT>
551 inline const Matrix<General,Var,Fixed<M>,AT> Matrix<General,Fixed<M>,Var,AT>::T()
const {
552 Matrix<General,Var,Fixed<M>,AT> A(cols(),NONINIT);
553 for(
int i=0; i<N; i++)
554 for(
int j=0; j<M; j++)
559 template <
int M,
class AT>
template <
class Row>
560 inline void Matrix<General,Fixed<M>,Var,AT>::set(
int j,
const Vector<Row,AT> &x) {
561 #ifndef FMATVEC_NO_BOUNDS_CHECK
563 assert(rows()==x.size());
565 for(
int i=0; i<rows(); i++)
569 template <
int M,
class AT>
template <
class Col>
570 inline void Matrix<General,Fixed<M>,Var,AT>::set(
int i,
const RowVector<Col,AT> &x) {
571 #ifndef FMATVEC_NO_BOUNDS_CHECK
573 assert(cols()==x.size());
575 for(
int j=0; j<cols(); j++)
579 template <
int M,
class AT>
template<
class Type,
class Row,
class Col>
580 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) {
582 #ifndef FMATVEC_NO_BOUNDS_CHECK
583 assert(I.end()<rows());
584 assert(J.end()<cols());
585 assert(I.size()==A.rows());
586 assert(J.size()==A.cols());
589 for(
int i=I.start(), ii=0; i<=I.end(); i++, ii++)
590 for(
int j=J.start(), jj=0; j<=J.end(); j++, jj++)
594 template <
int M,
class AT>
template <
class Row>
595 inline void Matrix<General,Fixed<M>,Var,AT>::add(
int j,
const Vector<Row,AT> &x) {
596 #ifndef FMATVEC_NO_BOUNDS_CHECK
598 assert(rows()==x.size());
600 for(
int i=0; i<rows(); i++)
604 template <
int M,
class AT>
template <
class Col>
605 inline void Matrix<General,Fixed<M>,Var,AT>::add(
int i,
const RowVector<Col,AT> &x) {
606 #ifndef FMATVEC_NO_BOUNDS_CHECK
608 assert(cols()==x.size());
610 for(
int j=0; j<cols(); j++)
614 template <
int M,
class AT>
template<
class Type,
class Row,
class Col>
615 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) {
617 #ifndef FMATVEC_NO_BOUNDS_CHECK
618 assert(I.end()<rows());
619 assert(J.end()<cols());
620 assert(I.size()==A.rows());
621 assert(J.size()==A.cols());
624 for(
int i=I.start(), ii=0; i<=I.end(); i++, ii++)
625 for(
int j=J.start(), jj=0; j<=J.end(); j++, jj++)
626 e(i,j) += A.e(ii,jj);
629 template <
int M,
class AT>
631 std::vector<std::vector<AT> > ret(rows());
632 for(
int r=0; r<rows(); r++) {
633 ret[r].resize(cols());
634 for(
int c=0; c<cols(); c++)
640 template <
int M,
class AT>
642 #ifndef FMATVEC_NO_SIZE_CHECK
643 assert(m.size() == M);
644 assert(m[0].size() == N);
646 for(
int r=0; r<rows(); r++) {
647 assert(m[r].size()==cols());
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++)
void resize(int m, int n)
Definition: fixed_var_general_matrix.h:148
AT & operator()(int i, int j)
Element operator.
Definition: fixed_var_general_matrix.h:179
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
Matrix()
Standard constructor.
Definition: fixed_var_general_matrix.h:63
This is a matrix class for general matrices.
Definition: var_general_matrix.h:38
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:86
int rows() const
Number of rows.
Definition: fixed_var_general_matrix.h:246
const AT & operator()(int i, int j) const
Element operator.
Definition: fixed_var_general_matrix.h:194
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: fixed_var_general_matrix.h:282
const AT & e(int i) const
Element operator.
Definition: fixed_var_general_matrix.h:225
AT * operator()()
Pointer operator.
Definition: fixed_var_general_matrix.h:234
const AT & e(int i, int j) const
Element operator.
Definition: fixed_var_general_matrix.h:213
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:252
~Matrix()
Destructor.
Definition: fixed_var_general_matrix.h:124
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:261
int ldim() const
Leading dimension.
Definition: fixed_var_general_matrix.h:258
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: fixed_var_general_matrix.h:271
const AT * operator()() const
Pointer operator.
Definition: fixed_var_general_matrix.h:240
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:88
This is a vector class of general shape in dense storage format.
Definition: var_row_vector.h:39