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); }
67 explicit Matrix(
int m,
Eye ini,
const AT &a=1) : M(m), ele(
new AT[M*N]) { init(ini,a); }
69 explicit Matrix(
int m,
int n,
Noinit) : M(m), ele(
new AT[M*N]) { FMATVEC_ASSERT(n==N, AT); }
70 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); }
72 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 FMATVEC_ASSERT(M == src.rows(), AT);
105 template<
class Row,
class Col>
107 FMATVEC_ASSERT(A.
cols() == N, AT);
116 template<
class Type,
class Row,
class Col>
118 FMATVEC_ASSERT(A.
cols() == N, AT);
134 Matrix(
const std::string &strs);
137 using iterator = AT *;
138 using const_iterator =
const AT *;
139 iterator begin() {
return &ele[0]; }
140 iterator end() {
return &ele[M*N]; }
141 const_iterator begin()
const {
return &ele[0]; }
142 const_iterator end()
const {
return &ele[M*N]; }
157 Matrix<General,Var,Fixed<N>,AT>& resize(
int m, Init ini=INIT,
const AT &a=AT()) {
return resize(m,Noinit()).init(a); }
160 Matrix<General,Var,Fixed<N>,AT>& resize(
int m, Eye ini,
const AT &a=1) {
return resize(m,Noinit()).init(ini,a); }
167 throw std::runtime_error(
"A var-fixed matrix can only be resized in the first dimension.");
178 FMATVEC_ASSERT(M == A.
rows(), AT);
188 template <
class Type,
class Row,
class Col>
190 FMATVEC_ASSERT(N == A.
cols(), AT);
191 FMATVEC_ASSERT(M == A.
rows(), AT);
201 template <
class Type,
class Row,
class Col>
203 FMATVEC_ASSERT(N == A.
cols(), AT);
204 if(M!=A.
rows()) resize(A.
rows(),NONINIT);
208 inline Matrix<General,Var,Fixed<N>,AT>& operator<<=(Matrix<General,Var,Fixed<N>,AT> &&src) {
209 FMATVEC_ASSERT(N == src.cols(), AT);
228 FMATVEC_ASSERT(i>=0, AT);
229 FMATVEC_ASSERT(j>=0, AT);
230 FMATVEC_ASSERT(i<M, AT);
231 FMATVEC_ASSERT(j<N, AT);
241 FMATVEC_ASSERT(i>=0, AT);
242 FMATVEC_ASSERT(j>=0, AT);
243 FMATVEC_ASSERT(i<M, AT);
244 FMATVEC_ASSERT(j<N, AT);
249 AT& e(
int i,
int j) {
257 const AT&
e(
int i,
int j)
const {
269 const AT&
e(
int i)
const {
290 constexpr int rows()
const {
return M;}
296 constexpr int cols()
const {
return N;}
322 return CblasRowMajor;
342 inline Matrix<General,Var,Fixed<N>,AT>& init(Eye,
const AT &val=1);
344 inline Matrix<General,Var,Fixed<N>,AT>& init(Noinit,
const AT &a=AT()) {
return *
this; }
350 explicit inline operator std::vector<std::vector<AT>>()
const;
357 explicit inline Matrix(
const std::vector<std::vector<AT>> &m);
378 inline const Matrix<General,Fixed<N>,Var,AT> T()
const;
380 template<
class Row>
inline void set(
int j,
const Vector<Row,AT> &x);
382 template<
class Col>
inline void set(
int i,
const RowVector<Col,AT> &x);
384 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);
386 template<
class Row>
inline void add(
int j,
const Vector<Row,AT> &x);
388 template<
class Col>
inline void add(
int i,
const RowVector<Col,AT> &x);
390 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);
393 template <
int N,
class AT>
395 std::istringstream iss(strs);
401 throw std::runtime_error(
"Input not fully read.");
406 template <
int N,
class AT>
410 #pragma GCC diagnostic push
411 #pragma GCC diagnostic ignored "-Warray-bounds"
412 #pragma GCC diagnostic ignored "-Wstringop-overflow"
414 for(
int i=0; i<M*N; i++)
417 #pragma GCC diagnostic pop
422 template <
int N,
class AT>
424 for(
int i=0; i<M; i++)
425 for(
int j=0; j<N; j++)
426 e(i,j) = (i==j) ? val : 0;
430 template <
int N,
class AT>
431 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 {
432 FMATVEC_ASSERT(I.end()<M, AT);
433 FMATVEC_ASSERT(J.end()<N, AT);
434 Matrix<General,Var,Var,AT> A(I.size(),J.size(),NONINIT);
436 for(
int i=0; i<A.
rows(); i++)
437 for(
int j=0; j<A.
cols(); j++)
438 A.e(i,j) = e(I.start()+i,J.start()+j);
443 template <
int N,
class AT>
444 inline const RowVector<Fixed<N>,AT> Matrix<General,Var,Fixed<N>,AT>::row(
int i)
const {
446 FMATVEC_ASSERT(i>=0, AT);
447 FMATVEC_ASSERT(i<M, AT);
449 RowVector<Fixed<N>,AT> x(NONINIT);
451 for(
int j=0; j<N; j++)
458 template <
int N,
class AT>
459 inline const Vector<Var,AT> Matrix<General,Var,Fixed<N>,AT>::col(
int j)
const {
461 FMATVEC_ASSERT(j>=0, AT);
462 FMATVEC_ASSERT(j<N, AT);
464 Vector<Var,AT> x(M,NONINIT);
466 for(
int i=0; i<M; i++)
473 template <
int N,
class AT>
474 inline const Matrix<General,Fixed<N>,Var,AT> Matrix<General,Var,Fixed<N>,AT>::T()
const {
475 Matrix<General,Fixed<N>,Var,AT> A(rows(),NONINIT);
476 for(
int i=0; i<N; i++)
477 for(
int j=0; j<M; j++)
482 template <
int N,
class AT>
template <
class Row>
483 inline void Matrix<General,Var,Fixed<N>,AT>::set(
int j,
const Vector<Row,AT> &x) {
484 FMATVEC_ASSERT(j<cols(), AT);
485 FMATVEC_ASSERT(rows()==x.size(), AT);
486 for(
int i=0; i<rows(); i++)
490 template <
int N,
class AT>
template <
class Col>
491 inline void Matrix<General,Var,Fixed<N>,AT>::set(
int i,
const RowVector<Col,AT> &x) {
492 FMATVEC_ASSERT(i<rows(), AT);
493 FMATVEC_ASSERT(cols()==x.size(), AT);
494 for(
int j=0; j<cols(); j++)
498 template <
int N,
class AT>
template<
class Type,
class Row,
class Col>
499 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) {
501 FMATVEC_ASSERT(I.end()<rows(), AT);
502 FMATVEC_ASSERT(J.end()<cols(), AT);
503 FMATVEC_ASSERT(I.size()==A.rows(), AT);
504 FMATVEC_ASSERT(J.size()==A.cols(), AT);
506 for(
int i=I.start(), ii=0; i<=I.end(); i++, ii++)
507 for(
int j=J.start(), jj=0; j<=J.end(); j++, jj++)
511 template <
int N,
class AT>
template <
class Row>
512 inline void Matrix<General,Var,Fixed<N>,AT>::add(
int j,
const Vector<Row,AT> &x) {
513 FMATVEC_ASSERT(j<cols(), AT);
514 FMATVEC_ASSERT(rows()==x.size(), AT);
515 for(
int i=0; i<rows(); i++)
519 template <
int N,
class AT>
template <
class Col>
520 inline void Matrix<General,Var,Fixed<N>,AT>::add(
int i,
const RowVector<Col,AT> &x) {
521 FMATVEC_ASSERT(i<rows(), AT);
522 FMATVEC_ASSERT(cols()==x.size(), AT);
523 for(
int j=0; j<cols(); j++)
527 template <
int N,
class AT>
template<
class Type,
class Row,
class Col>
528 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) {
530 FMATVEC_ASSERT(I.end()<rows(), AT);
531 FMATVEC_ASSERT(J.end()<cols(), AT);
532 FMATVEC_ASSERT(I.size()==A.rows(), AT);
533 FMATVEC_ASSERT(J.size()==A.cols(), AT);
535 for(
int i=I.start(), ii=0; i<=I.end(); i++, ii++)
536 for(
int j=J.start(), jj=0; j<=J.end(); j++, jj++)
537 e(i,j) += A.e(ii,jj);
540 template <
int N,
class AT>
542 std::vector<std::vector<AT>> ret(rows(),std::vector<AT>(cols()));
543 for(
int r=0; r<rows(); r++) {
544 for(
int c=0; c<cols(); c++)
550 template <
int N,
class AT>
552 if((m.empty()?0:m[0].size()) != N)
553 throw std::runtime_error(
"The input has "+std::to_string(m.empty()?0:m[0].size())+
" columns but "+std::to_string(N)+
" columns are required.");
554 for(
int r=0; r<
rows(); r++) {
555 if(
static_cast<int>(m[r].size())!=
cols())
556 throw std::runtime_error(
"The rows of the input have different length.");
557 for(
int c=0; c<
cols(); c++)
564 template <
int N,
class AT>
template <
class Type,
class Row,
class Col>
566 for(
int i=0; i<M; i++)
567 for(
int j=0; j<N; j++)
Shape class for general matrices.
Definition: types.h:116
constexpr int cols() const
Number of columns.
Definition: var_fixed_general_matrix.h:296
void resize(int m, int n)
Definition: var_fixed_general_matrix.h:165
constexpr int rows() const
Number of rows.
Definition: var_fixed_general_matrix.h:290
Matrix(const Matrix< General, Var, Fixed< N >, AT > &A)
Copy Constructor.
Definition: var_fixed_general_matrix.h:96
Matrix(const Matrix< General, Row, Col, AT > &A)
Copy Constructor.
Definition: var_fixed_general_matrix.h:106
const AT & operator()(int i, int j) const
Element operator.
Definition: var_fixed_general_matrix.h:240
const AT & e(int i) const
Element operator.
Definition: var_fixed_general_matrix.h:269
CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: var_fixed_general_matrix.h:310
AT * operator()()
Pointer operator.
Definition: var_fixed_general_matrix.h:278
AT & operator()(int i, int j)
Element operator.
Definition: var_fixed_general_matrix.h:227
CBLAS_ORDER blasOrder() const
Storage convention.
Definition: var_fixed_general_matrix.h:321
Matrix< General, Var, Fixed< N >, AT > & operator=(const Matrix< General, Var, Fixed< N >, AT > &A)
Assignment operator.
Definition: var_fixed_general_matrix.h:177
~Matrix()
Destructor.
Definition: var_fixed_general_matrix.h:146
int ldim() const
Leading dimension.
Definition: var_fixed_general_matrix.h:302
Matrix(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: var_fixed_general_matrix.h:117
const AT & e(int i, int j) const
Element operator.
Definition: var_fixed_general_matrix.h:257
const AT * operator()() const
Pointer operator.
Definition: var_fixed_general_matrix.h:284
This is a matrix class for general matrices.
Definition: var_general_matrix.h:41
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
This is a vector class of general shape in dense storage format.
Definition: var_vector.h:39
Namespace fmatvec.
Definition: _memory.cc:28