NUMCXX  0.13.20181108
Numerical library for small projects and teaching purposes
Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Private Member Functions | Private Attributes | Friends | List of all members
numcxx::TSparseMatrix< T > Class Template Reference

Description

template<typename T>
class numcxx::TSparseMatrix< T >

Sparse matrix class using CRS storage scheme.

Examples:
15-numcxx-umfpack.cxx, 40-stationary-heat-fe.cxx, 41-stationary-heat-fv.cxx, 42-convtest-fem-sin.cxx, 43-convtest-fvm-sin.cxx, 44-transient-heat-fe.cxx, 45-convdiff1d.cxx, and 46-nonlin-fvm.cxx.

Definition at line 23 of file tsparsematrix.hxx.

+ Inheritance diagram for numcxx::TSparseMatrix< T >:
+ Collaboration diagram for numcxx::TSparseMatrix< T >:

Public Types

typedef T value_type
 

Public Member Functions

 TSparseMatrix ()
 
 TSparseMatrix (index n1, index n2)
 Create an empty sparse matrix representing an n1 x n2 system of linear equations (currently, n1 must be equal to n2) More...
 
 TSparseMatrix (const std::initializer_list< std::initializer_list< T >> &il)
 Create an empty sparse matrix representing an n x n system of linear equations. More...
 
T & operator() (int i, int j)
 Access operator. More...
 
void flush ()
 Re-create the internal data structure in order to accomodated all newly created elements. More...
 
void clear ()
 
void apply (const TArray< T > &U, TArray< T > &V) const
 Apply sparse matrix to vector. More...
 
std::shared_ptr< TMatrix< T > > copy_as_dense ()
 Create a dense matrix from the sparse matrix. More...
 
index shape (int idim) const
 Return the shape od the matrix. More...
 
 TSparseMatrix (const TSparseMatrix< T > &A)=delete
 Copy constructor is deleted. More...
 
std::shared_ptr< TMatrix< T > > calculate_inverse ()
 Calculate inverse of sparse matrix whuch is dense. More...
 
bool pattern_changed () const
 Check if pattern has changed after last solver update. More...
 

Static Public Member Functions

static std::shared_ptr< TSparseMatrix< T > > create (index n1, index n2)
 Static wrapper around corresponding constructor. More...
 
static std::shared_ptr< TSparseMatrix< T > > create (const std::initializer_list< std::initializer_list< T >> &il)
 Static wrapper around corresponding constructor. More...
 

Public Attributes

std::shared_ptr< TArray1< int > > pIA
 Row pointers. More...
 
std::shared_ptr< TArray1< int > > pJA
 Column indices. More...
 
std::shared_ptr< TArray1< T > > pA
 Entries. More...
 

Private Member Functions

void pattern_changed (bool chg)
 
bool empty () const
 

Private Attributes

bool _pattern_changed =false
 
const index n
 
int maxrow =0
 
bool _first_flush_done =false
 
std::shared_ptr< Extension > pExt =nullptr
 

Friends

class TSolverUMFPACK< T >
 
class TPreconJacobi< T >
 
class TPreconILU< T >
 

Member Typedef Documentation

template<typename T>
typedef T numcxx::TSparseMatrix< T >::value_type

Definition at line 29 of file tsparsematrix.hxx.

Constructor & Destructor Documentation

template<typename T>
numcxx::TSparseMatrix< T >::TSparseMatrix ( )
inline

Definition at line 31 of file tsparsematrix.hxx.

31 :n(0) {};
template<typename T >
numcxx::TSparseMatrix< T >::TSparseMatrix ( index  n1,
index  n2 
)
inline

Create an empty sparse matrix representing an n1 x n2 system of linear equations (currently, n1 must be equal to n2)

Definition at line 63 of file tsparsematrix.ixx.

63  :
64  n(n1),
65  pA(std::make_shared<TArray1<T>>(n1)),
66  pJA(std::make_shared<TArray1<int>>(n1)),
67  pIA(std::make_shared<TArray1<int>>(n1+1))
68  {
69  if (n1!=n2)
70  {
71  char errormsg[80];
72  snprintf(errormsg,80,"numcxx::TSparseMatrix::TSparseMatrix unexpected non-equal matrix dimensions\n");
73  throw std::length_error(errormsg);
74  }
75 
76 
77  auto &IA=*pIA;
78  auto &JA=*pJA;
79  auto &A=*pA;
80  IA=0;
81  JA=0;
82  A=0.0;
83  }
std::shared_ptr< TArray1< int > > pJA
Column indices.
std::shared_ptr< TArray1< T > > pA
Entries.
std::shared_ptr< TArray1< int > > pIA
Row pointers.
template<typename T >
numcxx::TSparseMatrix< T >::TSparseMatrix ( const std::initializer_list< std::initializer_list< T >> &  il)
inline

Create an empty sparse matrix representing an n x n system of linear equations.

Definition at line 86 of file tsparsematrix.ixx.

86  :
87  TSparseMatrix(il.size(), il.begin()->size())
88  {
89  index i=0;
90  for (auto jl = il.begin() ; jl != il.end(); jl++,i++)
91  {
92  index j=0;
93  for (auto x = jl->begin() ; x != jl->end(); x++,j++)
94  this->operator()(i,j)= *x;
95 
96  }
97  flush();
98  }
void flush()
Re-create the internal data structure in order to accomodated all newly created elements.
unsigned int index
Definition: numcxx.hxx:21

+ Here is the call graph for this function:

template<typename T>
numcxx::TSparseMatrix< T >::TSparseMatrix ( const TSparseMatrix< T > &  A)
delete

Copy constructor is deleted.

Member Function Documentation

template<typename T>
static std::shared_ptr<TSparseMatrix <T> > numcxx::TSparseMatrix< T >::create ( index  n1,
index  n2 
)
inlinestatic

Static wrapper around corresponding constructor.

Examples:
16-numcxx-umfpack-sharedptr.cxx.

Definition at line 40 of file tsparsematrix.hxx.

40 {return std::make_shared<TSparseMatrix<T>>(n1,n2);}

+ Here is the call graph for this function:

template<typename T >
std::shared_ptr< TSparseMatrix< T > > numcxx::TSparseMatrix< T >::create ( const std::initializer_list< std::initializer_list< T >> &  il)
inlinestatic

Static wrapper around corresponding constructor.

Definition at line 101 of file tsparsematrix.ixx.

102  {
103  return std::make_shared<TSparseMatrix <T>>(il);
104  }
template<typename T >
T & numcxx::TSparseMatrix< T >::operator() ( int  i,
int  j 
)
inline

Access operator.

This operator accesses the sparse matrix element at position i,j. If it is not existent, it is created. All freshly created elements are collected in an extension list which is joined with the main data structure using the flush() method.

Definition at line 226 of file tsparsematrix.ixx.

227  {
228  auto &IA=*pIA;
229  auto &JA=*pJA;
230  auto &A=*pA;
231 #ifdef NUMCXX_CHECK_BOUNDS
232  if (i<0||j<0||i>=n||j>=n)
233  {
234  char errormsg[80];
235  snprintf(errormsg,80,"numcxx::TSparseMatrix::op(): shape=(%d %d) but using index (%d %d)\n",n,n,i,j);
236  throw std::out_of_range(errormsg);
237  }
238 #endif
239  for (int k=IA[i];k<IA[i+1];k++)
240  if (JA[k]==j)
241  return A[k];
242 
243  if (pExt==nullptr) pExt=std::make_shared<Extension>(n);
244  pattern_changed(true);
245  return pExt->entry(i,j);
246  }
std::shared_ptr< TArray1< int > > pJA
Column indices.
std::shared_ptr< Extension > pExt
bool pattern_changed() const
Check if pattern has changed after last solver update.
std::shared_ptr< TArray1< T > > pA
Entries.
std::shared_ptr< TArray1< int > > pIA
Row pointers.

+ Here is the call graph for this function:

template<typename T >
void numcxx::TSparseMatrix< T >::flush ( )
inline

Re-create the internal data structure in order to accomodated all newly created elements.

Examples:
15-numcxx-umfpack.cxx, and 45-convdiff1d.cxx.

Definition at line 107 of file tsparsematrix.ixx.

108  {
109  if (pExt==nullptr) return;
110  if (pExt->empty()) return;
111  int nJA_old=0;
112  if (!empty())
113  nJA_old=pJA->size();
114  int next=pExt->next;
115  int nJA_new=nJA_old+next;
116 
117  auto pNew_IA=TArray1<int>::create(n+1);
118  auto pNew_JA=TArray1<int>::create(nJA_new);
119  auto pNew_A=TArray1<T>::create(nJA_new);
120 
121  auto &New_IA=*pNew_IA;
122  auto &New_JA=*pNew_JA;
123  auto &New_A=*pNew_A;
124 
125  auto &Old_IA=*pIA;
126  auto &Old_JA=*pJA;
127  auto &Old_A=*pA;
128 
129  auto &Ext_IA=*pExt->pIA;
130  auto &Ext_JA=*pExt->pJA;
131  auto &Ext_A=*pExt->pA;
132 
133  int maxrow_ext=0;
134  for (index i=0;i<n;i++)
135  {
136  int k0=i;
137  int lrow=1;
138  for (int k=k0; k>=0;k0=k, k=Ext_IA[k]) lrow++;
139  maxrow_ext=std::max(lrow,maxrow_ext);
140  }
141 
142  std::vector<RowEntry> row(maxrow+maxrow_ext+10);
143  int New_maxrow=0;
144  int j=0;
145  int i;
146  for(i=0;i<n;i++)
147  {
148  // put extension entries into row and sort them
149  int lxrow=0;
150  int k0=i;
151  for (int k=k0; k>=0;k0=k, k=Ext_IA[k])
152  if (Ext_JA[k]>=0)
153  {
154  row[lxrow].i=Ext_JA[k];
155  row[lxrow].a=Ext_A[k];
156  lxrow++;
157  }
158 
159 
160  std::sort(row.begin(),row.begin()+lxrow,
161  [](const RowEntry&e1, const RowEntry &e2)-> bool
162  {
163  return (e1.i<e2.i);
164  });
165 
166  // jointly sort Old and New entries into New_JA
167  int j0;
168  j0=j;
169  New_IA[i]=j;
170  int irow=0;
171  int k=Old_IA[i];
172 
173  for(;;)
174  {
175  if (k<Old_IA[i+1] && ((irow>lxrow) ||Old_JA[k]<row[irow].i))
176  {
177  New_JA[j]=Old_JA[k];
178  New_A[j]=Old_A[k];
179  k++;
180  j++;
181  continue;
182  }
183 
184  if (irow<lxrow)
185  {
186  New_JA[j]=row[irow].i;
187  New_A[j]=row[irow].a;
188  irow++;
189  j++;
190  continue;
191  }
192  break;
193  }
194  New_maxrow=std::max(New_maxrow,j-j0);
195  }
196  New_IA[i]=j;
197 
198  pIA=pNew_IA;
199  pJA=pNew_JA;
200  pA=pNew_A;
201  maxrow=New_maxrow;
202  _pattern_changed=false;
203  _first_flush_done=true;
204  pExt=nullptr;
205  }
std::shared_ptr< TArray1< int > > pJA
Column indices.
A::value_type max(const A &a)
Maximum of array or expression.
Definition: util.ixx:70
std::shared_ptr< Extension > pExt
std::shared_ptr< TArray1< T > > pA
Entries.
unsigned int index
Definition: numcxx.hxx:21
static std::shared_ptr< TArray1< T > > create(index n1)
Construct smart pointer empty 1D Array.
Definition: tarray1.hxx:81
std::shared_ptr< TArray1< int > > pIA
Row pointers.

+ Here is the call graph for this function:

template<typename T>
void numcxx::TSparseMatrix< T >::clear ( )
inline

Definition at line 63 of file tsparsematrix.hxx.

63 { flush(); (*pIA)=0.0;}
void flush()
Re-create the internal data structure in order to accomodated all newly created elements.

+ Here is the call graph for this function:

template<typename T >
void numcxx::TSparseMatrix< T >::apply ( const TArray< T > &  U,
TArray< T > &  V 
) const
inlinevirtual

Apply sparse matrix to vector.

Reimplemented from numcxx::TLinOperator< T >.

Definition at line 209 of file tsparsematrix.ixx.

210  {
211  v.resize(u.size());
212  auto &IA=*pIA;
213  auto &JA=*pJA;
214  auto &A=*pA;
215  for (int i=0;i<n;i++)
216  {
217  v[i]=0;
218  for (int j=IA[i];j<IA[i+1];j++)
219  v[i]+=A[j]*u[JA[j]];
220  }
221  if (pExt!=nullptr)
222  pExt->apply(u,v);
223  }
std::shared_ptr< TArray1< int > > pJA
Column indices.
std::shared_ptr< Extension > pExt
std::shared_ptr< TArray1< T > > pA
Entries.
std::shared_ptr< TArray1< int > > pIA
Row pointers.

+ Here is the call graph for this function:

template<typename T >
std::shared_ptr< TMatrix< T > > numcxx::TSparseMatrix< T >::copy_as_dense ( )
inline

Create a dense matrix from the sparse matrix.

Definition at line 249 of file tsparsematrix.ixx.

250  {
251  flush();
252  auto pM=TMatrix<T>::create(n,n);
253 
254  auto &M=*pM;
255  auto &IA=*pIA;
256  auto &JA=*pJA;
257  auto &A=*pA;
258  M=0.0;
259  for (int i=0;i<n;i++)
260  for (int j=IA[i];j<IA[i+1];j++)
261  M(i,JA[j])=A[j];
262  return pM;
263  }
std::shared_ptr< TArray1< int > > pJA
Column indices.
static std::shared_ptr< TMatrix< T > > create(index n1, index n2)
Construct empty square matrix.
Definition: tmatrix.hxx:81
void flush()
Re-create the internal data structure in order to accomodated all newly created elements.
std::shared_ptr< TArray1< T > > pA
Entries.
std::shared_ptr< TArray1< int > > pIA
Row pointers.

+ Here is the call graph for this function:

template<typename T>
index numcxx::TSparseMatrix< T >::shape ( int  idim) const
inline

Return the shape od the matrix.

Definition at line 72 of file tsparsematrix.hxx.

72 {return n;}

+ Here is the call graph for this function:

template<typename T >
std::shared_ptr< TMatrix< T > > numcxx::TSparseMatrix< T >::calculate_inverse ( )
inline

Calculate inverse of sparse matrix whuch is dense.

Definition at line 403 of file tsparsematrix.ixx.

404  {
405  auto pA=copy_as_dense(); // essentially superfluous, but we seldomly use this
406  auto pLU=TSolverLapackLU<T>::create(pA);
407  return pLU->calculate_inverse();
408  }
std::shared_ptr< TMatrix< T > > copy_as_dense()
Create a dense matrix from the sparse matrix.
static std::shared_ptr< TSolverLapackLU< T > > create(const std::shared_ptr< TMatrix< T >> pMatrix)
Static wrapper around constructor.
std::shared_ptr< TArray1< T > > pA
Entries.

+ Here is the call graph for this function:

template<typename T>
bool numcxx::TSparseMatrix< T >::pattern_changed ( ) const
inline

Check if pattern has changed after last solver update.

Definition at line 100 of file tsparsematrix.hxx.

template<typename T>
void numcxx::TSparseMatrix< T >::pattern_changed ( bool  chg)
inlineprivate

Definition at line 107 of file tsparsematrix.hxx.

template<typename T>
bool numcxx::TSparseMatrix< T >::empty ( ) const
inlineprivate

Definition at line 115 of file tsparsematrix.hxx.

115 { return !_first_flush_done;};

Friends And Related Function Documentation

template<typename T>
friend class TSolverUMFPACK< T >
friend

Definition at line 25 of file tsparsematrix.hxx.

template<typename T>
friend class TPreconJacobi< T >
friend

Definition at line 26 of file tsparsematrix.hxx.

template<typename T>
friend class TPreconILU< T >
friend

Definition at line 27 of file tsparsematrix.hxx.

Member Data Documentation

template<typename T>
std::shared_ptr<TArray1<int> > numcxx::TSparseMatrix< T >::pIA

Row pointers.

Definition at line 87 of file tsparsematrix.hxx.

template<typename T>
std::shared_ptr<TArray1<int> > numcxx::TSparseMatrix< T >::pJA

Column indices.

Definition at line 90 of file tsparsematrix.hxx.

template<typename T>
std::shared_ptr<TArray1 <T> > numcxx::TSparseMatrix< T >::pA

Entries.

Definition at line 93 of file tsparsematrix.hxx.

template<typename T>
bool numcxx::TSparseMatrix< T >::_pattern_changed =false
private

Definition at line 110 of file tsparsematrix.hxx.

template<typename T>
const index numcxx::TSparseMatrix< T >::n
private

Definition at line 111 of file tsparsematrix.hxx.

template<typename T>
int numcxx::TSparseMatrix< T >::maxrow =0
private

Definition at line 112 of file tsparsematrix.hxx.

template<typename T>
bool numcxx::TSparseMatrix< T >::_first_flush_done =false
private

Definition at line 114 of file tsparsematrix.hxx.

template<typename T>
std::shared_ptr<Extension> numcxx::TSparseMatrix< T >::pExt =nullptr
private

Definition at line 122 of file tsparsematrix.hxx.


The documentation for this class was generated from the following files: