LinearSolveAndInverse.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_LINEARSOLVEANDINVERSE_H
17 #define SURGSIM_MATH_LINEARSOLVEANDINVERSE_H
18 
19 #include <Eigen/Core>
20 
22 #include "SurgSim/Math/Vector.h"
23 #include "SurgSim/Math/Matrix.h"
24 
25 namespace SurgSim
26 {
27 
28 namespace Math
29 {
30 
36 {
37 public:
39 
42  virtual void setMatrix(const Matrix& matrix) = 0;
43 
47  virtual Vector solve(const Vector& b) = 0;
48 
50  virtual Matrix getInverse() = 0;
51 };
52 
55 {
56 public:
57  void setMatrix(const Matrix& matrix) override;
58 
59  Vector solve(const Vector& b) override;
60 
61  Matrix getInverse() override;
62 
63 private:
64  Eigen::PartialPivLU<typename Eigen::MatrixBase<Matrix>::PlainObject> m_luDecomposition;
65 };
66 
69 {
70 private:
72 
73 public:
74  void setMatrix(const Matrix& matrix) override;
75 
76  Vector solve(const Vector& b) override;
77 
78  Matrix getInverse() override;
79 };
80 
83 template <size_t BlockSize>
85 {
86 public:
87  void setMatrix(const Matrix& matrix) override;
88 
89  Vector solve(const Vector& b) override;
90 
91  Matrix getInverse() override;
92 
93 protected:
102  void inverseTriDiagonalBlock(const SurgSim::Math::Matrix& A, SurgSim::Math::Matrix* inv, bool isSymmetric = false);
103 
106 
107 private:
108  typedef Eigen::Matrix<Matrix::Scalar, BlockSize, BlockSize, Matrix::Options> Block;
109 
114  const Eigen::Block<const Matrix, BlockSize, BlockSize> minusAi(const SurgSim::Math::Matrix& A, size_t i) const;
115 
120  const Eigen::Block<const Matrix, BlockSize, BlockSize> Bi(const SurgSim::Math::Matrix& A, size_t i) const;
121 
126  const Eigen::Block<const Matrix, BlockSize, BlockSize> minusCi(const SurgSim::Math::Matrix& A, size_t i) const;
127 
130  std::vector<Block> m_Di, m_Ei, m_Bi_AiDiminus1_inv;
132 };
133 
136 template <size_t BlockSize>
139 {
140 public:
141  void setMatrix(const Matrix& matrix) override;
142 
145 };
146 
147 }; // namespace Math
148 
149 }; // namespace SurgSim
150 
152 
153 #endif // SURGSIM_MATH_LINEARSOLVEANDINVERSE_H
Derivation for dense matrix type.
Definition: LinearSolveAndInverse.h:54
Definition: CompoundShapeToGraphics.cpp:29
LinearSolveAndInverse aims at performing an efficient linear system resolution and calculating its in...
Definition: LinearSolveAndInverse.h:35
virtual void setMatrix(const Matrix &matrix)=0
Set the linear solver matrix.
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > Matrix
A dynamic size matrix.
Definition: Matrix.h:65
virtual Vector solve(const Vector &b)=0
Solve the linear system (matrix.x=b) using the matrix provided by the latest setMatrix call...
virtual ~LinearSolveAndInverse()
Definition: LinearSolveAndInverse.h:38
Derivation for symmetric tri-diagonal block matrix type.
Definition: LinearSolveAndInverse.h:137
Derivation for diagonal matrix type.
Definition: LinearSolveAndInverse.h:68
Eigen::Matrix< double, Eigen::Dynamic, 1 > Vector
A dynamic size column vector.
Definition: Vector.h:68
std::vector< Block > m_Ei
Definition: LinearSolveAndInverse.h:130
Matrix m_inverse
Member variable to hold the inverse matrix in case only the solving is requested. ...
Definition: LinearSolveAndInverse.h:105
Definitions of small fixed-size square matrix types.
The header that provides the assertion API.
Definitions of small fixed-size vector types.
Vector m_inverseDiagonal
Definition: LinearSolveAndInverse.h:71
Eigen::Matrix< Matrix::Scalar, BlockSize, BlockSize, Matrix::Options > Block
Definition: LinearSolveAndInverse.h:108
Eigen::PartialPivLU< typename Eigen::MatrixBase< Matrix >::PlainObject > m_luDecomposition
Definition: LinearSolveAndInverse.h:64
Derivation for tri-diagonal block matrix type.
Definition: LinearSolveAndInverse.h:84