FEMTIC
DoubleSparseMatrix.h
Go to the documentation of this file.
1 //-------------------------------------------------------------------------------------------------------
2 // The MIT License (MIT)
3 //
4 // Copyright (c) 2021 Yoshiya Usui
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in all
14 // copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 // SOFTWARE.
23 //-------------------------------------------------------------------------------------------------------.
24 #ifndef DBLDEF_DOUBLE_SPARSE_MATRIX
25 #define DBLDEF_DOUBLE_SPARSE_MATRIX
26 
27 #include <map>
28 
30 
31 public:
32 
33  //Default Constructer
34  explicit DoubleSparseMatrix();
35 
36  //Constructer
37  explicit DoubleSparseMatrix( const int nrows, const int ncols, const int nrhs = 1 );
38 
39  //Destructer
40  virtual ~DoubleSparseMatrix();
41 
42  // Set number of rows and columns
43  virtual void setNumRowsAndColumns( const int nrows, const int ncols );
44 
45  // Set matrix structure ( locations of non-zero components ) by triplet format
46  virtual void setStructureByTripletFormat( const int row, const int col );
47 
48  // Set matrix structure ( locations of non-zero components ) and add values by triplet format
49  virtual void setStructureAndAddValueByTripletFormat( const int row, const int col, const double val );
50 
51  //Convert matrix from triplet format to CRS format
52  void convertToCRSFormat();
53 
54  //Add non-zero value to matrix
55  //virtual void addNonZeroValues( const int row, const int col, const std::complex<double> val );
56  virtual void addNonZeroValues( const int row, const int col, const double val );
57 
58  //Zero clear non-zero values of matrix stored by CSR format
60 
61  //Add non-zero value to the right hand side vector
62  void addRightHandSideVector( const int row, const double val, const int irhs = 0 );
63 
64  //Zero clear non-zero values of the right hand side vector
66 
67  //Initialize matrix and right-hand side vectors
68  virtual void initializeMatrixAndRhsVectors( const int nrows, const int ncols, const int nrhs );
69 
70  // Get number of rows
71  int getNumRows() const;
72 
73  // Get number of columns
74  int getNumColumns() const;
75 
76  //Get total number of right hand side vector
77  int getNumRightHandSideVectors() const;
78 
79  //Return whether matrix has already been converted to CRS format
80  bool hasConvertedToCRSFormat() const;
81 
82  //Reallocate memory for right hand side vectors
83  void reallocateMemoryForRightHandSideVectors( const int nrhs );
84 
85  //Release memory
86  virtual void releaseMemory();
87 
89  //void multiplyMatrixByVectorAndAddResult( const double* vecIn, const int* convertArray, double* vecOut ) const;
90 
92  //void multiplyMatrixByVectorAndSubtractResult( const double* vecIn, const int* convertArray, double* vecOut ) const;
93 
94  //Copy right-hand side vector to another vector
95  void copyRhsVector( double* vecOut ) const;
96 
97  //Debug write the matrix componets
98  void debugWriteMatrix() const;
99 
100  //Debug write the componets of right hand side vector
101  void debugWriteRightHandSide() const;
102 
103  //Get row indices of the compressed row storage format
104  int getRowIndexCRS( const int iRow ) const;
105 
106  //Get column in which non-zero compnents exist
107  int getColumnsCRS( const int iNonZero ) const;
108 
109  //Get value of non-zero compnents
110  double getValueCRS( const int iNonZero ) const;
111 
112  //Get right hand side vector
113  double getRightHandSideVector( const int row, const int irhs = 0 ) const;
114 
115  // Calculate matrix-vector product of coefficient matrix and inputted vector
116  void calcMatrixVectorProduct( const double* invVec, double* outVec ) const;
117 
118  // Calculate matrix-vector product of transposed coefficient matrix and inputted vector
119  void calcMatrixVectorProductUsingTransposedMatrix( const double* invVec, double* outVec ) const;
120 
121 protected:
122  //Delete the matrix of triplet ( Coordinate ) format
123  void deleteTripletMatrix();
124 
125  //Total number of rows
127 
128  //Total number of columns
130 
131  //Total number of non-zero elements in the matrix
133 
134  //Total number of right hand side vectors
136 
137  //Flag indicating whether matrix has already been converted to CRS format
139 
140  //Matrix stored by triplet ( Coordinate ) format
141  std::map< int, double >* m_matrixTripletFormat;
142 
143  //Row indices of the compressed row storage format
144  long long int* m_rowIndex;
145 
146  //Columns in which non-zero compnents exist
147  long long int* m_columns;
148 
149  //Values of non-zero compnents
150  double* m_values;
151 
152  //Right hand side vector
154 
155 private:
156  //Copy constructer
157  DoubleSparseMatrix(const DoubleSparseMatrix &matrix );
158 
159  // Assignment operator
161 
162 };
163 
164 #endif
Definition: DoubleSparseMatrix.h:29
int getColumnsCRS(const int iNonZero) const
Definition: DoubleSparseMatrix.cpp:575
long long int * m_rowIndex
Definition: DoubleSparseMatrix.h:144
int getNumRows() const
Definition: DoubleSparseMatrix.cpp:421
virtual void setStructureByTripletFormat(const int row, const int col)
Definition: DoubleSparseMatrix.cpp:135
std::map< int, double > * m_matrixTripletFormat
Definition: DoubleSparseMatrix.h:141
bool hasConvertedToCRSFormat() const
Definition: DoubleSparseMatrix.cpp:436
void addRightHandSideVector(const int row, const double val, const int irhs=0)
Definition: DoubleSparseMatrix.cpp:325
void reallocateMemoryForRightHandSideVectors(const int nrhs)
Definition: DoubleSparseMatrix.cpp:477
int getNumColumns() const
Definition: DoubleSparseMatrix.cpp:426
void zeroClearNonZeroValues()
Definition: DoubleSparseMatrix.cpp:308
int m_numNonZeros
Definition: DoubleSparseMatrix.h:132
virtual void setNumRowsAndColumns(const int nrows, const int ncols)
Definition: DoubleSparseMatrix.cpp:103
virtual void addNonZeroValues(const int row, const int col, const double val)
Definition: DoubleSparseMatrix.cpp:261
double * m_values
Definition: DoubleSparseMatrix.h:150
void calcMatrixVectorProduct(const double *invVec, double *outVec) const
Definition: DoubleSparseMatrix.cpp:598
void deleteTripletMatrix()
Definition: DoubleSparseMatrix.cpp:633
virtual void setStructureAndAddValueByTripletFormat(const int row, const int col, const double val)
Definition: DoubleSparseMatrix.cpp:151
long long int * m_columns
Definition: DoubleSparseMatrix.h:147
void convertToCRSFormat()
Definition: DoubleSparseMatrix.cpp:172
double getValueCRS(const int iNonZero) const
Definition: DoubleSparseMatrix.cpp:582
virtual void releaseMemory()
Definition: DoubleSparseMatrix.cpp:441
void zeroClearRightHandSideVector()
Definition: DoubleSparseMatrix.cpp:346
int getRowIndexCRS(const int iRow) const
Definition: DoubleSparseMatrix.cpp:568
double getRightHandSideVector(const int row, const int irhs=0) const
Definition: DoubleSparseMatrix.cpp:589
double * m_rightHandSideVector
Definition: DoubleSparseMatrix.h:153
int m_numRightHandSideVectors
Definition: DoubleSparseMatrix.h:135
void debugWriteRightHandSide() const
Definition: DoubleSparseMatrix.cpp:557
virtual ~DoubleSparseMatrix()
Definition: DoubleSparseMatrix.cpp:70
int m_numRows
Definition: DoubleSparseMatrix.h:126
DoubleSparseMatrix & operator=(const DoubleSparseMatrix &rhs)
Definition: DoubleSparseMatrix.cpp:652
void copyRhsVector(double *vecOut) const
Definition: DoubleSparseMatrix.cpp:529
int getNumRightHandSideVectors() const
Definition: DoubleSparseMatrix.cpp:431
bool m_hasConvertedToCRSFormat
Definition: DoubleSparseMatrix.h:138
virtual void initializeMatrixAndRhsVectors(const int nrows, const int ncols, const int nrhs)
Definition: DoubleSparseMatrix.cpp:356
DoubleSparseMatrix()
Definition: DoubleSparseMatrix.cpp:32
void debugWriteMatrix() const
Definition: DoubleSparseMatrix.cpp:540
int m_numColumns
Definition: DoubleSparseMatrix.h:129
void calcMatrixVectorProductUsingTransposedMatrix(const double *invVec, double *outVec) const
Definition: DoubleSparseMatrix.cpp:613