LinearSparseSolveAndInverse.h
Go to the documentation of this file.
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013, SimQuest Solutions Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef SURGSIM_MATH_LINEARSPARSESOLVEANDINVERSE_H
17 #define SURGSIM_MATH_LINEARSPARSESOLVEANDINVERSE_H
18 
19 #if defined(_MSC_VER)
20 #pragma warning(push)
21 #pragma warning(disable:4244)
22 #endif
23 
24 #include <Eigen/SparseCore>
25 #include <unordered_map>
26 
27 #include <boost/assign/list_of.hpp> // for 'map_list_of()'
28 
29 #include "SurgSim/Math/Matrix.h"
31 #include "SurgSim/Math/Vector.h"
32 
33 namespace SurgSim
34 {
35 
36 namespace Math
37 {
38 
42 {
46 };
47 
48 const std::unordered_map<LinearSolver, std::string, std::hash<int>> LinearSolverNames =
49  boost::assign::map_list_of
50  (LINEARSOLVER_LU, "LINEARSOLVER_LU")
51  (LINEARSOLVER_CONJUGATEGRADIENT, "LINEARSOLVER_CONJUGATEGRADIENT");
52 
58 {
59 public:
61 
62 
65  virtual void setMatrix(const SparseMatrix& matrix) = 0;
66 
71  virtual Matrix solve(const Matrix& b) const = 0;
72 
74  virtual Matrix getInverse() const;
75 
76 protected:
79 };
80 
83 {
84 public:
85  void setMatrix(const SparseMatrix& matrix) override;
86 
87  Matrix solve(const Matrix& b) const override;
88 
89 private:
90  Eigen::SparseLU<SparseMatrix> m_solver;
91 };
92 
95 {
96 public:
99  void setTolerance(double tolerance);
100 
103  double getTolerance();
104 
107  void setMaxIterations(SparseMatrix::Index iterations);
108 
111  SparseMatrix::Index getMaxIterations();
112 
113  void setMatrix(const SparseMatrix& matrix) override;
114 
115  Matrix solve(const Matrix& b) const override;
116 
117 private:
118  Eigen::ConjugateGradient<SparseMatrix> m_solver;
119 };
120 
121 }; // namespace Math
122 
123 }; // namespace SurgSim
124 
125 #if defined(_MSC_VER)
126 #pragma warning(pop)
127 #endif
128 
129 #endif // SURGSIM_MATH_LINEARSPARSESOLVEANDINVERSE_H
Definition: CompoundShapeToGraphics.cpp:29
const std::unordered_map< LinearSolver, std::string, std::hash< int > > LinearSolverNames
Definition: LinearSparseSolveAndInverse.h:48
Definition: LinearSparseSolveAndInverse.h:45
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > Matrix
A dynamic size matrix.
Definition: Matrix.h:65
Derivation for sparse CG solver.
Definition: LinearSparseSolveAndInverse.h:94
Definition: LinearSparseSolveAndInverse.h:44
LinearSolver
The linear numerical integration scheme supported Each Linear Solver should have its own entry in thi...
Definition: LinearSparseSolveAndInverse.h:41
virtual Matrix getInverse() const
Definition: LinearSparseSolveAndInverse.cpp:26
Definitions of useful sparse matrix functions.
Eigen::SparseMatrix< double > SparseMatrix
A sparse matrix.
Definition: SparseMatrix.h:32
virtual Matrix solve(const Matrix &b) const =0
Solve the linear system (matrix.x=b) using the matrix provided by the latest setMatrix call for all c...
virtual ~LinearSparseSolveAndInverse()
Definition: LinearSparseSolveAndInverse.h:60
Definitions of small fixed-size square matrix types.
Definitions of small fixed-size vector types.
Derivation for sparse LU solver.
Definition: LinearSparseSolveAndInverse.h:82
Eigen::ConjugateGradient< SparseMatrix > m_solver
Definition: LinearSparseSolveAndInverse.h:118
SparseMatrix m_matrix
A copy of the system matrix for use when an inverse is necessary.
Definition: LinearSparseSolveAndInverse.h:78
Definition: LinearSparseSolveAndInverse.h:43
LinearSparseSolveAndInverse aims at performing an efficient linear system resolution and calculating ...
Definition: LinearSparseSolveAndInverse.h:57
Eigen::SparseLU< SparseMatrix > m_solver
Definition: LinearSparseSolveAndInverse.h:90
virtual void setMatrix(const SparseMatrix &matrix)=0
Set the linear solver matrix.