Solves a linear system of equations A*x=b.
Depending on the scalar types of A and b, the scalar type of x is summarized in this table.
b \ A | double | ADS | Expr |
---|---|---|---|
double | double | ADS | NA |
ADS | ADS | ADS | NA |
Expr | NA | NA | Expr |
where ADS stands for Eigen::AutoDiffScalar, and Expr stands for symbolic::Expression.
Using LinearSolver class is as fast as using Eigen's linear solver directly when neither A nor b contains AutoDiffScalar. When either A or b contains AutoDiffScalar, using LinearSolver is much faster than using Eigen's autodiffable linear solver (for example Eigen::LDLT<Eigen::Matrix<Eigen::AutoDiffScalar, 3, 3>>).
Here is the example code
#include <drake/math/linear_solve.h>
Public Types | |
using | SolverType = internal::EigenLinearSolver< LinearSolverType, DerivedA > |
template<typename DerivedB > | |
using | SolutionType = std::conditional_t< std::is_same_v< typename DerivedA::Scalar, typename DerivedB::Scalar > &&internal::is_double_or_symbolic_v< typename DerivedA::Scalar >, Eigen::Solve< SolverType, DerivedB >, internal::Solution< DerivedA, DerivedB > > |
The return type of Solve() function. More... | |
Public Member Functions | |
LinearSolver () | |
Default constructor. More... | |
LinearSolver (const Eigen::MatrixBase< DerivedA > &A) | |
template<typename DerivedB > | |
SolutionType< DerivedB > | Solve (const Eigen::MatrixBase< DerivedB > &b) const |
Solves system A*x = b. More... | |
const SolverType & | eigen_linear_solver () const |
Getter for the Eigen linear solver. More... | |
using SolutionType = std::conditional_t< std::is_same_v<typename DerivedA::Scalar, typename DerivedB::Scalar> && internal::is_double_or_symbolic_v<typename DerivedA::Scalar>, Eigen::Solve<SolverType, DerivedB>, internal::Solution<DerivedA, DerivedB> > |
The return type of Solve() function.
When both A and B contain the same scalar, and that scalar type is double or symbolic::Expression, then the return type is Eigen::Solve<Decomposition, DerivedB>, same as the return type of Decomposition::solve() function in Eigen. This avoids unnecessary copies and heap memory allocations if we were to evaluate Eigen::Solve<Decomposition, DerivedB> to a concrete Eigen::Matrix type. Otherwise we return an Eigen::Matrix with the same size as DerivedB and proper scalar type.
using SolverType = internal::EigenLinearSolver<LinearSolverType, DerivedA> |
LinearSolver | ( | ) |
Default constructor.
Constructs an empty linear solver.
|
explicit |
const SolverType& eigen_linear_solver | ( | ) | const |
Getter for the Eigen linear solver.
The scalar type in the Eigen linear solver depends on the scalar type in A matrix, as shown in this table
A | double | ADS | Expr |
---|---|---|---|
linear_solver | double | double | Expr |
where ADS stands for Eigen::AutoDiffScalar, Expr stands for symbolic::Expression.
Note that when A contains autodiffscalar, we only use the double version of Eigen linear solver. By using implicit-function theorem with the double-valued Eigen linear solver, we can compute the gradient of the solution much faster than directly autodiffing the Eigen linear solver.
SolutionType<DerivedB> Solve | ( | const Eigen::MatrixBase< DerivedB > & | b | ) | const |
Solves system A*x = b.
Return type is as described in the table above.