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
SurgSim::Math::LinearSolveAndInverseTriDiagonalBlockMatrix::m_Di
std::vector< Block > m_Di
Definition: LinearSolveAndInverse.h:130
Vector.h
Definitions of small fixed-size vector types.
SurgSim::Math::LinearSolveAndInverseTriDiagonalBlockMatrix::inverseTriDiagonalBlock
void inverseTriDiagonalBlock(const SurgSim::Math::Matrix &A, SurgSim::Math::Matrix *inv, bool isSymmetric=false)
Computes the inverse matrix.
Definition: LinearSolveAndInverse-inl.h:49
SurgSim::Math::LinearSolveAndInverseDiagonalMatrix::setMatrix
void setMatrix(const Matrix &matrix) override
Set the linear solver matrix.
Definition: LinearSolveAndInverse.cpp:24
SurgSim::Math::LinearSolveAndInverseDenseMatrix::m_luDecomposition
Eigen::PartialPivLU< typename Eigen::MatrixBase< Matrix >::PlainObject > m_luDecomposition
Definition: LinearSolveAndInverse.h:64
SurgSim::Math::LinearSolveAndInverseTriDiagonalBlockMatrix
Derivation for tri-diagonal block matrix type.
Definition: LinearSolveAndInverse.h:85
SurgSim::Math::LinearSolveAndInverseDiagonalMatrix
Derivation for diagonal matrix type.
Definition: LinearSolveAndInverse.h:69
SurgSim::Math::LinearSolveAndInverseDiagonalMatrix::m_inverseDiagonal
Vector m_inverseDiagonal
Definition: LinearSolveAndInverse.h:71
SurgSim::Math::LinearSolveAndInverse::setMatrix
virtual void setMatrix(const Matrix &matrix)=0
Set the linear solver matrix.
SurgSim::Math::LinearSolveAndInverseTriDiagonalBlockMatrix::solve
Vector solve(const Vector &b) override
Solve the linear system (matrix.x=b) using the matrix provided by the latest setMatrix call.
Definition: LinearSolveAndInverse-inl.h:161
SurgSim::Math::LinearSolveAndInverseSymmetricTriDiagonalBlockMatrix
Derivation for symmetric tri-diagonal block matrix type.
Definition: LinearSolveAndInverse.h:139
Matrix.h
Definitions of small fixed-size square matrix types.
Assert.h
The header that provides the assertion API.
SurgSim::Math::LinearSolveAndInverseTriDiagonalBlockMatrix::m_Ei
std::vector< Block > m_Ei
Definition: LinearSolveAndInverse.h:130
SurgSim
Definition: CompoundShapeToGraphics.cpp:30
SurgSim::Math::LinearSolveAndInverse::~LinearSolveAndInverse
virtual ~LinearSolveAndInverse()
Definition: LinearSolveAndInverse.h:38
SurgSim::Math::LinearSolveAndInverse::solve
virtual Vector solve(const Vector &b)=0
Solve the linear system (matrix.x=b) using the matrix provided by the latest setMatrix call.
SurgSim::Math::LinearSolveAndInverseDiagonalMatrix::getInverse
Matrix getInverse() override
Definition: LinearSolveAndInverse.cpp:36
SurgSim::Math::LinearSolveAndInverseDenseMatrix
Derivation for dense matrix type.
Definition: LinearSolveAndInverse.h:55
SurgSim::Math::LinearSolveAndInverseDiagonalMatrix::solve
Vector solve(const Vector &b) override
Solve the linear system (matrix.x=b) using the matrix provided by the latest setMatrix call.
Definition: LinearSolveAndInverse.cpp:31
LinearSolveAndInverse-inl.h
SurgSim::Math::LinearSolveAndInverseTriDiagonalBlockMatrix::setMatrix
void setMatrix(const Matrix &matrix) override
Set the linear solver matrix.
Definition: LinearSolveAndInverse-inl.h:155
SurgSim::Math::LinearSolveAndInverseSymmetricTriDiagonalBlockMatrix::setMatrix
void setMatrix(const Matrix &matrix) override
Set the linear solver matrix.
Definition: LinearSolveAndInverse-inl.h:173
SurgSim::Math::LinearSolveAndInverseTriDiagonalBlockMatrix::minusAi
const Eigen::Block< const Matrix, BlockSize, BlockSize > minusAi(const SurgSim::Math::Matrix &A, size_t i) const
Gets a lower-diagonal block element (named -Ai in the algorithm)
Definition: LinearSolveAndInverse-inl.h:29
SurgSim::Math::Vector
Eigen::Matrix< double, Eigen::Dynamic, 1 > Vector
A dynamic size column vector.
Definition: Vector.h:68
SurgSim::Math::LinearSolveAndInverse
LinearSolveAndInverse aims at performing an efficient linear system resolution and calculating its in...
Definition: LinearSolveAndInverse.h:36
SurgSim::Math::LinearSolveAndInverseDenseMatrix::solve
Vector solve(const Vector &b) override
Solve the linear system (matrix.x=b) using the matrix provided by the latest setMatrix call.
Definition: LinearSolveAndInverse.cpp:48
SurgSim::Math::LinearSolveAndInverseTriDiagonalBlockMatrix::m_inverse
Matrix m_inverse
Member variable to hold the inverse matrix in case only the solving is requested.
Definition: LinearSolveAndInverse.h:105
SurgSim::Math::LinearSolveAndInverseTriDiagonalBlockMatrix::Block
Eigen::Matrix< Matrix::Scalar, BlockSize, BlockSize, Matrix::Options > Block
Definition: LinearSolveAndInverse.h:108
SurgSim::Math::LinearSolveAndInverse::getInverse
virtual Matrix getInverse()=0
SurgSim::Math::LinearSolveAndInverseTriDiagonalBlockMatrix::minusCi
const Eigen::Block< const Matrix, BlockSize, BlockSize > minusCi(const SurgSim::Math::Matrix &A, size_t i) const
Gets a upper-diagonal block element (named -Ci in the algorithm)
Definition: LinearSolveAndInverse-inl.h:43
SurgSim::Math::Matrix
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > Matrix
A dynamic size matrix.
Definition: Matrix.h:65
SurgSim::Math::LinearSolveAndInverseDenseMatrix::getInverse
Matrix getInverse() override
Definition: LinearSolveAndInverse.cpp:53
SurgSim::Math::LinearSolveAndInverseTriDiagonalBlockMatrix::Bi
const Eigen::Block< const Matrix, BlockSize, BlockSize > Bi(const SurgSim::Math::Matrix &A, size_t i) const
Gets a diagonal block element (named Bi in the algorithm)
Definition: LinearSolveAndInverse-inl.h:36
SurgSim::Math::LinearSolveAndInverseDenseMatrix::setMatrix
void setMatrix(const Matrix &matrix) override
Set the linear solver matrix.
Definition: LinearSolveAndInverse.cpp:41
SurgSim::Math::LinearSolveAndInverseTriDiagonalBlockMatrix::getInverse
Matrix getInverse() override
Definition: LinearSolveAndInverse-inl.h:167
SurgSim::Math::LinearSolveAndInverseTriDiagonalBlockMatrix::m_Bi_AiDiminus1_inv
std::vector< Block > m_Bi_AiDiminus1_inv
Definition: LinearSolveAndInverse.h:130