22 #ifndef fixed_general_matrix_h 23 #define fixed_general_matrix_h 43 typedef AT value_type;
69 explicit Matrix(
Init ini=INIT,
const AT &a=AT()) { init(a); }
70 explicit Matrix(
Eye ini,
const AT &a=1) { init(ini,a); }
71 explicit Matrix(
int m,
int n,
Noinit) { FMATVEC_ASSERT(m==M && n==N, AT); }
72 explicit Matrix(
int m,
int n,
Init ini=INIT,
const AT &a=AT()) { FMATVEC_ASSERT(m==M && n==N, AT); init(a); }
73 explicit Matrix(
int m,
int n,
Eye ini,
const AT &a=1) { FMATVEC_ASSERT(m==M && n==N, AT); init(ini,a); }
87 template<
class Row,
class Col>
89 FMATVEC_ASSERT(A.
rows() == M, AT);
90 FMATVEC_ASSERT(A.
cols() == N, AT);
99 template<
class Type,
class Row,
class Col>
101 FMATVEC_ASSERT(A.
rows() == M, AT);
102 FMATVEC_ASSERT(A.
cols() == N, AT);
118 Matrix(
const std::string &strs);
135 template <
class Type,
class Row,
class Col>
137 FMATVEC_ASSERT(A.
rows() == M, AT);
138 FMATVEC_ASSERT(A.
cols() == N, AT);
148 template<
class Type,
class Row,
class Col>
154 for(
size_t r=0; r<M; ++r)
155 for(
size_t c=0; c<N; ++c)
156 ret(r,c) = (*this)(r,c);
170 FMATVEC_ASSERT(i>=0, AT);
171 FMATVEC_ASSERT(j>=0, AT);
172 FMATVEC_ASSERT(i<M, AT);
173 FMATVEC_ASSERT(j<N, AT);
183 FMATVEC_ASSERT(i>=0, AT);
184 FMATVEC_ASSERT(j>=0, AT);
185 FMATVEC_ASSERT(i<M, AT);
186 FMATVEC_ASSERT(j<N, AT);
191 AT& e(
int i,
int j) {
199 const AT&
e(
int i,
int j)
const {
238 throw std::runtime_error(
"A fixed matrix cannot be resized.");
259 return CblasRowMajor;
268 template <
int M1,
int M2,
int N1,
int N2>
270 template <
int M1,
int M2>
273 template <
int N1,
int N2>
288 inline Matrix<General,Fixed<M>,Fixed<N>,AT>& init(Eye,
const AT &val=1);
289 inline Matrix<General,Fixed<M>,Fixed<N>,AT>& init(Noinit,
const AT &a=AT()) {
return *
this; }
295 explicit inline operator std::vector<std::vector<AT>>()
const;
302 explicit Matrix(
const std::vector<std::vector<AT>> &m);
324 inline const Matrix<General,Fixed<N>,Fixed<M>,AT> T()
const;
329 template<
class Row>
inline void set(
int j,
const Vector<Row,AT> &x);
334 template<
class Col>
inline void set(
int i,
const RowVector<Col,AT> &x);
342 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);
347 template<
class Row>
inline void add(
int j,
const Vector<Row,AT> &x);
352 template<
class Col>
inline void add(
int i,
const RowVector<Col,AT> &x);
354 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);
357 template <
int M,
int N,
class AT>
359 std::istringstream iss(strs);
365 throw std::runtime_error(
"Input not fully read.");
370 template <
int M,
int N,
class AT>
372 for(
int i=0; i<M; i++)
373 for(
int j=0; j<N; j++)
378 template <
int M,
int N,
class AT>
380 for(
int i=0; i<M; i++)
381 for(
int j=0; j<N; j++)
382 e(i,j) = (i==j) ? val : 0;
386 template <
int M,
int N,
class AT>
388 FMATVEC_ASSERT(I.end()<M, AT);
389 FMATVEC_ASSERT(J.end()<N, AT);
392 for(
int i=0; i<A.
rows(); i++)
393 for(
int j=0; j<A.
cols(); j++)
394 A.e(i,j) = e(I.start()+i,J.start()+j);
399 template <
int M,
int N,
class AT>
template <
int M1,
int M2,
int N1,
int N2>
401 FMATVEC_ASSERT(M2<M, AT);
402 FMATVEC_ASSERT(N2<N, AT);
405 for(
int i=0; i<A.
rows(); i++)
406 for(
int j=0; j<A.
cols(); j++)
407 A.e(i,j) = e(M1+i,J.start()+j);
412 template <
int M,
int N,
class AT>
template <
int M1,
int M2>
413 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 {
414 FMATVEC_ASSERT(M2<M, AT);
415 FMATVEC_ASSERT(J.end()<N, AT);
416 Matrix<General,Fixed<M2-M1+1>,Var,AT> A(J.end()-J.start()+1,NONINIT);
418 for(
int i=0; i<A.
rows(); i++)
419 for(
int j=0; j<A.
cols(); j++)
420 A.e(i,j) = e(I.start()+i,J.start()+j);
425 template <
int M,
int N,
class AT>
template <
int N1,
int N2>
426 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 {
427 FMATVEC_ASSERT(I.end()<M, AT);
428 FMATVEC_ASSERT(N2<N, AT);
429 Matrix<General,Var,Fixed<N2-N1+1>,AT> A(I.end()-I.start()+1,NONINIT);
431 for(
int i=0; i<A.
rows(); i++)
432 for(
int j=0; j<A.
cols(); j++)
433 A.e(i,j) = e(I.start()+i,J.start()+j);
438 template <
int M,
int N,
class AT>
439 inline const RowVector<Fixed<N>,AT> Matrix<General,Fixed<M>,Fixed<N>,AT>::row(
int i)
const {
441 FMATVEC_ASSERT(i>=0, AT);
442 FMATVEC_ASSERT(i<M, AT);
444 RowVector<Fixed<N>,AT> x(NONINIT);
446 for(
int j=0; j<N; j++)
452 template <
int M,
int N,
class AT>
453 inline const Vector<Fixed<M>,AT> Matrix<General,Fixed<M>,Fixed<N>,AT>::col(
int j)
const {
455 FMATVEC_ASSERT(j>=0, AT);
456 FMATVEC_ASSERT(j<N, AT);
458 Vector<Fixed<M>,AT> x(NONINIT);
460 for(
int i=0; i<M; i++)
466 template <
int M,
int N,
class AT>
469 for(
int i=0; i<N; i++)
470 for(
int j=0; j<M; j++)
475 template <
int M,
int N,
class AT>
template <
class Row>
477 FMATVEC_ASSERT(j<cols(), AT);
478 FMATVEC_ASSERT(rows()==x.size(), AT);
479 for(
int i=0; i<rows(); i++)
483 template <
int M,
int N,
class AT>
template <
class Col>
485 FMATVEC_ASSERT(i<rows(), AT);
486 FMATVEC_ASSERT(cols()==x.size(), AT);
487 for(
int j=0; j<cols(); j++)
491 template <
int M,
int N,
class AT>
template<
class Type,
class Row,
class Col>
492 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) {
494 FMATVEC_ASSERT(I.
end()<rows(), AT);
495 FMATVEC_ASSERT(J.
end()<cols(), AT);
496 FMATVEC_ASSERT(I.
size()==A.
rows(), AT);
497 FMATVEC_ASSERT(J.
size()==A.
cols(), AT);
499 for(
int i=I.
start(), ii=0; i<=I.
end(); i++, ii++)
500 for(
int j=J.
start(), jj=0; j<=J.
end(); j++, jj++)
504 template <
int M,
int N,
class AT>
template <
class Row>
506 FMATVEC_ASSERT(j<cols(), AT);
507 FMATVEC_ASSERT(rows()==x.size(), AT);
508 for(
int i=0; i<rows(); i++)
512 template <
int M,
int N,
class AT>
template <
class Col>
514 FMATVEC_ASSERT(i<rows(), AT);
515 FMATVEC_ASSERT(cols()==x.size(), AT);
516 for(
int j=0; j<cols(); j++)
520 template <
int M,
int N,
class AT>
template<
class Type,
class Row,
class Col>
521 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) {
523 FMATVEC_ASSERT(I.
end()<rows(), AT);
524 FMATVEC_ASSERT(J.
end()<cols(), AT);
525 FMATVEC_ASSERT(I.
size()==A.
rows(), AT);
526 FMATVEC_ASSERT(J.
size()==A.
cols(), AT);
528 for(
int i=I.
start(), ii=0; i<=I.
end(); i++, ii++)
529 for(
int j=J.
start(), jj=0; j<=J.
end(); j++, jj++)
530 e(i,j) += A.e(ii,jj);
533 template <
int M,
int N,
class AT>
535 std::vector<std::vector<AT>> ret(rows());
536 for(
int r=0; r<rows(); r++) {
537 ret[r].resize(cols());
538 for(
int c=0; c<cols(); c++)
544 template <
int M,
int N,
class AT>
547 throw std::runtime_error(
"The input has "+std::to_string(m.size())+
" rows but "+std::to_string(M)+
" rows are required.");
549 throw std::runtime_error(
"The input has "+std::to_string(m[0].size())+
" columne but "+std::to_string(N)+
" columns are required.");
550 for(
int r=0; r<rows(); r++) {
551 if(static_cast<int>(m[r].size())!=cols())
552 throw std::runtime_error(
"The rows of the input have different length.");
553 for(
int c=0; c<cols(); c++)
560 template <
int M,
int N,
class AT>
template <
class Type,
class Row,
class Col>
562 for(
int i=0; i<M; i++)
563 for(
int j=0; j<N; j++)
568 template<
int M,
int N,
class AT>
template<
class Row>
569 inline Matrix<General,Fixed<M>,Fixed<N>,AT>& Matrix<General,Fixed<M>,Fixed<N>,AT>::copy(
const Matrix<Symmetric,Row,Row,AT> &A) {
570 for(
int i=0; i<A.size(); i++) {
572 for(
int j=i+1; j<A.size(); j++)
573 e(i,j) = e(j,i) = A.ej(i,j);
587 template<
int M,
class AT>
590 typedef AT value_type;
594 Matrix(
Init ini=INIT,
const AT &a=0) { this->init(a); }
595 Matrix(
Eye ini,
const AT &a=1) { this->init(ini,a); }
598 Matrix(
int m,
int n,
Init ini,
const AT &a=0) { this->init(a); }
599 Matrix(
int m,
int n,
Eye ini,
const AT &a=1) { this->init(ini,a); }
608 template<
class Type,
class Row,
class Col>
621 template <
int M,
class AT>
624 for(
int i=0; i<M; i++)
625 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:136
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:182
AT * operator()()
Pointer operator.
Definition: fixed_general_matrix.h:208
int ldim() const
Leading dimension.
Definition: fixed_general_matrix.h:232
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:82
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:100
int cols() const
Number of columns.
const AT & e(int i, int j) const
Element operator.
Definition: fixed_general_matrix.h:199
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: fixed_general_matrix.h:247
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: fixed_general_matrix.h:258
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
int cols() const
Number of columns.
Definition: fixed_general_matrix.h:226
int rows() const
Number of rows.
Definition: fixed_general_matrix.h:220
const AT * operator()() const
Pointer operator.
Definition: fixed_general_matrix.h:214
AT & operator()(int i, int j)
Element operator.
Definition: fixed_general_matrix.h:169
Basic shape class for matrices.
Definition: types.h:132
void resize(int m, int n)
Definition: fixed_general_matrix.h:236
Matrix(const Matrix< General, Row, Col, AT > &A)
Copy Constructor.
Definition: fixed_general_matrix.h:88
int rows() const
Number of rows.
This is an index class for creating submatrices.
Definition: range.h:44