22 #ifndef var_fixed_general_matrix_h
23 #define var_fixed_general_matrix_h
42 typedef AT value_type;
59 Matrix() : ele(
nullptr) { }
71 Matrix(
int m,
Init ini=INIT,
const AT &a=0) : M(m), ele(
new AT[M*N]) { init(a); }
72 Matrix(
int m,
Eye ini,
const AT &a=1) : M(m), ele(
new AT[M*N]) { init(ini,a); }
73 Matrix(
int m,
int n,
Noinit) : M(m), ele(
new AT[M*N]) { }
74 Matrix(
int m,
int n,
Init ini=INIT,
const AT &a=0) : M(m), ele(
new AT[M*N]) { init(a); }
75 Matrix(
int m,
int n,
Eye ini,
const AT &a=1) : M(m), ele(
new AT[M*N]) { init(ini,a); }
88 template<
class Row,
class Col>
94 template<
class Type,
class Row,
class Col>
95 explicit Matrix(
const Matrix<Type,Row,Col,AT> &A) : M(A.
rows()), ele(new AT[M*N]) {
97 #ifndef FMATVEC_NO_SIZE_CHECK
98 assert(A.cols() == N);
116 Matrix(
const std::string &strs);
117 Matrix(
const char *strs);
132 Matrix<General,Var,Fixed<N>,AT>& resize(
int m, Noinit) {
139 Matrix<General,Var,Fixed<N>,AT>& resize(
int m, Init ini=INIT,
const AT &a=0) {
return resize(m,Noinit()).init(a); }
141 Matrix<General,Var,Fixed<N>,AT>& resize(
int m, Eye ini,
const AT &a=1) {
return resize(m,Noinit()).init(ini,a); }
147 throw std::runtime_error(
"A var-fixed matrix can only be resized in the first dimension.");
159 template <
class Type,
class Row,
class Col>
162 template <
class Type,
class Row,
class Col>
177 #ifndef FMATVEC_NO_BOUNDS_CHECK
192 #ifndef FMATVEC_NO_BOUNDS_CHECK
202 AT& e(
int i,
int j) {
210 const AT&
e(
int i,
int j)
const {
222 const AT&
e(
int i)
const {
280 return CblasColMajor;
297 inline Matrix<General,Var,Fixed<N>,AT>& init(Eye,
const AT &val=1);
298 inline Matrix<General,Var,Fixed<N>,AT>& init(Noinit,
const AT &a=0) {
return *
this; }
304 inline operator std::vector<std::vector<AT> >()
const;
311 inline Matrix(std::vector<std::vector<AT> > m);
313 inline const Matrix<General,Fixed<N>,Var,AT> T()
const;
315 template<
class Row>
inline void set(
int j,
const Vector<Row,AT> &x);
317 template<
class Col>
inline void set(
int i,
const RowVector<Col,AT> &x);
319 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);
321 template<
class Row>
inline void add(
int j,
const Vector<Row,AT> &x);
323 template<
class Col>
inline void add(
int i,
const RowVector<Col,AT> &x);
325 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);
329 template <
int N,
class AT>
331 std::istringstream iss(strs);
337 throw std::runtime_error(
"Input not fully read.");
342 template <
int N,
class AT>
template<
class Type,
class Row,
class Col>
343 inline Matrix<General,Var,Fixed<N>,AT>& Matrix<General,Var,Fixed<N>,AT>::operator=(
const Matrix<Type,Row,Col,AT> &A) {
345 #ifndef FMATVEC_NO_SIZE_CHECK
346 assert(N == A.cols());
353 #ifndef FMATVEC_NO_SIZE_CHECK
354 assert(M == A.rows());
363 template <
int N,
class AT>
371 #ifndef FMATVEC_NO_SIZE_CHECK
372 assert(M == A.rows());
381 template <
int N,
class AT>
template<
class Type,
class Row,
class Col>
384 #ifndef FMATVEC_NO_SIZE_CHECK
385 assert(N == A.cols());
398 template <
int N,
class AT>
400 for(
int i=0; i<M*N; i++)
405 template <
int N,
class AT>
407 for(
int i=0; i<M; i++)
408 for(
int j=0; j<N; j++)
409 e(i,j) = (i==j) ? val : 0;
413 template <
int N,
class AT>
414 inline const Matrix<General,Var,Var,AT> Matrix<General,Var,Fixed<N>,AT>::operator()(
const Range<Var,Var> &I,
const Range<Var,Var> &J)
const {
415 #ifndef FMATVEC_NO_BOUNDS_CHECK
419 Matrix<General,Var,Var,AT> A(I.end()-I.start()+1,J.end()-J.start()+1,NONINIT);
421 for(
int i=0; i<A.rows(); i++)
422 for(
int j=0; j<A.cols(); j++)
423 A.e(i,j) = e(I.start()+i,J.start()+j);
428 template <
int N,
class AT>
429 inline const RowVector<Fixed<N>,AT> Matrix<General,Var,Fixed<N>,AT>::row(
int i)
const {
431 #ifndef FMATVEC_NO_BOUNDS_CHECK
436 RowVector<Fixed<N>,AT> x(NONINIT);
438 for(
int j=0; j<N; j++)
445 template <
int N,
class AT>
446 inline const Vector<Var,AT> Matrix<General,Var,Fixed<N>,AT>::col(
int j)
const {
448 #ifndef FMATVEC_NO_BOUNDS_CHECK
453 Vector<Var,AT> x(M,NONINIT);
455 for(
int i=0; i<M; i++)
462 template <
int N,
class AT>
463 inline const Matrix<General,Fixed<N>,Var,AT> Matrix<General,Var,Fixed<N>,AT>::T()
const {
464 Matrix<General,Fixed<N>,Var,AT> A(rows(),NONINIT);
465 for(
int i=0; i<N; i++)
466 for(
int j=0; j<M; j++)
471 template <
int N,
class AT>
template <
class Row>
472 inline void Matrix<General,Var,Fixed<N>,AT>::set(
int j,
const Vector<Row,AT> &x) {
473 #ifndef FMATVEC_NO_BOUNDS_CHECK
475 assert(rows()==x.size());
477 for(
int i=0; i<rows(); i++)
481 template <
int N,
class AT>
template <
class Col>
482 inline void Matrix<General,Var,Fixed<N>,AT>::set(
int i,
const RowVector<Col,AT> &x) {
483 #ifndef FMATVEC_NO_BOUNDS_CHECK
485 assert(cols()==x.size());
487 for(
int j=0; j<cols(); j++)
491 template <
int N,
class AT>
template<
class Type,
class Row,
class Col>
492 inline void Matrix<General,Var,Fixed<N>,AT>::set(
const Range<Var,Var> &I,
const Range<Var,Var> &J,
const Matrix<Type,Row,Col,AT> &A) {
494 #ifndef FMATVEC_NO_BOUNDS_CHECK
495 assert(I.end()<rows());
496 assert(J.end()<cols());
497 assert(I.size()==A.rows());
498 assert(J.size()==A.cols());
501 for(
int i=I.start(), ii=0; i<=I.end(); i++, ii++)
502 for(
int j=J.start(), jj=0; j<=J.end(); j++, jj++)
506 template <
int N,
class AT>
template <
class Row>
507 inline void Matrix<General,Var,Fixed<N>,AT>::add(
int j,
const Vector<Row,AT> &x) {
508 #ifndef FMATVEC_NO_BOUNDS_CHECK
510 assert(rows()==x.size());
512 for(
int i=0; i<rows(); i++)
516 template <
int N,
class AT>
template <
class Col>
517 inline void Matrix<General,Var,Fixed<N>,AT>::add(
int i,
const RowVector<Col,AT> &x) {
518 #ifndef FMATVEC_NO_BOUNDS_CHECK
520 assert(cols()==x.size());
522 for(
int j=0; j<cols(); j++)
526 template <
int N,
class AT>
template<
class Type,
class Row,
class Col>
527 inline void Matrix<General,Var,Fixed<N>,AT>::add(
const Range<Var,Var> &I,
const Range<Var,Var> &J,
const Matrix<Type,Row,Col,AT> &A) {
529 #ifndef FMATVEC_NO_BOUNDS_CHECK
530 assert(I.end()<rows());
531 assert(J.end()<cols());
532 assert(I.size()==A.rows());
533 assert(J.size()==A.cols());
536 for(
int i=I.start(), ii=0; i<=I.end(); i++, ii++)
537 for(
int j=J.start(), jj=0; j<=J.end(); j++, jj++)
538 e(i,j) += A.e(ii,jj);
541 template <
int N,
class AT>
543 std::vector<std::vector<AT> > ret(rows());
544 for(
int r=0; r<rows(); r++) {
545 ret[r].resize(cols());
546 for(
int c=0; c<cols(); c++)
552 template <
int N,
class AT>
555 throw std::runtime_error(
"The input must have at least 1 row.");
557 throw std::runtime_error(
"The input has "+std::to_string(m[0].size())+
" columns but "+std::to_string(N)+
" columns are required.");
558 for(
int r=0; r<
rows(); r++) {
559 if(static_cast<int>(m[r].size())!=
cols())
560 throw std::runtime_error(
"The rows of the input have different length.");
561 for(
int c=0; c<
cols(); c++)
568 template <
int N,
class AT>
template <
class Type,
class Row,
class Col>
570 for(
int i=0; i<M; i++)
571 for(
int j=0; j<N; j++)
const AT & operator()(int i, int j) const
Element operator.
Definition: var_fixed_general_matrix.h:191
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
This is a vector class of general shape in dense storage format.
Definition: var_vector.h:39
Matrix(const Matrix< General, Var, Fixed< N >, AT > &A)
Copy Constructor.
Definition: var_fixed_general_matrix.h:84
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
~Matrix()
Destructor.
Definition: var_fixed_general_matrix.h:121
int rows() const
Number of rows.
Definition: var_fixed_general_matrix.h:243
void resize(int m, int n)
Definition: var_fixed_general_matrix.h:145
bool transposed() const
A var-fixed matrix is stored in fortran-storage order -> return always false.
Definition: var_fixed_general_matrix.h:258
int rows() const
Number of rows.
const AT & e(int i) const
Element operator.
Definition: var_fixed_general_matrix.h:222
int cols() const
Number of columns.
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: var_fixed_general_matrix.h:268
const AT & e(int i, int j) const
Element operator.
Definition: var_fixed_general_matrix.h:210
int ldim() const
Leading dimension.
Definition: var_fixed_general_matrix.h:255
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: var_fixed_general_matrix.h:279
AT * operator()()
Pointer operator.
Definition: var_fixed_general_matrix.h:231
Basic shape class for matrices.
Definition: types.h:100
const AT * operator()() const
Pointer operator.
Definition: var_fixed_general_matrix.h:237
int cols() const
Number of columns.
Definition: var_fixed_general_matrix.h:249
This is an index class for creating submatrices.
Definition: range.h:44
AT & operator()(int i, int j)
Element operator.
Definition: var_fixed_general_matrix.h:176