22#ifndef fixed_general_matrix_h
23#define fixed_general_matrix_h
44 static constexpr bool isVector {
false};
46 using iterator = AT *;
47 using const_iterator =
const AT *;
48 using value_type = AT;
75 explicit Matrix(
Init ini=INIT,
const AT &a=AT()) { init(a); }
76 explicit Matrix(
Eye ini,
const AT &a=1) { init(ini,a); }
77 explicit Matrix(
int m,
int n,
Noinit) { FMATVEC_ASSERT(m==M && n==N, AT); }
78 explicit Matrix(
int m,
int n,
Init ini=INIT,
const AT &a=AT()) { FMATVEC_ASSERT(m==M && n==N, AT); init(a); }
79 explicit Matrix(
int m,
int n,
Eye ini,
const AT &a=1) { FMATVEC_ASSERT(m==M && n==N, AT); init(ini,a); }
93 template<
class Row,
class Col>
95 FMATVEC_ASSERT(A.
rows() == M, AT);
96 FMATVEC_ASSERT(A.
cols() == N, AT);
105 template<
class Type,
class Row,
class Col>
107 FMATVEC_ASSERT(A.
rows() == M, AT);
108 FMATVEC_ASSERT(A.
cols() == N, AT);
124 Matrix(
const std::string &strs);
141 template <
class Type,
class Row,
class Col>
143 FMATVEC_ASSERT(A.
rows() == M, AT);
144 FMATVEC_ASSERT(A.
cols() == N, AT);
154 template<
class Type,
class Row,
class Col>
158 operator Matrix<General,Fixed<M>,Fixed<N>,AT2>()
const {
159 Matrix<General,Fixed<M>,Fixed<N>,AT2> ret;
160 for(
size_t r=0; r<M; ++r)
161 for(
size_t c=0; c<N; ++c)
162 ret(r,c) = (*this)(r,c);
166 iterator begin() {
return &ele[0][0]; }
167 iterator end() {
return &ele[M-1][N]; }
168 const_iterator begin()
const {
return &ele[0][0]; }
169 const_iterator end()
const {
return &ele[M-1][N]; }
181 FMATVEC_ASSERT(i>=0, AT);
182 FMATVEC_ASSERT(j>=0, AT);
183 FMATVEC_ASSERT(i<M, AT);
184 FMATVEC_ASSERT(j<N, AT);
194 FMATVEC_ASSERT(i>=0, AT);
195 FMATVEC_ASSERT(j>=0, AT);
196 FMATVEC_ASSERT(i<M, AT);
197 FMATVEC_ASSERT(j<N, AT);
202 AT& e(
int i,
int j) {
210 const AT&
e(
int i,
int j)
const {
231 constexpr int rows()
const {
return M;}
237 constexpr int cols()
const {
return N;}
249 throw std::runtime_error(
"A fixed matrix cannot be resized.");
270 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=AT()) {
return *
this; }
306 explicit inline operator std::vector<std::vector<AT>>()
const;
313 explicit Matrix(
const std::vector<std::vector<AT>> &m);
335 inline const Matrix<General,Fixed<N>,Fixed<M>,AT> T()
const;
340 template<
class Row>
inline void set(
int j,
const Vector<Row,AT> &x);
345 template<
class Col>
inline void set(
int i,
const RowVector<Col,AT> &x);
353 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);
358 template<
class Row>
inline void add(
int j,
const Vector<Row,AT> &x);
363 template<
class Col>
inline void add(
int i,
const RowVector<Col,AT> &x);
365 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);
368 template <
int M,
int N,
class AT>
370 std::istringstream iss(strs);
376 throw std::runtime_error(
"Input not fully read.");
381 template <
int M,
int N,
class AT>
383 for(
int i=0; i<M; i++)
384 for(
int j=0; j<N; j++)
389 template <
int M,
int N,
class AT>
391 for(
int i=0; i<M; i++)
392 for(
int j=0; j<N; j++)
393 e(i,j) = (i==j) ? val : 0;
397 template <
int M,
int N,
class AT>
399 FMATVEC_ASSERT(I.end()<M, AT);
400 FMATVEC_ASSERT(J.end()<N, AT);
403 for(
int i=0; i<A.
rows(); i++)
404 for(
int j=0; j<A.
cols(); j++)
405 A.e(i,j) = e(I.start()+i,J.start()+j);
410 template <
int M,
int N,
class AT>
template <
int M1,
int M2,
int N1,
int N2>
412 FMATVEC_ASSERT(M2<M, AT);
413 FMATVEC_ASSERT(N2<N, AT);
416 for(
int i=0; i<A.
rows(); i++)
417 for(
int j=0; j<A.
cols(); j++)
418 A.e(i,j) = e(M1+i,J.start()+j);
423 template <
int M,
int N,
class AT>
template <
int M1,
int M2>
424 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 {
425 FMATVEC_ASSERT(M2<M, AT);
426 FMATVEC_ASSERT(J.end()<N, AT);
427 Matrix<General,Fixed<M2-M1+1>,Var,AT> A(J.size(),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 FMATVEC_ASSERT(I.end()<M, AT);
439 FMATVEC_ASSERT(N2<N, AT);
440 Matrix<General,Var,Fixed<N2-N1+1>,AT> A(I.size(),NONINIT);
442 for(
int i=0; i<A.
rows(); i++)
443 for(
int j=0; j<A.
cols(); j++)
444 A.e(i,j) = e(I.start()+i,J.start()+j);
449 template <
int M,
int N,
class AT>
450 inline const RowVector<Fixed<N>,AT> Matrix<General,Fixed<M>,Fixed<N>,AT>::row(
int i)
const {
452 FMATVEC_ASSERT(i>=0, AT);
453 FMATVEC_ASSERT(i<M, AT);
455 RowVector<Fixed<N>,AT> x(NONINIT);
457 for(
int j=0; j<N; j++)
463 template <
int M,
int N,
class AT>
464 inline const Vector<Fixed<M>,AT> Matrix<General,Fixed<M>,Fixed<N>,AT>::col(
int j)
const {
466 FMATVEC_ASSERT(j>=0, AT);
467 FMATVEC_ASSERT(j<N, AT);
469 Vector<Fixed<M>,AT> x(NONINIT);
471 for(
int i=0; i<M; i++)
477 template <
int M,
int N,
class AT>
478 inline const Matrix<General,Fixed<N>,Fixed<M>,AT> Matrix<General,Fixed<M>,Fixed<N>,AT>::T()
const {
479 Matrix<General,Fixed<N>,Fixed<M>,AT> A(NONINIT);
480 for(
int i=0; i<N; i++)
481 for(
int j=0; j<M; j++)
486 template <
int M,
int N,
class AT>
template <
class Row>
488 FMATVEC_ASSERT(j<cols(), AT);
489 FMATVEC_ASSERT(rows()==x.size(), AT);
490 for(
int i=0; i<rows(); i++)
494 template <
int M,
int N,
class AT>
template <
class Col>
496 FMATVEC_ASSERT(i<rows(), AT);
497 FMATVEC_ASSERT(cols()==x.size(), AT);
498 for(
int j=0; j<cols(); j++)
502 template <
int M,
int N,
class AT>
template<
class Type,
class Row,
class Col>
503 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) {
505 FMATVEC_ASSERT(I.
end()<rows(), AT);
506 FMATVEC_ASSERT(J.
end()<cols(), AT);
507 FMATVEC_ASSERT(I.
size()==A.
rows(), AT);
508 FMATVEC_ASSERT(J.
size()==A.
cols(), AT);
510 for(
int i=I.
start(), ii=0; i<=I.
end(); i++, ii++)
511 for(
int j=J.
start(), jj=0; j<=J.
end(); j++, jj++)
515 template <
int M,
int N,
class AT>
template <
class Row>
517 FMATVEC_ASSERT(j<cols(), AT);
518 FMATVEC_ASSERT(rows()==x.size(), AT);
519 for(
int i=0; i<rows(); i++)
523 template <
int M,
int N,
class AT>
template <
class Col>
525 FMATVEC_ASSERT(i<rows(), AT);
526 FMATVEC_ASSERT(cols()==x.size(), AT);
527 for(
int j=0; j<cols(); j++)
531 template <
int M,
int N,
class AT>
template<
class Type,
class Row,
class Col>
532 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) {
534 FMATVEC_ASSERT(I.
end()<rows(), AT);
535 FMATVEC_ASSERT(J.
end()<cols(), AT);
536 FMATVEC_ASSERT(I.
size()==A.
rows(), AT);
537 FMATVEC_ASSERT(J.
size()==A.
cols(), AT);
539 for(
int i=I.
start(), ii=0; i<=I.
end(); i++, ii++)
540 for(
int j=J.
start(), jj=0; j<=J.
end(); j++, jj++)
541 e(i,j) += A.e(ii,jj);
544 template <
int M,
int N,
class AT>
546 std::vector<std::vector<AT>> ret(rows(),std::vector<AT>(cols()));
547 for(
int r=0; r<rows(); r++) {
548 for(
int c=0; c<cols(); c++)
554 template <
int M,
int N,
class AT>
557 throw std::runtime_error(
"The input has "+std::to_string(m.size())+
" rows but "+std::to_string(M)+
" rows are required.");
558 if((m.empty()?0:m[0].size()) != N)
559 throw std::runtime_error(
"The input has "+std::to_string((m.empty()?0:m[0].size()))+
" columne but "+std::to_string(N)+
" columns are required.");
560 for(
int r=0; r<rows(); r++) {
561 if(
static_cast<int>(m[r].size())!=cols())
562 throw std::runtime_error(
"The rows of the input have different length.");
563 for(
int c=0; c<cols(); c++)
570 template <
int M,
int N,
class AT>
template <
class Type,
class Row,
class Col>
572 for(
int i=0; i<M; i++)
573 for(
int j=0; j<N; j++)
578 template<
int M,
int N,
class AT>
template<
class Row>
579 inline Matrix<General,Fixed<M>,Fixed<N>,AT>& Matrix<General,Fixed<M>,Fixed<N>,AT>::copy(
const Matrix<Symmetric,Row,Row,AT> &A) {
580 for(
int i=0; i<A.size(); i++) {
582 for(
int j=i+1; j<A.size(); j++)
583 e(i,j) = e(j,i) = A.ej(i,j);
597 template<
int M,
class AT>
600 using value_type = AT;
605 Matrix(
Init ini=INIT,
const AT &a=0) { this->init(a); }
606 Matrix(
Eye ini,
const AT &a=1) { this->init(ini,a); }
610 Matrix(
int m,
int n,
Init ini,
const AT &a=0) { this->init(a); }
611 Matrix(
int m,
int n,
Eye ini,
const AT &a=1) { this->init(ini,a); }
620 template<
class Type,
class Row,
class Col>
633 template <
int M,
class AT>
636 for(
int i=0; i<M; i++)
637 for(
int j=0; j<M; j++)
Shape class for general matrices.
Definition: types.h:116
Matrix(const Matrix< General, Row, Col, AT > &A)
Copy Constructor.
Definition: fixed_general_matrix.h:94
Matrix(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: fixed_general_matrix.h:106
Matrix(const Matrix< General, Fixed< M >, Fixed< N >, AT > &A)=default
Copy Constructor.
constexpr int rows() const
Number of rows.
Definition: fixed_general_matrix.h:231
Matrix< General, Fixed< M >, Fixed< N >, AT > & operator=(const Matrix< General, Fixed< M >, Fixed< N >, AT > &A)=default
Assignment operator.
CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: fixed_general_matrix.h:258
void resize(int m, int n)
Definition: fixed_general_matrix.h:247
constexpr int cols() const
Number of columns.
Definition: fixed_general_matrix.h:237
AT & operator()(int i, int j)
Element operator.
Definition: fixed_general_matrix.h:180
int ldim() const
Leading dimension.
Definition: fixed_general_matrix.h:243
CBLAS_ORDER blasOrder() const
Storage convention.
Definition: fixed_general_matrix.h:269
const AT * operator()() const
Pointer operator.
Definition: fixed_general_matrix.h:225
const AT & operator()(int i, int j) const
Element operator.
Definition: fixed_general_matrix.h:193
AT * operator()()
Pointer operator.
Definition: fixed_general_matrix.h:219
const AT & e(int i, int j) const
Element operator.
Definition: fixed_general_matrix.h:210
This is a matrix class for general matrices.
Definition: var_general_matrix.h:41
constexpr int cols() const
Number of columns.
Definition: var_general_matrix.h:303
constexpr int rows() const
Number of rows.
Definition: var_general_matrix.h:297
This is the basic matrix class for arbitrary matrices.
Definition: matrix.h:52
int rows() const
Number of rows.
int cols() const
Number of columns.
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:84
This is an index class for creating submatrices.
Definition: range.h:44
int end() const
Last element.
Definition: range.h:91
int size() const
Size.
Definition: range.h:97
int start() const
First element.
Definition: range.h:85
This is an index class for creating submatrices.
Definition: range.h:35
Shape class for rotation matrices.
Definition: types.h:140
Namespace fmatvec.
Definition: _memory.cc:28