22 #ifndef fixed_general_matrix_h
23 #define fixed_general_matrix_h
43 typedef AT value_type;
69 Matrix(
Init ini=INIT,
const AT &a=0) { init(a); }
70 Matrix(
Eye ini,
const AT &a=1) { init(ini,a); }
72 Matrix(
int m,
int n,
Init ini=INIT,
const AT &a=0) { init(a); }
73 Matrix(
int m,
int n,
Eye ini,
const AT &a=1) { init(ini,a); }
75 template<
class Row,
class Col>
77 #ifndef FMATVEC_NO_SIZE_CHECK
78 assert(A.
rows() == M);
79 assert(A.
cols() == N);
85 template<
class Type,
class Row,
class Col>
88 #ifndef FMATVEC_NO_SIZE_CHECK
89 assert(A.
rows() == M);
90 assert(A.
cols() == N);
108 Matrix(
const std::string &strs);
111 template <
class Type,
class Row,
class Col>
125 #ifndef FMATVEC_NO_BOUNDS_CHECK
140 #ifndef FMATVEC_NO_BOUNDS_CHECK
150 AT& e(
int i,
int j) {
158 const AT&
e(
int i,
int j)
const {
202 throw std::runtime_error(
"A fixed matrix cannot be resized.");
223 return CblasRowMajor;
279 template <
int M1,
int M2,
int N1,
int N2>
281 template <
int M1,
int M2>
284 template <
int N1,
int N2>
299 inline Matrix<General,Fixed<M>,Fixed<N>,AT>& init(Eye,
const AT &val=1);
300 inline Matrix<General,Fixed<M>,Fixed<N>,AT>& init(Noinit,
const AT &a=0) {
return *
this; }
306 inline operator std::vector<std::vector<AT> >()
const;
313 Matrix(std::vector<std::vector<AT> > m);
315 inline const Matrix<General,Fixed<N>,Fixed<M>,AT> T()
const;
320 template<
class Row>
inline void set(
int j,
const Vector<Row,AT> &x);
325 template<
class Col>
inline void set(
int i,
const RowVector<Col,AT> &x);
333 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);
338 template<
class Row>
inline void add(
int j,
const Vector<Row,AT> &x);
343 template<
class Col>
inline void add(
int i,
const RowVector<Col,AT> &x);
345 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);
349 template <
int M,
int N,
class AT>
351 std::istringstream iss(strs);
357 throw std::runtime_error(
"Input not fully read.");
362 template <
int M,
int N,
class AT>
template<
class Type,
class Row,
class Col>
363 inline Matrix<General,Fixed<M>,Fixed<N>,AT>& Matrix<General,Fixed<M>,Fixed<N>,AT>::operator=(
const Matrix<Type,Row,Col,AT> &A) {
365 #ifndef FMATVEC_NO_SIZE_CHECK
366 assert(A.rows() == M);
367 assert(A.cols() == N);
375 template <
int M,
int N,
class AT>
377 for(
int i=0; i<M; i++)
378 for(
int j=0; j<N; j++)
383 template <
int M,
int N,
class AT>
385 for(
int i=0; i<M; i++)
386 for(
int j=0; j<N; j++)
387 e(i,j) = (i==j) ? val : 0;
391 template <
int M,
int N,
class AT>
393 #ifndef FMATVEC_NO_BOUNDS_CHECK
399 for(
int i=0; i<A.rows(); i++)
400 for(
int j=0; j<A.cols(); j++)
401 A.e(i,j) = e(I.start()+i,J.start()+j);
406 template <
int M,
int N,
class AT>
template <
int M1,
int M2,
int N1,
int N2>
408 #ifndef FMATVEC_NO_BOUNDS_CHECK
414 for(
int i=0; i<A.rows(); i++)
415 for(
int j=0; j<A.cols(); j++)
416 A.e(i,j) = e(M1+i,J.start()+j);
421 template <
int M,
int N,
class AT>
template <
int M1,
int M2>
422 inline const Matrix<General,Fixed<M2-M1+1>,Var,AT> Matrix<General,Fixed<M>,Fixed<N>,AT>::operator()(
const Range<Fixed<M1>,Fixed<M2> > &I,
const Range<Var,Var> &J)
const {
423 #ifndef FMATVEC_NO_BOUNDS_CHECK
427 Matrix<General,Fixed<M2-M1+1>,Var,AT> A(J.end()-J.start()+1,NONINIT);
429 for(
int i=0; i<A.rows(); i++)
430 for(
int j=0; j<A.cols(); j++)
431 A.e(i,j) = e(I.start()+i,J.start()+j);
436 template <
int M,
int N,
class AT>
template <
int N1,
int N2>
437 inline const Matrix<General,Var,Fixed<N2-N1+1>,AT> Matrix<General,Fixed<M>,Fixed<N>,AT>::operator()(
const Range<Var,Var> &I,
const Range<Fixed<N1>,Fixed<N2> > &J)
const {
438 #ifndef FMATVEC_NO_BOUNDS_CHECK
442 Matrix<General,Var,Fixed<N2-N1+1>,AT> A(I.end()-I.start()+1,NONINIT);
444 for(
int i=0; i<A.rows(); i++)
445 for(
int j=0; j<A.cols(); j++)
446 A.e(i,j) = e(I.start()+i,J.start()+j);
451 template <
int M,
int N,
class AT>
452 inline const RowVector<Fixed<N>,AT> Matrix<General,Fixed<M>,Fixed<N>,AT>::row(
int i)
const {
454 #ifndef FMATVEC_NO_BOUNDS_CHECK
459 RowVector<Fixed<N>,AT> x(NONINIT);
461 for(
int j=0; j<N; j++)
468 template <
int M,
int N,
class AT>
469 inline const Vector<Fixed<M>,AT> Matrix<General,Fixed<M>,Fixed<N>,AT>::col(
int j)
const {
471 #ifndef FMATVEC_NO_BOUNDS_CHECK
476 Vector<Fixed<M>,AT> x(NONINIT);
478 for(
int i=0; i<M; i++)
485 template <
int M,
int N,
class AT>
486 inline const Matrix<General,Fixed<N>,Fixed<M>,AT> Matrix<General,Fixed<M>,Fixed<N>,AT>::T()
const {
487 Matrix<General,Fixed<N>,Fixed<M>,AT> A(NONINIT);
488 for(
int i=0; i<N; i++)
489 for(
int j=0; j<M; j++)
494 template <
int M,
int N,
class AT>
template <
class Row>
496 #ifndef FMATVEC_NO_BOUNDS_CHECK
498 assert(rows()==x.size());
500 for(
int i=0; i<rows(); i++)
504 template <
int M,
int N,
class AT>
template <
class Col>
506 #ifndef FMATVEC_NO_BOUNDS_CHECK
508 assert(cols()==x.size());
510 for(
int j=0; j<cols(); j++)
514 template <
int M,
int N,
class AT>
template<
class Type,
class Row,
class Col>
515 inline void Matrix<General,Fixed<M>,
Fixed<N>,AT>::set(
const Range<Var,Var> &I,
const Range<Var,Var> &J,
const Matrix<Type,Row,Col,AT> &A) {
517 #ifndef FMATVEC_NO_BOUNDS_CHECK
518 assert(I.
end()<rows());
519 assert(J.
end()<cols());
524 for(
int i=I.
start(), ii=0; i<=I.
end(); i++, ii++)
525 for(
int j=J.
start(), jj=0; j<=J.
end(); j++, jj++)
529 template <
int M,
int N,
class AT>
template <
class Row>
531 #ifndef FMATVEC_NO_BOUNDS_CHECK
533 assert(rows()==x.size());
535 for(
int i=0; i<rows(); i++)
539 template <
int M,
int N,
class AT>
template <
class Col>
541 #ifndef FMATVEC_NO_BOUNDS_CHECK
543 assert(cols()==x.size());
545 for(
int j=0; j<cols(); j++)
549 template <
int M,
int N,
class AT>
template<
class Type,
class Row,
class Col>
550 inline void Matrix<General,Fixed<M>,
Fixed<N>,AT>::add(
const Range<Var,Var> &I,
const Range<Var,Var> &J,
const Matrix<Type,Row,Col,AT> &A) {
552 #ifndef FMATVEC_NO_BOUNDS_CHECK
553 assert(I.
end()<rows());
554 assert(J.
end()<cols());
559 for(
int i=I.
start(), ii=0; i<=I.
end(); i++, ii++)
560 for(
int j=J.
start(), jj=0; j<=J.
end(); j++, jj++)
561 e(i,j) += A.e(ii,jj);
564 template <
int M,
int N,
class AT>
566 std::vector<std::vector<AT> > ret(rows());
567 for(
int r=0; r<rows(); r++) {
568 ret[r].resize(cols());
569 for(
int c=0; c<cols(); c++)
575 template <
int M,
int N,
class AT>
578 throw std::runtime_error(
"The input has "+std::to_string(m.size())+
" rows but "+std::to_string(M)+
" rows are required.");
580 throw std::runtime_error(
"The input has "+std::to_string(m[0].size())+
" columne but "+std::to_string(N)+
" columns are required.");
581 for(
int r=0; r<rows(); r++) {
582 if(static_cast<int>(m[r].size())!=cols())
583 throw std::runtime_error(
"The rows of the input have different length.");
584 for(
int c=0; c<cols(); c++)
591 template <
int M,
int N,
class AT>
template <
class Type,
class Row,
class Col>
593 for(
int i=0; i<M; i++)
594 for(
int j=0; j<N; j++)
598 template<
int M,
int N,
class AT>
template<
class Row>
599 inline void Matrix<General,Fixed<M>,Fixed<N>,AT>::deepCopy(
const Matrix<Symmetric,Row,Row,AT> &A) {
600 for(
int i=0; i<A.size(); i++) {
602 for(
int j=i+1; j<A.size(); j++)
603 e(i,j) = e(j,i) = A.ej(i,j);
619 typedef double value_type;
623 Matrix(
Init ini=INIT,
const double &a=0) { this->init(a); }
624 Matrix(
Eye ini,
const double &a=1) { this->init(ini,a); }
626 Matrix(
int m,
int n,
Init ini,
const double &a=0) { this->init(a); }
627 Matrix(
int m,
int n,
Eye ini,
const double &a=1) { this->init(ini,a); }
628 explicit Matrix(
const Matrix<General,Fixed<M>,Fixed<M>,
double>& A) { this->deepCopy(A); }
632 #ifndef FMATVEC_NO_SIZE_CHECK
633 assert(A.size() == M);
638 template<
class Type,
class Row,
class Col>
640 #ifndef FMATVEC_NO_SIZE_CHECK
641 assert(A.
rows() == M);
642 assert(A.
cols() == M);
const AT & operator()(int i, int j) const
Element operator.
Definition: fixed_general_matrix.h:139
bool transposed() const
A fixed matrix is stored in c-storage order -> transposed is always true.
Definition: fixed_general_matrix.h:194
const AT * operator()() const
Pointer operator.
Definition: fixed_general_matrix.h:173
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
AT * operator()()
Pointer operator.
Definition: fixed_general_matrix.h:167
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
Shape class for rotation matrices.
Definition: types.h:124
int rows() const
Number of rows.
int cols() const
Number of columns.
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: fixed_general_matrix.h:222
int cols() const
Number of columns.
Definition: fixed_general_matrix.h:185
const AT & e(int i, int j) const
Element operator.
Definition: fixed_general_matrix.h:158
int rows() const
Number of rows.
Definition: fixed_general_matrix.h:179
This is an index class for creating submatrices.
Definition: range.h:35
int size() const
Size.
Definition: range.h:101
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: fixed_general_matrix.h:211
int ldim() const
Leading dimension.
Definition: fixed_general_matrix.h:191
AT & operator()(int i, int j)
Element operator.
Definition: fixed_general_matrix.h:124
Basic shape class for matrices.
Definition: types.h:100
void resize(int m, int n)
Definition: fixed_general_matrix.h:200
int end() const
Last element.
Definition: range.h:95
This is an index class for creating submatrices.
Definition: range.h:44
int start() const
First element.
Definition: range.h:89