forked from deepmodeling/abacus-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplexmatrix.cpp
More file actions
457 lines (412 loc) · 8.62 KB
/
complexmatrix.cpp
File metadata and controls
457 lines (412 loc) · 8.62 KB
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#include <cassert>
#include <new>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include "complexmatrix.h"
#ifdef __NORMAL
#else
#include "module_external/blas_connector.h"
#endif
namespace ModuleBase
{
// constructor with sizes
ComplexMatrix::ComplexMatrix(const int nrows, const int ncols, const bool flag_zero)
:nr(nrows),
nc(ncols),
size(nrows*ncols),
c(nullptr)
{
if( size )
{
c = new std::complex<double>[size];
if(flag_zero) zero_out();
}
}
// zero out the ComplexMatrix
void ComplexMatrix::zero_out(void)
{
for (int i=0; i<size; i++) c[i] = std::complex<double>(0.0,0.0);
}
/*
void need_more_memory()
{
std::cout << "\n Sorry to crash... but the running need more momory! Exit." << std::endl;
exit(0);
}
*/
// Copy constructor
ComplexMatrix::ComplexMatrix(const ComplexMatrix &m1)
:nr(m1.nr),
nc(m1.nc),
size(m1.size),
c(nullptr)
{
if(size)
{
c = new std::complex<double>[size];
memcpy( c, m1.c, size*sizeof(std::complex<double>) );
}
}
// Peize Lin add 2016-08-05
ComplexMatrix::ComplexMatrix( ComplexMatrix && m1 )
:nr(m1.nr),
nc(m1.nc),
size(m1.size),
c(m1.c)
{
m1.nr = m1.nc = m1.size = 0;
m1.c = nullptr;
}
// Peize Lin add 2017-03-29
ComplexMatrix::ComplexMatrix(const matrix &m)
:nr(m.nr),
nc(m.nc),
size(m.nr*m.nc),
c(nullptr)
{
if( size )
{
c = new std::complex<double>[size];
for( int i=0; i<size; ++i)
{
c[i] = m.c[i];
}
}
}
// deconstructor
ComplexMatrix::~ComplexMatrix()
{
if(c)
{
delete[] c;
c = nullptr;
}
}
// reallocate memory for Complex Matrix
void ComplexMatrix::create(const int nr_in, const int nc_in, const bool flag_zero)
{
if( nr_in && nc_in )
{
if(c)
{
const int size_in=nr_in*nc_in;
if( size_in!=nr*nc )
{
delete[] c;
c = new std::complex<double>[size_in];
}
}
else
{
c = new std::complex<double>[nr_in * nc_in];
}
nr = nr_in;
nc = nc_in;
size = nr*nc;
if(flag_zero) zero_out();
}
else
{
if(c) delete[] c;
c = nullptr;
nr = nr_in;
nc = nc_in;
size = nr*nc;
}
}
void ComplexMatrix::set_as_identity_matrix(void)
{
for(int i=0; i<nr; i++)
{
for(int j=0; j<nc; j++)
{
if(i==j) c[nc * i + j] = std::complex<double>(1.0, 0.0);
else c[nc * i + j] = std::complex<double>(0.0, 0.0);
}
}
return;
}
// Adding matrices, as a friend
ComplexMatrix operator+(const ComplexMatrix &m1, const ComplexMatrix &m2)
{
assert(m1.nr == m2.nr);
assert(m2.nc == m2.nc);
ComplexMatrix tm(m1);
tm+=m2;
return tm;
}
// Subtracting matrices, as a friend
ComplexMatrix operator-(const ComplexMatrix &m1, const ComplexMatrix &m2)
{
assert(m1.nr == m2.nr);
assert(m2.nc == m2.nc);
ComplexMatrix tm(m1);
tm-=m2;
return tm;
}
// Multiplying matrices, as a friend
// mprod = m1 * m2
ComplexMatrix operator*(const ComplexMatrix &m1, const ComplexMatrix &m2)
{
assert(m1.nc == m2.nr);
ComplexMatrix mprod(m1.nr, m2.nc);
// mohan add 2021-04-05
#ifdef __NORMAL
std::complex<double> z;
for (int i = 0;i < m1.nr;i++)
{
for (int j = 0;j < m2.nc;j++)
{
z = std::complex<double>(0,0);
for (int k = 0;k < m1.nc;k++)
{
z += m1(i, k) * m2(k, j);
}
mprod(i, j) = z;
}
}
#else
// Peize Lin accelerate 2017-10-27
BlasConnector::gemm('N', 'N', m1.nr, m2.nc, m1.nc,
1, m1.c, m1.nc, m2.c, m2.nc,
0, mprod.c, mprod.nc);
#endif
return mprod;
}
// Scale a ComplexMatrix
ComplexMatrix operator*(const std::complex<double> &c,const ComplexMatrix &m)
{
ComplexMatrix sm(m);
for (int i=0 ;i<m.size; i++) sm.c[i] *= c;
return sm;
}
// ComplexMatrix scalar
ComplexMatrix operator*(const ComplexMatrix &m,const std::complex<double> &c)
{
ComplexMatrix sm(m);
for (int i = 0;i < m.size;i++) sm.c[i] *= c;
return sm;
}
ComplexMatrix operator*(const double &r,const ComplexMatrix &m)
{
ComplexMatrix sm(m);
for(int i=0; i<m.size; i++) sm.c[i]*= r;
return sm;
}
ComplexMatrix operator*(const ComplexMatrix &m,const double &r)
{
ComplexMatrix sm(m);
for (int i=0; i<m.size; i++) sm.c[i] *= r;
return sm;
}
ComplexMatrix& ComplexMatrix::operator=(const ComplexMatrix &m)
{
this->create(m.nr, m.nc, false);
memcpy( c, m.c, size*sizeof(std::complex<double>) );
return *this;
}
// Peize Lin add 2016-08-05
ComplexMatrix& ComplexMatrix::operator=( ComplexMatrix && m )
{
nr = m.nr; nc = m.nc; size = m.size;
if(c) delete[] c;
c = m.c;
m.nr = m.nc = m.size = 0;
m.c = nullptr;
return *this;
}
ComplexMatrix& ComplexMatrix::operator*=(const std::complex<double> &s)
{
for (int i = 0;i < this->size;i++) c[i] *= s;
return *this;
}
// Accumulate to a ComplexMatrix in place
ComplexMatrix& ComplexMatrix::operator+=(const ComplexMatrix &m)
{
for(int i=0; i<size; i++) this->c[i] += m.c[i];
return *this;
}
// decumulate to a ComplexMatrix in place
ComplexMatrix& ComplexMatrix::operator-=(const ComplexMatrix &m)
{
for(int i=0; i<size; i++) this->c[i] -= m.c[i];
return *this;
}
// Peize Lin add 2017-03-29
matrix ComplexMatrix::real() const
{
matrix m(nr,nc,false);
for( int i=0; i<this->size; ++i) m.c[i] = c[i].real();
return m;
}
// Returns trace of ComplexMatrix
std::complex<double> trace(const ComplexMatrix &m)
{
std::complex<double> tr=std::complex<double>(0,0);
assert(m.nr == m.nc);
for (int i=0; i<m.nr; i++) tr += m(i, i);
return tr;
}
// Do mout += s*min
void scale_accumulate(const std::complex<double> &s,
const ComplexMatrix &min,
ComplexMatrix &mout)
{
assert(min.nr == mout.nr);
assert(min.nc == mout.nc);
for (int j=0; j<min.size; j++)
{
mout.c[j] += s * min.c[j];
}
return;
}
// Do mout[i] += s*min[i]
void scale_accumulate(const int &nmat,
const std::complex<double> &s,
ComplexMatrix **min,
ComplexMatrix **mout)
{
assert(nmat>=0);
for (int i=0; i<nmat; i++)
{
scale_accumulate(s, *min[i], *mout[i]);
}
return;
}
// Do mout = s1*m1 + s2*m2
void scaled_sum(const std::complex<double> &s1,
const ComplexMatrix &m1,
const std::complex<double> &s2,
const ComplexMatrix &m2,
ComplexMatrix &mout)
{
assert(m1.nr == m2.nr);
assert(m1.nr == mout.nr);
assert(m1.nc == m2.nc);
assert(m1.nc == mout.nc);
for(int i=0; i<m1.size; i++)
{
mout.c[i] = s1 * m1.c[i] + s2 * m2.c[i];
}
return;
}
// Does mout[i] = s1*m1[i] + s2*m2[i]
void scaled_sum(const int &nmat,
const std::complex<double> &s1,
ComplexMatrix **m1,
const std::complex<double> &s2,
ComplexMatrix **m2,
ComplexMatrix **mout)
{
assert(nmat>0);
for(int i=0; i<nmat; i++)
{
scaled_sum(s1, *m1[i], s2, *m2[i], *mout[i]);
}
return;
}
double abs2_row(const ComplexMatrix &m,const int ir)
{
double r=0.0;
std::complex<double> z;
for(int ic=0;ic<m.nc;ic++)
{
z = m.c[ m.nc*ir + ic];
r += z.real()*z.real() + z.imag()*z.imag();
}
return r;
}
double abs2_column(const ComplexMatrix &m,const int ic)
{
double r=0.0;
std::complex<double> z;
for(int ir=0;ir<m.nr;ir++)
{
z = m.c[ m.nc*ir + ic ];
r += z.real()*z.real() + z.imag()*z.imag();
}
return r;
}
// returns absolute square magnitude of sum of all ComplexMatrix elements
double abs2(const ComplexMatrix &m)
{
double r=0.0;
std::complex<double> z;
for (int i = 0;i < m.size;i++)
{
z = m.c[i];
r += z.real() * z.real() + z.imag() * z.imag();
}
return r;
}
// Same for an array of matrices
double abs2(const int nmat, ComplexMatrix **m)
{
double r = 0.0;
for (int i = 0;i < nmat;i++)
{
r += abs2(*m[i]);
}
return r;
}
ComplexMatrix transpose(const ComplexMatrix &m, const bool &conjugate)
{
ComplexMatrix tm(m.nc, m.nr, false);
if(conjugate)
for (int i = 0;i < m.nr;i++)
for (int j = 0;j < m.nc;j++)
tm(j, i) = conj ( m(i, j) );
else
for (int i = 0;i < m.nr;i++)
for (int j = 0;j < m.nc;j++)
tm(j, i) = m(i, j);
return tm;
}
ComplexMatrix conj(const ComplexMatrix &m)
{
ComplexMatrix cm( m.nr, m.nc, false );
for(int i=0; i!=m.size; ++i)
cm.c[i] = conj(m.c[i]);
return cm;
}
// Peize Lin add 2021.09.08
std::ostream & ComplexMatrix::print( std::ostream & os, const double threshold_abs, const double threshold_imag ) const
{
for( int ir=0; ir!=this->nr; ++ir )
{
for( int ic=0; ic!=this->nc; ++ic )
{
const std::complex<double> & data = (*this)(ir,ic);
if(std::abs(data)>threshold_abs)
{
if(std::abs(std::imag(data))>threshold_imag)
os<<data<<"\t";
else
os<<std::real(data)<<"\t";
}
else
{
os<<0<<"\t";
}
}
os<<std::endl;
}
return os;
}
bool ComplexMatrix::checkreal(void) const
{
const double tiny = 1e-12;
for(int i=0;i<this->nr;i++)
{
for(int j=0;j<this->nc;j++)
{
if(std::imag((*this)(i,j)) > tiny)
{
return 0;
}
}
}
return 1;
}
}