Drake
Drake C++ Documentation
LinearSolver< LinearSolverType, DerivedA > Class Template Reference

Detailed Description

template<template< typename, int... > typename LinearSolverType, typename DerivedA>
class drake::math::LinearSolver< LinearSolverType, DerivedA >

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

Eigen::Matrix<AutoDiffXd, 2, 2> A;
A(0, 0).value() = 1;
A(0, 0).derivatives() = Eigen::Vector3d(1, 2, 3);
A(0, 1).value() = 2;
A(0, 1).derivatives() = Eigen::Vector3d(2, 3, 4);
A(1, 0).value() = 2;
A(1, 0).derivatives() = Eigen::Vector3d(3, 4, 5);
A(1, 1).value() = 5;
A(1, 1).derivatives() = Eigen::Vector3d(4, 5, 6);
LinearSolver<Eigen::LLT, Eigen::Matrix<AutoDiffXd, 2, 2>> solver(A);
Eigen::Matrix<AutoDiffXd, 2, 1> b;
b(0).value() = 2;
b(0).derivatives() = Eigen::Vector3d(1, 2, 3);
b(1).value() = 3;
b(1).derivatives() = Eigen::Vector3d(4, 5, 6);
Eigen::Matrix<AutoDiffXd, 2, 1> x = solver.Solve(b);

#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 SolverTypeeigen_linear_solver () const
 Getter for the Eigen linear solver. More...
 

Member Typedef Documentation

◆ SolutionType

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.

◆ SolverType

using SolverType = internal::EigenLinearSolver<LinearSolverType, DerivedA>

Constructor & Destructor Documentation

◆ LinearSolver() [1/2]

Default constructor.

Constructs an empty linear solver.

◆ LinearSolver() [2/2]

LinearSolver ( const Eigen::MatrixBase< DerivedA > &  A)
explicit

Member Function Documentation

◆ eigen_linear_solver()

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.

◆ Solve()

SolutionType<DerivedB> Solve ( const Eigen::MatrixBase< DerivedB > &  b) const

Solves system A*x = b.

Return type is as described in the table above.


The documentation for this class was generated from the following file: