NeoPZ
tpzverysparsematrix.cpp
Go to the documentation of this file.
1 
6 #include "tpzverysparsematrix.h"
7 
8 using namespace std;
9 
10 template<class TVar>
13 fExtraSparseData()
14 {
15 }
16 
17 template<class TVar>
19 {
20 }
21 
22 template<class TVar>
23 int TPZVerySparseMatrix<TVar>::PutVal(const int64_t row,const int64_t col, const TVar &val)
24 {
25  if (row < 0 || col < 0 || row >this->fRow || col >this->fCol)
26  {
27  cout<< "ERRO! em TPZVerySparseMatrix::PutVal: The row i or column j are incompatible with the rows or columns of a matrix"<< endl;
28  return -1;
29  }
30 
31  pair <int64_t,int64_t> position(row,col);
32  typename std::map <std::pair<int64_t, int64_t>, TVar>::iterator it = this->fExtraSparseData.find(position);
33  if(val == (TVar)0. && it != fExtraSparseData.end())
34  {
35  fExtraSparseData.erase(it);
36  }
37  else if( it != fExtraSparseData.end())
38  {
39  it->second = val;
40  }
41  else
42  {
43  fExtraSparseData[position] = val;
44  }
45  return 0;
46 }
47 
48 template<class TVar>
50 {
51  for(int64_t i=0; i<this->fRow; i++)
52  {
53  for(int64_t j=0; j<this->fCol; j++)
54  {
55  TVar a = cp.GetVal(i,j);
56  if(!IsZero(a)) PutVal(i,j,a);
57  }
58  }
59 }
60 
61 template<class TVar>
63 
64  int64_t rows = this->Rows();
65  int64_t cols = this->Cols();
66 
67  // TODO: introduce error handling mechanism
68  if ( rows != cols) {
69  cerr << "Error: Simetrize only work for square matrices";
70  return;
71  }
72 
73  typename std::map <std::pair<int64_t, int64_t>, TVar>::iterator it = this->fExtraSparseData.begin();
74  typename std::map <std::pair<int64_t, int64_t>, TVar>::iterator end = this->fExtraSparseData.end();
75  typename std::map <std::pair<int64_t, int64_t>, TVar>::iterator next;
76 
77  std::list< std::pair< std::pair<int64_t, int64_t>, TVar > > temp;
78 
79  for(; it != end; it=next) {
80  const std::pair<int64_t, int64_t>& key = it->first;
81  next = it;
82  next++;
83 
84  if(key.first < key.second) {
85  temp.push_back( std::pair< std::pair<int64_t, int64_t>, TVar > (std::pair<int64_t, int64_t>(key.second, key.first), it->second));
86  }
87  else if (key.first > key.second) {
88  this->fExtraSparseData.erase(it);
89  }
90  }
91 
92  typename std::list< std::pair< std::pair<int64_t, int64_t>, TVar > >::iterator at = temp.begin();
93  typename std::list< std::pair< std::pair<int64_t, int64_t>, TVar > >::iterator atEnd = temp.end();
94 
95  for(; at != atEnd; at++) {
96  this->fExtraSparseData [ at->first ] = at->second;
97  }
98 
99 }
100 
101 template<class TVar>
103  int64_t rows = this->Rows();
104  int64_t cols = this->Cols();
105  T->Resize( cols, rows );
106  T->fExtraSparseData.clear();
107  typename std::map <std::pair<int64_t, int64_t>, TVar>::const_iterator it = this->fExtraSparseData.begin();
108  typename std::map <std::pair<int64_t, int64_t>, TVar>::const_iterator end = this->fExtraSparseData.end();
109 
110  for (; it != end; it++) {
111  const std::pair<int64_t, int64_t>& key = it->first;
112  T->fExtraSparseData[std::pair<int64_t,int64_t>(key.second, key.first)] = it->second;
113  }
114 }
115 
116 template<class TVar>
117 const TVar & TPZVerySparseMatrix<TVar>::GetVal(const int64_t row, const int64_t col) const
118 {
119  if (row < 0 || col < 0 || row >this->fRow || col >this->fCol)
120  {
121  cout<< "ERRO! em TPZVerySparseMatrix::GetVal: The row i or column j are incompatible with the rows or columns of a matrix"<< endl;
122  return this->gZero;
123  }
124 
125  pair <int64_t,int64_t> position(row,col);
126  typename map<pair<int64_t,int64_t>, TVar>::const_iterator it;
127  it = fExtraSparseData.find(position);
128 
129  if (it == fExtraSparseData.end() )
130  {
131  return this->gZero;
132  }
133 
134  return it->second;
135 }
136 
137 template<class TVar>
139  const TVar alpha, const TVar beta, const int opt) const
140 {
141  if (!opt)
142  {
143  if(this->Cols() != x.Rows() || this->Rows() != y.Rows())
144  {
145  cout << "\nERROR! em TPZVerySparseMatrix::MultiplyAdd: incompatible dimensions in opt=false\n";
146  return;
147  }
148  }
149  else
150  if (this->Rows() != x.Rows() || this->Cols() != y.Rows())
151  {
152  cout << "\nERROR! em TPZVerySparseMatrix::MultiplyAdd: incompatible dimensions in opt=true\n";
153  return;
154  }
155  if(x.Cols() != y.Cols() || x.Cols() != z.Cols() || y.Rows() != z.Rows() )
156  {
157  cout << "\nERROR! em TPZVerySparseMatrix::MultiplyAdd : incompatible dimensions in x, y or z\n";
158  return;
159  }
160 
161  int64_t xcols = x.Cols();
162  int64_t ic, c, r;
163  this->PrepareZ(y,z,beta,opt);
164  TVar val = 0.;
165 
166  for (ic = 0; ic < xcols; ic++)
167  {
168  if(!opt)
169  {
170  typename map< pair<int64_t,int64_t>, TVar>::const_iterator it;
171 
172  for(it = fExtraSparseData.begin(); it!= fExtraSparseData.end(); it++)
173  {
174  pair <int64_t, int64_t> position(it->first);
175  c = position.second;
176  r = position.first;
177  TVar matrixval = it->second;
178 
179  val = z(r,ic) + alpha * matrixval * x.GetVal(c,ic);
180  z.PutVal(r,ic,val);
181  }
182  } else
183  {
184  typename map<pair<int64_t,int64_t>, TVar>::const_iterator it;
185 
186  for(it = fExtraSparseData.begin(); it != fExtraSparseData.end(); it++)
187  {
188  pair <int64_t, int64_t> posicao(it->first);
189  r = posicao.second;
190  c = posicao.first;
191  TVar matrixval = it->second;
192  z(r,ic) += (alpha*matrixval)*x.GetVal(c,ic);
193  }
194  }
195  }
196 }
197 template<class TVar>
198 void TPZVerySparseMatrix<TVar>::Write(TPZStream &buf, int withclassid) const
199 {
200  buf.Write(&this->fCol, 1);
201  buf.Write(&this->fDecomposed, 1);
202  buf.Write(&this->fDefPositive, 1);
203  buf.Write(&this->fRow, 1);
204  buf.Write(&this->gZero, 1);
205  WriteMap(buf, withclassid, this->fExtraSparseData);
206 
207 }
208 template<class TVar>
209 void TPZVerySparseMatrix<TVar>::WriteMap(TPZStream &buf, int withclassid, const std::map<std::pair<int64_t, int64_t>, TVar> & TheMap) const
210 {
211  int mapsz = TheMap.size();
212  buf.Write(&mapsz, 1);
213  typename std::map<std::pair<int64_t, int64_t>, TVar>::const_iterator it;
214  for(it = TheMap.begin(); it != TheMap.end(); it++)
215  {
216  int ii = 0, jj = 0;
217  ii = it->first.first;
218  jj = it->first.second;
219  buf.Write(&ii, 1);
220  buf.Write(&jj, 1);
221  buf.Write(&it->second, 1);
222  }
223 }
224 
225 template<class TVar>
227 {
228  buf.Read(&this->fCol, 1);
229  buf.Read(&this->fDecomposed, 1);
230  buf.Read(&this->fDefPositive, 1);
231  buf.Read(&this->fRow, 1);
232  buf.Read(&this->gZero, 1);
233  ReadMap(buf, context, this->fExtraSparseData);
234 
235 }
236 template<class TVar>
237 void TPZVerySparseMatrix<TVar>::ReadMap(TPZStream &buf, void *context, std::map<std::pair<int64_t, int64_t>, TVar> & TheMap)
238 {
239  TheMap.clear();
240  int size = 0;
241  buf.Read(&size, 1);
242  int i;
243  for(i = 0; i < size; i++)
244  {
245  int ii = 0, jj = 0;
246  TVar value = 0.;
247  buf.Read(&ii, 1);
248  buf.Read(&jj, 1);
249  std::pair<int64_t, int64_t> item(ii, jj);
250  buf.Read(&value, 1);
251  std::pair<std::pair<int64_t, int64_t>, TVar > fullitem(item, value);
252  TheMap.insert(fullitem);
253  }
254 }
255 
256 template class TPZVerySparseMatrix<float>;
257 template class TPZVerySparseMatrix<double>;
258 template class TPZVerySparseMatrix<long double>;
259 
263 
264 
void WriteMap(TPZStream &buf, int withclassid, const std::map< std::pair< int64_t, int64_t >, TVar > &TheMap) const
Auxiliary functions only reading and writing a map as the third paremeter.
bool IsZero(long double a)
Returns if the value a is close Zero as the allowable tolerance.
Definition: pzreal.h:668
std::map< std::pair< int64_t, int64_t >, TVar > fExtraSparseData
Save elements different from zero, of Sparse matrix.
virtual int Resize(const int64_t newRows, const int64_t newCols)
Redimensions a matriz keeping the previous values.
Definition: pzmatrix.h:278
int64_t fRow
Number of rows in matrix.
Definition: pzmatrix.h:779
void PrepareZ(const TPZFMatrix< TVar > &y, TPZFMatrix< TVar > &z, const TVar beta, const int opt) const
Is an auxiliar method used by MultiplyAdd.
Definition: pzmatrix.cpp:135
char fDecomposed
Decomposition type used to decompose the current matrix.
Definition: pzmatrix.h:783
REAL val(STATE &number)
Returns value of the variable.
Definition: pzartdiff.h:23
char fDefPositive
Definite Posistiveness of current matrix.
Definition: pzmatrix.h:785
static TVar gZero
Initializing value to static variable.
Definition: pzmatrix.h:786
virtual const TVar & GetVal(const int64_t row, const int64_t col) const override
Get values checking bounds.
void Read(TPZStream &buf, void *context) override
Unpacks the object structure from a stream of bytes.
virtual void Write(const bool val)
Definition: TPZStream.cpp:8
void ReadMap(TPZStream &buf, void *context, std::map< std::pair< int64_t, int64_t >, TVar > &TheMap)
Implements a matrix whose nonzero elements are stored in binary tree. Matrix.
Definition: pzfmatrix.h:33
int64_t Rows() const
Returns number of rows.
Definition: pzmatrix.h:803
Full matrix class. Matrix.
Definition: pzfmatrix.h:32
int64_t fCol
Number of cols in matrix.
Definition: pzmatrix.h:781
void Simetrize() override
Simetrizes copies the data of the matrix to make its data simetric.
int PutVal(const int64_t row, const int64_t col, const TVar &val) override
Put values checking bounds.
Contains TPZVerySparseMatrix class which implements a matrix whose nonzero elements are stored in bin...
int64_t Cols() const
Returns number of cols.
Definition: pzmatrix.h:809
Defines the interface for saving and reading data. Persistency.
Definition: TPZStream.h:50
virtual void MultAdd(const TPZFMatrix< TVar > &x, const TPZFMatrix< TVar > &y, TPZFMatrix< TVar > &z, const TVar alpha=1, const TVar beta=0, const int opt=0) const override
It computes z = beta * y + alpha * opt(this)*x but z and x can not overlap in memory.
void Write(TPZStream &buf, int withclassid) const override
Packs the object structure in a stream of bytes.
int ClassId() const override
Saveable methods.
int PutVal(const int64_t row, const int64_t col, const TVar &value) override
Put values without bounds checking This method is faster than "Put" if DEBUG is defined.
Definition: pzfmatrix.h:548
const TVar & GetVal(const int64_t row, const int64_t col) const override
Get values without bounds checking This method is faster than "Get" if DEBUG is defined.
Definition: pzfmatrix.h:566
virtual void Read(bool &val)
Definition: TPZStream.cpp:91
virtual void Transpose(TPZVerySparseMatrix< TVar > *T) const
It makes *T the transpose of current matrix.
Root matrix class (abstract). Matrix.
Definition: pzmatrix.h:60