22 #ifndef symmetric_matrix_h 23 #define symmetric_matrix_h 55 const AT* elePtr(
int i,
int j)
const {
56 return j > i ? ele+i*lda+j : ele+i+j*lda;
59 AT* elePtr(
int i,
int j) {
60 return j > i ? ele+i*lda+j : ele+i+j*lda;
67 typedef AT value_type;
73 explicit Matrix() : memory(), ele(nullptr) {
76 explicit Matrix(
int n_,
Noinit) : memory(n_*n_), ele((AT*)memory.get()), n(n_), lda(n_) { }
77 explicit Matrix(
int n_, Init ini=INIT,
const AT &a=AT()) : memory(n_*n_), ele((AT*)memory.get()), n(n_), lda(n_) { init(a); }
78 explicit Matrix(
int n_, Eye ini,
const AT &a=1) : memory(n_*n_), ele((AT*)memory.get()), n(n_), lda(n_) { init(ini,a); }
79 explicit Matrix(
int m_,
int n_, Noinit) : memory(n_*n_), ele((AT*)memory.get()), n(n_), lda(n_) { }
80 explicit Matrix(
int m_,
int n_, Init ini=INIT,
const AT &a=AT()) : memory(n_*n_), ele((AT*)memory.get()), n(n_), lda(n_) { init(a); }
81 explicit 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); }
107 template<
class Type,
class Row,
class Col>
109 FMATVEC_ASSERT(A.
rows() == A.
cols(), AT);
120 explicit Matrix(
int n_, AT* ele_) : memory(), ele(ele_), n(n_), lda(n_) { }
129 ele = (AT*)memory.get();
133 Matrix<Symmetric,Ref,Ref,AT>& resize(
int n, Init ini=INIT,
const AT &a=AT()) {
return resize(n,Noinit()).
init(a); }
135 Matrix<Symmetric,Ref,Ref,AT>& resize(
int n, Eye ini,
const AT &a=1) {
return resize(n,Noinit()).
init(ini,a); }
141 throw std::runtime_error(
"A symmetric matrix cannot have different dimensions for rows and columns.");
152 FMATVEC_ASSERT(n == A.
rows(), AT);
162 template<
class Type,
class Row,
class Col>
164 FMATVEC_ASSERT(A.
rows() == A.
cols(), AT);
165 FMATVEC_ASSERT(n == A.
rows(), AT);
189 template<
class Type,
class Row,
class Col>
191 FMATVEC_ASSERT(A.
rows() == A.
cols(), AT);
192 if(n!=A.
rows()) resize(A.
rows(),NONINIT);
206 FMATVEC_ASSERT(i>=0, AT);
207 FMATVEC_ASSERT(j>=0, AT);
208 FMATVEC_ASSERT(i<n, AT);
209 FMATVEC_ASSERT(j<n, AT);
218 FMATVEC_ASSERT(i>=0, AT);
219 FMATVEC_ASSERT(j>=0, AT);
220 FMATVEC_ASSERT(i<n, AT);
221 FMATVEC_ASSERT(j<n, AT);
226 AT& ei(
int i,
int j) {
230 const AT& ei(
int i,
int j)
const {
234 AT& ej(
int i,
int j) {
238 const AT& ej(
int i,
int j)
const {
242 AT& e(
int i,
int j) {
243 return j > i ? ej(i,j) : ei(i,j);
246 const AT& e(
int i,
int j)
const {
247 return j > i ? ej(i,j) : ei(i,j);
295 return CblasColMajor;
318 inline Matrix<Symmetric,Ref,Ref,AT>& init(Eye,
const AT &val=1);
319 inline Matrix<Symmetric,Ref,Ref,AT>& init(Noinit,
const AT &a=AT()) {
return *
this; }
323 inline const Matrix<General,Ref,Ref,AT>
operator()(
const Range<Var,Var> &I,
const Range<Var,Var> &J)
const;
327 inline const Matrix<Symmetric,Ref,Ref,AT>
operator()(
const Range<Var,Var> &I)
const;
341 explicit inline operator std::vector<std::vector<AT>>()
const;
348 explicit Matrix(
const std::vector<std::vector<AT>> &m);
365 inline int countElements()
const;
371 for(
int i=0; i<
rows(); i++)
372 for(
int j=i; j<
cols(); j++)
381 for(
int i=0; i<size(); i++) {
383 for(
int j=i+1; j<size(); j++) {
392 FMATVEC_ASSERT(I.
end()<n, AT);
393 FMATVEC_ASSERT(J.
end()<n, AT);
396 for(
int i=0; i<A.
rows(); i++)
397 for(
int j=0; j<A.
cols(); j++)
405 FMATVEC_ASSERT(I.
end()<n, AT);
409 for(
int i=0; i<A.
rows(); i++)
410 for(
int j=i; j<A.
cols(); j++)
416 template <
class AT>
template<
class Type,
class Row,
class Col>
417 inline void Matrix<Symmetric,Ref,Ref,AT>::set(
const fmatvec::Range<Var,Var> &I,
const fmatvec::Range<Var,Var> &J,
const Matrix<Type,Row,Col,AT> &A) {
420 else FMATVEC_ASSERT(J.
start()>=I.
end(), AT);
421 FMATVEC_ASSERT(I.
end()<
rows(), AT);
422 FMATVEC_ASSERT(J.
end()<
cols(), AT);
423 FMATVEC_ASSERT(I.
size()==A.
rows(), AT);
424 FMATVEC_ASSERT(J.
size()==A.
cols(), AT);
426 for(
int i=I.
start(), ii=0; i<=I.
end(); i++, ii++)
427 for(
int j=J.
start(), jj=0; j<=J.
end(); j++, jj++)
431 template <
class AT>
template<
class Type,
class Row,
class Col>
432 inline void Matrix<Symmetric,Ref,Ref,AT>::add(
const fmatvec::Range<Var,Var> &I,
const fmatvec::Range<Var,Var> &J,
const Matrix<Type,Row,Col,AT> &A) {
435 else FMATVEC_ASSERT(J.
start()>=I.
end(), AT);
436 FMATVEC_ASSERT(I.
end()<
rows(), AT);
437 FMATVEC_ASSERT(J.
end()<
cols(), AT);
438 FMATVEC_ASSERT(I.
size()==A.rows(), AT);
439 FMATVEC_ASSERT(J.
size()==A.cols(), AT);
441 for(
int i=I.
start(), ii=0; i<=I.
end(); i++, ii++)
442 for(
int j=J.
start(), jj=0; j<=J.
end(); j++, jj++)
443 e(i,j) += A.e(ii,jj);
446 template <
class AT>
template<
class Row>
447 inline void Matrix<Symmetric,Ref,Ref,AT>::set(
const fmatvec::Range<Var,Var> &I,
const Matrix<Symmetric,Row,Row,AT> &A) {
449 FMATVEC_ASSERT(I.
end()<size(), AT);
450 FMATVEC_ASSERT(I.
size()==A.size(), AT);
452 for(
int i=I.
start(), ii=0; i<=I.
end(); i++, ii++)
453 for(
int j=i, jj=ii; j<=I.
end(); j++, jj++)
457 template <
class AT>
template<
class Row>
458 inline void Matrix<Symmetric,Ref,Ref,AT>::add(
const fmatvec::Range<Var,Var> &I,
const Matrix<Symmetric,Row,Row,AT> &A) {
460 FMATVEC_ASSERT(I.
end()<size(), AT);
461 FMATVEC_ASSERT(I.
size()==A.size(), AT);
463 for(
int i=I.
start(), ii=0; i<=I.
end(); i++, ii++)
464 for(
int j=i, jj=ii; j<=I.
end(); j++, jj++)
465 e(i,j) += A.e(ii,jj);
469 inline void Matrix<Symmetric,Ref,Ref,AT>::ref(Matrix<Symmetric,Ref,Ref,AT> &A,
const fmatvec::Range<Var,Var> &I) {
470 FMATVEC_ASSERT(I.
end()<A.size(), AT);
479 std::vector<std::vector<AT>> ret(rows());
480 for(
int r=0; r<rows(); r++) {
481 ret[r].resize(cols());
482 for(
int c=0; c<cols(); c++)
483 ret[r][c]=
operator()(r,c);
490 for(
int r=0; r<
rows(); r++) {
491 if(static_cast<int>(m[r].size())!=
cols())
492 throw std::runtime_error(
"The rows of the input have different length.");
493 for(
int c=0; c<
cols(); c++) {
495 if(r>c && abs(m[r][c]-m[c][r])>abs(m[r][c])*1e-13+1e-13)
496 throw std::runtime_error(
"The input is not symmetric.");
504 for (
int j = 0; j < n; j++) {
505 for (
int i = j+1; i < n; i++) {
506 if (fabs(e(i, j)) > 1e-16) {
518 for(
int i=0; i<n; i++)
519 for(
int j=i; j<n; j++)
524 template <
class AT>
template <
class Type,
class Row,
class Col>
525 inline Matrix<Symmetric,Ref,Ref,AT>& Matrix<Symmetric,Ref,Ref,AT>::copy(
const Matrix<Type,Row,Col,AT> &A) {
526 for(
int i=0; i<n; i++)
527 for(
int j=i; j<n; j++)
int end() const
Last element.
Definition: range.h:91
const AT & operator()(int i, int j) const
Element operator.
Definition: symmetric_matrix.h:217
int cols() const
Number of columns.
Definition: symmetric_matrix.h:279
AT & operator()(int i, int j)
Element operator.
Definition: symmetric_matrix.h:205
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
Shape class for symmetric matrices.
Definition: types.h:148
Matrix< Symmetric, Ref, Ref, AT > & operator=(const Matrix< Symmetric, Ref, Ref, AT > &A)
Assignment operator.
Definition: symmetric_matrix.h:151
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:82
Matrix(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: symmetric_matrix.h:108
const CBLAS_UPLO blasUplo() const
Symmetry convention.
Definition: symmetric_matrix.h:305
const AT * operator()() const
Pointer operator.
Definition: symmetric_matrix.h:261
int start() const
First element.
Definition: range.h:85
int ldim() const
Leading dimension.
Definition: symmetric_matrix.h:285
int cols() const
Number of columns.
Matrix(int n_, AT *ele_)
Regular Constructor.
Definition: symmetric_matrix.h:120
This is a matrix class for general matrices.
Definition: general_matrix.h:41
AT * operator()()
Pointer operator.
Definition: symmetric_matrix.h:255
Matrix(const Matrix< Symmetric, Row, Row, AT > &A)
Copy Constructor.
Definition: symmetric_matrix.h:98
Namespace fmatvec.
Definition: _memory.cc:28
int rows() const
Number of rows.
Definition: symmetric_matrix.h:273
int size() const
Size.
Definition: range.h:97
Matrix(const Matrix< Symmetric, Ref, Ref, AT > &A)
Copy Constructor.
Definition: symmetric_matrix.h:88
This is a matrix class for symmetric matrices.
Definition: symmetric_matrix.h:39
Basic shape class for matrices.
Definition: types.h:132
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: symmetric_matrix.h:294
Matrix()
Standard constructor.
Definition: symmetric_matrix.h:73
int rows() const
Number of rows.
void resize(int n, int m)
Definition: symmetric_matrix.h:139
Matrix< Symmetric, Ref, Ref, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: symmetric_matrix.h:163
Matrix< Symmetric, Ref, Ref, AT > & init(const AT &val)
Initialization.
Definition: symmetric_matrix.h:369
int size() const
Size.
Definition: symmetric_matrix.h:267
This is an index class for creating submatrices.
Definition: range.h:44
Matrix< Symmetric, Ref, Ref, AT > & operator&=(Matrix< Symmetric, Ref, Ref, AT > &A)
Reference operator.
Definition: symmetric_matrix.h:175