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
SurgSim::Math::LinearSparseSolveAndInverseCG::solve
Matrix solve(const Matrix &b) const override
Solve the linear system (matrix.x=b) using the matrix provided by the latest setMatrix call for all c...
Definition: LinearSparseSolveAndInverse.cpp:71
SurgSim::Math::LinearSparseSolveAndInverseLU::solve
Matrix solve(const Matrix &b) const override
Solve the linear system (matrix.x=b) using the matrix provided by the latest setMatrix call for all c...
Definition: LinearSparseSolveAndInverse.cpp:39
SurgSim::Math::LinearSparseSolveAndInverse::~LinearSparseSolveAndInverse
virtual ~LinearSparseSolveAndInverse()
Definition: LinearSparseSolveAndInverse.h:60
Vector.h
SurgSim::Math::LinearSparseSolveAndInverseCG::getTolerance
double getTolerance()
Get the conjugate gradient convergence tolerance.
Definition: LinearSparseSolveAndInverse.cpp:49
Matrix.h
SurgSim::Math::LinearSparseSolveAndInverseCG::m_solver
Eigen::ConjugateGradient< SparseMatrix > m_solver
Definition: LinearSparseSolveAndInverse.h:118
SparseMatrix.h
SurgSim::Math::LinearSparseSolveAndInverseLU::m_solver
Eigen::SparseLU< SparseMatrix > m_solver
Definition: LinearSparseSolveAndInverse.h:90
SurgSim
Definition: CompoundShapeToGraphics.cpp:29
SurgSim::Math::LINEARSOLVER_LU
Definition: LinearSparseSolveAndInverse.h:43
SurgSim::Math::LinearSolver
LinearSolver
The linear numerical integration scheme supported Each Linear Solver should have its own entry in thi...
Definition: LinearSparseSolveAndInverse.h:41
SurgSim::Math::LinearSparseSolveAndInverse::setMatrix
virtual void setMatrix(const SparseMatrix &matrix)=0
Set the linear solver matrix.
SurgSim::Math::LinearSparseSolveAndInverseCG
Derivation for sparse CG solver.
Definition: LinearSparseSolveAndInverse.h:94
SurgSim::Math::LinearSparseSolveAndInverse::m_matrix
SparseMatrix m_matrix
A copy of the system matrix for use when an inverse is necessary.
Definition: LinearSparseSolveAndInverse.h:78
SurgSim::Math::LinearSparseSolveAndInverse
LinearSparseSolveAndInverse aims at performing an efficient linear system resolution and calculating ...
Definition: LinearSparseSolveAndInverse.h:57
SurgSim::Math::MAX_LINEARSOLVER
Definition: LinearSparseSolveAndInverse.h:45
SurgSim::Math::LinearSparseSolveAndInverseCG::setMatrix
void setMatrix(const SparseMatrix &matrix) override
Set the linear solver matrix.
Definition: LinearSparseSolveAndInverse.cpp:64
SurgSim::Math::LinearSparseSolveAndInverseCG::setMaxIterations
void setMaxIterations(SparseMatrix::Index iterations)
Set the maximum number of iterations for conjugate gradient.
Definition: LinearSparseSolveAndInverse.cpp:54
SurgSim::Math::SparseMatrix
Eigen::SparseMatrix< double > SparseMatrix
A sparse matrix.
Definition: SparseMatrix.h:32
SurgSim::Math::LinearSolverNames
const std::unordered_map< LinearSolver, std::string, std::hash< int > > LinearSolverNames
Definition: LinearSparseSolveAndInverse.h:48
SurgSim::Math::LinearSparseSolveAndInverseCG::setTolerance
void setTolerance(double tolerance)
Set the conjugate gradient convergence tolerance.
Definition: LinearSparseSolveAndInverse.cpp:44
SurgSim::Math::LINEARSOLVER_CONJUGATEGRADIENT
Definition: LinearSparseSolveAndInverse.h:44
SurgSim::Math::LinearSparseSolveAndInverseLU::setMatrix
void setMatrix(const SparseMatrix &matrix) override
Set the linear solver matrix.
Definition: LinearSparseSolveAndInverse.cpp:31
SurgSim::Math::LinearSparseSolveAndInverseLU
Derivation for sparse LU solver.
Definition: LinearSparseSolveAndInverse.h:82
SurgSim::Math::LinearSparseSolveAndInverseCG::getMaxIterations
SparseMatrix::Index getMaxIterations()
Get the conjugate gradient maximum iterations.
Definition: LinearSparseSolveAndInverse.cpp:59
SurgSim::Math::LinearSparseSolveAndInverse::getInverse
virtual Matrix getInverse() const
Definition: LinearSparseSolveAndInverse.cpp:26
SurgSim::Math::Matrix
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > Matrix
A dynamic size matrix.
Definition: Matrix.h:65
SurgSim::Math::LinearSparseSolveAndInverse::solve
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...