22 #ifndef fixed_general_matrix_h 23 #define fixed_general_matrix_h 42 static constexpr
bool isVector {
false};
45 typedef const AT* const_iterator;
46 typedef AT value_type;
73 explicit Matrix(
Init ini=INIT,
const AT &a=AT()) { init(a); }
74 explicit Matrix(
Eye ini,
const AT &a=1) { init(ini,a); }
75 explicit Matrix(
int m,
int n,
Noinit) { FMATVEC_ASSERT(m==M && n==N, AT); }
76 explicit Matrix(
int m,
int n,
Init ini=INIT,
const AT &a=AT()) { FMATVEC_ASSERT(m==M && n==N, AT); init(a); }
77 explicit Matrix(
int m,
int n,
Eye ini,
const AT &a=1) { FMATVEC_ASSERT(m==M && n==N, AT); init(ini,a); }
91 template<
class Row,
class Col>
93 FMATVEC_ASSERT(A.
rows() == M, AT);
94 FMATVEC_ASSERT(A.
cols() == N, AT);
103 template<
class Type,
class Row,
class Col>
105 FMATVEC_ASSERT(A.
rows() == M, AT);
106 FMATVEC_ASSERT(A.
cols() == N, AT);
122 Matrix(
const std::string &strs);
139 template <
class Type,
class Row,
class Col>
141 FMATVEC_ASSERT(A.
rows() == M, AT);
142 FMATVEC_ASSERT(A.
cols() == N, AT);
152 template<
class Type,
class Row,
class Col>
158 for(
size_t r=0; r<M; ++r)
159 for(
size_t c=0; c<N; ++c)
160 ret(r,c) = (*this)(r,c);
164 iterator begin() {
return &ele[0][0]; }
165 iterator end() {
return &ele[M-1][N]; }
166 const_iterator begin()
const {
return &ele[0][0]; }
167 const_iterator end()
const {
return &ele[M-1][N]; }
179 FMATVEC_ASSERT(i>=0, AT);
180 FMATVEC_ASSERT(j>=0, AT);
181 FMATVEC_ASSERT(i<M, AT);
182 FMATVEC_ASSERT(j<N, AT);
192 FMATVEC_ASSERT(i>=0, AT);
193 FMATVEC_ASSERT(j>=0, AT);
194 FMATVEC_ASSERT(i<M, AT);
195 FMATVEC_ASSERT(j<N, AT);
200 AT& e(
int i,
int j) {
208 const AT&
e(
int i,
int j)
const {
229 constexpr
int rows()
const {
return M;}
235 constexpr
int cols()
const {
return N;}
247 throw std::runtime_error(
"A fixed matrix cannot be resized.");
268 return CblasRowMajor;
277 template <
int M1,
int M2,
int N1,
int N2>
279 template <
int M1,
int M2>
282 template <
int N1,
int N2>
297 inline Matrix<General,Fixed<M>,Fixed<N>,AT>& init(Eye,
const AT &val=1);
298 inline Matrix<General,Fixed<M>,Fixed<N>,AT>& init(Noinit,
const AT &a=AT()) {
return *
this; }
304 explicit inline operator std::vector<std::vector<AT>>()
const;
311 explicit Matrix(
const std::vector<std::vector<AT>> &m);
333 inline const Matrix<General,Fixed<N>,Fixed<M>,AT> T()
const;
338 template<
class Row>
inline void set(
int j,
const Vector<Row,AT> &x);
343 template<
class Col>
inline void set(
int i,
const RowVector<Col,AT> &x);
351 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);
356 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);
366 template <
int M,
int N,
class AT>
368 std::istringstream iss(strs);
374 throw std::runtime_error(
"Input not fully read.");
379 template <
int M,
int N,
class AT>
381 for(
int i=0; i<M; i++)
382 for(
int j=0; j<N; j++)
387 template <
int M,
int N,
class AT>
389 for(
int i=0; i<M; i++)
390 for(
int j=0; j<N; j++)
391 e(i,j) = (i==j) ? val : 0;
395 template <
int M,
int N,
class AT>
397 FMATVEC_ASSERT(I.end()<M, AT);
398 FMATVEC_ASSERT(J.end()<N, AT);
401 for(
int i=0; i<A.
rows(); i++)
402 for(
int j=0; j<A.
cols(); j++)
403 A.e(i,j) = e(I.start()+i,J.start()+j);
408 template <
int M,
int N,
class AT>
template <
int M1,
int M2,
int N1,
int N2>
410 FMATVEC_ASSERT(M2<M, AT);
411 FMATVEC_ASSERT(N2<N, AT);
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 FMATVEC_ASSERT(M2<M, AT);
424 FMATVEC_ASSERT(J.end()<N, AT);
425 Matrix<General,Fixed<M2-M1+1>,Var,AT> A(J.end()-J.start()+1,NONINIT);
427 for(
int i=0; i<A.
rows(); i++)
428 for(
int j=0; j<A.
cols(); j++)
429 A.e(i,j) = e(I.start()+i,J.start()+j);
434 template <
int M,
int N,
class AT>
template <
int N1,
int N2>
435 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 {
436 FMATVEC_ASSERT(I.end()<M, AT);
437 FMATVEC_ASSERT(N2<N, AT);
438 Matrix<General,Var,Fixed<N2-N1+1>,AT> A(I.end()-I.start()+1,NONINIT);
440 for(
int i=0; i<A.
rows(); i++)
441 for(
int j=0; j<A.
cols(); j++)
442 A.e(i,j) = e(I.start()+i,J.start()+j);
447 template <
int M,
int N,
class AT>
448 inline const RowVector<Fixed<N>,AT> Matrix<General,Fixed<M>,Fixed<N>,AT>::row(
int i)
const {
450 FMATVEC_ASSERT(i>=0, AT);
451 FMATVEC_ASSERT(i<M, AT);
453 RowVector<Fixed<N>,AT> x(NONINIT);
455 for(
int j=0; j<N; j++)
461 template <
int M,
int N,
class AT>
462 inline const Vector<Fixed<M>,AT> Matrix<General,Fixed<M>,Fixed<N>,AT>::col(
int j)
const {
464 FMATVEC_ASSERT(j>=0, AT);
465 FMATVEC_ASSERT(j<N, AT);
467 Vector<Fixed<M>,AT> x(NONINIT);
469 for(
int i=0; i<M; i++)
475 template <
int M,
int N,
class AT>
478 for(
int i=0; i<N; i++)
479 for(
int j=0; j<M; j++)
484 template <
int M,
int N,
class AT>
template <
class Row>
486 FMATVEC_ASSERT(j<cols(), AT);
487 FMATVEC_ASSERT(rows()==x.size(), AT);
488 for(
int i=0; i<rows(); i++)
492 template <
int M,
int N,
class AT>
template <
class Col>
494 FMATVEC_ASSERT(i<rows(), AT);
495 FMATVEC_ASSERT(cols()==x.size(), AT);
496 for(
int j=0; j<cols(); j++)
500 template <
int M,
int N,
class AT>
template<
class Type,
class Row,
class Col>
501 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) {
503 FMATVEC_ASSERT(I.
end()<rows(), AT);
504 FMATVEC_ASSERT(J.
end()<cols(), AT);
505 FMATVEC_ASSERT(I.
size()==A.
rows(), AT);
506 FMATVEC_ASSERT(J.
size()==A.
cols(), AT);
508 for(
int i=I.
start(), ii=0; i<=I.
end(); i++, ii++)
509 for(
int j=J.
start(), jj=0; j<=J.
end(); j++, jj++)
513 template <
int M,
int N,
class AT>
template <
class Row>
515 FMATVEC_ASSERT(j<cols(), AT);
516 FMATVEC_ASSERT(rows()==x.size(), AT);
517 for(
int i=0; i<rows(); i++)
521 template <
int M,
int N,
class AT>
template <
class Col>
523 FMATVEC_ASSERT(i<rows(), AT);
524 FMATVEC_ASSERT(cols()==x.size(), AT);
525 for(
int j=0; j<cols(); j++)
529 template <
int M,
int N,
class AT>
template<
class Type,
class Row,
class Col>
530 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) {
532 FMATVEC_ASSERT(I.
end()<rows(), AT);
533 FMATVEC_ASSERT(J.
end()<cols(), AT);
534 FMATVEC_ASSERT(I.
size()==A.
rows(), AT);
535 FMATVEC_ASSERT(J.
size()==A.
cols(), AT);
537 for(
int i=I.
start(), ii=0; i<=I.
end(); i++, ii++)
538 for(
int j=J.
start(), jj=0; j<=J.
end(); j++, jj++)
539 e(i,j) += A.e(ii,jj);
542 template <
int M,
int N,
class AT>
544 std::vector<std::vector<AT>> ret(rows());
545 for(
int r=0; r<rows(); r++) {
546 ret[r].resize(cols());
547 for(
int c=0; c<cols(); c++)
553 template <
int M,
int N,
class AT>
556 throw std::runtime_error(
"The input has "+std::to_string(m.size())+
" rows but "+std::to_string(M)+
" rows are required.");
558 throw std::runtime_error(
"The input has "+std::to_string(m[0].size())+
" columne but "+std::to_string(N)+
" columns are required.");
559 for(
int r=0; r<rows(); r++) {
560 if(static_cast<int>(m[r].size())!=cols())
561 throw std::runtime_error(
"The rows of the input have different length.");
562 for(
int c=0; c<cols(); c++)
569 template <
int M,
int N,
class AT>
template <
class Type,
class Row,
class Col>
571 for(
int i=0; i<M; i++)
572 for(
int j=0; j<N; j++)
577 template<
int M,
int N,
class AT>
template<
class Row>
578 inline Matrix<General,Fixed<M>,Fixed<N>,AT>& Matrix<General,Fixed<M>,Fixed<N>,AT>::copy(
const Matrix<Symmetric,Row,Row,AT> &A) {
579 for(
int i=0; i<A.size(); i++) {
581 for(
int j=i+1; j<A.size(); j++)
582 e(i,j) = e(j,i) = A.ej(i,j);
596 template<
int M,
class AT>
599 typedef AT value_type;
604 Matrix(
Init ini=INIT,
const AT &a=0) { this->init(a); }
605 Matrix(
Eye ini,
const AT &a=1) { this->init(ini,a); }
609 Matrix(
int m,
int n,
Init ini,
const AT &a=0) { this->init(a); }
610 Matrix(
int m,
int n,
Eye ini,
const AT &a=1) { this->init(ini,a); }
619 template<
class Type,
class Row,
class Col>
632 template <
int M,
class AT>
635 for(
int i=0; i<M; i++)
636 for(
int j=0; j<M; j++)
int end() const
Last element.
Definition: range.h:91
Matrix< General, Fixed< M >, Fixed< N >, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: fixed_general_matrix.h:140
constexpr int rows() const
Number of rows.
Definition: fixed_general_matrix.h:229
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
const AT & operator()(int i, int j) const
Element operator.
Definition: fixed_general_matrix.h:191
AT * operator()()
Pointer operator.
Definition: fixed_general_matrix.h:217
int ldim() const
Leading dimension.
Definition: fixed_general_matrix.h:241
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:84
Shape class for rotation matrices.
Definition: types.h:156
int start() const
First element.
Definition: range.h:85
Matrix(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: fixed_general_matrix.h:104
int cols() const
Number of columns.
const AT & e(int i, int j) const
Element operator.
Definition: fixed_general_matrix.h:208
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: fixed_general_matrix.h:256
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: fixed_general_matrix.h:267
This is an index class for creating submatrices.
Definition: range.h:35
Namespace fmatvec.
Definition: _memory.cc:28
int size() const
Size.
Definition: range.h:97
const AT * operator()() const
Pointer operator.
Definition: fixed_general_matrix.h:223
AT & operator()(int i, int j)
Element operator.
Definition: fixed_general_matrix.h:178
Basic shape class for matrices.
Definition: types.h:132
void resize(int m, int n)
Definition: fixed_general_matrix.h:245
Matrix(const Matrix< General, Row, Col, AT > &A)
Copy Constructor.
Definition: fixed_general_matrix.h:92
constexpr int cols() const
Number of columns.
Definition: fixed_general_matrix.h:235
int rows() const
Number of rows.
This is an index class for creating submatrices.
Definition: range.h:44