matrix.h Source File

LibRPA: matrix.h Source File
LibRPA
matrix.h
Go to the documentation of this file.
1 
8 #ifndef MATRIX_H
9 #define MATRIX_H
10 
11 // Peize Lin update 2018-07-29
12 /*
13 #ifdef _MCD_CHECK
14 #include "mcd.h"
15 #endif
16 */
17 #include<ostream>
18 #include<cassert>
19 
20 #include<fstream>// test
21 
22 class matrix
23 {
24  /* data */
25 public:
27  static const double EQUAL_THRES;
28  int nr=0;
29  int nc=0; /* Number of rows and columns */
30  int size=0;
31  double *c=nullptr; /* Holds the data */
32 
33  /* Constructors and destructor */
34  matrix(): nr(0), nc(0), size(0), c(nullptr){}
35  matrix( const int nrows, const int ncols, const bool flag_zero=true ); // Peize Lin add flag_zero 2018-07-02
36  matrix( const matrix &m1 ); /* copy constructor */
37  matrix( matrix && m1 ); // Peize Lin add 2016-08-05
38  ~matrix();
39 
40  void create( const int nrow, const int ncol, const bool flag_zero=true ); // Peize Lin add flag_zero 2018-07-02
41  matrix& operator=(const matrix &m1); // Peize Lin change 2018-03-12
42  matrix& operator=( matrix && m1 ); // Peize Lin add 2016-08-05
43 
44  double &operator()(const int ir,const int ic)
45  {
46 // assert(ir>=0); assert(ir<nr); assert(ic>=0); assert(ic<nc);
47  return c[ir*nc+ic];
48  }
49 
50  const double &operator()(const int ir,const int ic) const
51  {
52 // assert(ir>=0); assert(ir<nr); assert(ic>=0); assert(ic<nc);
53  return c[ir*nc+ic];
54  }
55 
56 // inline double &operator()(int i,int j) const
57 // { return c[nc*i+j]; }
58 
59  void operator*=(const double &s);
60  void operator+=(const double &s);
61  void operator+=(const matrix &m);
62  void operator-=(const double &s);
63  void operator-=(const matrix &m);
64  bool operator==(const matrix &m);
65 
66  matrix operator+(double s) const;
67  matrix operator-(double s) const;
68 
69  /* member function: */
70  matrix Inverse(void);
71 
72  double det(void);
73 
74  // mohan add 2011-01-13
75  double trace_on(void) const;
76 
77  /* zero out all the entries */
78  void zero_out(void);
79 
80  void get_extreme_eigen_values(double &ev_lower, double &ev_upper) const; // mohan add 2011-01-13
81 
82  void reshape( const double nr_new, const double nc_new ); // Peize Lin add 2017-05-27
83 
84  double max() const; // Peize Lin add 2016-09-08
85  double max(int &ir, int &ic) const; // minyez add 2022-05-05
86  double min() const; // Peize Lin add 2016-09-08
87  double absmax() const; // Peize Lin add 2018-07-02
89  unsigned count_absle(double thres) const; // minyez add 2022-05-05
91  unsigned count_absge(double thres) const; // minyez add 2022-05-05
92  //double norm() const; // Peize Lin add 2018-08-12
93 };
94 
95 
96 matrix operator+(const matrix &m1, const matrix &m2);
97 matrix operator-(const matrix &m1, const matrix &m2);
98 matrix operator*(const matrix &m1, const matrix &m2);
99 matrix operator*(const double &s, const matrix &m);
100 matrix operator*(const matrix &m, const double &s);
101 
102 matrix transpose(const matrix &m);
103 double trace_on(const matrix &A, const matrix &B); // mohan add 2011-01-13
104 double mdot(const matrix &A, const matrix &B); // mohan add 2011-01-13
105 
106 std::ostream & operator<<( std::ostream & os, const matrix & m ); // Peize Lin add 2016-09-08
107 
109 matrix power_symat(matrix &mat, double power,
110  double threshold = -1e16); // Minye Zhang add 2022-06-04
111 
112 // minyez copied from Shi Rong's cal_periodic_chi0.cpp, for test use
113 void print_matrix(const char *desc, const matrix &mat);
114 
115 void print_matrix_mm(const matrix &mat, std::ostream &os, double threshold = 1e-15, bool row_first = true);
116 void print_matrix_mm(const matrix &mat, const std::string &fn, double threshold = 1e-15, bool row_first = true);
117 #endif // MATRIX_H
Definition: matrix.h:23
static const double EQUAL_THRES
Threshold to treat the matrix as equal, added by minyez 2022-05-06.
Definition: matrix.h:27
unsigned count_absle(double thres) const
count the matrix element whose abs value is less equal than threshold
Definition: matrix.cpp:440
unsigned count_absge(double thres) const
count the matrix element whose abs value is greater equal than threshold
Definition: matrix.cpp:448
matrix power_symat(matrix &mat, double power, double threshold=-1e16)
compute the power of a symmetric matrix. Symmetry not checked itself
Definition: matrix.cpp:457