22 #ifndef var_fixed_general_matrix_h 23 #define var_fixed_general_matrix_h 42 typedef AT value_type;
58 explicit Matrix() : ele(
nullptr) { }
60 explicit Matrix(
int m,
Noinit) : M(m), ele(
new AT[M*N]) { }
61 explicit Matrix(
int m,
Init ini=INIT,
const AT &a=AT()) : M(m), ele(
new AT[M*N]) { init(a); }
62 explicit Matrix(
int m,
Eye ini,
const AT &a=1) : M(m), ele(
new AT[M*N]) { init(ini,a); }
63 explicit Matrix(
int m,
int n,
Noinit) : M(m), ele(
new AT[M*N]) { FMATVEC_ASSERT(n==N, AT); }
64 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); }
65 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); }
81 template<
class Row,
class Col>
83 FMATVEC_ASSERT(A.
cols() == N, AT);
92 template<
class Type,
class Row,
class Col>
94 FMATVEC_ASSERT(A.
cols() == N, AT);
110 Matrix(
const std::string &strs);
126 Matrix<General,Var,Fixed<N>,AT>& resize(
int m, Init ini=INIT,
const AT &a=AT()) {
return resize(m,Noinit()).init(a); }
128 Matrix<General,Var,Fixed<N>,AT>& resize(
int m, Eye ini,
const AT &a=1) {
return resize(m,Noinit()).init(ini,a); }
134 throw std::runtime_error(
"A var-fixed matrix can only be resized in the first dimension.");
145 FMATVEC_ASSERT(M == A.
rows(), AT);
155 template <
class Type,
class Row,
class Col>
157 FMATVEC_ASSERT(N == A.
cols(), AT);
158 FMATVEC_ASSERT(M == A.
rows(), AT);
168 template <
class Type,
class Row,
class Col>
170 FMATVEC_ASSERT(N == A.
cols(), AT);
171 if(M!=A.
rows()) resize(A.
rows(),NONINIT);
185 FMATVEC_ASSERT(i>=0, AT);
186 FMATVEC_ASSERT(j>=0, AT);
187 FMATVEC_ASSERT(i<M, AT);
188 FMATVEC_ASSERT(j<N, AT);
198 FMATVEC_ASSERT(i>=0, AT);
199 FMATVEC_ASSERT(j>=0, AT);
200 FMATVEC_ASSERT(i<M, AT);
201 FMATVEC_ASSERT(j<N, AT);
206 AT& e(
int i,
int j) {
214 const AT&
e(
int i,
int j)
const {
226 const AT&
e(
int i)
const {
279 return CblasRowMajor;
296 inline Matrix<General,Var,Fixed<N>,AT>& init(Eye,
const AT &val=1);
297 inline Matrix<General,Var,Fixed<N>,AT>& init(Noinit,
const AT &a=AT()) {
return *
this; }
303 explicit inline operator std::vector<std::vector<AT>>()
const;
310 explicit inline Matrix(
const std::vector<std::vector<AT>> &m);
331 inline const Matrix<General,Fixed<N>,Var,AT> T()
const;
333 template<
class Row>
inline void set(
int j,
const Vector<Row,AT> &x);
335 template<
class Col>
inline void set(
int i,
const RowVector<Col,AT> &x);
337 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);
339 template<
class Row>
inline void add(
int j,
const Vector<Row,AT> &x);
341 template<
class Col>
inline void add(
int i,
const RowVector<Col,AT> &x);
343 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);
346 template <
int N,
class AT>
347 Matrix<General,Var,Fixed<N>,AT>::Matrix(
const std::string &strs) : ele(0) {
348 std::istringstream iss(strs);
354 throw std::runtime_error(
"Input not fully read.");
356 template <
int N,
class AT> Matrix<General,Var,Fixed<N>,AT>::Matrix(
const char * strs) :
357 Matrix<General,Var,Fixed<N>,AT>::Matrix(std::string(strs)) {}
359 template <
int N,
class AT>
360 inline Matrix<General,Var,Fixed<N>,AT>& Matrix<General,Var,Fixed<N>,AT>::init(
const AT &val) {
361 for(
int i=0; i<M*N; i++)
366 template <
int N,
class AT>
367 inline Matrix<General,Var,Fixed<N>,AT>& Matrix<General,Var,Fixed<N>,AT>::init(Eye eye,
const AT &val) {
368 for(
int i=0; i<M; i++)
369 for(
int j=0; j<N; j++)
370 e(i,j) = (i==j) ? val : 0;
374 template <
int N,
class AT>
375 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 {
376 FMATVEC_ASSERT(I.end()<M, AT);
377 FMATVEC_ASSERT(J.end()<N, AT);
378 Matrix<General,Var,Var,AT> A(I.end()-I.start()+1,J.end()-J.start()+1,NONINIT);
380 for(
int i=0; i<A.
rows(); i++)
381 for(
int j=0; j<A.
cols(); j++)
382 A.e(i,j) = e(I.start()+i,J.start()+j);
387 template <
int N,
class AT>
388 inline const RowVector<Fixed<N>,AT> Matrix<General,Var,Fixed<N>,AT>::row(
int i)
const {
390 FMATVEC_ASSERT(i>=0, AT);
391 FMATVEC_ASSERT(i<M, AT);
393 RowVector<Fixed<N>,AT> x(NONINIT);
395 for(
int j=0; j<N; j++)
402 template <
int N,
class AT>
403 inline const Vector<Var,AT> Matrix<General,Var,Fixed<N>,AT>::col(
int j)
const {
405 FMATVEC_ASSERT(j>=0, AT);
406 FMATVEC_ASSERT(j<N, AT);
408 Vector<Var,AT> x(M,NONINIT);
410 for(
int i=0; i<M; i++)
417 template <
int N,
class AT>
418 inline const Matrix<General,Fixed<N>,Var,AT> Matrix<General,Var,Fixed<N>,AT>::T()
const {
419 Matrix<General,Fixed<N>,Var,AT> A(rows(),NONINIT);
420 for(
int i=0; i<N; i++)
421 for(
int j=0; j<M; j++)
426 template <
int N,
class AT>
template <
class Row>
427 inline void Matrix<General,Var,Fixed<N>,AT>::set(
int j,
const Vector<Row,AT> &x) {
428 FMATVEC_ASSERT(j<cols(), AT);
429 FMATVEC_ASSERT(rows()==x.size(), AT);
430 for(
int i=0; i<rows(); i++)
434 template <
int N,
class AT>
template <
class Col>
435 inline void Matrix<General,Var,Fixed<N>,AT>::set(
int i,
const RowVector<Col,AT> &x) {
436 FMATVEC_ASSERT(i<rows(), AT);
437 FMATVEC_ASSERT(cols()==x.size(), AT);
438 for(
int j=0; j<cols(); j++)
442 template <
int N,
class AT>
template<
class Type,
class Row,
class Col>
443 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) {
445 FMATVEC_ASSERT(I.end()<rows(), AT);
446 FMATVEC_ASSERT(J.end()<cols(), AT);
447 FMATVEC_ASSERT(I.size()==A.rows(), AT);
448 FMATVEC_ASSERT(J.size()==A.cols(), AT);
450 for(
int i=I.start(), ii=0; i<=I.end(); i++, ii++)
451 for(
int j=J.start(), jj=0; j<=J.end(); j++, jj++)
455 template <
int N,
class AT>
template <
class Row>
456 inline void Matrix<General,Var,Fixed<N>,AT>::add(
int j,
const Vector<Row,AT> &x) {
457 FMATVEC_ASSERT(j<cols(), AT);
458 FMATVEC_ASSERT(rows()==x.size(), AT);
459 for(
int i=0; i<rows(); i++)
463 template <
int N,
class AT>
template <
class Col>
464 inline void Matrix<General,Var,Fixed<N>,AT>::add(
int i,
const RowVector<Col,AT> &x) {
465 FMATVEC_ASSERT(i<rows(), AT);
466 FMATVEC_ASSERT(cols()==x.size(), AT);
467 for(
int j=0; j<cols(); j++)
471 template <
int N,
class AT>
template<
class Type,
class Row,
class Col>
472 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) {
474 FMATVEC_ASSERT(I.end()<rows(), AT);
475 FMATVEC_ASSERT(J.end()<cols(), AT);
476 FMATVEC_ASSERT(I.size()==A.rows(), AT);
477 FMATVEC_ASSERT(J.size()==A.cols(), AT);
479 for(
int i=I.start(), ii=0; i<=I.end(); i++, ii++)
480 for(
int j=J.start(), jj=0; j<=J.end(); j++, jj++)
481 e(i,j) += A.e(ii,jj);
484 template <
int N,
class AT>
485 inline Matrix<General,Var,Fixed<N>,AT>::operator std::vector<std::vector<AT>>()
const {
486 std::vector<std::vector<AT>> ret(rows());
487 for(
int r=0; r<rows(); r++) {
488 ret[r].resize(cols());
489 for(
int c=0; c<cols(); c++)
495 template <
int N,
class AT>
496 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]) {
498 throw std::runtime_error(
"The input has "+std::to_string(m[0].size())+
" columns but "+std::to_string(N)+
" columns are required.");
499 for(
int r=0; r<
rows(); r++) {
500 if(static_cast<int>(m[r].size())!=
cols())
501 throw std::runtime_error(
"The rows of the input have different length.");
502 for(
int c=0; c<
cols(); c++)
509 template <
int N,
class AT>
template <
class Type,
class Row,
class Col>
510 inline Matrix<General,Var,Fixed<N>,AT>& Matrix<General,Var,Fixed<N>,AT>::copy(
const Matrix<Type,Row,Col,AT> &A) {
511 for(
int i=0; i<M; i++)
512 for(
int j=0; j<N; j++)
int rows() const
Number of rows.
Definition: var_fixed_general_matrix.h:247
Matrix< General, Var, Fixed< N >, AT > & operator=(const Matrix< General, Var, Fixed< N >, AT > &A)
Assignment operator.
Definition: var_fixed_general_matrix.h:144
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:226
const AT * operator()() const
Pointer operator.
Definition: var_fixed_general_matrix.h:241
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:82
Matrix(const Matrix< General, Row, Col, AT > &A)
Copy Constructor.
Definition: var_fixed_general_matrix.h:82
AT * operator()()
Pointer operator.
Definition: var_fixed_general_matrix.h:235
AT & operator()(int i, int j)
Element operator.
Definition: var_fixed_general_matrix.h:184
int ldim() const
Leading dimension.
Definition: var_fixed_general_matrix.h:259
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: var_fixed_general_matrix.h:267
int cols() const
Number of columns.
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: var_fixed_general_matrix.h:278
int cols() const
Number of columns.
Definition: var_fixed_general_matrix.h:253
Namespace fmatvec.
Definition: _memory.cc:28
const AT & operator()(int i, int j) const
Element operator.
Definition: var_fixed_general_matrix.h:197
~Matrix()
Destructor.
Definition: var_fixed_general_matrix.h:115
Matrix< General, Var, Fixed< N >, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: var_fixed_general_matrix.h:156
const AT & e(int i, int j) const
Element operator.
Definition: var_fixed_general_matrix.h:214
void resize(int m, int n)
Definition: var_fixed_general_matrix.h:132
Basic shape class for matrices.
Definition: types.h:132
Matrix(const Matrix< General, Var, Fixed< N >, AT > &A)
Copy Constructor.
Definition: var_fixed_general_matrix.h:72
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:93