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); }
77 FMATVEC_ASSERT(M == src.rows(), AT);
99 template<
class Row,
class Col>
101 FMATVEC_ASSERT(A.
cols() == N, AT);
110 template<
class Type,
class Row,
class Col>
112 FMATVEC_ASSERT(A.
cols() == N, AT);
128 Matrix(
const std::string &strs);
131 typedef AT* iterator;
132 typedef const AT* const_iterator;
133 iterator begin() {
return &ele[0]; }
134 iterator end() {
return &ele[M*N]; }
135 const_iterator begin()
const {
return &ele[0]; }
136 const_iterator end()
const {
return &ele[M*N]; }
151 Matrix<General,Var,Fixed<N>,AT>& resize(
int m, Init ini=INIT,
const AT &a=AT()) {
return resize(m,Noinit()).init(a); }
153 Matrix<General,Var,Fixed<N>,AT>& resize(
int m, Eye ini,
const AT &a=1) {
return resize(m,Noinit()).init(ini,a); }
159 throw std::runtime_error(
"A var-fixed matrix can only be resized in the first dimension.");
170 FMATVEC_ASSERT(M == A.
rows(), AT);
180 template <
class Type,
class Row,
class Col>
182 FMATVEC_ASSERT(N == A.
cols(), AT);
183 FMATVEC_ASSERT(M == A.
rows(), AT);
193 template <
class Type,
class Row,
class Col>
195 FMATVEC_ASSERT(N == A.
cols(), AT);
196 if(M!=A.
rows()) resize(A.
rows(),NONINIT);
200 template <
class Type,
class Row,
class Col>
202 FMATVEC_ASSERT(N == src.cols(), AT);
221 FMATVEC_ASSERT(i>=0, AT);
222 FMATVEC_ASSERT(j>=0, AT);
223 FMATVEC_ASSERT(i<M, AT);
224 FMATVEC_ASSERT(j<N, AT);
234 FMATVEC_ASSERT(i>=0, AT);
235 FMATVEC_ASSERT(j>=0, AT);
236 FMATVEC_ASSERT(i<M, AT);
237 FMATVEC_ASSERT(j<N, AT);
242 AT& e(
int i,
int j) {
250 const AT&
e(
int i,
int j)
const {
262 const AT&
e(
int i)
const {
283 constexpr
int rows()
const {
return M;}
289 constexpr
int cols()
const {
return N;}
315 return CblasRowMajor;
332 inline Matrix<General,Var,Fixed<N>,AT>& init(Eye,
const AT &val=1);
333 inline Matrix<General,Var,Fixed<N>,AT>& init(Noinit,
const AT &a=AT()) {
return *
this; }
339 explicit inline operator std::vector<std::vector<AT>>()
const;
346 explicit inline Matrix(
const std::vector<std::vector<AT>> &m);
367 inline const Matrix<General,Fixed<N>,Var,AT> T()
const;
369 template<
class Row>
inline void set(
int j,
const Vector<Row,AT> &x);
371 template<
class Col>
inline void set(
int i,
const RowVector<Col,AT> &x);
373 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);
375 template<
class Row>
inline void add(
int j,
const Vector<Row,AT> &x);
377 template<
class Col>
inline void add(
int i,
const RowVector<Col,AT> &x);
379 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);
382 template <
int N,
class AT>
383 Matrix<General,Var,Fixed<N>,AT>::Matrix(
const std::string &strs) : ele(0) {
384 std::istringstream iss(strs);
390 throw std::runtime_error(
"Input not fully read.");
392 template <
int N,
class AT> Matrix<General,Var,Fixed<N>,AT>::Matrix(
const char * strs) :
393 Matrix<General,Var,Fixed<N>,AT>::Matrix(std::string(strs)) {}
395 template <
int N,
class AT>
396 inline Matrix<General,Var,Fixed<N>,AT>& Matrix<General,Var,Fixed<N>,AT>::init(
const AT &val) {
397 for(
int i=0; i<M*N; i++)
402 template <
int N,
class AT>
403 inline Matrix<General,Var,Fixed<N>,AT>& Matrix<General,Var,Fixed<N>,AT>::init(Eye eye,
const AT &val) {
404 for(
int i=0; i<M; i++)
405 for(
int j=0; j<N; j++)
406 e(i,j) = (i==j) ? val : 0;
410 template <
int N,
class AT>
411 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 {
412 FMATVEC_ASSERT(I.end()<M, AT);
413 FMATVEC_ASSERT(J.end()<N, AT);
414 Matrix<General,Var,Var,AT> A(I.end()-I.start()+1,J.end()-J.start()+1,NONINIT);
416 for(
int i=0; i<A.
rows(); i++)
417 for(
int j=0; j<A.
cols(); j++)
418 A.e(i,j) = e(I.start()+i,J.start()+j);
423 template <
int N,
class AT>
424 inline const RowVector<Fixed<N>,AT> Matrix<General,Var,Fixed<N>,AT>::row(
int i)
const {
426 FMATVEC_ASSERT(i>=0, AT);
427 FMATVEC_ASSERT(i<M, AT);
429 RowVector<Fixed<N>,AT> x(NONINIT);
431 for(
int j=0; j<N; j++)
438 template <
int N,
class AT>
439 inline const Vector<Var,AT> Matrix<General,Var,Fixed<N>,AT>::col(
int j)
const {
441 FMATVEC_ASSERT(j>=0, AT);
442 FMATVEC_ASSERT(j<N, AT);
444 Vector<Var,AT> x(M,NONINIT);
446 for(
int i=0; i<M; i++)
453 template <
int N,
class AT>
454 inline const Matrix<General,Fixed<N>,Var,AT> Matrix<General,Var,Fixed<N>,AT>::T()
const {
455 Matrix<General,Fixed<N>,Var,AT> A(rows(),NONINIT);
456 for(
int i=0; i<N; i++)
457 for(
int j=0; j<M; j++)
462 template <
int N,
class AT>
template <
class Row>
463 inline void Matrix<General,Var,Fixed<N>,AT>::set(
int j,
const Vector<Row,AT> &x) {
464 FMATVEC_ASSERT(j<cols(), AT);
465 FMATVEC_ASSERT(rows()==x.size(), AT);
466 for(
int i=0; i<rows(); i++)
470 template <
int N,
class AT>
template <
class Col>
471 inline void Matrix<General,Var,Fixed<N>,AT>::set(
int i,
const RowVector<Col,AT> &x) {
472 FMATVEC_ASSERT(i<rows(), AT);
473 FMATVEC_ASSERT(cols()==x.size(), AT);
474 for(
int j=0; j<cols(); j++)
478 template <
int N,
class AT>
template<
class Type,
class Row,
class Col>
479 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) {
481 FMATVEC_ASSERT(I.end()<rows(), AT);
482 FMATVEC_ASSERT(J.end()<cols(), AT);
483 FMATVEC_ASSERT(I.size()==A.rows(), AT);
484 FMATVEC_ASSERT(J.size()==A.cols(), AT);
486 for(
int i=I.start(), ii=0; i<=I.end(); i++, ii++)
487 for(
int j=J.start(), jj=0; j<=J.end(); j++, jj++)
491 template <
int N,
class AT>
template <
class Row>
492 inline void Matrix<General,Var,Fixed<N>,AT>::add(
int j,
const Vector<Row,AT> &x) {
493 FMATVEC_ASSERT(j<cols(), AT);
494 FMATVEC_ASSERT(rows()==x.size(), AT);
495 for(
int i=0; i<rows(); i++)
499 template <
int N,
class AT>
template <
class Col>
500 inline void Matrix<General,Var,Fixed<N>,AT>::add(
int i,
const RowVector<Col,AT> &x) {
501 FMATVEC_ASSERT(i<rows(), AT);
502 FMATVEC_ASSERT(cols()==x.size(), AT);
503 for(
int j=0; j<cols(); j++)
507 template <
int N,
class AT>
template<
class Type,
class Row,
class Col>
508 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) {
510 FMATVEC_ASSERT(I.end()<rows(), AT);
511 FMATVEC_ASSERT(J.end()<cols(), AT);
512 FMATVEC_ASSERT(I.size()==A.rows(), AT);
513 FMATVEC_ASSERT(J.size()==A.cols(), AT);
515 for(
int i=I.start(), ii=0; i<=I.end(); i++, ii++)
516 for(
int j=J.start(), jj=0; j<=J.end(); j++, jj++)
517 e(i,j) += A.e(ii,jj);
520 template <
int N,
class AT>
521 inline Matrix<General,Var,Fixed<N>,AT>::operator std::vector<std::vector<AT>>()
const {
522 std::vector<std::vector<AT>> ret(rows());
523 for(
int r=0; r<rows(); r++) {
524 ret[r].resize(cols());
525 for(
int c=0; c<cols(); c++)
531 template <
int N,
class AT>
532 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]) {
534 throw std::runtime_error(
"The input has "+std::to_string(m[0].size())+
" columns but "+std::to_string(N)+
" columns are required.");
535 for(
int r=0; r<
rows(); r++) {
536 if(static_cast<int>(m[r].size())!=
cols())
537 throw std::runtime_error(
"The rows of the input have different length.");
538 for(
int c=0; c<
cols(); c++)
545 template <
int N,
class AT>
template <
class Type,
class Row,
class Col>
546 inline Matrix<General,Var,Fixed<N>,AT>& Matrix<General,Var,Fixed<N>,AT>::copy(
const Matrix<Type,Row,Col,AT> &A) {
547 for(
int i=0; i<M; i++)
548 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:169
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:262
const AT * operator()() const
Pointer operator.
Definition: var_fixed_general_matrix.h:277
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:100
constexpr int rows() const
Number of rows.
Definition: var_fixed_general_matrix.h:283
AT * operator()()
Pointer operator.
Definition: var_fixed_general_matrix.h:271
AT & operator()(int i, int j)
Element operator.
Definition: var_fixed_general_matrix.h:220
int ldim() const
Leading dimension.
Definition: var_fixed_general_matrix.h:295
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: var_fixed_general_matrix.h:303
int cols() const
Number of columns.
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: var_fixed_general_matrix.h:314
Namespace fmatvec.
Definition: _memory.cc:28
const AT & operator()(int i, int j) const
Element operator.
Definition: var_fixed_general_matrix.h:233
~Matrix()
Destructor.
Definition: var_fixed_general_matrix.h:140
Matrix< General, Var, Fixed< N >, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: var_fixed_general_matrix.h:181
const AT & e(int i, int j) const
Element operator.
Definition: var_fixed_general_matrix.h:250
void resize(int m, int n)
Definition: var_fixed_general_matrix.h:157
Basic shape class for matrices.
Definition: types.h:136
constexpr int cols() const
Number of columns.
Definition: var_fixed_general_matrix.h:289
Matrix(const Matrix< General, Var, Fixed< N >, AT > &A)
Copy Constructor.
Definition: var_fixed_general_matrix.h:90
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:111