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 using value_type = AT;
73 explicit Matrix() : memEle(), ele(0) { }
75 explicit Matrix(
int m_,
int n_,
int k_,
Noinit) : memEle(k_), memI(m_+1), memJ(k_), ele((AT*)memEle.get()), I((int*)memI.get()), J((int*)memJ.get()), m(m_), n(n_), k(k_) { }
76 explicit Matrix(
int m_,
int n_,
int k_, Init ini=INIT,
const AT &a=AT()) : memEle(k_), memI(m_+1), memJ(k_), ele((AT*)memEle.get()), I((int*)memI.get()), J((int*)memJ.get()), m(m_), n(n_), k(k_) { init(a); }
83 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) {
87 explicit Matrix(
int m_,
int n_,
int k_,
int *I_,
int *J_,
Init ini=INIT,
const AT &a=AT()) : memEle(k_), memI(), memJ(), ele((AT*)memEle.get()), I(I_), J(J_), m(m_), n(n_), k(k_) {
96 m = m_; n = n_; k = k_;
100 ele = (AT*)memEle.get();
101 I = (
int*)memI.get();
102 J = (
int*)memJ.get();
106 Matrix<Sparse,Ref,Ref,AT>& resize(
int m,
int n,
int k,
Init ini=INIT,
const AT &a=AT()) {
return resize(m,n,k,
Noinit()).
init(a); }
120 FMATVEC_ASSERT(m == A.m, AT);
121 FMATVEC_ASSERT(n == A.n, AT);
122 FMATVEC_ASSERT(k == A.k, AT);
132 template<
class Type,
class Row,
class Col>
134 FMATVEC_ASSERT(m == A.
rows(), AT);
135 FMATVEC_ASSERT(n == A.
cols(), AT);
136 FMATVEC_ASSERT(k == A.nonZeroElements(), AT);
166 template<
class Type,
class Row,
class Col>
168 int k_ = A.nonZeroElements();
169 if(m!=A.
rows() || k!=k_) resize(A.
rows(),A.
cols(),k_,NONINIT);
191 FMATVEC_ASSERT(i>=0, AT);
192 FMATVEC_ASSERT(j>=0, AT);
193 FMATVEC_ASSERT(i<m, AT);
194 FMATVEC_ASSERT(j<n, AT);
200 FMATVEC_ASSERT(i>=0, AT);
201 FMATVEC_ASSERT(j>=0, AT);
202 FMATVEC_ASSERT(i<m, AT);
203 FMATVEC_ASSERT(j<n, AT);
208 AT& e(
int i,
int j) {
209 return ele[pos(i,j)];
212 const AT& e(
int i,
int j)
const {
213 return ele[pos(i,j)];
216 int pos(
int i,
int j)
const {
217 for(
int k=I[i]; k<I[i+1]; k++)
224 throw std::runtime_error(
"Matrix<Sparse, Ref, Ref, AT>::ldim() cannot be called.");
231 const int*
Ip()
const {
247 const int*
Jp()
const {
274 throw std::runtime_error(
"Matrix<Sparse, Ref, Ref, AT>::blasOrder() cannot be called.");
286 inline Matrix<Sparse,Ref,Ref,AT>& init(Noinit,
const AT &a=AT()) {
return *
this; }
288 int nonZeroElements()
const {
return k; }
293 template <
class AT>
template <
class Type,
class Row,
class Col>
294 inline Matrix<Sparse,Ref,Ref,AT>& Matrix<Sparse,Ref,Ref,AT>::copy(
const Matrix<Type,Row,Col,AT> &A) {
297 for(i=0; i<A.rows(); i++) {
301 for(
int j=0; j<i; j++) {
303 if(fabs(A.e(i,j))>1e-16) {
308 for(
int j=i+1; j<A.cols(); j++) {
310 if(fabs(A.e(i,j))>1e-16) {
320 template <
class AT>
template <
class Row>
321 inline Matrix<Sparse,Ref,Ref,AT>& Matrix<Sparse,Ref,Ref,AT>::copy(
const Matrix<Symmetric,Row,Row,AT> &A) {
324 for(i=0; i<A.size(); i++) {
328 for(
int j=0; j<i; j++) {
330 if(fabs(A.ei(i,j))>1e-16) {
335 for(
int j=i+1; j<A.size(); j++) {
337 if(fabs(A.ej(i,j))>1e-16) {
348 inline Matrix<Sparse,Ref,Ref,AT>& Matrix<Sparse,Ref,Ref,AT>::copy(
const Matrix<Sparse,Ref,Ref,AT> &A) {
349 for(
int i=0; i<=m; i++) {
352 for(
int i=0; i<k; i++) {
361 for(
int i=0; i<k; i++) {
This is a matrix class for sparse quadratic matrices.
Definition: sparse_matrix.h:44
Matrix< Sparse, Ref, Ref, AT > & operator&=(Matrix< Sparse, Ref, Ref, AT > &A)
Reference operator.
Definition: sparse_matrix.h:146
~Matrix()=default
Destructor.
int cols() const
Number of columns.
Definition: sparse_matrix.h:269
const int * Ip() const
Pointer operator.
Definition: sparse_matrix.h:231
Matrix(const Matrix< Sparse, Ref, Ref, AT > &A)
Copy Constructor.
Definition: sparse_matrix.h:83
const int * Jp() const
Pointer operator.
Definition: sparse_matrix.h:247
CBLAS_ORDER blasOrder() const
Storage convention.
Definition: sparse_matrix.h:273
Matrix< Sparse, Ref, Ref, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: sparse_matrix.h:133
Matrix()
Standard constructor.
Definition: sparse_matrix.h:73
Matrix< Sparse, Ref, Ref, AT > & init(const AT &val)
Initialization.
Definition: sparse_matrix.h:360
int * Ip()
Pointer operator.
Definition: sparse_matrix.h:239
const AT * operator()() const
Pointer operator.
Definition: sparse_matrix.h:177
Matrix< Sparse, Ref, Ref, AT > & operator=(const Matrix< Sparse, Ref, Ref, AT > &A)
Assignment operator.
Definition: sparse_matrix.h:119
AT * operator()()
Pointer operator.
Definition: sparse_matrix.h:186
int rows() const
Number of rows.
Definition: sparse_matrix.h:263
int * Jp()
Pointer operator.
Definition: sparse_matrix.h:255
void resize(int m, int n)
Resize a sparse matrix.
Definition: sparse_matrix.h:109
This is the basic matrix class for arbitrary matrices.
Definition: matrix.h:52
int rows() const
Number of rows.
int cols() const
Number of columns.
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:84
Shape class for sparse matrices.
Definition: types.h:156
Namespace fmatvec.
Definition: _memory.cc:28