LinearSparseSolveAndInverse.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_LINEARSPARSESOLVEANDINVERSE_H
#define SURGSIM_MATH_LINEARSPARSESOLVEANDINVERSE_H
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable:4244)
#endif
#include <Eigen/SparseCore>
#include <unordered_map>
#include <boost/assign/list_of.hpp> // for 'map_list_of()'
#include "SurgSim/Math/Matrix.h"
#include "SurgSim/Math/SparseMatrix.h"
#include "SurgSim/Math/Vector.h"
namespace SurgSim
{
namespace Math
{
enum LinearSolver
{
LINEARSOLVER_LU = 0,
LINEARSOLVER_CONJUGATEGRADIENT,
MAX_LINEARSOLVER
};
const std::unordered_map<LinearSolver, std::string, std::hash<int>> LinearSolverNames =
boost::assign::map_list_of
(LINEARSOLVER_LU, "LINEARSOLVER_LU")
(LINEARSOLVER_CONJUGATEGRADIENT, "LINEARSOLVER_CONJUGATEGRADIENT");
class LinearSparseSolveAndInverse
{
public:
virtual ~LinearSparseSolveAndInverse() {}
virtual void setMatrix(const SparseMatrix& matrix) = 0;
virtual Matrix solve(const Matrix& b) const = 0;
virtual Matrix getInverse() const;
protected:
SparseMatrix m_matrix;
};
class LinearSparseSolveAndInverseLU : public LinearSparseSolveAndInverse
{
public:
void setMatrix(const SparseMatrix& matrix) override;
Matrix solve(const Matrix& b) const override;
private:
Eigen::SparseLU<SparseMatrix> m_solver;
};
class LinearSparseSolveAndInverseCG : public LinearSparseSolveAndInverse
{
public:
void setTolerance(double tolerance);
double getTolerance();
void setMaxIterations(SparseMatrix::Index iterations);
SparseMatrix::Index getMaxIterations();
void setMatrix(const SparseMatrix& matrix) override;
Matrix solve(const Matrix& b) const override;
private:
Eigen::ConjugateGradient<SparseMatrix> m_solver;
};
}; // namespace Math
}; // namespace SurgSim
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
#endif // SURGSIM_MATH_LINEARSPARSESOLVEANDINVERSE_H
SurgSim
Definition: CompoundShapeToGraphics.cpp:29
SurgSim::Math::LinearSolverNames
const std::unordered_map< LinearSolver, std::string, std::hash< int > > LinearSolverNames
Definition: LinearSparseSolveAndInverse.h:48
SurgSim::Math::MAX_LINEARSOLVER
Definition: LinearSparseSolveAndInverse.h:45
SurgSim::Math::Matrix
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > Matrix
A dynamic size matrix.
Definition: Matrix.h:65
SurgSim::Math::LinearSparseSolveAndInverseCG
Derivation for sparse CG solver.
Definition: LinearSparseSolveAndInverse.h:94
SurgSim::Math::LINEARSOLVER_CONJUGATEGRADIENT
Definition: LinearSparseSolveAndInverse.h:44
SurgSim::Math::LinearSolver
LinearSolver
The linear numerical integration scheme supported Each Linear Solver should have its own entry in thi...
Definition: LinearSparseSolveAndInverse.h:41
SurgSim::Math::LinearSparseSolveAndInverse::getInverse
virtual Matrix getInverse() const
Definition: LinearSparseSolveAndInverse.cpp:26
SparseMatrix.h
Definitions of useful sparse matrix functions.
SurgSim::Math::SparseMatrix
Eigen::SparseMatrix< double > SparseMatrix
A sparse matrix.
Definition: SparseMatrix.h:32
SurgSim::Math::LinearSparseSolveAndInverse::solve
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...
SurgSim::Math::LinearSparseSolveAndInverse::~LinearSparseSolveAndInverse
virtual ~LinearSparseSolveAndInverse()
Definition: LinearSparseSolveAndInverse.h:60
Matrix.h
Definitions of small fixed-size square matrix types.
Vector.h
Definitions of small fixed-size vector types.
SurgSim::Math::LinearSparseSolveAndInverseLU
Derivation for sparse LU solver.
Definition: LinearSparseSolveAndInverse.h:82
SurgSim::Math::LinearSparseSolveAndInverseCG::m_solver
Eigen::ConjugateGradient< SparseMatrix > m_solver
Definition: LinearSparseSolveAndInverse.h:118
SurgSim::Math::LinearSparseSolveAndInverse::m_matrix
SparseMatrix m_matrix
A copy of the system matrix for use when an inverse is necessary.
Definition: LinearSparseSolveAndInverse.h:78
SurgSim::Math::LINEARSOLVER_LU
Definition: LinearSparseSolveAndInverse.h:43
SurgSim::Math::LinearSparseSolveAndInverse
LinearSparseSolveAndInverse aims at performing an efficient linear system resolution and calculating ...
Definition: LinearSparseSolveAndInverse.h:57
SurgSim::Math::LinearSparseSolveAndInverseLU::m_solver
Eigen::SparseLU< SparseMatrix > m_solver
Definition: LinearSparseSolveAndInverse.h:90
SurgSim::Math::LinearSparseSolveAndInverse::setMatrix
virtual void setMatrix(const SparseMatrix &matrix)=0
Set the linear solver matrix.