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};
65 typedef AT value_type;
71 explicit Matrix() : memEle(), ele(0) { }
82 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_) { }
83 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); }
85 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_) { }
86 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); }
88 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_) { }
89 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); }
97 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) {
106 m = n_; n = n_; k = k_;
110 ele = (AT*)memEle.get();
111 I = (
int*)memI.get();
112 J = (
int*)memJ.get();
116 Matrix<Sparse,Ref,Ref,AT>& resize(
int n,
int k, Init ini=INIT,
const AT &a=AT()) {
return resize(m,k,Noinit()).
init(a); }
125 FMATVEC_ASSERT(m == A.m, AT);
126 FMATVEC_ASSERT(k == A.k, AT);
136 template<
class Type,
class Row,
class Col>
138 FMATVEC_ASSERT(A.
rows() == A.
cols(), AT);
139 FMATVEC_ASSERT(m == A.
cols(), AT);
140 FMATVEC_ASSERT(k == A.countElements(), AT);
170 template<
class Type,
class Row,
class Col>
172 FMATVEC_ASSERT(A.
rows() == A.
cols(), AT);
173 int k_ = A.countElements();
174 if(m!=A.
rows() || k!=k_) resize(A.
rows(),k_,NONINIT);
196 throw std::runtime_error(
"Matrix<Sparse, Ref, Ref, AT>::operator(int i, int j) is not implemented.");
200 throw std::runtime_error(
"Matrix<Sparse, Ref, Ref, AT>::operator(int i, int j) const is not implemented.");
204 throw std::runtime_error(
"Matrix<Sparse, Ref, Ref, AT>::ldim() cannot be called.");
211 const int*
Ip()
const {
227 const int*
Jp()
const {
254 throw std::runtime_error(
"Matrix<Sparse, Ref, Ref, AT>::blasOrder() cannot be called.");
266 inline Matrix<Sparse,Ref,Ref,AT>& init(Noinit,
const AT &a=AT()) {
return *
this; }
268 int countElements()
const {
return k; }
273 template <
class AT>
template <
class Type,
class Row,
class Col>
274 inline Matrix<Sparse,Ref,Ref,AT>& Matrix<Sparse,Ref,Ref,AT>::copy(
const Matrix<Type,Row,Col,AT> &A) {
277 for(i=0; i<A.rows(); i++) {
281 for(
int j=0; j<i; j++) {
283 if(fabs(A(i,j))>1e-16) {
288 for(
int j=i+1; j<A.cols(); j++) {
290 if(fabs(A(i,j))>1e-16) {
300 template <
class AT>
template <
class Row>
301 inline Matrix<Sparse,Ref,Ref,AT>& Matrix<Sparse,Ref,Ref,AT>::copy(
const Matrix<Symmetric,Row,Row,AT> &A) {
304 for(i=0; i<A.size(); i++) {
308 for(
int j=0; j<i; j++) {
310 if(fabs(A(i,j))>1e-16) {
315 for(
int j=i+1; j<A.size(); j++) {
317 if(fabs(A(i,j))>1e-16) {
328 inline Matrix<Sparse,Ref,Ref,AT>& Matrix<Sparse,Ref,Ref,AT>::copy(
const Matrix<Sparse,Ref,Ref,AT> &A) {
329 for(
int i=0; i<=m; i++) {
332 for(
int i=0; i<k; i++) {
341 for(
int i=0; i<k; i++) {
Matrix()
Standard constructor.
Definition: sparse_matrix.h:71
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:82
const int * Jp() const
Pointer operator.
Definition: sparse_matrix.h:227
AT * operator()()
Pointer operator.
Definition: sparse_matrix.h:191
int rows() const
Number of rows.
Definition: sparse_matrix.h:243
Matrix< Sparse, Ref, Ref, AT > & init(const AT &val)
Initialization.
Definition: sparse_matrix.h:340
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:253
int * Ip()
Pointer operator.
Definition: sparse_matrix.h:219
Matrix< Sparse, Ref, Ref, AT > & operator&=(Matrix< Sparse, Ref, Ref, AT > &A)
Reference operator.
Definition: sparse_matrix.h:150
int * Jp()
Pointer operator.
Definition: sparse_matrix.h:235
int cols() const
Number of columns.
Definition: sparse_matrix.h:249
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:124
const int * Ip() const
Pointer operator.
Definition: sparse_matrix.h:211
Namespace fmatvec.
Definition: _memory.cc:28
Shape class for sparse matrices.
Definition: types.h:172
Matrix(const Matrix< Sparse, Ref, Ref, AT > &A)
Copy Constructor.
Definition: sparse_matrix.h:97
const AT * operator()() const
Pointer operator.
Definition: sparse_matrix.h:182
Matrix< Sparse, Ref, Ref, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: sparse_matrix.h:137
int rows() const
Number of rows.