| 1 |
/*! \file SparseMatrix.hpp |
| 2 |
\brief Sparse matrix definition |
| 3 |
\author Xavier Juvigny, ONERA |
| 4 |
\date Jan. 25th 2005 |
| 5 |
\modification allocation of array by STL vector class |
| 6 |
\author Atsushi Suzuki, Laboratoire Jacques-Louis Lions |
| 7 |
\date Jun. 11th 2013 |
| 8 |
\date Jul. 12th 2015 |
| 9 |
\date Nov. 30th 2016 |
| 10 |
*/ |
| 11 |
|
| 12 |
// This file is part of Dissection |
| 13 |
// |
| 14 |
// Dissection is free software: you can redistribute it and/or modify |
| 15 |
// it under the terms of the GNU General Public License as published by |
| 16 |
// the Free Software Foundation, either version 3 of the License, or |
| 17 |
// (at your option) any later version. |
| 18 |
// |
| 19 |
// Linking Dissection statically or dynamically with other modules is making |
| 20 |
// a combined work based on Disssection. Thus, the terms and conditions of |
| 21 |
// the GNU General Public License cover the whole combination. |
| 22 |
// |
| 23 |
// As a special exception, the copyright holders of Dissection give you |
| 24 |
// permission to combine Dissection program with free software programs or |
| 25 |
// libraries that are released under the GNU LGPL and with independent modules |
| 26 |
// that communicate with Dissection solely through the Dissection-fortran |
| 27 |
// interface. You may copy and distribute such a system following the terms of |
| 28 |
// the GNU GPL for Dissection and the licenses of the other code concerned, |
| 29 |
// provided that you include the source code of that other code when and as |
| 30 |
// the GNU GPL requires distribution of source code and provided that you do |
| 31 |
// not modify the Dissection-fortran interface. |
| 32 |
// |
| 33 |
// Note that people who make modified versions of Dissection are not obligated |
| 34 |
// to grant this special exception for their modified versions; it is their |
| 35 |
// choice whether to do so. The GNU General Public License gives permission to |
| 36 |
// release a modified version without this exception; this exception also makes |
| 37 |
// it possible to release a modified version which carries forward this |
| 38 |
// exception. If you modify the Dissection-fortran interface, this exception |
| 39 |
// does not apply to your modified version of Dissection, and you must remove |
| 40 |
// this exception when you distribute your modified version. |
| 41 |
// |
| 42 |
// This exception is an additional permission under section 7 of the GNU |
| 43 |
// General Public License, version 3 ("GPLv3") |
| 44 |
// |
| 45 |
// Dissection is distributed in the hope that it will be useful, |
| 46 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 47 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 48 |
// GNU General Public License for more details. |
| 49 |
// |
| 50 |
// You should have received a copy of the GNU General Public License |
| 51 |
// along with Dissection. If not, see <http://www.gnu.org/licenses/>. |
| 52 |
// |
| 53 |
|
| 54 |
#ifndef _ALGEBRA_SPARSEMATRIX_HPP_ |
| 55 |
#define _ALGEBRA_SPARSEMATRIX_HPP_ |
| 56 |
|
| 57 |
#include <vector> |
| 58 |
#include "Compiler/blas.hpp" |
| 59 |
|
| 60 |
using std::vector; |
| 61 |
|
| 62 |
template<typename T> |
| 63 |
class SparseMatrix |
| 64 |
{ |
| 65 |
public: |
| 66 |
|
| 67 |
SparseMatrix() : _isSymmetric(false), _isUpper(true), _isWhole(false) {} |
| 68 |
|
| 69 |
SparseMatrix(bool isSym, bool isUpper = true, bool isWhole = false) : |
| 70 |
_isSymmetric(isSym), _isUpper(isUpper), _isWhole(isWhole) |
| 71 |
{} |
| 72 |
|
| 73 |
SparseMatrix( int n, int nnz, const T* coefs, |
| 74 |
bool isSym = false, |
| 75 |
bool isUpper = true, |
| 76 |
bool isWhole = false) : |
| 77 |
_isSymmetric(isSym), _isUpper(isUpper), _isWhole(isWhole) |
| 78 |
{ |
| 79 |
_ptRows.resize(n+1); |
| 80 |
_indCols.resize(nnz); |
| 81 |
_coefs.resize(nnz); |
| 82 |
for (int i = 0 ; i < nnz; i++) { |
| 83 |
_coefs[i] = coefs[i]; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
// added by Atsushi, 01 Feb.2013 : symbolic data are given by integer arrays |
| 88 |
SparseMatrix( int n, int nnz, const int *ptRows, const int *indCols, |
| 89 |
bool isSym = false, bool isUpper = true, |
| 90 |
bool isWhole = false) : |
| 91 |
_isSymmetric(isSym), |
| 92 |
_isUpper(isUpper), |
| 93 |
_isWhole(isWhole) |
| 94 |
{ |
| 95 |
_ptRows.resize(n+1); |
| 96 |
for (int i = 0; i < (n + 1); i++) { |
| 97 |
_ptRows[i] = ptRows[i]; |
| 98 |
} |
| 99 |
_indCols.resize(nnz); |
| 100 |
for (int i = 0; i < nnz; i++) { |
| 101 |
_indCols[i] = indCols[i]; |
| 102 |
} |
| 103 |
_coefs.resize(nnz); |
| 104 |
} |
| 105 |
|
| 106 |
SparseMatrix( int n, int nnz, |
| 107 |
const long long int *ptRows64, |
| 108 |
const long long int *indCols64, |
| 109 |
bool isSym = false, bool isUpper = true, |
| 110 |
bool isWhole = false) : |
| 111 |
_isSymmetric(isSym), |
| 112 |
_isUpper(isUpper), |
| 113 |
_isWhole(isWhole) |
| 114 |
{ |
| 115 |
_ptRows.resize(n+1); |
| 116 |
for (int i = 0; i < (n + 1); i++) { |
| 117 |
_ptRows[i] = ptRows64[i]; |
| 118 |
} |
| 119 |
_indCols.resize(nnz); |
| 120 |
for (int i = 0; i < nnz; i++) { |
| 121 |
_indCols[i] = indCols64[i]; |
| 122 |
} |
| 123 |
_coefs.resize(nnz); |
| 124 |
} |
| 125 |
|
| 126 |
SparseMatrix( int n, int nnz, const vector<int>& ptRows, |
| 127 |
bool isSym = false, |
| 128 |
bool isUpper = true, |
| 129 |
bool isWhole = false) : |
| 130 |
_isSymmetric(isSym), _isUpper(isUpper), _isWhole(isWhole) |
| 131 |
{ |
| 132 |
_indCols.resize(nnz); |
| 133 |
_ptRows.resize(n + 1); |
| 134 |
for (int i = 0; i < (n + 1); i++) { |
| 135 |
_ptRows[i] = ptRows[i]; |
| 136 |
} |
| 137 |
_coefs.resize(nnz); |
| 138 |
} |
| 139 |
|
| 140 |
SparseMatrix( const SparseMatrix& A ) : |
| 141 |
_ptRows(A._ptRows), _indCols(A._indCols), _coefs(A._coefs), |
| 142 |
_isSymmetric(A._isSymmetric), _isUpper(A._isUpper), _isWhole(A._isWhole) |
| 143 |
{ } |
| 144 |
|
| 145 |
~SparseMatrix() { free(); } |
| 146 |
|
| 147 |
void free() { |
| 148 |
_ptRows.clear(); |
| 149 |
_indCols.clear(); |
| 150 |
_coefs.clear(); |
| 151 |
} |
| 152 |
|
| 153 |
inline int dimension() const { return (_ptRows.size() - 1); } |
| 154 |
|
| 155 |
inline int nnz() const { return _indCols.size(); } |
| 156 |
|
| 157 |
inline bool isSymmetric() const { return _isSymmetric; } |
| 158 |
|
| 159 |
inline bool isUpper() const { return _isUpper; } |
| 160 |
|
| 161 |
inline bool isWhole() const { return _isWhole; } |
| 162 |
|
| 163 |
// |
| 164 |
inline int *getRows() { return &_ptRows[0]; } |
| 165 |
// |
| 166 |
inline int *getIndCols() { return &_indCols[0]; } |
| 167 |
|
| 168 |
inline T* getCoef() { return &_coefs[0]; } |
| 169 |
|
| 170 |
inline T* getCoef() const { return &_coefs[0]; } |
| 171 |
|
| 172 |
inline int ptRow(int i) const { return _ptRows[i]; } |
| 173 |
|
| 174 |
inline int indCol(int i) const { return _indCols[i]; } |
| 175 |
|
| 176 |
inline int& indCol(int i) { return _indCols[i]; } |
| 177 |
|
| 178 |
inline const T Coef(int i) const { return _coefs[i]; } |
| 179 |
|
| 180 |
inline T& Coef(int i) { return _coefs[i]; } |
| 181 |
|
| 182 |
inline vector<int>& ptRows() { return _ptRows; } |
| 183 |
inline vector<int>& indCols() { return _indCols; } |
| 184 |
inline vector<T> & coefs() { return _coefs; } |
| 185 |
|
| 186 |
void prod(const T *u, T *v) const; |
| 187 |
void prodt(const T *u, T *v ) const; |
| 188 |
|
| 189 |
// void normalize(const int type, const W* coefs0, Z* precDiag); |
| 190 |
|
| 191 |
void extractSquareMatrix(T *DSsingCofes, vector<int> &singVal); |
| 192 |
|
| 193 |
|
| 194 |
SparseMatrix<T>* PartialCopyCSR(vector<int> &permute, const int n, |
| 195 |
bool transposed); |
| 196 |
|
| 197 |
private: |
| 198 |
SparseMatrix& operator = ( const SparseMatrix& A ); |
| 199 |
vector<int> _ptRows; |
| 200 |
vector<int> _indCols; |
| 201 |
vector<T> _coefs; |
| 202 |
bool _isSymmetric; |
| 203 |
bool _isUpper; |
| 204 |
bool _isWhole; |
| 205 |
}; // End class SparseMatrix |
| 206 |
|
| 207 |
template<typename T, typename U> void |
| 208 |
normalize(const int type, const T* coefs0, SparseMatrix<T> *ptDA, U* u); |
| 209 |
|
| 210 |
|
| 211 |
int CSR_sym2unsym(int *ptRows, int *indCols, int *toSym, |
| 212 |
const int *ptSymRows, const int *indSymCols, |
| 213 |
const int dim, const bool upper_flag); |
| 214 |
|
| 215 |
#endif |