22 #ifndef general_matrix_h
23 #define general_matrix_h
45 typedef AT value_type;
49 friend class Vector<Ref,AT>;
52 friend class Matrix<Symmetric,Ref,Ref,AT>;
69 const AT* elePtr(
int i,
int j)
const {
70 return tp ? ele+i*lda+j : ele+i+j*lda;
73 AT* elePtr(
int i,
int j) {
74 return tp ? ele+i*lda+j : ele+i+j*lda;
77 Matrix(
int m_,
int n_,
int lda_,
bool tp_, Memory<AT> memory_,
const AT* ele_) : memory(memory_), ele((AT*)ele_), m(m_), n(n_), lda(lda_), tp(tp_) {
88 Matrix() : memory(), ele(nullptr) { }
96 Matrix(
int m_,
int n_,
Noinit) : memory(m_*n_), ele((AT*)memory.get()), m(m_), n(n_), lda(m_) { }
97 Matrix(
int m_,
int n_, Init ini=INIT,
const AT &a=0) : memory(m_*n_), ele((AT*)memory.get()), m(m_), n(n_), lda(m_) { init0(a); }
98 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); }
117 Matrix(
int m_,
int n_, AT* ele_) : memory(), ele(ele_), m(m_), n(n_), lda(m_) {
132 Matrix(
const std::string &strs);
139 template<
class Type,
class Row,
class Col>
145 Matrix<General,Ref,Ref,AT>& resize() {
153 Matrix<General,Ref,Ref,AT>& resize(
int m_,
int n_, Noinit) {
154 m = m_; n = n_; lda = m;
157 ele = (AT*)memory.get();
161 Matrix<General,Ref,Ref,AT>& resize(
int m,
int n, Init ini=INIT,
const AT &a=0) {
return resize(m,n,Noinit()).init0(a); }
163 Matrix<General,Ref,Ref,AT>& resize(
int m,
int n, Eye ini,
const AT &a=1) {
return resize(m,n,Noinit()).init(ini,a); }
173 inline Matrix<General,Ref,Ref,AT>& operator=(
const Matrix<General,Ref,Ref,AT> &A);
175 template<
class Type,
class Row,
class Col>
176 Matrix<General,Ref,Ref,AT>& operator=(
const Matrix<Type,Row,Col,AT> &A);
184 template<
class Type,
class Row,
class Col>
185 inline Matrix<General,Ref,Ref,AT>& operator<<(const Matrix<Type,Row,Col,AT> &A);
193 inline Matrix<General,Ref,Ref,AT>&
operator>>(
const Matrix<General,Ref,Ref,AT> &A);
207 #ifndef FMATVEC_NO_BOUNDS_CHECK
222 #ifndef FMATVEC_NO_BOUNDS_CHECK
232 AT& er(
int i,
int j) {
236 const AT& er(
int i,
int j)
const {
240 AT& et(
int i,
int j) {
244 const AT& et(
int i,
int j)
const {
248 AT& e(
int i,
int j) {
249 return tp ? et(i,j) : er(i,j);
252 const AT& e(
int i,
int j)
const {
253 return tp ? et(i,j) : er(i,j);
285 int ldim()
const {
return lda;};
301 return (tp)? CblasTrans : CblasNoTrans;
312 return CblasColMajor;
430 inline Matrix<General,Ref,Ref,AT>& init(Eye,
const AT &val=1);
431 inline Matrix<General,Ref,Ref,AT>& init(Noinit,
const AT &a=0) {
return *
this; }
432 inline Matrix<General,Ref,Ref,AT>& init0(
const AT &val=0);
433 inline Matrix<General,Ref,Ref,AT>& init0(Init,
const AT &a=0) {
return init0(a); }
439 inline operator std::vector<std::vector<AT> >()
const;
446 inline Matrix(std::vector<std::vector<AT> > m);
448 Matrix<General,Ref,Ref,AT> T() {
449 return Matrix<General,Ref,Ref,AT>(n,m,lda,tp?
false:
true,memory,ele);
452 const Matrix<General,Ref,Ref,AT> T()
const {
453 return Matrix<General,Ref,Ref,AT>(n,m,lda,tp?
false:
true,memory,ele);
459 std::istringstream iss(strs);
465 throw std::runtime_error(
"Input not fully read.");
491 ele = (AT*)memory.get();
493 #ifndef FMATVEC_NO_SIZE_CHECK
494 assert(m == A.
rows());
495 assert(n == A.
cols());
504 template <
class AT>
template<
class Type,
class Row,
class Col>
513 ele = (AT*)memory.get();
515 #ifndef FMATVEC_NO_SIZE_CHECK
516 assert(m == A.
rows());
517 assert(n == A.
cols());
526 template <
class AT>
template<
class Type,
class Row,
class Col>
535 ele = (AT*)memory.get();
545 for(
int i=0; i<m*n; i++) ele[i]=val;
553 for(
int i=0; i<
rows(); i++)
554 for(
int j=0; j<
cols(); j++)
558 for(
int i=0; i<
rows(); i++)
559 for(
int j=0; j<
cols(); j++)
569 for(
int i=0; i<m; i++)
570 for(
int j=0; j<n; j++)
571 et(i,j) = (i==j) ? val : 0;
573 for(
int i=0; i<m; i++)
574 for(
int j=0; j<n; j++)
575 er(i,j) = (i==j) ? val : 0;
591 #ifndef FMATVEC_NO_BOUNDS_CHECK
600 #ifndef FMATVEC_NO_BOUNDS_CHECK
609 #ifndef FMATVEC_NO_BOUNDS_CHECK
617 #ifndef FMATVEC_NO_BOUNDS_CHECK
626 #ifndef FMATVEC_NO_BOUNDS_CHECK
637 #ifndef FMATVEC_NO_BOUNDS_CHECK
648 #ifndef FMATVEC_NO_BOUNDS_CHECK
659 #ifndef FMATVEC_NO_BOUNDS_CHECK
678 std::vector<std::vector<AT> > ret(rows());
680 for(
int r=0; r<rows(); r++) {
681 ret[r].resize(cols());
682 for(
int c=0; c<cols(); c++)
687 for(
int r=0; r<rows(); r++) {
688 ret[r].resize(cols());
689 for(
int c=0; c<cols(); c++)
698 #ifndef FMATVEC_NO_INITIALIZATION
701 for(
int r=0; r<
rows(); r++) {
702 if(static_cast<int>(m[r].size())!=
cols())
703 throw std::runtime_error(
"The rows of the input have different length.");
704 for(
int c=0; c<
cols(); c++)
711 template <
class AT>
template <
class Type,
class Row,
class Col>
714 for(
int i=0; i<m; i++)
715 for(
int j=0; j<n; j++)
719 for(
int i=0; i<m; i++)
720 for(
int j=0; j<n; j++)
726 inline void Matrix<General,Ref,Ref,AT>::deepCopy(
const Matrix<General,Ref,Ref,AT> &A) {
729 for(
int i=0; i<m; i++)
730 for(
int j=0; j<n; j++)
734 for(
int i=0; i<m; i++)
735 for(
int j=0; j<n; j++)
740 for(
int i=0; i<m; i++)
741 for(
int j=0; j<n; j++)
745 for(
int i=0; i<m; i++)
746 for(
int j=0; j<n; j++)
753 inline void Matrix<General,Ref,Ref,AT>::deepCopy(
const Matrix<Symmetric,Ref,Ref,AT> &A) {
754 for(
int i=0; i<A.size(); i++) {
756 for(
int j=i+1; j<A.size(); j++)
757 er(i,j) = et(i,j) = A.ej(i,j);
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
int ldim() const
Leading dimension.
Definition: general_matrix.h:285
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:90
int rows() const
Number of rows.
Definition: general_matrix.h:273
AT & operator()(int i, int j)
Element operator.
Definition: general_matrix.h:206
Matrix(const Matrix< General, Ref, Ref, AT > &A)
Copy Constructor.
Definition: general_matrix.h:107
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: general_matrix.h:300
int rows() const
Number of rows.
int cols() const
Number of columns.
This is a rowvector class of general shape in dense storage format.
Definition: row_vector.h:36
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: general_matrix.h:311
This is a matrix class for general matrices.
Definition: general_matrix.h:41
const AT & operator()(int i, int j) const
Element operator.
Definition: general_matrix.h:221
std::istream & operator>>(std::istream &is, Matrix< Type, Row, Col, AT > &A)
Matrix input.
Definition: matrix.h:166
AT * operator()()
Pointer operator.
Definition: general_matrix.h:261
Matrix(int m_, int n_, AT *ele_)
Regular Constructor.
Definition: general_matrix.h:117
RowVector< Ref, AT > trans(const Vector< Ref, AT > &x)
Transpose of a vector.
Definition: linear_algebra.h:1470
This is a matrix class for symmetric matrices.
Definition: symmetric_matrix.h:39
bool transposed() const
Transposed status.
Definition: general_matrix.h:292
Basic shape class for matrices.
Definition: types.h:100
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:88
int end() const
Last element.
Definition: range.h:95
This is an index class for creating submatrices.
Definition: range.h:44
const AT * operator()() const
Pointer operator.
Definition: general_matrix.h:267
int start() const
First element.
Definition: range.h:89
int cols() const
Number of columns.
Definition: general_matrix.h:279