PolyDiM
C++ library for POLYtopal DIscretization Methods
Loading...
Searching...
No Matches
Eigen_SparseArray.hpp
Go to the documentation of this file.
1// _LICENSE_HEADER_
2//
3// Copyright (C) 2019 - 2025.
4// Terms register on the GPL-3.0 license.
5//
6// This file can be redistributed and/or modified under the license terms.
7//
8// See top level LICENSE file for more details.
9//
10// This file can be used citing references in CITATION.cff file.
11
12#ifndef __Eigen_SparseArray_HPP
13#define __Eigen_SparseArray_HPP
14
15#include "Eigen/Eigen"
16#include "IOUtilities.hpp"
17#include "ISparseArray.hpp"
18#include "LAPACK_utilities.hpp"
19
20#if ENABLE_SUITESPARSE == 1
22#endif
23
24namespace Gedim
25{
27template <typename Eigen_ArrayType = Eigen::VectorXd, typename Eigen_SparseArrayType = Eigen::SparseMatrix<double>>
29{
30 private:
31 Eigen_SparseArrayType _matrix;
32 SparseArrayTypes _matrixType;
33 std::list<Eigen::Triplet<double>> _triplets;
34
35 public:
37 {
38 _matrixType = SparseArrayTypes::None;
39 }
41 {
42 Reset();
43 }
44
46 {
47 _matrix = matrix;
48 _matrixType = type;
49 }
50
52 {
53 return _matrix;
54 }
55 operator const Eigen_SparseArrayType &() const
56 {
57 return _matrix;
58 }
64 {
66 }
67 inline const Eigen_SparseArrayType &Cast(const ISparseArray &v) const
68 {
70 }
71
72 inline void SetSize(const unsigned int &numRows, const unsigned int &numCols, const SparseArrayTypes &type = SparseArrayTypes::None)
73 {
74 _matrix.resize(numRows, numCols);
75 _matrixType = type;
76 }
77
78 inline SparseArrayTypes Type() const
79 {
80 return _matrixType;
81 }
82
83 void Create();
84 inline void Destroy()
85 {
86 Reset();
87 }
88 inline void Flush()
89 {
90 }
91 inline void Reset()
92 {
93 _triplets.clear();
94 _matrix.prune(0.0);
95 }
96
97 void Triplet(const unsigned int &i, const unsigned int &j, const double &value);
98
99 void Triplets(const std::vector<unsigned int> &i, const std::vector<unsigned int> &j, const std::vector<double> &values);
100
101 inline std::ostream &Print(std::ostream &output) const
102 {
103 return output << _matrix;
104 }
105
107 {
109 _matrix += Cast(A);
110 return *this;
111 }
113 {
115 _matrix -= Cast(A);
116 return *this;
117 }
118 inline ISparseArray &operator*=(const double &c)
119 {
120 _matrix *= c;
121 return *this;
122 }
123 inline ISparseArray &operator/=(const double &c)
124 {
125 _matrix /= c;
126 return *this;
127 }
128
129 inline void Copy(const ISparseArray &A)
130 {
131 _matrix = Cast(A);
132 }
133
134 inline unsigned int rows() const
135 {
136 return _matrix.rows();
137 }
138
139 inline unsigned int cols() const
140 {
141 return _matrix.cols();
142 }
143
149
150 void ToBinaryFile(const std::string &filePath, const bool &append = false) const;
151
152 inline double Norm() const
153 {
154 return _matrix.norm();
155 }
156
158 {
159 switch (algorithm)
160 {
162 return LAPACK_utilities::cond(LAPACK_utilities::svd(Eigen::MatrixXd(_matrix)));
164 return 1.0 / LAPACK_utilities::rcondest(_matrix);
165#if ENABLE_SUITESPARSE == 1
167 return Gedim::SuiteSparse_Utilities::condest(_matrix);
168#endif
169 default:
170 throw std::runtime_error("Not valid method to compute the condition number.");
171 }
172 }
173
174 inline unsigned int NonZeros() const
175 {
176 return _matrix.nonZeros();
177 }
178};
179} // namespace Gedim
180
181#endif // __Eigen_SparseArray_HPP
Eigen column vector.
Definition Eigen_Array.hpp:23
Eigen sparse array.
Definition Eigen_SparseArray.hpp:29
ISparseArray & operator/=(const double &c)
Definition Eigen_SparseArray.hpp:123
void SetSize(const unsigned int &numRows, const unsigned int &numCols, const SparseArrayTypes &type=SparseArrayTypes::None)
Resize the Matrix.
Definition Eigen_SparseArray.hpp:72
void Copy(const ISparseArray &A)
Definition Eigen_SparseArray.hpp:129
void Destroy()
Definition Eigen_SparseArray.hpp:84
unsigned int NonZeros() const
Definition Eigen_SparseArray.hpp:174
void Triplet(const unsigned int &i, const unsigned int &j, const double &value)
Given the row index i and the column index j the value val is put into the Matrix in ADD or INSERT mo...
Definition Eigen_SparseArray.cpp:32
Eigen_SparseArray(const Eigen_SparseArrayType &matrix, const SparseArrayTypes &type=SparseArrayTypes::None)
Definition Eigen_SparseArray.hpp:45
Eigen_SparseArray()
Definition Eigen_SparseArray.hpp:36
Eigen_SparseArray< Eigen_ArrayType, Eigen_SparseArrayType > & operator=(Eigen_SparseArrayType &&matrix)
Definition Eigen_SparseArray.hpp:144
std::ostream & Print(std::ostream &output) const
Print the array.
Definition Eigen_SparseArray.hpp:101
const Eigen_SparseArrayType & Cast(const ISparseArray &v) const
Definition Eigen_SparseArray.hpp:67
void Create()
Matrix Allocation is implemented in child classes.
Definition Eigen_SparseArray.cpp:24
unsigned int rows() const
Definition Eigen_SparseArray.hpp:134
void ToBinaryFile(const std::string &filePath, const bool &append=false) const
Write sparse array to binary file.
Definition Eigen_SparseArray.cpp:74
unsigned int cols() const
Definition Eigen_SparseArray.hpp:139
~Eigen_SparseArray()
Definition Eigen_SparseArray.hpp:40
void Reset()
Put zero-values in the Matrix.
Definition Eigen_SparseArray.hpp:91
const Eigen_SparseArrayType & Cast(const ISparseArray &v)
Definition Eigen_SparseArray.hpp:63
Eigen_SparseArrayType & Cast(ISparseArray &v)
Definition Eigen_SparseArray.hpp:59
ISparseArray & operator-=(const ISparseArray &A)
Definition Eigen_SparseArray.hpp:112
SparseArrayTypes Type() const
Definition Eigen_SparseArray.hpp:78
void Triplets(const std::vector< unsigned int > &i, const std::vector< unsigned int > &j, const std::vector< double > &values)
Given the row indices i and the column indices j the values val are put into the Matrix in ADD or INS...
Definition Eigen_SparseArray.cpp:61
void Flush()
Intermediate flush of the Matrix during creation.
Definition Eigen_SparseArray.hpp:88
double Cond(const ISparseArray::ConditionNumberAlgorithm &algorithm=ISparseArray::ConditionNumberAlgorithm::SVDLapack) const
Definition Eigen_SparseArray.hpp:157
ISparseArray & operator+=(const ISparseArray &A)
Definition Eigen_SparseArray.hpp:106
double Norm() const
Definition Eigen_SparseArray.hpp:152
ISparseArray & operator*=(const double &c)
Definition Eigen_SparseArray.hpp:118
Interface used for sparse Array of double.
Definition ISparseArray.hpp:25
SparseArrayTypes
Definition ISparseArray.hpp:28
ConditionNumberAlgorithm
Definition ISparseArray.hpp:37
virtual SparseArrayTypes Type() const =0
static void Assert(const bool &logicResult)
Assert for all code, generate exception if something goes wrong.
Definition IOUtilities.hpp:126
Definition Eigen_Array.cpp:22
double cond(const Eigen::VectorXd &s)
Compute condition number in norm 2 given singular values.
Definition LAPACK_utilities.hpp:38
double rcondest(const Eigen::SparseMatrix< double > &sparseA)
Definition LAPACK_utilities.cpp:117
void svd(Eigen::MatrixXd A, Eigen::MatrixXd &V, Eigen::VectorXd &S)
Given A = U * S * V' returns only S and V'.
Definition LAPACK_utilities.cpp:166