complexmatrix.h Source File

LibRPA: complexmatrix.h Source File
LibRPA
complexmatrix.h
1 //==========================================================
2 // Author : Lixin He, Mohan Chen
3 // Update : Peize Lin
4 // Last Update : 2018-09-04
5 //==========================================================
6 #ifndef COMPLEXMATRIX_H
7 #define COMPLEXMATRIX_H
8 
9 #include <complex>
10 using namespace std;
11 
12 #include "matrix.h"
13 #include "utils_io.h"
14 
15 //#ifdef _MCD_CHECK
16 //#include "src_parallel/mcd.h"
17 //#endif
18 
20 {
21 
22 public:
23 
24  int nr=0;
25  int nc=0;
26  int size=0;
27  complex<double> *c=nullptr;
28 
29  ComplexMatrix(): nr(0), nc(0), size(0), c(nullptr){}
30  ComplexMatrix(const int nrows,const int ncols,const bool flag_zero=true); // Peize Lin add flag_zero 2019-05-13
31  ComplexMatrix(const ComplexMatrix &m1);
32  ComplexMatrix(ComplexMatrix && m1); // Peize Lin add 2016-08-05
33  explicit ComplexMatrix(const matrix &m); // Peize Lin add 2017-03-29
34  ~ComplexMatrix();
35 
36  void create(const int nrow,const int ncol,const bool flag_zero=true); // Peize Lin add flag_zero 2019-05-13
37  ComplexMatrix& operator=(const ComplexMatrix &m);
38  ComplexMatrix& operator=(ComplexMatrix && m); // Peize Lin add 2016-08-05
39 
40  //============
41  // Operators
42  //============
43  complex<double> &operator()(const int ir,const int ic)
44  {
45  if (ir >= nr) LIBRPA::utils::lib_printf("ir %d nr %d\n", ir, nr);
46  if (ic >= nc) LIBRPA::utils::lib_printf("ic %d nc %d\n", ic, nc);
47  assert(ir>=0); assert(ir<nr); assert(ic>=0); assert(ic<nc);
48  return c[ir*nc+ic];//mohan modify in-line 2007-10-1
49  }
50  const complex<double> &operator()(const int ir,const int ic)const
51  {
52  if (ir >= nr) LIBRPA::utils::lib_printf("ir %d nr %d\n", ir, nr);
53  if (ic >= nc) LIBRPA::utils::lib_printf("ic %d nc %d\n", ic, nc);
54  assert(ir>=0); assert(ir<nr); assert(ic>=0); assert(ic<nc);
55  return c[ir*nc+ic];//mohan modify in-line 2007-10-13
56  }
57 
58  ComplexMatrix& operator*=(const complex<double> &s);
59  ComplexMatrix& operator+=(const ComplexMatrix &m);
60  ComplexMatrix& operator+=(const complex<double> &s); // minez add 2022-06-06
61  ComplexMatrix& operator-=(const ComplexMatrix &m);
62  matrix real() const; // Peize Lin add 2017-03-29
63  matrix imag() const;
64 
65  //==================
66  // member function:
67  //==================
68  void zero_out(void);
69  void set_as_identity_matrix(void);
71  double get_max_real() const; // minyez add 2022-05-05
72  double get_max_real(int &ir, int &ic) const; // minyez add 2022-05-05
74  double get_max_imag() const; // minyez add 2022-10-18
75  double get_max_abs() const; // minyez add 2024-03-13
76  double get_max_abs_imag() const; // minyez add 2022-10-26
77  double get_max_abs_offdiag() const;
78  bool is_diagonal(const double &thres = 1e-14) const;
79 };
80 
81 ComplexMatrix operator+(const ComplexMatrix &m1, const ComplexMatrix &m2);
82 ComplexMatrix operator-(const ComplexMatrix &m1, const ComplexMatrix &m2);
83 ComplexMatrix operator-(const complex<double> &s, const ComplexMatrix &m); // minyez add 2022-06-06
84 ComplexMatrix operator-(const ComplexMatrix &m, const complex<double> &s); // minyez add 2022-06-06
85 ComplexMatrix operator*(const ComplexMatrix &m1, const ComplexMatrix &m2);
86 ComplexMatrix operator*(const complex<double> &s, const ComplexMatrix &m);
87 ComplexMatrix operator*(const ComplexMatrix &m, const complex<double> &s);
88 ComplexMatrix operator*(const double &s, const ComplexMatrix &m);
89 ComplexMatrix operator*(const ComplexMatrix &m, const double &s);
90 
91 complex<double> trace(const ComplexMatrix &m);
92 
93 double abs2_row(const ComplexMatrix &m,const int ir); // mohan add 2008-7-1
94 double abs2_column(const ComplexMatrix &m,const int ic); // mohan add 2008-7-1
95 double abs2(const ComplexMatrix &m);
96 double abs2(const int nmat, ComplexMatrix **m);
97 
98 ComplexMatrix transpose(const ComplexMatrix &m, const bool &conjugate);
99 ComplexMatrix conj(const ComplexMatrix &m); // Peize Lin add 2019-05-13
100 
101 void scale_accumulate(
102  const complex<double> &s,
103  const ComplexMatrix &min,
104  ComplexMatrix &mout);
105 
106 void scale_accumulate(
107  const int &nmat,
108  const complex<double> &s,
109  ComplexMatrix **min,
110  ComplexMatrix **mout);
111 
112 void scaled_sum(
113  const complex<double> &s1,
114  const ComplexMatrix &m1,
115  const complex<double> &s2,
116  const ComplexMatrix &m2,
117  ComplexMatrix &mout);
118 
119 void scaled_sum(
120  const int &nmat,
121  const complex<double> &s1,
122  ComplexMatrix **m1,
123  const complex<double> &s2,
124  ComplexMatrix **m2,
125  ComplexMatrix **mout);
126 
128 /*
129  * @param cmat: the complex matrix to compute power
130  * @param power: the power to compute
131  * @param keep_ev: whether to keep the eigenvectors in cmat
132  * @param filter_original: whether to filter the small values when recovering the original matrix
133  * @param threshold: eigenvalue threshold to filter out in computing the power matrix
134  */
135 ComplexMatrix power_hemat(ComplexMatrix &cmat, double power, bool keep_ev = false, bool filter_original = false,
136  double threshold = -1e16); // Minye Zhang add 2022-06-04
137 
139 /*
140  * @param cmat: the complex matrix to compute power
141  * @param power: the power to compute
142  * @param threshold: eigenvalue threshold to filter out in computing the power matrix
143  */
144 void power_hemat_onsite(ComplexMatrix &cmat, double power, double threshold = -1e16); // Minye Zhang add 2022-07-07
145 
146 void print_complex_matrix(const char *desc, const ComplexMatrix &mat);
147 
148 void print_complex_matrix_file(const char *desc, const ComplexMatrix &mat, ofstream &fs, bool use_scientific);
149 void print_complex_matrix_mm(const ComplexMatrix &mat, ofstream &fs, double threshold = 1e-15, bool row_first = true);
150 void print_complex_matrix_file(const char *desc, const ComplexMatrix &mat, const string &fn, bool use_scientific);
151 void print_complex_matrix_mm(const ComplexMatrix &mat, const string &fn, double threshold = 1e-15, bool row_first = true);
152 void print_complex_real_matrix(const char* desc, const ComplexMatrix &mat );
153 #endif
Definition: complexmatrix.h:20
Definition: matrix.h:23
utilies to handle square matrix and related operations
void lib_printf(const char *format, Args &&... args)
printf that handles the stdout redirect
Definition: utils_io.h:13