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};
66 typedef AT value_type;
83 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_) { }
84 Matrix(
int m_,
int n_,
int k_, Init ini=INIT,
const AT &a=0) : 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); }
86 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_) { }
87 Matrix(
int n_, Init ini=INIT,
const AT &a=0) : 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); }
89 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_) { }
90 Matrix(
int m_,
int n_, Init ini=INIT,
const AT &a=0) : 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); }
100 Matrix(
const Matrix<Sparse,Ref,Ref,AT> &A) : memEle(A.memEle), memI(A.memI), memJ(A.memJ), ele(A.ele), I(A.I), J(A.J), m(A.m), n(A.n), k(A.k) {
117 Matrix<Sparse,Ref,Ref,AT>& resize(
int n_,
int k_, Noinit) {
118 m = n_; n = n_; k = k_;
122 ele = (AT*)memEle.get();
123 I = (
int*)memI.get();
124 J = (
int*)memJ.get();
128 Matrix<Sparse,Ref,Ref,AT>& resize(
int n,
int k, Init ini=INIT,
const AT &a=0) {
return resize(m,k,Noinit()).
init(a); }
136 inline Matrix<Sparse,Ref,Ref,AT>& operator<<(const Matrix<Sparse,Ref,Ref,AT> &A);
144 inline Matrix<Sparse,Ref,Ref,AT>&
operator>>(
const Matrix<Sparse,Ref,Ref,AT> &A);
150 inline Matrix<Sparse,Ref,Ref,AT>& operator<<(const SquareMatrix<Ref,AT> &A);
156 inline Matrix<Sparse,Ref,Ref,AT>& operator<<(const Matrix<Symmetric, Var, Var ,AT> &A);
166 inline Matrix<Sparse,Ref,Ref,AT>& operator=(
const Matrix<Sparse,Ref,Ref,AT> &A);
186 throw std::runtime_error(
"Matrix<Sparse, Ref, Ref, AT>::operator(int i, int j) is not implemented.");
190 throw std::runtime_error(
"Matrix<Sparse, Ref, Ref, AT>::transposed() cannot be called.");
194 throw std::runtime_error(
"Matrix<Sparse, Ref, Ref, AT>::ldim() cannot be called.");
201 const int*
Ip()
const {
217 const int*
Jp()
const {
257 inline Matrix<Sparse,Ref,Ref,AT>& init(Noinit,
const AT &a=0) {
return *
this; }
290 ele = (AT*)memEle.get();
291 I = (
int*)memI.get();
292 J = (
int*)memJ.get();
295 #ifndef FMATVEC_NO_SIZE_CHECK
310 if(m!=A.m || n!=A.n || k!=A.k) {
317 ele = (AT*)memEle.get();
318 I = (
int*)memI.get();
319 J = (
int*)memJ.get();
337 ele = (AT*)memEle.get();
338 I = (
int*)memI.get();
339 J = (
int*)memJ.get();
346 ele = (AT*)memEle.get();
347 J = (
int*)memJ.get();
359 if (m != A.
rows() || n != A.
cols()) {
366 ele = (AT*) memEle.get();
367 I = (
int*) memI.get();
368 J = (
int*) memJ.get();
376 ele = (AT*) memEle.get();
377 J = (
int*) memJ.get();
398 for(
int i=0; i<=m; i++) {
401 for(
int i=0; i<k; i++) {
407 template <
class AT>
void Matrix<Sparse,Ref,Ref,AT>::deepCopy(
const SquareMatrix<Ref,AT> &A) {
410 for(i=0; i<A.size(); i++) {
414 for(
int j=0; j<i; j++) {
416 if(fabs(A(i,j))>1e-16) {
421 for(
int j=i+1; j<A.size(); j++) {
423 if(fabs(A(i,j))>1e-16) {
432 template <
class AT>
void Matrix<Sparse,Ref,Ref,AT>::deepCopy(
const Matrix<Symmetric, Var, Var,AT> &A) {
435 for(i=0; i<A.size(); i++) {
439 for(
int j=0; j<i; j++) {
441 if(fabs(A(i,j))>1e-16) {
446 for(
int j=i+1; j<A.size(); j++) {
448 if(fabs(A(i,j))>1e-16) {
459 for(
int i=0; i<k; i++) {
Matrix()
Standard constructor.
Definition: sparse_matrix.h:72
This is a matrix class for symmetric matrices.
Definition: var_symmetric_matrix.h:37
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
int rows() const
Number of rows.
Definition: var_symmetric_matrix.h:245
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:90
int rows() const
Number of rows.
Definition: general_matrix.h:273
AT * operator()()
Pointer operator.
Definition: sparse_matrix.h:181
Matrix< Sparse, Ref, Ref, AT > & init(const AT &val)
Initialization.
Definition: sparse_matrix.h:458
int countElements(const SquareMatrix< Ref, AT > &A)
Count nonzero elements.
Definition: linear_algebra.h:1839
This is a matrix class for sparse quadratic matrices.
Definition: sparse_matrix.h:44
int * Ip()
Pointer operator.
Definition: sparse_matrix.h:209
int rows() const
Number of rows.
Definition: sparse_matrix.h:233
const int * Jp() const
Pointer operator.
Definition: sparse_matrix.h:217
int * Jp()
Pointer operator.
Definition: sparse_matrix.h:225
std::istream & operator>>(std::istream &is, Matrix< Type, Row, Col, AT > &A)
Matrix input.
Definition: matrix.h:166
const int * Ip() const
Pointer operator.
Definition: sparse_matrix.h:201
Shape class for sparse matrices.
Definition: types.h:140
Matrix(const Matrix< Sparse, Ref, Ref, AT > &A)
Copy Constructor.
Definition: sparse_matrix.h:100
This is a matrix class of general quadratic matrices.
Definition: square_matrix.h:39
int cols() const
Number of columns.
Definition: var_symmetric_matrix.h:251
int countElementsLT(const Matrix< Symmetric, Ref, Ref, AT > &A)
Count nonzero elements of the low triangular part of a symmetric matrix.
Definition: linear_algebra.h:1857
const AT * operator()() const
Pointer operator.
Definition: sparse_matrix.h:172
int cols() const
Number of columns.
Definition: sparse_matrix.h:239
int cols() const
Number of columns.
Definition: general_matrix.h:279