22 #ifndef var_general_matrix_h
23 #define var_general_matrix_h
42 typedef AT value_type;
70 Matrix(
int m,
int n,
Noinit) : M(m), N(n), ele(new AT[M*N]) { }
71 Matrix(
int m,
int n, Init ini=INIT,
const AT &a=0) : M(m), N(n), ele(new AT[M*N]) { init(a); }
72 Matrix(
int m,
int n, Eye ini,
const AT &a=1) : M(m), N(n), ele(new AT[M*N]) { init(ini,a); }
85 template<
class Row,
class Col>
91 template<
class Type,
class Row,
class Col>
92 explicit Matrix(
const Matrix<Type,Row,Col,AT> &A) : M(A.
rows()), N(A.
cols()), ele(new AT[M*N]) {
109 Matrix(
const std::string &strs);
110 Matrix(
const char *strs);
125 Matrix<General,Var,Var,AT>& resize(
int m,
int n, Noinit) {
133 Matrix<General,Var,Var,AT>& resize(
int m,
int n, Init ini=INIT,
const AT &a=0) {
return resize(m,n,Noinit()).
init(a); }
135 Matrix<General,Var,Var,AT>& resize(
int m,
int n, Eye ini,
const AT &a=1) {
return resize(m,n,Noinit()).
init(ini,a); }
143 inline Matrix<General,Var,Var,AT>& operator=(
const Matrix<General,Var,Var,AT> &A);
145 template <
class Type,
class Row,
class Col>
146 inline Matrix<General,Var,Var,AT>& operator=(
const Matrix<Type,Row,Col,AT> &A);
154 template<
class Type,
class Row,
class Col>
155 inline Matrix<General,Var,Var,AT>& operator<<(const Matrix<Type,Row,Col,AT> &A);
169 #ifndef FMATVEC_NO_BOUNDS_CHECK
184 #ifndef FMATVEC_NO_BOUNDS_CHECK
194 AT& e(
int i,
int j) {
202 const AT&
e(
int i,
int j)
const {
214 const AT&
e(
int i)
const {
272 return CblasColMajor;
309 template <
int M1,
int M2,
int N1,
int N2>
312 template <
int M1,
int M2>
315 template <
int N1,
int N2>
330 inline Matrix<General,Var,Var,AT>& init(Eye eye,
const AT &val=1);
331 inline Matrix<General,Var,Var,AT>& init(Noinit,
const AT &a=0) {
return *
this; }
337 inline operator std::vector<std::vector<AT> >()
const;
344 inline Matrix(std::vector<std::vector<AT> > m);
346 inline const Matrix<General,Var,Var,AT> T()
const;
348 template<
class Row>
inline void set(
int j,
const Vector<Row,AT> &x);
350 template<
class Col>
inline void set(
int i,
const RowVector<Col,AT> &x);
352 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);
354 template<
class Row>
inline void add(
int j,
const Vector<Row,AT> &x);
356 template<
class Col>
inline void add(
int i,
const RowVector<Col,AT> &x);
358 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);
364 std::istringstream iss(strs);
370 throw std::runtime_error(
"Input not fully read.");
374 template <
class AT>
template<
class Type,
class Row,
class Col>
383 #ifndef FMATVEC_NO_SIZE_CHECK
384 assert(M == A.rows());
385 assert(N == A.cols());
403 #ifndef FMATVEC_NO_SIZE_CHECK
404 assert(M == A.
rows());
405 assert(N == A.
cols());
416 for(
int i=0; i<M*N; i++)
423 for(
int i=0; i<M; i++)
424 for(
int j=0; j<N; j++)
425 e(i,j) = (i==j) ? val : 0;
429 template <
class AT>
template<
class Type,
class Row,
class Col>
446 #ifndef FMATVEC_NO_BOUNDS_CHECK
452 for(
int i=0; i<A.rows(); i++)
453 for(
int j=0; j<A.cols(); j++)
459 template <
class AT>
template <
int M1,
int M2,
int N1,
int N2>
461 #ifndef FMATVEC_NO_BOUNDS_CHECK
467 for(
int i=0; i<A.rows(); i++)
468 for(
int j=0; j<A.cols(); j++)
469 A.e(i,j) = e(I.start()+i,J.start()+j);
474 template <
class AT>
template <
int M1,
int M2>
476 #ifndef FMATVEC_NO_BOUNDS_CHECK
480 Matrix<General,Fixed<M2-M1+1>,Var,AT> A(J.end()-J.start()+1,NONINIT);
482 for(
int i=0; i<A.rows(); i++)
483 for(
int j=0; j<A.cols(); j++)
484 A.e(i,j) = e(I.start()+i,J.start()+j);
489 template <
class AT>
template <
int N1,
int N2>
491 #ifndef FMATVEC_NO_BOUNDS_CHECK
495 Matrix<General,Var,Fixed<N2-N1+1>,AT> A(I.end()-I.start()+1,NONINIT);
497 for(
int i=0; i<A.rows(); i++)
498 for(
int j=0; j<A.cols(); j++)
499 A.e(i,j) = e(I.start()+i,J.start()+j);
505 inline const RowVector<Var,AT> Matrix<General,Var,Var,AT>::row(
int i)
const {
507 #ifndef FMATVEC_NO_BOUNDS_CHECK
512 RowVector<Var,AT> x(N,NONINIT);
514 for(
int j=0; j<N; j++)
522 inline const Vector<Var,AT> Matrix<General,Var,Var,AT>::col(
int j)
const {
524 #ifndef FMATVEC_NO_BOUNDS_CHECK
529 Vector<Var,AT> x(M,NONINIT);
531 for(
int i=0; i<M; i++)
539 inline const Matrix<General,Var,Var,AT> Matrix<General,Var,Var,AT>::T()
const {
540 Matrix<General,Var,Var,AT> A(N,M,NONINIT);
541 for(
int i=0; i<N; i++)
542 for(
int j=0; j<M; j++)
547 template <
class AT>
template <
class Row>
548 inline void Matrix<General,Var,Var,AT>::set(
int j,
const Vector<Row,AT> &x) {
549 #ifndef FMATVEC_NO_BOUNDS_CHECK
551 assert(
rows()==x.size());
553 for(
int i=0; i<
rows(); i++)
557 template <
class AT>
template <
class Col>
558 inline void Matrix<General,Var,Var,AT>::set(
int i,
const RowVector<Col,AT> &x) {
559 #ifndef FMATVEC_NO_BOUNDS_CHECK
561 assert(
cols()==x.size());
563 for(
int j=0; j<
cols(); j++)
567 template <
class AT>
template<
class Type,
class Row,
class Col>
568 inline void Matrix<General,Var,Var,AT>::set(
const Range<Var,Var> &I,
const Range<Var,Var> &J,
const Matrix<Type,Row,Col,AT> &A) {
570 #ifndef FMATVEC_NO_BOUNDS_CHECK
571 assert(I.end()<
rows());
572 assert(J.end()<
cols());
573 assert(I.size()==A.rows());
574 assert(J.size()==A.cols());
577 for(
int i=I.start(), ii=0; i<=I.end(); i++, ii++)
578 for(
int j=J.start(), jj=0; j<=J.end(); j++, jj++)
582 template <
class AT>
template <
class Row>
583 inline void Matrix<General,Var,Var,AT>::add(
int j,
const Vector<Row,AT> &x) {
584 #ifndef FMATVEC_NO_BOUNDS_CHECK
586 assert(
rows()==x.size());
588 for(
int i=0; i<
rows(); i++)
592 template <
class AT>
template <
class Col>
593 inline void Matrix<General,Var,Var,AT>::add(
int i,
const RowVector<Col,AT> &x) {
594 #ifndef FMATVEC_NO_BOUNDS_CHECK
596 assert(
cols()==x.size());
598 for(
int j=0; j<
cols(); j++)
602 template <
class AT>
template<
class Type,
class Row,
class Col>
603 inline void Matrix<General,Var,Var,AT>::add(
const Range<Var,Var> &I,
const Range<Var,Var> &J,
const Matrix<Type,Row,Col,AT> &A) {
605 #ifndef FMATVEC_NO_BOUNDS_CHECK
606 assert(I.end()<
rows());
607 assert(J.end()<
cols());
608 assert(I.size()==A.rows());
609 assert(J.size()==A.cols());
612 for(
int i=I.start(), ii=0; i<=I.end(); i++, ii++)
613 for(
int j=J.start(), jj=0; j<=J.end(); j++, jj++)
614 e(i,j) += A.e(ii,jj);
619 std::vector<std::vector<AT> > ret(rows());
620 for(
int r=0; r<rows(); r++) {
621 ret[r].resize(cols());
622 for(
int c=0; c<cols(); c++)
630 for(
int r=0; r<
rows(); r++) {
631 if(static_cast<int>(m[r].size())!=
cols())
632 throw std::runtime_error(
"The rows of the input have different length.");
633 for(
int c=0; c<
cols(); c++)
640 template <
class AT>
template <
class Type,
class Row,
class Col>
642 for(
int i=0; i<M; i++)
643 for(
int j=0; j<N; j++)
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
~Matrix()
Destructor.
Definition: var_general_matrix.h:114
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: var_general_matrix.h:271
This is a vector class of general shape in dense storage format.
Definition: var_vector.h:39
This is a matrix class for general matrices.
Definition: var_general_matrix.h:39
AT & operator()(int i, int j)
Element operator.
Definition: var_general_matrix.h:168
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:90
const AT & operator()(int i, int j) const
Element operator.
Definition: var_general_matrix.h:183
bool transposed() const
The storage format of a var matrix is fortran-storage order -> return always false.
Definition: var_general_matrix.h:250
int rows() const
Number of rows.
Definition: var_general_matrix.h:235
const AT * operator()() const
Pointer operator.
Definition: var_general_matrix.h:229
int rows() const
Number of rows.
int cols() const
Number of columns.
Matrix< General, Var, Var, AT > & init(const AT &val=0)
Initialization.
Definition: var_general_matrix.h:415
Matrix()
Standard constructor.
Definition: var_general_matrix.h:62
int cols() const
Number of columns.
Definition: var_general_matrix.h:241
Matrix< General, Var, Var, AT > & operator=(const Matrix< General, Var, Var, AT > &A)
Assignment operator.
Definition: var_general_matrix.h:395
Matrix(const Matrix< General, Var, Var, AT > &A)
Copy Constructor.
Definition: var_general_matrix.h:81
const AT & e(int i, int j) const
Element operator.
Definition: var_general_matrix.h:202
This is an index class for creating submatrices.
Definition: range.h:35
int ldim() const
Leading dimension.
Definition: var_general_matrix.h:247
AT * operator()()
Pointer operator.
Definition: var_general_matrix.h:223
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: var_general_matrix.h:260
Basic shape class for matrices.
Definition: types.h:100
int end() const
Last element.
Definition: range.h:95
This is an index class for creating submatrices.
Definition: range.h:44
const AT & e(int i) const
Element operator.
Definition: var_general_matrix.h:214
This is a vector class of general shape in dense storage format.
Definition: var_row_vector.h:39
int start() const
First element.
Definition: range.h:89