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