22 #ifndef var_fixed_general_matrix_h 23 #define var_fixed_general_matrix_h 42 static constexpr
bool isVector {
false};
43 typedef AT value_type;
60 explicit Matrix() : ele(
nullptr) { }
62 explicit Matrix(
int m,
Noinit) : M(m), ele(
new AT[M*N]) { }
63 explicit Matrix(
int m,
Init ini=INIT,
const AT &a=AT()) : M(m), ele(
new AT[M*N]) { init(a); }
64 explicit Matrix(
int m,
Eye ini,
const AT &a=1) : M(m), ele(
new AT[M*N]) { init(ini,a); }
65 explicit Matrix(
int m,
int n,
Noinit) : M(m), ele(
new AT[M*N]) { FMATVEC_ASSERT(n==N, AT); }
66 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); }
67 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); }
83 template<
class Row,
class Col>
85 FMATVEC_ASSERT(A.
cols() == N, AT);
94 template<
class Type,
class Row,
class Col>
96 FMATVEC_ASSERT(A.
cols() == N, AT);
112 Matrix(
const std::string &strs);
115 typedef AT* iterator;
116 typedef const AT* const_iterator;
117 iterator begin() {
return &ele[0]; }
118 iterator end() {
return &ele[M*N]; }
119 const_iterator begin()
const {
return &ele[0]; }
120 const_iterator end()
const {
return &ele[M*N]; }
135 Matrix<General,Var,Fixed<N>,AT>& resize(
int m, Init ini=INIT,
const AT &a=AT()) {
return resize(m,Noinit()).init(a); }
137 Matrix<General,Var,Fixed<N>,AT>& resize(
int m, Eye ini,
const AT &a=1) {
return resize(m,Noinit()).init(ini,a); }
143 throw std::runtime_error(
"A var-fixed matrix can only be resized in the first dimension.");
154 FMATVEC_ASSERT(M == A.
rows(), AT);
164 template <
class Type,
class Row,
class Col>
166 FMATVEC_ASSERT(N == A.
cols(), AT);
167 FMATVEC_ASSERT(M == A.
rows(), AT);
177 template <
class Type,
class Row,
class Col>
179 FMATVEC_ASSERT(N == A.
cols(), AT);
180 if(M!=A.
rows()) resize(A.
rows(),NONINIT);
194 FMATVEC_ASSERT(i>=0, AT);
195 FMATVEC_ASSERT(j>=0, AT);
196 FMATVEC_ASSERT(i<M, AT);
197 FMATVEC_ASSERT(j<N, AT);
207 FMATVEC_ASSERT(i>=0, AT);
208 FMATVEC_ASSERT(j>=0, AT);
209 FMATVEC_ASSERT(i<M, AT);
210 FMATVEC_ASSERT(j<N, AT);
215 AT& e(
int i,
int j) {
223 const AT&
e(
int i,
int j)
const {
235 const AT&
e(
int i)
const {
256 constexpr
int rows()
const {
return M;}
262 constexpr
int cols()
const {
return N;}
288 return CblasRowMajor;
305 inline Matrix<General,Var,Fixed<N>,AT>& init(Eye,
const AT &val=1);
306 inline Matrix<General,Var,Fixed<N>,AT>& init(Noinit,
const AT &a=AT()) {
return *
this; }
312 explicit inline operator std::vector<std::vector<AT>>()
const;
319 explicit inline Matrix(
const std::vector<std::vector<AT>> &m);
340 inline const Matrix<General,Fixed<N>,Var,AT> T()
const;
342 template<
class Row>
inline void set(
int j,
const Vector<Row,AT> &x);
344 template<
class Col>
inline void set(
int i,
const RowVector<Col,AT> &x);
346 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);
348 template<
class Row>
inline void add(
int j,
const Vector<Row,AT> &x);
350 template<
class Col>
inline void add(
int i,
const RowVector<Col,AT> &x);
352 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);
355 template <
int N,
class AT>
356 Matrix<General,Var,Fixed<N>,AT>::Matrix(
const std::string &strs) : ele(0) {
357 std::istringstream iss(strs);
363 throw std::runtime_error(
"Input not fully read.");
365 template <
int N,
class AT> Matrix<General,Var,Fixed<N>,AT>::Matrix(
const char * strs) :
366 Matrix<General,Var,Fixed<N>,AT>::Matrix(std::string(strs)) {}
368 template <
int N,
class AT>
369 inline Matrix<General,Var,Fixed<N>,AT>& Matrix<General,Var,Fixed<N>,AT>::init(
const AT &val) {
370 for(
int i=0; i<M*N; i++)
375 template <
int N,
class AT>
376 inline Matrix<General,Var,Fixed<N>,AT>& Matrix<General,Var,Fixed<N>,AT>::init(Eye eye,
const AT &val) {
377 for(
int i=0; i<M; i++)
378 for(
int j=0; j<N; j++)
379 e(i,j) = (i==j) ? val : 0;
383 template <
int N,
class AT>
384 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 {
385 FMATVEC_ASSERT(I.end()<M, AT);
386 FMATVEC_ASSERT(J.end()<N, AT);
387 Matrix<General,Var,Var,AT> A(I.end()-I.start()+1,J.end()-J.start()+1,NONINIT);
389 for(
int i=0; i<A.
rows(); i++)
390 for(
int j=0; j<A.
cols(); j++)
391 A.e(i,j) = e(I.start()+i,J.start()+j);
396 template <
int N,
class AT>
397 inline const RowVector<Fixed<N>,AT> Matrix<General,Var,Fixed<N>,AT>::row(
int i)
const {
399 FMATVEC_ASSERT(i>=0, AT);
400 FMATVEC_ASSERT(i<M, AT);
402 RowVector<Fixed<N>,AT> x(NONINIT);
404 for(
int j=0; j<N; j++)
411 template <
int N,
class AT>
412 inline const Vector<Var,AT> Matrix<General,Var,Fixed<N>,AT>::col(
int j)
const {
414 FMATVEC_ASSERT(j>=0, AT);
415 FMATVEC_ASSERT(j<N, AT);
417 Vector<Var,AT> x(M,NONINIT);
419 for(
int i=0; i<M; i++)
426 template <
int N,
class AT>
427 inline const Matrix<General,Fixed<N>,Var,AT> Matrix<General,Var,Fixed<N>,AT>::T()
const {
428 Matrix<General,Fixed<N>,Var,AT> A(rows(),NONINIT);
429 for(
int i=0; i<N; i++)
430 for(
int j=0; j<M; j++)
435 template <
int N,
class AT>
template <
class Row>
436 inline void Matrix<General,Var,Fixed<N>,AT>::set(
int j,
const Vector<Row,AT> &x) {
437 FMATVEC_ASSERT(j<cols(), AT);
438 FMATVEC_ASSERT(rows()==x.size(), AT);
439 for(
int i=0; i<rows(); i++)
443 template <
int N,
class AT>
template <
class Col>
444 inline void Matrix<General,Var,Fixed<N>,AT>::set(
int i,
const RowVector<Col,AT> &x) {
445 FMATVEC_ASSERT(i<rows(), AT);
446 FMATVEC_ASSERT(cols()==x.size(), AT);
447 for(
int j=0; j<cols(); j++)
451 template <
int N,
class AT>
template<
class Type,
class Row,
class Col>
452 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) {
454 FMATVEC_ASSERT(I.end()<rows(), AT);
455 FMATVEC_ASSERT(J.end()<cols(), AT);
456 FMATVEC_ASSERT(I.size()==A.rows(), AT);
457 FMATVEC_ASSERT(J.size()==A.cols(), AT);
459 for(
int i=I.start(), ii=0; i<=I.end(); i++, ii++)
460 for(
int j=J.start(), jj=0; j<=J.end(); j++, jj++)
464 template <
int N,
class AT>
template <
class Row>
465 inline void Matrix<General,Var,Fixed<N>,AT>::add(
int j,
const Vector<Row,AT> &x) {
466 FMATVEC_ASSERT(j<cols(), AT);
467 FMATVEC_ASSERT(rows()==x.size(), AT);
468 for(
int i=0; i<rows(); i++)
472 template <
int N,
class AT>
template <
class Col>
473 inline void Matrix<General,Var,Fixed<N>,AT>::add(
int i,
const RowVector<Col,AT> &x) {
474 FMATVEC_ASSERT(i<rows(), AT);
475 FMATVEC_ASSERT(cols()==x.size(), AT);
476 for(
int j=0; j<cols(); j++)
480 template <
int N,
class AT>
template<
class Type,
class Row,
class Col>
481 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) {
483 FMATVEC_ASSERT(I.end()<rows(), AT);
484 FMATVEC_ASSERT(J.end()<cols(), AT);
485 FMATVEC_ASSERT(I.size()==A.rows(), AT);
486 FMATVEC_ASSERT(J.size()==A.cols(), AT);
488 for(
int i=I.start(), ii=0; i<=I.end(); i++, ii++)
489 for(
int j=J.start(), jj=0; j<=J.end(); j++, jj++)
490 e(i,j) += A.e(ii,jj);
493 template <
int N,
class AT>
494 inline Matrix<General,Var,Fixed<N>,AT>::operator std::vector<std::vector<AT>>()
const {
495 std::vector<std::vector<AT>> ret(rows());
496 for(
int r=0; r<rows(); r++) {
497 ret[r].resize(cols());
498 for(
int c=0; c<cols(); c++)
504 template <
int N,
class AT>
505 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]) {
507 throw std::runtime_error(
"The input has "+std::to_string(m[0].size())+
" columns but "+std::to_string(N)+
" columns are required.");
508 for(
int r=0; r<
rows(); r++) {
509 if(static_cast<int>(m[r].size())!=
cols())
510 throw std::runtime_error(
"The rows of the input have different length.");
511 for(
int c=0; c<
cols(); c++)
518 template <
int N,
class AT>
template <
class Type,
class Row,
class Col>
519 inline Matrix<General,Var,Fixed<N>,AT>& Matrix<General,Var,Fixed<N>,AT>::copy(
const Matrix<Type,Row,Col,AT> &A) {
520 for(
int i=0; i<M; i++)
521 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:153
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
const AT & e(int i) const
Element operator.
Definition: var_fixed_general_matrix.h:235
const AT * operator()() const
Pointer operator.
Definition: var_fixed_general_matrix.h:250
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:84
constexpr int rows() const
Number of rows.
Definition: var_fixed_general_matrix.h:256
AT * operator()()
Pointer operator.
Definition: var_fixed_general_matrix.h:244
AT & operator()(int i, int j)
Element operator.
Definition: var_fixed_general_matrix.h:193
int ldim() const
Leading dimension.
Definition: var_fixed_general_matrix.h:268
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: var_fixed_general_matrix.h:276
int cols() const
Number of columns.
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: var_fixed_general_matrix.h:287
Namespace fmatvec.
Definition: _memory.cc:28
const AT & operator()(int i, int j) const
Element operator.
Definition: var_fixed_general_matrix.h:206
~Matrix()
Destructor.
Definition: var_fixed_general_matrix.h:124
Matrix< General, Var, Fixed< N >, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: var_fixed_general_matrix.h:165
const AT & e(int i, int j) const
Element operator.
Definition: var_fixed_general_matrix.h:223
void resize(int m, int n)
Definition: var_fixed_general_matrix.h:141
Basic shape class for matrices.
Definition: types.h:132
constexpr int cols() const
Number of columns.
Definition: var_fixed_general_matrix.h:262
Matrix(const Matrix< General, Var, Fixed< N >, AT > &A)
Copy Constructor.
Definition: var_fixed_general_matrix.h:74
int rows() const
Number of rows.
This is an index class for creating submatrices.
Definition: range.h:44
Matrix(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: var_fixed_general_matrix.h:95