LinearSolveAndInverse.h
Go to the documentation of this file.
// This file is a part of the OpenSurgSim project.
// Copyright 2013, SimQuest Solutions Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SURGSIM_MATH_LINEARSOLVEANDINVERSE_H
#define SURGSIM_MATH_LINEARSOLVEANDINVERSE_H
#include <Eigen/Core>
#include "SurgSim/Framework/Assert.h"
#include "SurgSim/Math/Vector.h"
#include "SurgSim/Math/Matrix.h"
namespace SurgSim
{
namespace Math
{
class LinearSolveAndInverse
{
public:
virtual ~LinearSolveAndInverse(){}
virtual void setMatrix(const Matrix& matrix) = 0;
virtual Vector solve(const Vector& b) = 0;
virtual Matrix getInverse() = 0;
};
class LinearSolveAndInverseDenseMatrix : public LinearSolveAndInverse
{
public:
void setMatrix(const Matrix& matrix) override;
Vector solve(const Vector& b) override;
Matrix getInverse() override;
private:
Eigen::PartialPivLU<typename Eigen::MatrixBase<Matrix>::PlainObject> m_luDecomposition;
};
class LinearSolveAndInverseDiagonalMatrix : public LinearSolveAndInverse
{
private:
Vector m_inverseDiagonal;
public:
void setMatrix(const Matrix& matrix) override;
Vector solve(const Vector& b) override;
Matrix getInverse() override;
};
template <size_t BlockSize>
class LinearSolveAndInverseTriDiagonalBlockMatrix : public LinearSolveAndInverse
{
public:
void setMatrix(const Matrix& matrix) override;
Vector solve(const Vector& b) override;
Matrix getInverse() override;
protected:
void inverseTriDiagonalBlock(const SurgSim::Math::Matrix& A, SurgSim::Math::Matrix* inv, bool isSymmetric = false);
Matrix m_inverse;
private:
typedef Eigen::Matrix<Matrix::Scalar, BlockSize, BlockSize, Matrix::Options> Block;
const Eigen::Block<const Matrix, BlockSize, BlockSize> minusAi(const SurgSim::Math::Matrix& A, size_t i) const;
const Eigen::Block<const Matrix, BlockSize, BlockSize> Bi(const SurgSim::Math::Matrix& A, size_t i) const;
const Eigen::Block<const Matrix, BlockSize, BlockSize> minusCi(const SurgSim::Math::Matrix& A, size_t i) const;
std::vector<Block> m_Di, m_Ei, m_Bi_AiDiminus1_inv;
};
template <size_t BlockSize>
class LinearSolveAndInverseSymmetricTriDiagonalBlockMatrix :
public LinearSolveAndInverseTriDiagonalBlockMatrix<BlockSize>
{
public:
void setMatrix(const Matrix& matrix) override;
using LinearSolveAndInverseTriDiagonalBlockMatrix<BlockSize>::inverseTriDiagonalBlock;
using LinearSolveAndInverseTriDiagonalBlockMatrix<BlockSize>::m_inverse;
};
}; // namespace Math
}; // namespace SurgSim
#include "SurgSim/Math/LinearSolveAndInverse-inl.h"
#endif // SURGSIM_MATH_LINEARSOLVEANDINVERSE_H
SurgSim::Math::LinearSolveAndInverseDenseMatrix
Derivation for dense matrix type.
Definition: LinearSolveAndInverse.h:54
SurgSim
Definition: CompoundShapeToGraphics.cpp:29
SurgSim::Math::LinearSolveAndInverse
LinearSolveAndInverse aims at performing an efficient linear system resolution and calculating its in...
Definition: LinearSolveAndInverse.h:35
LinearSolveAndInverse-inl.h
SurgSim::Math::LinearSolveAndInverse::setMatrix
virtual void setMatrix(const Matrix &matrix)=0
Set the linear solver matrix.
SurgSim::Math::Matrix
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > Matrix
A dynamic size matrix.
Definition: Matrix.h:65
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::LinearSolveAndInverse::~LinearSolveAndInverse
virtual ~LinearSolveAndInverse()
Definition: LinearSolveAndInverse.h:38
SurgSim::Math::LinearSolveAndInverseSymmetricTriDiagonalBlockMatrix
Derivation for symmetric tri-diagonal block matrix type.
Definition: LinearSolveAndInverse.h:137
SurgSim::Math::LinearSolveAndInverseDiagonalMatrix
Derivation for diagonal matrix type.
Definition: LinearSolveAndInverse.h:68
SurgSim::Math::Vector
Eigen::Matrix< double, Eigen::Dynamic, 1 > Vector
A dynamic size column vector.
Definition: Vector.h:68
SurgSim::Math::LinearSolveAndInverseTriDiagonalBlockMatrix::m_Ei
std::vector< Block > m_Ei
Definition: LinearSolveAndInverse.h:130
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
Matrix.h
Definitions of small fixed-size square matrix types.
Assert.h
The header that provides the assertion API.
Vector.h
Definitions of small fixed-size vector types.
SurgSim::Math::LinearSolveAndInverseDiagonalMatrix::m_inverseDiagonal
Vector m_inverseDiagonal
Definition: LinearSolveAndInverse.h:71
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::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:84