22 #ifndef symmetric_matrix_h
23 #define symmetric_matrix_h
53 const AT* elePtr(
int i,
int j)
const {
54 return j > i ? ele+i*lda+j : ele+i+j*lda;
57 AT* elePtr(
int i,
int j) {
58 return j > i ? ele+i*lda+j : ele+i+j*lda;
61 Matrix(
int n_,
int lda_, Memory<AT> memory_,
const AT* ele_) : memory(memory_), ele((AT*)ele_), n(n_), lda(lda_) {
68 typedef AT value_type;
86 Matrix(
int n_,
Noinit) : memory(n_*n_), ele((AT*)memory.get()), n(n_), lda(n_) { }
87 Matrix(
int n_, Init ini=INIT,
const AT &a=0) : memory(n_*n_), ele((AT*)memory.get()), n(n_), lda(n_) { init(a); }
88 Matrix(
int n_, Eye ini,
const AT &a=1) : memory(n_*n_), ele((AT*)memory.get()), n(n_), lda(n_) { init(ini,a); }
89 Matrix(
int m_,
int n_, Noinit) : memory(n_*n_), ele((AT*)memory.get()), n(n_), lda(n_) { }
90 Matrix(
int m_,
int n_, Init ini=INIT,
const AT &a=0) : memory(n_*n_), ele((AT*)memory.get()), n(n_), lda(n_) { init(a); }
91 Matrix(
int m_,
int n_, Eye ini,
const AT &a=1) : memory(n_*n_), ele((AT*)memory.get()), n(n_), lda(n_) { init(ini,a); }
108 #ifndef FMATVEC_NO_SIZE_CHECK
113 template<
class Type,
class Row,
class Col>
115 #ifndef FMATVEC_NO_SIZE_CHECK
129 Matrix(
int n_, AT* ele_) : memory(), ele(ele_), n(n_), lda(n_) { }
131 Matrix(
const std::vector<std::vector<AT>> &A);
144 Matrix<Symmetric,Ref,Ref,AT>& resize(
int n_, Noinit) {
147 ele = (AT*)memory.get();
151 Matrix<Symmetric,Ref,Ref,AT>& resize(
int n, Init ini=INIT,
const AT &a=0) {
return resize(n,Noinit()).
init(a); }
153 Matrix<Symmetric,Ref,Ref,AT>& resize(
int n, Eye ini,
const AT &a=1) {
return resize(n,Noinit()).
init(ini,a); }
159 throw std::runtime_error(
"A symmetric matrix cannot have different dimensions for rows and columns.");
178 template<
class Type,
class Row,
class Col>
187 template<
class Type,
class Row,
class Col>
210 #ifndef FMATVEC_NO_BOUNDS_CHECK
224 #ifndef FMATVEC_NO_BOUNDS_CHECK
234 AT& ei(
int i,
int j) {
238 const AT& ei(
int i,
int j)
const {
242 AT& ej(
int i,
int j) {
246 const AT& ej(
int i,
int j)
const {
250 AT& e(
int i,
int j) {
251 return j > i ? ej(i,j) : ei(i,j);
254 const AT& e(
int i,
int j)
const {
255 return j > i ? ej(i,j) : ei(i,j);
293 int ldim()
const {
return lda;};
303 return CblasColMajor;
333 inline Matrix<Symmetric,Ref,Ref,AT>& init(Eye,
const AT &val=1);
334 inline Matrix<Symmetric,Ref,Ref,AT>& init(Noinit,
const AT &a=0) {
return *
this; }
347 inline Matrix<General,Ref,Ref,AT>
operator()(
const Range<Var,Var> &I,
const Range<Var,Var> &J);
353 inline const Matrix<General,Ref,Ref,AT>
operator()(
const Range<Var,Var> &I,
const Range<Var,Var> &J)
const;
366 inline Matrix<Symmetric,Ref,Ref,AT>
operator()(
const Range<Var,Var> &I);
372 inline const Matrix<Symmetric,Ref,Ref,AT>
operator()(
const Range<Var,Var> &I)
const;
385 inline Matrix<General,Ref,Ref,AT>
operator()(
int i1,
int j1,
int i2,
int j2);
391 inline const Matrix<General,Ref,Ref,AT>
operator()(
int i1,
int j1,
int i2,
int j2)
const;
397 inline operator std::vector<std::vector<AT> >()
const;
402 for(
int r=0; r<
rows(); r++) {
403 if(static_cast<int>(A[r].size())!=
cols())
404 throw std::runtime_error(
"The rows of the input have different length.");
405 for(
int c=0; c<
cols(); c++) {
407 if(r>c && fabs(A[r][c]-A[c][r])>fabs(A[r][c])*1e-13+1e-13)
408 throw std::runtime_error(
"The input is not symmetric.");
431 ele = (AT*)memory.get();
433 #ifndef FMATVEC_NO_SIZE_CHECK
434 assert(n == A.
size());
443 template <
class AT>
template<
class Type,
class Row,
class Col>
450 ele = (AT*)memory.get();
452 #ifndef FMATVEC_NO_SIZE_CHECK
454 assert(n == A.
rows());
464 template <
class AT>
template<
class Type,
class Row,
class Col>
467 #ifndef FMATVEC_NO_SIZE_CHECK
475 ele = (AT*)memory.get();
486 for(
int i=0; i<
rows(); i++)
487 for(
int j=i; j<
cols(); j++)
496 for(
int i=0; i<size(); i++) {
498 for(
int j=i+1; j<size(); j++) {
526 #ifndef FMATVEC_NO_BOUNDS_CHECK
542 #ifndef FMATVEC_NO_BOUNDS_CHECK
558 #ifndef FMATVEC_NO_BOUNDS_CHECK
567 #ifndef FMATVEC_NO_BOUNDS_CHECK
576 std::vector<std::vector<AT> > ret(rows());
577 for(
int r=0; r<rows(); r++) {
578 ret[r].resize(cols());
579 for(
int c=0; c<cols(); c++)
580 ret[r][c]=
operator()(r,c);
589 for(
int i=0; i<n; i++)
590 for(
int j=i; j<n; j++)
594 template <
class AT>
template <
class Type,
class Row,
class Col>
595 inline void Matrix<Symmetric,Ref,Ref,AT>::deepCopy(
const Matrix<Type,Row,Col,AT> &A) {
596 for(
int i=0; i<n; i++)
597 for(
int j=i; j<n; j++)
Matrix(const Matrix< General, Ref, Ref, AT > &A)
Element operator.
Definition: symmetric_matrix.h:107
AT & operator()(int i, int j)
Element operator.
Definition: symmetric_matrix.h:209
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
Shape class for symmetric matrices.
Definition: types.h:116
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:90
int rows() const
Number of rows.
Definition: general_matrix.h:273
bool transposed()
The storage format of a symmetric matrix is fortran-storage order -> return always false...
Definition: symmetric_matrix.h:164
int rows() const
Number of rows.
Definition: symmetric_matrix.h:281
int rows() const
Number of rows.
int cols() const
Number of columns.
const CBLAS_UPLO blasUplo() const
Symmetry convention.
Definition: symmetric_matrix.h:313
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: symmetric_matrix.h:302
int cols() const
Number of columns.
Definition: symmetric_matrix.h:287
Matrix(int n_, AT *ele_)
Regular Constructor.
Definition: symmetric_matrix.h:129
This is a matrix class for general matrices.
Definition: general_matrix.h:41
int size() const
Size.
Definition: symmetric_matrix.h:275
AT * operator()()
Pointer operator.
Definition: symmetric_matrix.h:263
std::istream & operator>>(std::istream &is, Matrix< Type, Row, Col, AT > &A)
Matrix input.
Definition: matrix.h:166
const AT & operator()(int i, int j) const
Element operator.
Definition: symmetric_matrix.h:223
int ldim() const
Leading dimension.
Definition: symmetric_matrix.h:293
Matrix(const Matrix< Symmetric, Ref, Ref, AT > &A)
Copy Constructor.
Definition: symmetric_matrix.h:100
This is a matrix class for symmetric matrices.
Definition: symmetric_matrix.h:39
int end() const
Last element.
Definition: range.h:95
const AT * operator()() const
Pointer operator.
Definition: symmetric_matrix.h:269
Matrix()
Standard constructor.
Definition: symmetric_matrix.h:74
void resize(int n, int m)
Definition: symmetric_matrix.h:157
Matrix< Symmetric, Ref, Ref, AT > & init(const AT &val)
Initialization.
Definition: symmetric_matrix.h:484
This is an index class for creating submatrices.
Definition: range.h:44
int start() const
First element.
Definition: range.h:89
int cols() const
Number of columns.
Definition: general_matrix.h:279