22 #ifndef sparse_matrix_h 23 #define sparse_matrix_h 26 #include "square_matrix.h" 51 Memory<int> memI, memJ;
53 int *I{
nullptr}, *J{
nullptr};
64 static constexpr
bool isVector {
false};
66 typedef AT value_type;
73 explicit Matrix() : memEle(), ele(0) { }
84 explicit Matrix(
int m_,
int n_,
int k_,
Noinit) : memEle(k_), memI(n_+1), memJ(k_), ele((AT*)memEle.get()), I((int*)memI.get()), J((int*)memJ.get()), m(n_), n(n_), k(k_) { }
85 explicit Matrix(
int m_,
int n_,
int k_, Init ini=INIT,
const AT &a=AT()) : memEle(k_), memI(n_+1), memJ(k_), ele((AT*)memEle.get()), I((int*)memI.get()), J((int*)memJ.get()), m(n_), n(n_), k(k_) { init(a); }
87 explicit Matrix(
int n_, Noinit) : memEle(n_*n_), memI(n_+1), memJ(n_*n_), ele((AT*)memEle.get()), I((int*)memI.get()), J((int*)memJ.get()), m(n_), n(n_), k(n_*n_) { }
88 explicit Matrix(
int n_, Init ini=INIT,
const AT &a=AT()) : memEle(n_*n_), memI(n_+1), memJ(n_*n_), ele((AT*)memEle.get()), I((int*)memI.get()), J((int*)memJ.get()), m(n_), n(n_), k(n_*n_) { init(a); }
90 explicit Matrix(
int m_,
int n_, Noinit) : memEle(n_*n_), memI(n_+1), memJ(n_*n_), ele((AT*)memEle.get()), I((int*)memI.get()), J((int*)memJ.get()), m(n_), n(n_), k(n_*n_) { }
91 explicit Matrix(
int m_,
int n_, Init ini=INIT,
const AT &a=AT()) : memEle(n_*n_), memI(n_+1), memJ(n_*n_), ele((AT*)memEle.get()), I((int*)memI.get()), J((int*)memJ.get()), m(n_), n(n_), k(n_*n_) { init(a); }
99 Matrix(
const Matrix<Sparse,Ref,Ref,AT> &A) : memEle(A.k), memI(A.n+1), memJ(A.k), ele((AT*)memEle.get()), I((int*)memI.get()), J((int*)memJ.get()), m(A.m), n(A.n), k(A.k) {
108 m = n_; n = n_; k = k_;
112 ele = (AT*)memEle.get();
113 I = (
int*)memI.get();
114 J = (
int*)memJ.get();
118 Matrix<Sparse,Ref,Ref,AT>& resize(
int n,
int k, Init ini=INIT,
const AT &a=AT()) {
return resize(m,k,Noinit()).init(a); }
127 FMATVEC_ASSERT(m == A.m, AT);
128 FMATVEC_ASSERT(k == A.k, AT);
138 template<
class Type,
class Row,
class Col>
140 FMATVEC_ASSERT(A.
rows() == A.
cols(), AT);
141 FMATVEC_ASSERT(m == A.
cols(), AT);
142 FMATVEC_ASSERT(k == A.countElements(), AT);
172 template<
class Type,
class Row,
class Col>
174 FMATVEC_ASSERT(A.
rows() == A.
cols(), AT);
175 int k_ = A.countElements();
176 if(m!=A.
rows() || k!=k_) resize(A.
rows(),k_,NONINIT);
198 throw std::runtime_error(
"Matrix<Sparse, Ref, Ref, AT>::operator(int i, int j) is not implemented.");
202 throw std::runtime_error(
"Matrix<Sparse, Ref, Ref, AT>::operator(int i, int j) const is not implemented.");
206 throw std::runtime_error(
"Matrix<Sparse, Ref, Ref, AT>::ldim() cannot be called.");
213 const int*
Ip()
const {
229 const int*
Jp()
const {
256 throw std::runtime_error(
"Matrix<Sparse, Ref, Ref, AT>::blasOrder() cannot be called.");
268 inline Matrix<Sparse,Ref,Ref,AT>& init(Noinit,
const AT &a=AT()) {
return *
this; }
270 int countElements()
const {
return k; }
275 template <
class AT>
template <
class Type,
class Row,
class Col>
276 inline Matrix<Sparse,Ref,Ref,AT>& Matrix<Sparse,Ref,Ref,AT>::copy(
const Matrix<Type,Row,Col,AT> &A) {
279 for(i=0; i<A.rows(); i++) {
283 for(
int j=0; j<i; j++) {
285 if(fabs(A(i,j))>1e-16) {
290 for(
int j=i+1; j<A.cols(); j++) {
292 if(fabs(A(i,j))>1e-16) {
302 template <
class AT>
template <
class Row>
303 inline Matrix<Sparse,Ref,Ref,AT>& Matrix<Sparse,Ref,Ref,AT>::copy(
const Matrix<Symmetric,Row,Row,AT> &A) {
306 for(i=0; i<A.size(); i++) {
310 for(
int j=0; j<i; j++) {
312 if(fabs(A(i,j))>1e-16) {
317 for(
int j=i+1; j<A.size(); j++) {
319 if(fabs(A(i,j))>1e-16) {
330 inline Matrix<Sparse,Ref,Ref,AT>& Matrix<Sparse,Ref,Ref,AT>::copy(
const Matrix<Sparse,Ref,Ref,AT> &A) {
331 for(
int i=0; i<=m; i++) {
334 for(
int i=0; i<k; i++) {
343 for(
int i=0; i<k; i++) {
Matrix()
Standard constructor.
Definition: sparse_matrix.h:73
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:84
const int * Jp() const
Pointer operator.
Definition: sparse_matrix.h:229
AT * operator()()
Pointer operator.
Definition: sparse_matrix.h:193
int rows() const
Number of rows.
Definition: sparse_matrix.h:245
This is a matrix class for sparse quadratic matrices.
Definition: sparse_matrix.h:44
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: sparse_matrix.h:255
int * Ip()
Pointer operator.
Definition: sparse_matrix.h:221
Matrix< Sparse, Ref, Ref, AT > & operator&=(Matrix< Sparse, Ref, Ref, AT > &A)
Reference operator.
Definition: sparse_matrix.h:152
int * Jp()
Pointer operator.
Definition: sparse_matrix.h:237
int cols() const
Number of columns.
Definition: sparse_matrix.h:251
int cols() const
Number of columns.
Matrix< Sparse, Ref, Ref, AT > & operator=(const Matrix< Sparse, Ref, Ref, AT > &A)
Assignment operator.
Definition: sparse_matrix.h:126
const int * Ip() const
Pointer operator.
Definition: sparse_matrix.h:213
Namespace fmatvec.
Definition: _memory.cc:28
Shape class for sparse matrices.
Definition: types.h:176
Matrix(const Matrix< Sparse, Ref, Ref, AT > &A)
Copy Constructor.
Definition: sparse_matrix.h:99
const AT * operator()() const
Pointer operator.
Definition: sparse_matrix.h:184
Matrix< Sparse, Ref, Ref, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: sparse_matrix.h:139
int rows() const
Number of rows.