22 #ifndef general_matrix_h
23 #define general_matrix_h
44 typedef AT AtomicType;
48 friend class Vector<Ref,AT>;
51 friend class Matrix<Symmetric,Ref,Ref,AT>;
68 const AT* elePtr(
int i,
int j)
const {
69 return tp ? ele+i*lda+j : ele+i+j*lda;
72 AT* elePtr(
int i,
int j) {
73 return tp ? ele+i*lda+j : ele+i+j*lda;
76 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_) {
87 Matrix() : memory(), ele(0), m(0), n(0), lda(0), tp(false) { }
95 Matrix(
int m_,
int n_,
Noinit) : memory(m_*n_), ele((AT*)memory.get()), m(m_), n(n_), lda(m_), tp(false) { }
96 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_), tp(false) { init0(a); }
97 Matrix(
int m_,
int n_, Eye ini,
const AT &a=1) : memory(m_*n_), ele((AT*)memory.get()), m(m_), n(n_), lda(m_), tp(false) { init(ini,a); }
116 Matrix(
int m_,
int n_, AT* ele_) : memory(), ele(ele_), m(m_), n(n_), lda(m_), tp(false) {
138 template<
class Type,
class Row,
class Col>
144 Matrix<General,Ref,Ref,AT>& resize() {
152 Matrix<General,Ref,Ref,AT>& resize(
int m_,
int n_, Noinit) {
153 m = m_; n = n_; lda = m;
156 ele = (AT*)memory.get();
160 Matrix<General,Ref,Ref,AT>& resize(
int m,
int n, Init ini=INIT,
const AT &a=0) {
return resize(m,n,Noinit()).init0(a); }
162 Matrix<General,Ref,Ref,AT>& resize(
int m,
int n, Eye ini,
const AT &a=1) {
return resize(m,n,Noinit()).init(ini,a); }
172 inline Matrix<General,Ref,Ref,AT>& operator=(
const Matrix<General,Ref,Ref,AT> &A);
174 template<
class Type,
class Row,
class Col>
175 Matrix<General,Ref,Ref,AT>& operator=(
const Matrix<Type,Row,Col,AT> &A);
183 template<
class Type,
class Row,
class Col>
184 inline Matrix<General,Ref,Ref,AT>& operator<<(const Matrix<Type,Row,Col,AT> &A);
192 inline Matrix<General,Ref,Ref,AT>&
operator>>(
const Matrix<General,Ref,Ref,AT> &A);
206 #ifndef FMATVEC_NO_BOUNDS_CHECK
221 #ifndef FMATVEC_NO_BOUNDS_CHECK
231 AT& er(
int i,
int j) {
235 const AT& er(
int i,
int j)
const {
239 AT& et(
int i,
int j) {
243 const AT& et(
int i,
int j)
const {
247 AT& e(
int i,
int j) {
248 return tp ? et(i,j) : er(i,j);
251 const AT& e(
int i,
int j)
const {
252 return tp ? et(i,j) : er(i,j);
284 int ldim()
const {
return lda;};
300 return (tp)? CblasTrans : CblasNoTrans;
311 return CblasColMajor;
429 inline Matrix<General,Ref,Ref,AT>& init(Eye,
const AT &a=1);
430 inline Matrix<General,Ref,Ref,AT>& init(Noinit,
const AT &a=0) {
return *
this; }
431 inline Matrix<General,Ref,Ref,AT>& init0(
const AT &a=0);
432 inline Matrix<General,Ref,Ref,AT>& init0(Init,
const AT &a=0) {
return init0(a); }
438 inline operator std::vector<std::vector<AT> >();
445 inline Matrix(std::vector<std::vector<AT> > m);
447 Matrix<General,Ref,Ref,AT> T() {
448 return Matrix<General,Ref,Ref,AT>(n,m,lda,tp?
false:
true,memory,ele);
451 const Matrix<General,Ref,Ref,AT> T()
const {
452 return Matrix<General,Ref,Ref,AT>(n,m,lda,tp?
false:
true,memory,ele);
461 std::istringstream iss(strs);
464 if(c==
'[') iss.str(strs);
465 else iss.str(std::string(
"[")+strs+
"]");
493 ele = (AT*)memory.get();
497 for(
int i=0; i<m; i++)
498 for(
int j=0; j<n; j++) {
527 ele = (AT*)memory.get();
529 #ifndef FMATVEC_NO_SIZE_CHECK
530 assert(m == A.
rows());
531 assert(n == A.
cols());
540 template <
class AT>
template<
class Type,
class Row,
class Col>
549 ele = (AT*)memory.get();
551 #ifndef FMATVEC_NO_SIZE_CHECK
552 assert(m == A.
rows());
553 assert(n == A.
cols());
562 template <
class AT>
template<
class Type,
class Row,
class Col>
571 ele = (AT*)memory.get();
581 for(
int i=0; i<m*n; i++) ele[i]=val;
589 for(
int i=0; i<
rows(); i++)
590 for(
int j=0; j<
cols(); j++)
594 for(
int i=0; i<
rows(); i++)
595 for(
int j=0; j<
cols(); j++)
605 for(
int i=0; i<m; i++)
606 for(
int j=0; j<n; j++)
607 et(i,j) = (i==j) ? val : 0;
609 for(
int i=0; i<m; i++)
610 for(
int j=0; j<n; j++)
611 er(i,j) = (i==j) ? val : 0;
627 #ifndef FMATVEC_NO_BOUNDS_CHECK
636 #ifndef FMATVEC_NO_BOUNDS_CHECK
645 #ifndef FMATVEC_NO_BOUNDS_CHECK
653 #ifndef FMATVEC_NO_BOUNDS_CHECK
662 #ifndef FMATVEC_NO_BOUNDS_CHECK
673 #ifndef FMATVEC_NO_BOUNDS_CHECK
684 #ifndef FMATVEC_NO_BOUNDS_CHECK
695 #ifndef FMATVEC_NO_BOUNDS_CHECK
714 std::vector<std::vector<AT> > ret(rows());
716 for(
int r=0; r<rows(); r++) {
717 ret[r].resize(cols());
718 for(
int c=0; c<cols(); c++)
723 for(
int r=0; r<rows(); r++) {
724 ret[r].resize(cols());
725 for(
int c=0; c<cols(); c++)
733 inline Matrix<General,Ref,Ref,AT>::Matrix(std::vector<std::vector<AT> > m) : memory(m.size()*m[0].size()), ele((AT*)memory.get()), m(m.size()), n(m[0].size()), lda(m.size()), tp(false) {
734 #ifndef FMATVEC_NO_INITIALIZATION
737 for(
int r=0; r<
rows(); r++) {
738 assert(m[r].size()==
cols());
739 for(
int c=0; c<
cols(); c++)
746 template <
class AT>
template <
class Type,
class Row,
class Col>
749 for(
int i=0; i<m; i++)
750 for(
int j=0; j<n; j++)
754 for(
int i=0; i<m; i++)
755 for(
int j=0; j<n; j++)
761 inline void Matrix<General,Ref,Ref,AT>::deepCopy(
const Matrix<General,Ref,Ref,AT> &A) {
764 for(
int i=0; i<m; i++)
765 for(
int j=0; j<n; j++)
769 for(
int i=0; i<m; i++)
770 for(
int j=0; j<n; j++)
775 for(
int i=0; i<m; i++)
776 for(
int j=0; j<n; j++)
780 for(
int i=0; i<m; i++)
781 for(
int j=0; j<n; j++)
788 inline void Matrix<General,Ref,Ref,AT>::deepCopy(
const Matrix<Symmetric,Ref,Ref,AT> &A) {
789 for(
int i=0; i<A.size(); i++) {
791 for(
int j=i+1; j<A.size(); j++)
792 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:284
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:86
int rows() const
Number of rows.
Definition: general_matrix.h:272
AT & operator()(int i, int j)
Element operator.
Definition: general_matrix.h:205
Matrix(const Matrix< General, Ref, Ref, AT > &A)
Copy Constructor.
Definition: general_matrix.h:106
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: general_matrix.h:299
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:310
This is a matrix class for general matrices.
Definition: general_matrix.h:40
const AT & operator()(int i, int j) const
Element operator.
Definition: general_matrix.h:220
std::istream & operator>>(std::istream &is, Matrix< Type, Row, Col, AT > &A)
Matrix input.
Definition: matrix.h:171
AT * operator()()
Pointer operator.
Definition: general_matrix.h:260
Matrix(int m_, int n_, AT *ele_)
Regular Constructor.
Definition: general_matrix.h:116
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:291
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:87
int end() const
Last element.
Definition: range.h:95
~Matrix()
Destructor.
Definition: general_matrix.h:135
This is an index class for creating submatrices.
Definition: range.h:44
const AT * operator()() const
Pointer operator.
Definition: general_matrix.h:266
int start() const
First element.
Definition: range.h:89
int cols() const
Number of columns.
Definition: general_matrix.h:278