ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
sparse_matrix.h
Go to the documentation of this file.
1#ifndef SPARSE_MATRIX_H
2#define SPARSE_MATRIX_H
3
4#include <iostream>
5#include <map>
6#include <utility>
7#include <vector>
8
9namespace ModuleIO
10{
11
22template <typename T>
24{
25 public:
26 // Default constructor
28 {
29 }
30
31 SparseMatrix(int rows, int cols) : _rows(rows), _cols(cols)
32 {
33 }
34
35 // add value to the matrix with row and column indices
36 void insert(int row, int col, T value);
37
38 // print data in CSR (Compressed Sparse Row) format
39 void printToCSR(std::ostream& ofs, int precision = 8);
40
41 // read CSR data from arrays
42 void readCSR(const std::vector<T>& values, const std::vector<int>& col_ind, const std::vector<int>& row_ptr);
43
44 // set number of rows
45 void setRows(int rows)
46 {
47 _rows = rows;
48 }
49
50 // set number of columns
51 void setCols(int cols)
52 {
53 _cols = cols;
54 }
55
56 // get number of rows
57 int getRows() const
58 {
59 return _rows;
60 }
61
62 // get number of columns
63 int getCols() const
64 {
65 return _cols;
66 }
67
68 // define the operator to index a matrix element
69 T operator()(int row, int col)const;
70
71 // set the threshold
72 void setSparseThreshold(double sparse_threshold)
73 {
74 _sparse_threshold = sparse_threshold;
75 }
76
77 // get the threshold
78 double getSparseThreshold() const
79 {
80 return _sparse_threshold;
81 }
82
83 // get the number of non-zero elements
84 int getNNZ() const
85 {
86 return elements.size();
87 }
88
89 // get elements
90 const std::map<std::pair<int, int>, T>& getElements() const
91 {
92 return elements;
93 }
94
95 private:
96 int _rows;
97 int _cols;
98 std::map<std::pair<int, int>, T> elements;
99 double _sparse_threshold = 1.0e-10;
100}; // class SparseMatrix
101
102} // namespace ModuleIO
103
104#endif // SPARSE_MATRIX_H
Sparse matrix class designed mainly for csr format input and output.
Definition sparse_matrix.h:24
int _cols
Definition sparse_matrix.h:97
const std::map< std::pair< int, int >, T > & getElements() const
Definition sparse_matrix.h:90
T operator()(int row, int col) const
Definition sparse_matrix.cpp:95
std::map< std::pair< int, int >, T > elements
Definition sparse_matrix.h:98
SparseMatrix(int rows, int cols)
Definition sparse_matrix.h:31
SparseMatrix()
Definition sparse_matrix.h:27
void setRows(int rows)
Definition sparse_matrix.h:45
int _rows
Definition sparse_matrix.h:96
int getRows() const
Definition sparse_matrix.h:57
void setSparseThreshold(double sparse_threshold)
Definition sparse_matrix.h:72
void readCSR(const std::vector< T > &values, const std::vector< int > &col_ind, const std::vector< int > &row_ptr)
Read CSR data from arrays.
Definition sparse_matrix.cpp:70
void setCols(int cols)
Definition sparse_matrix.h:51
int getCols() const
Definition sparse_matrix.h:63
double _sparse_threshold
Definition sparse_matrix.h:99
int getNNZ() const
Definition sparse_matrix.h:84
double getSparseThreshold() const
Definition sparse_matrix.h:78
void insert(int row, int col, T value)
Add value to the matrix with row and column indices.
Definition sparse_matrix.cpp:15
void printToCSR(std::ostream &ofs, int precision=8)
Print to CSR format.
Definition sparse_matrix.cpp:31
#define T
Definition exp.cpp:237
This class has two functions: restart psi from the previous calculation, and write psi to the disk.
Definition cal_dos.h:9