opensurgsim
LinearSparseSolveAndInverse.h
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 
41 enum LinearSolver
42 {
43  LINEARSOLVER_LU = 0,
44  LINEARSOLVER_CONJUGATEGRADIENT,
45  MAX_LINEARSOLVER
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:
60  virtual ~LinearSparseSolveAndInverse() {}
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 };
81 
84 {
85 public:
86  void setMatrix(const SparseMatrix& matrix) override;
87 
88  Matrix getInverse() const override;
89 
90  Matrix solve(const Matrix& b) const override;
91 
92 private:
93  Eigen::SparseLU<SparseMatrix> m_solver;
94  Matrix m_identity;
95 };
96 
99 {
100 public:
103  void setTolerance(double tolerance);
104 
107  double getTolerance();
108 
111  void setMaxIterations(Eigen::Index iterations);
112 
115  Eigen::Index getMaxIterations();
116 
117  void setMatrix(const SparseMatrix& matrix) override;
118 
119  Matrix solve(const Matrix& b) const override;
120 
121 private:
122  Eigen::ConjugateGradient<SparseMatrix> m_solver;
123 };
124 
125 }; // namespace Math
126 
127 }; // namespace SurgSim
128 
129 #if defined(_MSC_VER)
130 #pragma warning(pop)
131 #endif
132 
133 #endif // SURGSIM_MATH_LINEARSPARSESOLVEANDINVERSE_H
Wraps glewInit() to separate the glew opengl definitions from the osg opengl definitions only imgui n...
Definition: AddRandomSphereBehavior.cpp:36
Eigen::SparseMatrix< double > SparseMatrix
A sparse matrix.
Definition: SparseMatrix.h:32
Derivation for sparse CG solver.
Definition: LinearSparseSolveAndInverse.h:98
virtual Matrix getInverse() const
Definition: LinearSparseSolveAndInverse.cpp:26
Definitions of useful sparse matrix functions.
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...
Definitions of small fixed-size square matrix types.
Definitions of small fixed-size vector types.
Derivation for sparse LU solver.
Definition: LinearSparseSolveAndInverse.h:83
SparseMatrix m_matrix
A copy of the system matrix for use when an inverse is necessary.
Definition: LinearSparseSolveAndInverse.h:78
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > Matrix
A dynamic size matrix.
Definition: Matrix.h:65
LinearSparseSolveAndInverse aims at performing an efficient linear system resolution and calculating ...
Definition: LinearSparseSolveAndInverse.h:57
virtual void setMatrix(const SparseMatrix &matrix)=0
Set the linear solver matrix.