G+Smo  25.01.0
Geometry + Simulation Modules
 
Loading...
Searching...
No Matches
gsMatrixOp.h
Go to the documentation of this file.
1
13#pragma once
14
17
18namespace gismo
19{
20
21// left here for debugging purposes
22// template<typename T> struct is_ref { static const bool value = false; };
23// template<typename T> struct is_ref<T&> { static const bool value = true; };
24
32template <class MatrixType>
33class gsMatrixOp GISMO_FINAL : public gsLinearOperator<typename MatrixType::Scalar>
34{
35 typedef memory::shared_ptr<MatrixType> MatrixPtr;
36 typedef typename MatrixType::Nested NestedMatrix;
37
38public:
39 typedef typename MatrixType::Scalar T;
40
42 typedef memory::shared_ptr<gsMatrixOp> Ptr;
43
45 typedef memory::unique_ptr<gsMatrixOp> uPtr;
46
52 gsMatrixOp(const MatrixType& mat)
53 : m_mat(), m_expr(mat.derived())
54 {
55 //gsDebug<<typeid(m_expr).name()<<" Ref: "<<is_ref<NestedMatrix>::value<<"\n";
56 }
57
59 gsMatrixOp(MatrixPtr mat)
60 : m_mat(give(mat)), m_expr(m_mat->derived())
61 { }
62
67 static uPtr make(const MatrixType& mat)
68 { return uPtr( new gsMatrixOp(mat) ); }
69
71 static uPtr make(MatrixPtr mat)
72 { return uPtr( new gsMatrixOp(give(mat)) ); }
73
74 void apply(const gsMatrix<T> & input, gsMatrix<T> & x) const
75 { x.noalias() = m_expr * input; }
76
77 index_t rows() const
78 { return m_expr.rows(); }
79
80 index_t cols() const
81 { return m_expr.cols(); }
82
84 NestedMatrix matrix() const
85 { return m_expr; }
86
88 MatrixPtr matrixPtr() const {
89 GISMO_ENSURE( m_mat, "A shared pointer is only available if it was provided to gsMatrixOp." );
90 return m_mat;
91 }
92
93private:
94 const MatrixPtr m_mat;
95 NestedMatrix m_expr;
96};
97
125template <class Derived>
126typename gsMatrixOp<Derived>::uPtr makeMatrixOp(const gsEigen::EigenBase<Derived>& mat)
127{
128 return gsMatrixOp<Derived>::make(mat.derived());
129}
130
152template <class Derived>
153typename gsMatrixOp<Derived>::uPtr makeMatrixOp(memory::shared_ptr<Derived> mat)
154{
156}
157
158// We need an additional guide for the compiler to be able to work well with unique ptrs
159template <class Derived>
160typename gsMatrixOp<Derived>::uPtr makeMatrixOp(memory::unique_ptr<Derived> mat)
161{
162 return memory::make_unique(new gsMatrixOp<Derived>(memory::shared_ptr<Derived>(mat.release())));
163}
164
170template <class SolverType>
171class gsSolverOp GISMO_FINAL : public gsLinearOperator<typename SolverType::Scalar>
172{
173public:
174 typedef typename SolverType::Scalar T;
175
176 typedef typename SolverType::MatrixType MatrixType;
177
179 typedef memory::shared_ptr<gsSolverOp> Ptr;
180
182 typedef memory::unique_ptr<gsSolverOp> uPtr;
183
184
186 gsSolverOp(const MatrixType& mat)
187 {
188 GISMO_ASSERT(mat.rows() == mat.cols(), "Need square matrix");
189 m_size = mat.rows();
190
191 m_solver.compute(mat);
192 }
193
195 gsSolverOp(const memory::shared_ptr<MatrixType>& mat)
196 {
197 GISMO_ASSERT(mat->rows() == mat->cols(), "Need square matrix");
198 m_size = mat->rows();
199
200 m_solver.compute(*mat);
201 }
202
204 static uPtr make(const MatrixType& mat) { return memory::make_unique( new gsSolverOp(mat) ); }
205
206 void apply(const gsMatrix<T> & input, gsMatrix<T> & x) const
207 {
208 x = m_solver.solve(input);
209 }
210
211 index_t rows() const { return m_size; }
212
213 index_t cols() const { return m_size; }
214
216 SolverType& solver() { return m_solver; }
217
219 const SolverType& solver() const { return m_solver; }
220
221private:
222 SolverType m_solver;
223 index_t m_size;
224};
225
226
231template <class T, int _Rows, int _Cols, int _Opt>
233{
234 return memory::make_unique( new gsSolverOp< gsEigen::PartialPivLU< gsEigen::Matrix<T, _Rows, _Cols, _Opt> > >(mat) );
235}
236
242template <class T, int _Rows, int _Cols, int _Opt>
244{
245 return memory::make_unique( new gsSolverOp< gsEigen::PartialPivLU< gsEigen::Matrix<T, _Rows, _Cols, _Opt> > >(mat) );
246}
247
248
253template <class T, int _Rows, int _Cols, int _Opt>
255{
256 return memory::make_unique( new gsSolverOp< gsEigen::FullPivLU< gsEigen::Matrix<T, _Rows, _Cols, _Opt> > >(mat) );
257}
258
264template <class T, int _Rows, int _Cols, int _Opt>
266{
267 return memory::make_unique( new gsSolverOp< gsEigen::FullPivLU< gsEigen::Matrix<T, _Rows, _Cols, _Opt> > >(mat) );
268}
269
270
278template <class T, int _Rows, int _Cols, int _Opt>
280{
281 return memory::make_unique( new gsSolverOp< gsEigen::LDLT< gsEigen::Matrix<T, _Rows, _Cols, _Opt> > >(mat) );
282}
283
292template <class T, int _Rows, int _Cols, int _Opt>
294{
295 return memory::make_unique( new gsSolverOp< gsEigen::LDLT< gsEigen::Matrix<T, _Rows, _Cols, _Opt> > >(mat) );
296}
297
298
305template <typename T, int _Opt, typename _Index>
307{
308 return memory::make_unique( new gsSolverOp< typename gsSparseSolver<T>::LU >(mat) );
309}
310
317template <typename T, int _Opt, typename _Index>
319{
320 return memory::make_unique( new gsSolverOp< typename gsSparseSolver<T>::LU >(mat) );
321}
322
323
330template <typename T, int _Opt, typename _Index>
332{
333 return memory::make_unique( new gsSolverOp< typename gsSparseSolver<T>::QR >(mat) );
334}
335
342template <typename T, int _Opt, typename _Index>
344{
345 return memory::make_unique( new gsSolverOp< typename gsSparseSolver<T>::QR >(mat) );
346}
347
348
356template <typename T, int _Opt, typename _Index>
358{
359 return memory::make_unique( new gsSolverOp<typename gsSparseSolver<T>::SimplicialLDLT >(mat) );
360}
361
369template <typename T, int _Opt, typename _Index>
371{
372 return memory::make_unique( new gsSolverOp<typename gsSparseSolver<T>::SimplicialLDLT >(mat) );
373}
374
375
376} // namespace gismo
Simple abstract class for discrete operators.
Definition gsLinearOperator.h:29
Simple adapter class to use a matrix (or matrix-like object) as a linear operator....
Definition gsMatrixOp.h:34
index_t rows() const
Returns the number of rows of the operator.
Definition gsMatrixOp.h:77
gsMatrixOp< Derived >::uPtr makeMatrixOp(const gsEigen::EigenBase< Derived > &mat)
This essentially just calls the gsMatrixOp constructor, but the use of a template functions allows us...
Definition gsMatrixOp.h:126
gsMatrixOp(const MatrixType &mat)
Constructor taking a reference.
Definition gsMatrixOp.h:52
const MatrixPtr m_mat
Shared pointer to matrix (if needed)
Definition gsMatrixOp.h:94
static uPtr make(const MatrixType &mat)
Make function returning a smart pointer.
Definition gsMatrixOp.h:67
gsMatrixOp(MatrixPtr mat)
Constructor taking a shared pointer.
Definition gsMatrixOp.h:59
gsMatrixOp< Derived >::uPtr makeMatrixOp(memory::shared_ptr< Derived > mat)
This essentially just calls the gsMatrixOp constructor, but the use of a template functions allows us...
Definition gsMatrixOp.h:153
NestedMatrix m_expr
Nested Eigen expression.
Definition gsMatrixOp.h:95
MatrixPtr matrixPtr() const
Returns a shared pinter to the matrix.
Definition gsMatrixOp.h:88
NestedMatrix matrix() const
Returns the matrix.
Definition gsMatrixOp.h:84
memory::unique_ptr< gsMatrixOp > uPtr
Unique pointer for gsMatrixOp.
Definition gsMatrixOp.h:45
void apply(const gsMatrix< T > &input, gsMatrix< T > &x) const
apply the operator on the input vector and store the result in x
Definition gsMatrixOp.h:74
static uPtr make(MatrixPtr mat)
Make function returning a smart pointer.
Definition gsMatrixOp.h:71
memory::shared_ptr< gsMatrixOp > Ptr
Shared pointer for gsMatrixOp.
Definition gsMatrixOp.h:42
index_t cols() const
Returns the number of columns of the operator.
Definition gsMatrixOp.h:80
A matrix with arbitrary coefficient type and fixed or dynamic size.
Definition gsMatrix.h:41
Simple adapter class to use an Eigen solver (having a compute() and a solve() method) as a linear ope...
Definition gsMatrixOp.h:172
const SolverType & solver() const
Access the solver class.
Definition gsMatrixOp.h:219
index_t rows() const
Returns the number of rows of the operator.
Definition gsMatrixOp.h:211
gsSolverOp< typenamegsSparseSolver< T >::LU >::uPtr makeSparseLUSolver(const memory::shared_ptr< gsSparseMatrix< T, _Opt, _Index > > &mat)
Convenience function to create a sparse LU solver as a gsLinearOperator taking a shared pointer.
Definition gsMatrixOp.h:318
gsSolverOp< typenamegsSparseSolver< T >::SimplicialLDLT >::uPtr makeSparseCholeskySolver(const memory::shared_ptr< gsSparseMatrix< T, _Opt, _Index > > &mat)
Convenience function to create a sparse Cholesky (simplicial LDL^T) solver as a gsLinearOperator.
Definition gsMatrixOp.h:370
gsSolverOp< gsEigen::LDLT< gsEigen::Matrix< T, _Rows, _Cols, _Opt > > >::uPtr makeCholeskySolver(const gsMatrix< T, _Rows, _Cols, _Opt > &mat)
Convenience function to create a Cholesky (LDL^T) solver (for dense matrices) as a gsLinearOperator.
Definition gsMatrixOp.h:279
gsSolverOp< gsEigen::FullPivLU< gsEigen::Matrix< T, _Rows, _Cols, _Opt > > >::uPtr makeFullPivLUSolver(const memory::shared_ptr< gsMatrix< T, _Rows, _Cols, _Opt > > &mat)
Convenience function to create an LU solver with full pivoting (for dense matrices) as a gsLinearOper...
Definition gsMatrixOp.h:265
static uPtr make(const MatrixType &mat)
Make function taking a matrix OR a shared pointer.
Definition gsMatrixOp.h:204
gsSolverOp(const memory::shared_ptr< MatrixType > &mat)
Constructor taking a shared pointer.
Definition gsMatrixOp.h:195
gsSolverOp< typenamegsSparseSolver< T >::LU >::uPtr makeSparseLUSolver(const gsSparseMatrix< T, _Opt, _Index > &mat)
Convenience function to create a sparse LU solver as a gsLinearOperator.
Definition gsMatrixOp.h:306
gsSolverOp< typenamegsSparseSolver< T >::SimplicialLDLT >::uPtr makeSparseCholeskySolver(const gsSparseMatrix< T, _Opt, _Index > &mat)
Convenience function to create a sparse Cholesky (simplicial LDL^T) solver as a gsLinearOperator.
Definition gsMatrixOp.h:357
gsSolverOp< typenamegsSparseSolver< T >::QR >::uPtr makeSparseQRSolver(const gsSparseMatrix< T, _Opt, _Index > &mat)
Convenience function to create a sparse QR solver as a gsLinearOperator.
Definition gsMatrixOp.h:331
gsSolverOp< typenamegsSparseSolver< T >::QR >::uPtr makeSparseQRSolver(const memory::shared_ptr< gsSparseMatrix< T, _Opt, _Index > > &mat)
Convenience function to create a sparse QR solver as a gsLinearOperator taking a shared pointer.
Definition gsMatrixOp.h:343
gsSolverOp(const MatrixType &mat)
Constructor taking a matrix.
Definition gsMatrixOp.h:186
gsSolverOp< gsEigen::PartialPivLU< gsEigen::Matrix< T, _Rows, _Cols, _Opt > > >::uPtr makePartialPivLUSolver(const gsMatrix< T, _Rows, _Cols, _Opt > &mat)
Convenience function to create an LU solver with partial pivoting (for dense matrices) as a gsLinearO...
Definition gsMatrixOp.h:232
void apply(const gsMatrix< T > &input, gsMatrix< T > &x) const
apply the operator on the input vector and store the result in x
Definition gsMatrixOp.h:206
index_t cols() const
Returns the number of columns of the operator.
Definition gsMatrixOp.h:213
SolverType & solver()
Access the solver class.
Definition gsMatrixOp.h:216
gsSolverOp< gsEigen::PartialPivLU< gsEigen::Matrix< T, _Rows, _Cols, _Opt > > >::uPtr makePartialPivLUSolver(const memory::shared_ptr< gsMatrix< T, _Rows, _Cols, _Opt > > &mat)
Convenience function to create an LU solver with partial pivoting (for dense matrices) as a gsLinearO...
Definition gsMatrixOp.h:243
gsSolverOp< gsEigen::FullPivLU< gsEigen::Matrix< T, _Rows, _Cols, _Opt > > >::uPtr makeFullPivLUSolver(const gsMatrix< T, _Rows, _Cols, _Opt > &mat)
Convenience function to create an LU solver with full pivoting (for dense matrices) as a gsLinearOper...
Definition gsMatrixOp.h:254
gsSolverOp< gsEigen::LDLT< gsEigen::Matrix< T, _Rows, _Cols, _Opt > > >::uPtr makeCholeskySolver(const memory::shared_ptr< gsMatrix< T, _Rows, _Cols, _Opt > > &mat)
Convenience function to create a Cholesky (LDL^T) solver (for dense matrices) as a gsLinearOperator t...
Definition gsMatrixOp.h:293
memory::shared_ptr< gsSolverOp > Ptr
Shared pointer for gsSolverOp.
Definition gsMatrixOp.h:179
memory::unique_ptr< gsSolverOp > uPtr
Unique pointer for gsSolverOp.
Definition gsMatrixOp.h:182
Sparse matrix class, based on gsEigen::SparseMatrix.
Definition gsSparseMatrix.h:139
#define index_t
Definition gsConfig.h:32
#define GISMO_ENSURE(cond, message)
Definition gsDebug.h:102
#define GISMO_ASSERT(cond, message)
Definition gsDebug.h:89
This is the main header file that collects wrappers of Eigen for linear algebra.
Simple abstract class for (discrete) linear operators.
unique_ptr< T > make_unique(T *x)
Definition gsMemory.h:198
The G+Smo namespace, containing all definitions for the library.
S give(S &x)
Definition gsMemory.h:266