All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
20 
21 #include "SurgSim/Math/Vector.h"
22 #include "SurgSim/Math/Matrix.h"
23 
24 #include <Eigen/Core>
25 
26 namespace SurgSim
27 {
28 
29 namespace Math
30 {
31 
37 {
38 public:
40 
46  virtual void operator ()(const Matrix& A, const Vector& b, Vector* x = nullptr, Matrix* Ainv = nullptr) = 0;
47 };
48 
51 {
52 public:
53  virtual void operator ()(const Matrix& A, const Vector& b, Vector* x = nullptr, Matrix* Ainv = nullptr) override;
54 };
55 
58 {
59 public:
60  virtual void operator ()(const Matrix& A, const Vector& b, Vector* x = nullptr, Matrix* Ainv = nullptr) override;
61 };
62 
65 template <int BlockSize>
67 {
68 protected:
77  void inverseTriDiagonalBlock(const SurgSim::Math::Matrix& A, SurgSim::Math::Matrix* inv, bool isSymmetric = false);
78 
81 
82 private:
83  static_assert(BlockSize > 0,
84  "Cannot define a tri-diagonal block matrix with block size 0 or negative");
85 
86  typedef Eigen::Matrix<Matrix::Scalar, BlockSize, BlockSize, Matrix::Options> Block;
87 
92  const Eigen::Block<const Matrix, BlockSize, BlockSize> minusAi(const SurgSim::Math::Matrix& A, size_t i) const;
93 
98  const Eigen::Block<const Matrix, BlockSize, BlockSize> Bi(const SurgSim::Math::Matrix& A, size_t i) const;
99 
104  const Eigen::Block<const Matrix, BlockSize, BlockSize> minusCi(const SurgSim::Math::Matrix& A, size_t i) const;
105 
108  std::vector<Block> m_Di, m_Ei, m_Bi_AiDiminus1_inv;
110 
111 public:
112  virtual void operator ()(const Matrix& A, const Vector& b, Vector* x = nullptr, Matrix* Ainv = nullptr) override;
113 };
114 
117 template <int BlockSize>
120 {
121 public:
122  virtual void operator ()(const Matrix& A, const Vector& b, Vector* x = nullptr, Matrix* Ainv = nullptr) override;
123 
126 };
127 
128 }; // namespace Math
129 
130 }; // namespace SurgSim
131 
133 
134 #endif // SURGSIM_MATH_LINEARSOLVEANDINVERSE_H
Derivation for dense matrix type.
Definition: LinearSolveAndInverse.h:50
LinearSolveAndInverse aims at performing an efficient linear system resolution and calculating its in...
Definition: LinearSolveAndInverse.h:36
virtual void operator()(const Matrix &A, const Vector &b, Vector *x=nullptr, Matrix *Ainv=nullptr)=0
Solve a linear system A.x=b and compute the matrix A^-1.
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
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > Matrix
A dynamic size matrix.
Definition: Matrix.h:65
virtual void operator()(const Matrix &A, const Vector &b, Vector *x=nullptr, Matrix *Ainv=nullptr) override
Solve a linear system A.x=b and compute the matrix A^-1.
Definition: LinearSolveAndInverse-inl.h:176
virtual ~LinearSolveAndInverse()
Definition: LinearSolveAndInverse.h:39
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
Derivation for symmetric tri-diagonal block matrix type.
Definition: LinearSolveAndInverse.h:118
Derivation for diagonal matrix type.
Definition: LinearSolveAndInverse.h:57
Eigen::Matrix< double, Eigen::Dynamic, 1 > Vector
A dynamic size column vector.
Definition: Vector.h:67
std::vector< Block > m_Ei
Definition: LinearSolveAndInverse.h:108
Matrix m_inverse
Member variable to hold the inverse matrix in case only the solving is requested. ...
Definition: LinearSolveAndInverse.h:80
virtual void operator()(const Matrix &A, const Vector &b, Vector *x=nullptr, Matrix *Ainv=nullptr) override
Solve a linear system A.x=b and compute the matrix A^-1.
Definition: LinearSolveAndInverse-inl.h:155
Definitions of small fixed-size square matrix types.
std::vector< Block > m_Di
Definition: LinearSolveAndInverse.h:108
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
The header that provides the assertion API.
Definitions of small fixed-size vector types.
virtual void operator()(const Matrix &A, const Vector &b, Vector *x=nullptr, Matrix *Ainv=nullptr) override
Solve a linear system A.x=b and compute the matrix A^-1.
Definition: LinearSolveAndInverse.cpp:24
void inverseTriDiagonalBlock(const SurgSim::Math::Matrix &A, SurgSim::Math::Matrix *inv, bool isSymmetric=false)
Computes the inverse matrix.
Definition: LinearSolveAndInverse-inl.h:49
Eigen::Matrix< Matrix::Scalar, BlockSize, BlockSize, Matrix::Options > Block
Definition: LinearSolveAndInverse.h:84
virtual void operator()(const Matrix &A, const Vector &b, Vector *x=nullptr, Matrix *Ainv=nullptr) override
Solve a linear system A.x=b and compute the matrix A^-1.
Definition: LinearSolveAndInverse.cpp:48
Derivation for tri-diagonal block matrix type.
Definition: LinearSolveAndInverse.h:66
std::vector< Block > m_Bi_AiDiminus1_inv
Definition: LinearSolveAndInverse.h:108