22 #ifndef general_matrix_h 23 #define general_matrix_h 45 typedef AT value_type;
64 const AT* elePtr(
int i,
int j)
const {
68 AT* elePtr(
int i,
int j) {
80 explicit Matrix() : memory(), ele(nullptr) { }
88 explicit Matrix(
int m_,
int n_,
Noinit) : memory(m_*n_), ele((AT*)memory.get()), m(m_), n(n_), lda(m_) { }
89 explicit Matrix(
int m_,
int n_, Init ini=INIT,
const AT &a=AT()) : memory(m_*n_), ele((AT*)memory.get()), m(m_), n(n_), lda(m_) { init0(a); }
90 explicit Matrix(
int m_,
int n_, Eye ini,
const AT &a=1) : memory(m_*n_), ele((AT*)memory.get()), m(m_), n(n_), lda(m_) { init(ini,a); }
106 template<
class Row,
class Col>
116 template<
class Type,
class Row,
class Col>
128 explicit Matrix(
int m_,
int n_, AT* ele_) : memory(), ele(ele_), m(m_), n(n_), lda(m_) {
143 Matrix(
const std::string &strs);
151 m = m_; n = n_; lda = m;
153 ele = (AT*)memory.get();
157 Matrix<General,Ref,Ref,AT>& resize(
int m,
int n, Init ini=INIT,
const AT &a=AT()) {
return resize(m,n,Noinit()).init0(a); }
159 Matrix<General,Ref,Ref,AT>& resize(
int m,
int n, Eye ini,
const AT &a=1) {
return resize(m,n,Noinit()).
init(ini,a); }
168 FMATVEC_ASSERT(m == A.
rows(), AT);
169 FMATVEC_ASSERT(n == A.
cols(), AT);
179 template<
class Type,
class Row,
class Col>
181 FMATVEC_ASSERT(m == A.
rows(), AT);
182 FMATVEC_ASSERT(n == A.
cols(), AT);
207 template<
class Type,
class Row,
class Col>
216 for(
size_t r=0; r<
rows(); ++r)
217 for(
size_t c=0; c<
cols(); ++c)
218 ret(r,c) = (*this)(r,c);
232 FMATVEC_ASSERT(i>=0, AT);
233 FMATVEC_ASSERT(j>=0, AT);
234 FMATVEC_ASSERT(i<m, AT);
235 FMATVEC_ASSERT(j<n, AT);
245 FMATVEC_ASSERT(i>=0, AT);
246 FMATVEC_ASSERT(j>=0, AT);
247 FMATVEC_ASSERT(i<m, AT);
248 FMATVEC_ASSERT(j<n, AT);
253 AT& e(
int i,
int j) {
257 const AT& e(
int i,
int j)
const {
310 return CblasColMajor;
367 inline Matrix<General,Ref,Ref,AT>& init(Eye,
const AT &val=1);
368 inline Matrix<General,Ref,Ref,AT>& init(Noinit,
const AT &a=AT()) {
return *
this; }
369 inline Matrix<General,Ref,Ref,AT>& init0(
const AT &val=AT());
370 inline Matrix<General,Ref,Ref,AT>& init0(Init,
const AT &a=AT()) {
return init0(a); }
376 explicit inline operator std::vector<std::vector<AT>>()
const;
383 explicit inline Matrix(
const std::vector<std::vector<AT>> &m);
401 const Matrix<General,Ref,Ref,AT> T()
const;
403 template<
class Row>
inline void set(
int j,
const Vector<Row,AT> &x);
405 template<
class Col>
inline void set(
int i,
const RowVector<Col,AT> &x);
409 template<
class Row>
inline void add(
int j,
const Vector<Row,AT> &x);
411 template<
class Col>
inline void add(
int i,
const RowVector<Col,AT> &x);
427 int countElements()
const;
432 std::istringstream iss(strs);
438 throw std::runtime_error(
"Input not fully read.");
443 inline Matrix<General,Ref,Ref,AT>& Matrix<General,Ref,Ref,AT>::init0(
const AT &val) {
444 for(
int i=0; i<m*n; i++) ele[i]=val;
450 for(
int i=0; i<
rows(); i++)
451 for(
int j=0; j<
cols(); j++)
458 for(
int i=0; i<m; i++)
459 for(
int j=0; j<n; j++)
460 e(i,j) = (i==j) ? val : 0;
466 FMATVEC_ASSERT(I.
end()<m, AT);
467 FMATVEC_ASSERT(J.
end()<n, AT);
470 for(
int i=0; i<A.
rows(); i++)
471 for(
int j=0; j<A.
cols(); j++)
479 FMATVEC_ASSERT(I.
end()<m, AT);
482 for(
int i=0; i<A.
rows(); i++)
483 for(
int j=0; j<A.
cols(); j++)
492 FMATVEC_ASSERT(i>=0, AT);
493 FMATVEC_ASSERT(i<m, AT);
497 for(
int j=0; j<n; j++)
506 FMATVEC_ASSERT(j>=0, AT);
507 FMATVEC_ASSERT(j<n, AT);
511 for(
int i=0; i<m; i++)
519 std::vector<std::vector<AT>> ret(rows());
520 for(
int r=0; r<rows(); r++) {
521 ret[r].resize(cols());
522 for(
int c=0; c<cols(); c++)
529 inline Matrix<General,Ref,Ref,AT>::Matrix(
const std::vector<std::vector<AT>> &m) : memory(m.size()*m[0].size()), ele((AT*)memory.get()), m(static_cast<int>(m.size())), n(static_cast<int>(m[0].size())), lda(static_cast<int>(m.size())) {
530 for(
int r=0; r<
rows(); r++) {
531 if(static_cast<int>(m[r].size())!=
cols())
532 throw std::runtime_error(
"The rows of the input have different length.");
533 for(
int c=0; c<
cols(); c++)
540 template <
class AT>
template <
class Type,
class Row,
class Col>
542 for(
int i=0; i<m; i++)
543 for(
int j=0; j<n; j++)
550 Matrix<General,Ref,Ref,AT> A(n,m,NONINIT);
551 for(
int i=0; i<n; i++)
552 for(
int j=0; j<m; j++)
557 template <
class AT>
template <
class Row>
558 inline void Matrix<General,Ref,Ref,AT>::set(
int j,
const Vector<Row,AT> &x) {
559 FMATVEC_ASSERT(j<
cols(), AT);
560 FMATVEC_ASSERT(
rows()==x.size(), AT);
561 for(
int i=0; i<
rows(); i++)
565 template <
class AT>
template <
class Col>
566 inline void Matrix<General,Ref,Ref,AT>::set(
int i,
const RowVector<Col,AT> &x) {
567 FMATVEC_ASSERT(i<
rows(), AT);
568 FMATVEC_ASSERT(
cols()==x.size(), AT);
569 for(
int j=0; j<
cols(); j++)
573 template <
class AT>
template<
class Type,
class Row,
class Col>
574 inline void Matrix<General,Ref,Ref,AT>::set(
const fmatvec::Range<Var,Var> &I,
const fmatvec::Range<Var,Var> &J,
const Matrix<Type,Row,Col,AT> &A) {
576 FMATVEC_ASSERT(I.
end()<
rows(), AT);
577 FMATVEC_ASSERT(J.
end()<
cols(), AT);
578 FMATVEC_ASSERT(I.
size()==A.rows(), AT);
579 FMATVEC_ASSERT(J.
size()==A.cols(), AT);
581 for(
int i=I.
start(), ii=0; i<=I.
end(); i++, ii++)
582 for(
int j=J.
start(), jj=0; j<=J.
end(); j++, jj++)
586 template <
class AT>
template <
class Row>
587 inline void Matrix<General,Ref,Ref,AT>::add(
int j,
const Vector<Row,AT> &x) {
588 FMATVEC_ASSERT(j<
cols(), AT);
589 FMATVEC_ASSERT(
rows()==x.size(), AT);
590 for(
int i=0; i<
rows(); i++)
594 template <
class AT>
template <
class Col>
595 inline void Matrix<General,Ref,Ref,AT>::add(
int i,
const RowVector<Col,AT> &x) {
596 FMATVEC_ASSERT(i<
rows(), AT);
597 FMATVEC_ASSERT(
cols()==x.size(), AT);
598 for(
int j=0; j<
cols(); j++)
602 template <
class AT>
template<
class Type,
class Row,
class Col>
603 inline void Matrix<General,Ref,Ref,AT>::add(
const fmatvec::Range<Var,Var> &I,
const fmatvec::Range<Var,Var> &J,
const Matrix<Type,Row,Col,AT> &A) {
605 FMATVEC_ASSERT(I.
end()<
rows(), AT);
606 FMATVEC_ASSERT(J.
end()<
cols(), AT);
607 FMATVEC_ASSERT(I.
size()==A.rows(), AT);
608 FMATVEC_ASSERT(J.
size()==A.cols(), AT);
610 for(
int i=I.
start(), ii=0; i<=I.
end(); i++, ii++)
611 for(
int j=J.
start(), jj=0; j<=J.
end(); j++, jj++)
612 e(i,j) += A.e(ii,jj);
615 template <
class AT>
template<
class Type,
class Row,
class Col>
616 inline void Matrix<General,Ref,Ref,AT>::sub(
const fmatvec::Range<Var,Var> &I,
const fmatvec::Range<Var,Var> &J,
const Matrix<Type,Row,Col,AT> &A) {
618 FMATVEC_ASSERT(I.
end()<
rows(), AT);
619 FMATVEC_ASSERT(J.
end()<
cols(), AT);
620 FMATVEC_ASSERT(I.
size()==A.rows(), AT);
621 FMATVEC_ASSERT(J.
size()==A.cols(), AT);
623 for(
int i=I.
start(), ii=0; i<=I.
end(); i++, ii++)
624 for(
int j=J.
start(), jj=0; j<=J.
end(); j++, jj++)
625 e(i,j) -= A.e(ii,jj);
629 inline void Matrix<General,Ref,Ref,AT>::ref(Matrix<General,Ref,Ref,AT> &A,
const fmatvec::Range<Var,Var> &I,
const fmatvec::Range<Var,Var> &J) {
630 FMATVEC_ASSERT(I.
end()<A.rows(), AT);
631 FMATVEC_ASSERT(J.
end()<A.cols(), AT);
640 inline void Matrix<General,Ref,Ref,AT>::ref(Matrix<Symmetric,Ref,Ref,AT> &A,
const fmatvec::Range<Var,Var> &I,
const fmatvec::Range<Var,Var> &J) {
641 FMATVEC_ASSERT(I.
end()<A.size(), AT);
642 FMATVEC_ASSERT(J.
end()<A.size(), AT);
644 FMATVEC_ASSERT(J.
end()<=I.
start(), AT);
655 for (
int i = 0; i < m; i++) {
656 for (
int j = 0; j < n; j++) {
657 if (fabs(e(i, j)) > 1e-16 || i==j) {
int end() const
Last element.
Definition: range.h:91
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: general_matrix.h:309
Matrix(const Matrix< General, Row, Col, AT > &A)
Copy Constructor.
Definition: general_matrix.h:107
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
Shape class for symmetric matrices.
Definition: types.h:148
int rows() const
Number of rows.
Definition: general_matrix.h:278
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:82
AT & operator()(int i, int j)
Element operator.
Definition: general_matrix.h:231
Matrix(const Matrix< General, Ref, Ref, AT > &A)
Copy Constructor.
Definition: general_matrix.h:97
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: general_matrix.h:298
const Matrix< General, Ref, Ref, AT > T() const
Cast to AT.
This is a rowvector class of general shape in dense storage format.
Definition: row_vector.h:36
int start() const
First element.
Definition: range.h:85
Matrix< General, Ref, Ref, AT > & init(const AT &val=AT())
Initialization.
Definition: general_matrix.h:449
int countElements() const
Count nonzero elements.
const AT * operator()() const
Pointer operator.
Definition: general_matrix.h:272
int cols() const
Number of columns.
This is a matrix class for general matrices.
Definition: general_matrix.h:41
Namespace fmatvec.
Definition: _memory.cc:28
int cols() const
Number of columns.
Definition: general_matrix.h:284
int size() const
Size.
Definition: range.h:97
Matrix< General, Ref, Ref, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: general_matrix.h:180
AT * operator()()
Pointer operator.
Definition: general_matrix.h:266
Matrix< General, Ref, Ref, AT > & operator&=(Matrix< General, Ref, Ref, AT > &A)
Reference operator.
Definition: general_matrix.h:192
int ldim() const
Leading dimension.
Definition: general_matrix.h:290
Matrix(int m_, int n_, AT *ele_)
Regular Constructor.
Definition: general_matrix.h:128
Basic shape class for matrices.
Definition: types.h:132
const AT & operator()(int i, int j) const
Element operator.
Definition: general_matrix.h:244
This is a vector class of general shape in dense storage format.
Definition: vector.h:39
This is a matrix class of general quadratic matrices.
Definition: square_matrix.h:39
Matrix()
Standard constructor.
Definition: general_matrix.h:80
Matrix< General, Ref, Ref, AT > & operator=(const Matrix< General, Ref, Ref, AT > &A)
Assignment operator.
Definition: general_matrix.h:167
int rows() const
Number of rows.
Matrix(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: general_matrix.h:117
This is an index class for creating submatrices.
Definition: range.h:44