NeoPZ
pzshtmat.cpp
Go to the documentation of this file.
1 
6 #include "pzshtmat.h"
7 #include "pzerror.h"
8 
9 using namespace std;
10 
11 template <class TObj>
13  fRows = 0;
14  this->fCols = 0;
15  fMem = NULL;
16 }
17 
18 template <class TObj>
19 TPZGenMatrix<TObj>::TPZGenMatrix(int64_t Rows, int64_t columns) {
20  fRows = Rows;
21  this->fCols = columns;
22  fMem = new TObj[Rows*columns];
23 
24  if(fMem){
25  TObj *pbeg,*pfinal;
26  pfinal = &(fMem[Rows*columns]);
27  for (pbeg = fMem; pbeg<pfinal; *pbeg++=0) ;
28  } else {
29  cout << "TPZGenMatrix<TObj>.ct-->Cannot create matrix structure\n";
30  }
31 }
32 
33 
34 template <class TObj>
36 
37  //***** WARNING *****
38  // matrices created with copy initializer are always temporary, eg, they
39  // share the same storage with another matrix
40  int64_t naloc = A.fRows*A.fCols;
41  fMem = new TObj[naloc];
42  if(fMem) {
43  fRows = A.fRows;
44  this->fCols = A.fCols;
45  TObj *f = fMem,*l = f+naloc,*fa = A.fMem;
46  while(f<l) *f++ = *fa++;
47  } else {
48  fRows = 0;
49  this->fCols = 0;
50  }
51 }
52 
53 template <class TObj>
54 void TPZGenMatrix<TObj>::Resize(const int64_t newrow, const int64_t newcol) {
55  TObj *sht = new TObj[newrow*newcol];
56  if(!sht) return;
57  int64_t minrow = (newrow < Rows()) ? newrow : Rows();
58  int64_t mincol = (newcol < Cols()) ? newcol : Cols();
59  for(int64_t i=0; i<minrow; i++) {
60  for(int64_t j=0; j<mincol; j++) {
61  sht[i*newcol+j] = (*this)(i,j);
62  }
63  }
64  delete []fMem;
65  fMem = sht;
66  fRows = newrow;
67  this->fCols = newcol;
68 }
69 
70 template <class TObj>
72  if(fMem) delete []fMem;
73  fMem = 0;
74  fRows = 0;
75  this->fCols = 0;
76 }
77 
78 template <class TObj>
80 
81  if(this == &rval) return *this;
82  if(fMem) delete fMem;
83  fRows = rval.fRows;
84  this->fCols = rval.fCols;
85  fMem = new TObj[fRows*this->fCols];
86 
87  if(fMem) {
88  TObj *pbeg,*pfinal;
89  pfinal = &(fMem[fRows*this->fCols]);
90  TObj *rvalm = rval.fMem;
91  for (pbeg = fMem; pbeg<pfinal; pbeg++,rvalm++) *pbeg = *rvalm;
92  } else {
93  cout << "TPZGenMatrix<TObj>.= -->Cannot allocate matrix storage\n";
94  }
95  return (*this);
96 }
97 
98 
99 template <class TObj>
101  if ( (this->fRows != rval.fRows) || (this->fCols != rval.fCols) )
102  cout << "ERROR-> different TPZGenMatrix<TObj> size for addition";
103 
104 
105  TPZGenAMatrix<TObj> sum(this->Rows(), this->Cols());
106  TObj *pbeg1 = this->fMem, *pfin;
107  TObj *pbeg2 = sum.fMem, *pbeg3 = rval.fMem;
108  pfin = pbeg1 + (this->fRows*this->fCols);
109  for ( ; pbeg1<pfin; pbeg1++, pbeg2++, pbeg3++)
110  *pbeg2 = *pbeg1 + *pbeg3;
111  return sum;
112 }
113 
114 template <class TObj>
116 
117  TPZGenAMatrix<TObj> sum(this->Rows(), this->Cols());
118 
119  for (int64_t i=0; i<this->fRows*this->fCols; i++)
120  sum.fMem[i] = this->fMem[i] + x;
121  return sum;
122 }
123 
124 template <class TObj>
126 
127  TPZGenAMatrix<TObj> unaryminus(this->Rows(), this->Cols());
128 
129  for (int64_t i=0; i<this->fRows*this->fCols; i++)
130  unaryminus.fMem[i] = - this->fMem[i];
131  return unaryminus;
132 }
133 
134 template <class TObj>
136 
137  if (this->fCols != rval.fRows)
138  cout << "ERROR-> unsuitable TPZGenMatrix<TObj> size for multiplication\n";
139  cout.flush();
140 
141  TPZGenAMatrix<TObj> result(this->Rows(), rval.Cols());
142 
143  TObj *ptr = result.fMem;
144  for (int64_t i=0; i<(this->fRows*this->fCols); i+=this->fCols)
145  for (int64_t j=0; j<rval.fCols; j++, ptr++)
146  for (int64_t k=0; k<this->fCols; k++)
147  *ptr += this->fMem[i+k] * rval.fMem[j+k*rval.fCols];
148 
149  return result;
150 }
151 
152 template <class TObj>
154 
155  TPZGenAMatrix<TObj> result(this->Rows(), this->Cols());
156 
157  for (int64_t i=0; i<this->fRows*this->fCols; i++)
158  result.fMem[i] = this->fMem[i] * x;
159  return result;
160 }
161 
162 template <class TObj>
164 
165  if ( (this->fRows != rval.fRows) || (this->fCols != rval.fCols) )
166  cout << "ERROR-> different TPZGenMatrix<TObj> size for addition";
167  cout.flush();
168 
169  TObj *pbeg1 = this->fMem, *pfin;
170  TObj *pbeg3 = rval.fMem;
171  pfin = pbeg1 + (this->fRows*this->fCols);
172  for ( ; pbeg1<pfin; pbeg1++, pbeg3++)
173  *pbeg1 += *pbeg3;
174  return (*this);
175 }
176 
177 template <class TObj>
179 
180  for (int64_t i=0; i<this->fRows*this->fCols; i++)
181  this->fMem[i] += x;
182  return (*this);
183 }
184 
185 template <class TObj>
187 
188  if ( (this->fRows != rval.fRows) || (this->fCols != rval.fCols) )
189  cout << "ERROR-> different TPZGenMatrix<TObj> size for addition";
190  cout.flush();
191 
192  TObj *pbeg1 = this->fMem, *pfin;
193  TObj *pbeg3 = rval.fMem;
194  pfin = pbeg1 + (this->fRows*this->fCols);
195  for ( ; pbeg1<pfin; pbeg1++, pbeg3++)
196  *pbeg1 -= *pbeg3;
197  return (*this);
198 }
199 
200 template <class TObj>
202 
203  for (int64_t i=0; i<this->fRows*this->fCols; i++)
204  this->fMem[i] -= x;
205  return (*this);
206 }
207 
208 template <class TObj>
210 
211  for (int64_t i=0; i<this->fRows*this->fCols; i++)
212  this->fMem[i] *= x;
213  return (*this);
214 }
215 
216 template <class TObj>
217 TObj & TPZGenMatrix<TObj>::operator()(const int64_t i,const int64_t j) const{
218 
219  if (i>=0 && i<this->fRows && j>=0 && j<this->fCols)
220  return fMem[i*this->fCols+j];
221  else {
222  cout << "ERROR-> TPZGenMatrix<TObj> index out of range\n"
223  " i = " << i << " j = " << j << " Rows = " <<
224  this->fRows << " columns = " << this->fCols << "\n";
225  cout.flush();
226 #ifdef PZDEBUG
227  DebugStop();
228 #endif
229  return fMem[0];
230  }
231 }
232 
233 template <class TObj>
235 
236  TPZGenAMatrix<TObj> transp(this->Cols(),this->Rows());
237 
238  TObj *ptr = this->fMem;
239 
240  for (int64_t i=0; i < this->fRows; i++) { //loop over columns of Transpose
241  TObj *trp = transp.fMem + i;
242  for (int64_t j=0; j<this->fCols; j++, ptr++, trp += this->fRows) {
243  *trp = *ptr;
244  }
245  }
246  return transp;
247 }
248 /*
249  void TPZGenMatrix<TObj>::Print (ostream & out) {
250 
251  if(fMem == NULL) {
252  cout << "NULL TPZGenMatrix<TObj>\n";
253  return;
254  }
255  out << "TPZGenMatrix<TObj> Rows = " << this->fRows << " columns = " << this->fCols << "\n";
256  for (int64_t i=0; i<this->fRows; i++) {
257  out << "\n row " << i;
258  for (int64_t j=0; j<this->fCols; j++) {
259  if ( !(j%6) ) out << "\n";
260  out << " " << fMem[(i*this->fCols)+j];
261  }
262  }
263  out << "\n";
264  return;
265  }
266 
267  */
268 template <class TObj>
269 void TPZGenMatrix<TObj>::Print (const char *c, ostream & out) const {
270 
271  if(fMem == NULL) {
272  cout << "NULL TPZGenMatrix<TObj>\n";
273  return;
274  }
275 // out << c << endl;
276 // out << "TPZGenMatrix<TObj> Rows = " << this->fRows << " columns = " << this->fCols << endl;
277 // for (int64_t i=0; i<this->fRows; i++) {
278 // out << "\n row " << i;
279 // for (int64_t j=0; j<this->fCols; j++) {
280 // if ( !(j%6) ) out << "\n";
281 // out << " " << fMem[(i*this->fCols)+j];
282 // }
283 // }
284 // out << "\n";
285  return;
286 }
287 
288 
289 template <class TObj>
291  if ( (this->fRows != rval.fRows) || (this->fCols != rval.fCols) )
292  cout << "ERROR-> different TPZGenMatrix<TObj> size for subtraction";
293  cout.flush();
294 
295 
296  TPZGenAMatrix<TObj> sum(this->Rows(), this->Cols());
297 
298  TObj *pbeg1 = this->fMem, *pfin;
299  TObj *pbeg2 = sum.fMem, *pbeg3 = rval.fMem;
300  pfin = pbeg1 + (this->fRows*this->fCols);
301  for ( ; pbeg1<pfin; pbeg1++, pbeg2++, pbeg3++)
302  *pbeg2 = *pbeg1 - *pbeg3;
303  return sum;
304 }
305 
306 template <class TObj>
308 
309 
310  TPZGenAMatrix<TObj> sum(this->Rows(), this->Cols());
311 
312  for (int64_t i=0; i<this->fRows*this->fCols; i++)
313  sum.fMem[i] = this->fMem[i] - x;
314  return sum;
315 }
316 
317 template class TPZGenMatrix<int>;
318 template class TPZGenAMatrix<int>;
319 template class TPZGenMatrix<int64_t>;
320 template class TPZGenAMatrix<int64_t>;
TPZGenAMatrix< TObj > & operator*=(const TObj x)
Definition: pzshtmat.cpp:209
TPZGenAMatrix< TObj > & operator-=(const TPZGenAMatrix< TObj > &rval)
Definition: pzshtmat.cpp:186
Implements a generic matrix of objects which implement arithmetic operations. Matrix.
Definition: pzshtmat.h:64
void Print(const char *mess, std::ostream &out=std::cout) const
Definition: pzshtmat.cpp:269
Defines PZError.
TPZGenMatrix()
Constructor creating Null matrix.
Definition: pzshtmat.cpp:12
TPZGenAMatrix operator+(const TPZGenAMatrix< TObj > &rval) const
Definition: pzshtmat.cpp:100
void Resize(const int64_t newrow, const int64_t newcol)
Definition: pzshtmat.cpp:54
TPZGenAMatrix< TObj > & operator+=(const TPZGenAMatrix< TObj > &rval)
Definition: pzshtmat.cpp:163
int64_t fRows
Number of rows and columns.
Definition: pzshtmat.h:24
TObj & operator()(const int64_t row, const int64_t column=0) const
Definition: pzshtmat.cpp:217
f
Definition: test.py:287
#define DebugStop()
Returns a message to user put a breakpoint in.
Definition: pzerror.h:20
TPZGenAMatrix operator-() const
Definition: pzshtmat.cpp:125
int64_t Rows() const
Returns number of rows.
Definition: pzmatrix.h:803
TPZGenAMatrix< TObj > Transpose() const
Definition: pzshtmat.cpp:234
Implements generic class which holds a matrix of objects. Matrix.
Definition: pzshtmat.h:18
TObj * fMem
Pointer to matrix.
Definition: pzshtmat.h:22
int64_t Cols() const
Definition: pzshtmat.h:47
TPZGenMatrix< TObj > & operator=(const TPZGenMatrix< TObj > &rval)
Definition: pzshtmat.cpp:79
int64_t Cols() const
Returns number of cols.
Definition: pzmatrix.h:809
Contains TPZGenMatrix class which implements generic class which holds a matrix of objects...
int64_t fCols
Definition: pzshtmat.h:24
TPZGenAMatrix operator*(const TPZGenAMatrix< TObj > &rval) const
Definition: pzshtmat.cpp:135