22 #ifndef sparse_matrix_h
23 #define sparse_matrix_h
26 #include "square_matrix.h"
51 Memory<int> memI, memJ;
66 typedef AT AtomicType;
72 Matrix() : memEle(), memI(), memJ(), ele(0), I(0), J(0), m(0), n(0), k(0) { }
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) {
118 Matrix<Sparse,Ref,Ref,AT>& resize(
int n_,
int k_, Noinit) {
119 m = n_; n = n_; k = k_;
123 ele = (AT*)memEle.get();
124 I = (
int*)memI.get();
125 J = (
int*)memJ.get();
129 Matrix<Sparse,Ref,Ref,AT>& resize(
int n,
int k, Init ini=INIT,
const AT &a=0) {
return resize(m,k,Noinit()).
init(a); }
137 inline Matrix<Sparse,Ref,Ref,AT>& operator<<(const Matrix<Sparse,Ref,Ref,AT> &A);
145 inline Matrix<Sparse,Ref,Ref,AT>&
operator>>(
const Matrix<Sparse,Ref,Ref,AT> &A);
151 inline Matrix<Sparse,Ref,Ref,AT>& operator<<(const SquareMatrix<Ref,AT> &A);
157 inline Matrix<Sparse,Ref,Ref,AT>& operator<<(const Matrix<Symmetric, Var, Var ,AT> &A);
167 inline Matrix<Sparse,Ref,Ref,AT>& operator=(
const Matrix<Sparse,Ref,Ref,AT> &A);
187 throw std::runtime_error(
"Matrix<Sparse, Ref, Ref, AT>::operator(int i, int j) is not implemented.");
191 throw std::runtime_error(
"Matrix<Sparse, Ref, Ref, AT>::transposed() cannot be called.");
195 throw std::runtime_error(
"Matrix<Sparse, Ref, Ref, AT>::ldim() cannot be called.");
202 const int*
Ip()
const {
218 const int*
Jp()
const {
258 inline Matrix<Sparse,Ref,Ref,AT>& init(Noinit,
const AT &a=0) {
return *
this; }
291 ele = (AT*)memEle.get();
292 I = (
int*)memI.get();
293 J = (
int*)memJ.get();
296 #ifndef FMATVEC_NO_SIZE_CHECK
311 if(m!=A.m || n!=A.n || k!=A.k) {
318 ele = (AT*)memEle.get();
319 I = (
int*)memI.get();
320 J = (
int*)memJ.get();
338 ele = (AT*)memEle.get();
339 I = (
int*)memI.get();
340 J = (
int*)memJ.get();
347 ele = (AT*)memEle.get();
348 J = (
int*)memJ.get();
360 if (m != A.
rows() || n != A.
cols()) {
367 ele = (AT*) memEle.get();
368 I = (
int*) memI.get();
369 J = (
int*) memJ.get();
377 ele = (AT*) memEle.get();
378 J = (
int*) memJ.get();
399 for(
int i=0; i<=m; i++) {
402 for(
int i=0; i<k; i++) {
408 template <
class AT>
void Matrix<Sparse,Ref,Ref,AT>::deepCopy(
const SquareMatrix<Ref,AT> &A) {
411 for(i=0; i<A.size(); i++) {
415 for(
int j=0; j<i; j++) {
417 if(fabs(A(i,j))>1e-16) {
422 for(
int j=i+1; j<A.size(); j++) {
424 if(fabs(A(i,j))>1e-16) {
433 template <
class AT>
void Matrix<Sparse,Ref,Ref,AT>::deepCopy(
const Matrix<Symmetric, Var, Var,AT> &A) {
436 for(i=0; i<A.size(); i++) {
440 for(
int j=0; j<i; j++) {
442 if(fabs(A(i,j))>1e-16) {
447 for(
int j=i+1; j<A.size(); j++) {
449 if(fabs(A(i,j))>1e-16) {
460 for(
int i=0; i<k; i++) {
Matrix< Sparse, Ref, Ref, AT > & init(const AT &a)
Initialization.
Definition: sparse_matrix.h:459
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:86
int rows() const
Number of rows.
Definition: general_matrix.h:272
AT * operator()()
Pointer operator.
Definition: sparse_matrix.h:182
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:210
int rows() const
Number of rows.
Definition: sparse_matrix.h:234
const int * Jp() const
Pointer operator.
Definition: sparse_matrix.h:218
int * Jp()
Pointer operator.
Definition: sparse_matrix.h:226
std::istream & operator>>(std::istream &is, Matrix< Type, Row, Col, AT > &A)
Matrix input.
Definition: matrix.h:171
const int * Ip() const
Pointer operator.
Definition: sparse_matrix.h:202
Shape class for sparse matrices.
Definition: types.h:140
Matrix(const Matrix< Sparse, Ref, Ref, AT > &A)
Copy Constructor.
Definition: sparse_matrix.h:100
~Matrix()
Destructor.
Definition: sparse_matrix.h:105
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:173
int cols() const
Number of columns.
Definition: sparse_matrix.h:240
int cols() const
Number of columns.
Definition: general_matrix.h:278