22 #ifndef var_fixed_general_matrix_h 23 #define var_fixed_general_matrix_h 44 static constexpr
bool isVector {
false};
45 using value_type = AT;
62 explicit Matrix() : ele(
nullptr) { }
64 explicit Matrix(
int m,
Noinit) : M(m), ele(
new AT[M*N]) { }
65 explicit Matrix(
int m,
Init ini=INIT,
const AT &a=AT()) : M(m), ele(
new AT[M*N]) { init(a); }
66 explicit Matrix(
int m,
Eye ini,
const AT &a=1) : M(m), ele(
new AT[M*N]) { init(ini,a); }
67 explicit Matrix(
int m,
int n,
Noinit) : M(m), ele(
new AT[M*N]) { FMATVEC_ASSERT(n==N, AT); }
68 explicit Matrix(
int m,
int n,
Init ini=INIT,
const AT &a=AT()) : M(m), ele(
new AT[M*N]) { FMATVEC_ASSERT(n==N, AT); init(a); }
69 explicit Matrix(
int m,
int n,
Eye ini,
const AT &a=1) : M(m), ele(
new AT[M*N]) { FMATVEC_ASSERT(n==N, AT); init(ini,a); }
79 FMATVEC_ASSERT(M == src.rows(), AT);
101 template<
class Row,
class Col>
103 FMATVEC_ASSERT(A.
cols() == N, AT);
112 template<
class Type,
class Row,
class Col>
114 FMATVEC_ASSERT(A.
cols() == N, AT);
130 Matrix(
const std::string &strs);
133 using iterator = AT *;
134 using const_iterator =
const AT *;
135 iterator begin() {
return &ele[0]; }
136 iterator end() {
return &ele[M*N]; }
137 const_iterator begin()
const {
return &ele[0]; }
138 const_iterator end()
const {
return &ele[M*N]; }
153 Matrix<General,Var,Fixed<N>,AT>& resize(
int m, Init ini=INIT,
const AT &a=AT()) {
return resize(m,Noinit()).init(a); }
155 Matrix<General,Var,Fixed<N>,AT>& resize(
int m, Eye ini,
const AT &a=1) {
return resize(m,Noinit()).init(ini,a); }
161 throw std::runtime_error(
"A var-fixed matrix can only be resized in the first dimension.");
172 FMATVEC_ASSERT(M == A.
rows(), AT);
182 template <
class Type,
class Row,
class Col>
184 FMATVEC_ASSERT(N == A.
cols(), AT);
185 FMATVEC_ASSERT(M == A.
rows(), AT);
195 template <
class Type,
class Row,
class Col>
197 FMATVEC_ASSERT(N == A.
cols(), AT);
198 if(M!=A.
rows()) resize(A.
rows(),NONINIT);
203 FMATVEC_ASSERT(N == src.cols(), AT);
222 FMATVEC_ASSERT(i>=0, AT);
223 FMATVEC_ASSERT(j>=0, AT);
224 FMATVEC_ASSERT(i<M, AT);
225 FMATVEC_ASSERT(j<N, AT);
235 FMATVEC_ASSERT(i>=0, AT);
236 FMATVEC_ASSERT(j>=0, AT);
237 FMATVEC_ASSERT(i<M, AT);
238 FMATVEC_ASSERT(j<N, AT);
243 AT& e(
int i,
int j) {
251 const AT&
e(
int i,
int j)
const {
263 const AT&
e(
int i)
const {
284 constexpr
int rows()
const {
return M;}
290 constexpr
int cols()
const {
return N;}
316 return CblasRowMajor;
333 inline Matrix<General,Var,Fixed<N>,AT>& init(Eye,
const AT &val=1);
334 inline Matrix<General,Var,Fixed<N>,AT>& init(Noinit,
const AT &a=AT()) {
return *
this; }
340 explicit inline operator std::vector<std::vector<AT>>()
const;
347 explicit inline Matrix(
const std::vector<std::vector<AT>> &m);
368 inline const Matrix<General,Fixed<N>,Var,AT> T()
const;
370 template<
class Row>
inline void set(
int j,
const Vector<Row,AT> &x);
372 template<
class Col>
inline void set(
int i,
const RowVector<Col,AT> &x);
374 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);
376 template<
class Row>
inline void add(
int j,
const Vector<Row,AT> &x);
378 template<
class Col>
inline void add(
int i,
const RowVector<Col,AT> &x);
380 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);
383 template <
int N,
class AT>
384 Matrix<General,Var,Fixed<N>,AT>::Matrix(
const std::string &strs) : ele(0) {
385 std::istringstream iss(strs);
391 throw std::runtime_error(
"Input not fully read.");
393 template <
int N,
class AT> Matrix<General,Var,Fixed<N>,AT>::Matrix(
const char * strs) :
394 Matrix<General,Var,Fixed<N>,AT>::Matrix(std::string(strs)) {}
396 template <
int N,
class AT>
397 inline Matrix<General,Var,Fixed<N>,AT>& Matrix<General,Var,Fixed<N>,AT>::init(
const AT &val) {
400 #pragma GCC diagnostic push 401 #pragma GCC diagnostic ignored "-Warray-bounds" 402 #pragma GCC diagnostic ignored "-Wstringop-overflow" 404 for(
int i=0; i<M*N; i++)
407 #pragma GCC diagnostic pop 412 template <
int N,
class AT>
413 inline Matrix<General,Var,Fixed<N>,AT>& Matrix<General,Var,Fixed<N>,AT>::init(Eye eye,
const AT &val) {
414 for(
int i=0; i<M; i++)
415 for(
int j=0; j<N; j++)
416 e(i,j) = (i==j) ? val : 0;
420 template <
int N,
class AT>
421 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 {
422 FMATVEC_ASSERT(I.end()<M, AT);
423 FMATVEC_ASSERT(J.end()<N, AT);
424 Matrix<General,Var,Var,AT> A(I.end()-I.start()+1,J.end()-J.start()+1,NONINIT);
426 for(
int i=0; i<A.
rows(); i++)
427 for(
int j=0; j<A.
cols(); j++)
428 A.e(i,j) = e(I.start()+i,J.start()+j);
433 template <
int N,
class AT>
434 inline const RowVector<Fixed<N>,AT> Matrix<General,Var,Fixed<N>,AT>::row(
int i)
const {
436 FMATVEC_ASSERT(i>=0, AT);
437 FMATVEC_ASSERT(i<M, AT);
439 RowVector<Fixed<N>,AT> x(NONINIT);
441 for(
int j=0; j<N; j++)
448 template <
int N,
class AT>
449 inline const Vector<Var,AT> Matrix<General,Var,Fixed<N>,AT>::col(
int j)
const {
451 FMATVEC_ASSERT(j>=0, AT);
452 FMATVEC_ASSERT(j<N, AT);
454 Vector<Var,AT> x(M,NONINIT);
456 for(
int i=0; i<M; i++)
463 template <
int N,
class AT>
464 inline const Matrix<General,Fixed<N>,Var,AT> Matrix<General,Var,Fixed<N>,AT>::T()
const {
465 Matrix<General,Fixed<N>,Var,AT> A(rows(),NONINIT);
466 for(
int i=0; i<N; i++)
467 for(
int j=0; j<M; j++)
472 template <
int N,
class AT>
template <
class Row>
473 inline void Matrix<General,Var,Fixed<N>,AT>::set(
int j,
const Vector<Row,AT> &x) {
474 FMATVEC_ASSERT(j<cols(), AT);
475 FMATVEC_ASSERT(rows()==x.size(), AT);
476 for(
int i=0; i<rows(); i++)
480 template <
int N,
class AT>
template <
class Col>
481 inline void Matrix<General,Var,Fixed<N>,AT>::set(
int i,
const RowVector<Col,AT> &x) {
482 FMATVEC_ASSERT(i<rows(), AT);
483 FMATVEC_ASSERT(cols()==x.size(), AT);
484 for(
int j=0; j<cols(); j++)
488 template <
int N,
class AT>
template<
class Type,
class Row,
class Col>
489 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) {
491 FMATVEC_ASSERT(I.end()<rows(), AT);
492 FMATVEC_ASSERT(J.end()<cols(), AT);
493 FMATVEC_ASSERT(I.size()==A.rows(), AT);
494 FMATVEC_ASSERT(J.size()==A.cols(), AT);
496 for(
int i=I.start(), ii=0; i<=I.end(); i++, ii++)
497 for(
int j=J.start(), jj=0; j<=J.end(); j++, jj++)
501 template <
int N,
class AT>
template <
class Row>
502 inline void Matrix<General,Var,Fixed<N>,AT>::add(
int j,
const Vector<Row,AT> &x) {
503 FMATVEC_ASSERT(j<cols(), AT);
504 FMATVEC_ASSERT(rows()==x.size(), AT);
505 for(
int i=0; i<rows(); i++)
509 template <
int N,
class AT>
template <
class Col>
510 inline void Matrix<General,Var,Fixed<N>,AT>::add(
int i,
const RowVector<Col,AT> &x) {
511 FMATVEC_ASSERT(i<rows(), AT);
512 FMATVEC_ASSERT(cols()==x.size(), AT);
513 for(
int j=0; j<cols(); j++)
517 template <
int N,
class AT>
template<
class Type,
class Row,
class Col>
518 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) {
520 FMATVEC_ASSERT(I.end()<rows(), AT);
521 FMATVEC_ASSERT(J.end()<cols(), AT);
522 FMATVEC_ASSERT(I.size()==A.rows(), AT);
523 FMATVEC_ASSERT(J.size()==A.cols(), AT);
525 for(
int i=I.start(), ii=0; i<=I.end(); i++, ii++)
526 for(
int j=J.start(), jj=0; j<=J.end(); j++, jj++)
527 e(i,j) += A.e(ii,jj);
530 template <
int N,
class AT>
531 inline Matrix<General,Var,Fixed<N>,AT>::operator std::vector<std::vector<AT>>()
const {
532 std::vector<std::vector<AT>> ret(rows(),std::vector<AT>(cols()));
533 for(
int r=0; r<rows(); r++) {
534 for(
int c=0; c<cols(); c++)
540 template <
int N,
class AT>
541 inline Matrix<General,Var,Fixed<N>,AT>::Matrix(
const std::vector<std::vector<AT>> &m) : M(static_cast<int>(m.size())), ele(new AT[M*N]) {
543 throw std::runtime_error(
"The input has "+std::to_string(m[0].size())+
" columns but "+std::to_string(N)+
" columns are required.");
544 for(
int r=0; r<
rows(); r++) {
545 if(static_cast<int>(m[r].size())!=
cols())
546 throw std::runtime_error(
"The rows of the input have different length.");
547 for(
int c=0; c<
cols(); c++)
554 template <
int N,
class AT>
template <
class Type,
class Row,
class Col>
555 inline Matrix<General,Var,Fixed<N>,AT>& Matrix<General,Var,Fixed<N>,AT>::copy(
const Matrix<Type,Row,Col,AT> &A) {
556 for(
int i=0; i<M; i++)
557 for(
int j=0; j<N; j++)
Matrix< General, Var, Fixed< N >, AT > & operator=(const Matrix< General, Var, Fixed< N >, AT > &A)
Assignment operator.
Definition: var_fixed_general_matrix.h:171
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:42
const AT & e(int i) const
Element operator.
Definition: var_fixed_general_matrix.h:263
const AT * operator()() const
Pointer operator.
Definition: var_fixed_general_matrix.h:278
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:84
Matrix(const Matrix< General, Row, Col, AT > &A)
Copy Constructor.
Definition: var_fixed_general_matrix.h:102
constexpr int rows() const
Number of rows.
Definition: var_fixed_general_matrix.h:284
AT * operator()()
Pointer operator.
Definition: var_fixed_general_matrix.h:272
AT & operator()(int i, int j)
Element operator.
Definition: var_fixed_general_matrix.h:221
int ldim() const
Leading dimension.
Definition: var_fixed_general_matrix.h:296
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: var_fixed_general_matrix.h:304
int cols() const
Number of columns.
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: var_fixed_general_matrix.h:315
Namespace fmatvec.
Definition: _memory.cc:28
const AT & operator()(int i, int j) const
Element operator.
Definition: var_fixed_general_matrix.h:234
~Matrix()
Destructor.
Definition: var_fixed_general_matrix.h:142
Matrix< General, Var, Fixed< N >, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: var_fixed_general_matrix.h:183
const AT & e(int i, int j) const
Element operator.
Definition: var_fixed_general_matrix.h:251
void resize(int m, int n)
Definition: var_fixed_general_matrix.h:159
Shape class for general matrices.
Definition: types.h:112
constexpr int cols() const
Number of columns.
Definition: var_fixed_general_matrix.h:290
Matrix(const Matrix< General, Var, Fixed< N >, AT > &A)
Copy Constructor.
Definition: var_fixed_general_matrix.h:92
int rows() const
Number of rows.
This is an index class for creating submatrices.
Definition: range.h:45
Matrix(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: var_fixed_general_matrix.h:113