Drake
Drake C++ Documentation
BarycentricMesh< T > Class Template Reference

Detailed Description

template<typename T>
class drake::math::BarycentricMesh< T >

Represents a multi-linear function (from vector inputs to vector outputs) by interpolating between points on a mesh using (triangular) barycentric interpolation.

For a technical description of barycentric interpolation, see e.g. Remi Munos and Andrew Moore, "Barycentric Interpolators for Continuous Space and Time Reinforcement Learning", NIPS 1998

Template Parameters
TThe scalar type, which must be double.

#include <drake/math/barycentric.h>

Public Types

typedef std::set< doubleCoordinates
 The mesh is represented by a std::set (to ensure uniqueness and provide logarithmic lookups) of coordinates in each input dimension. More...
 
typedef std::vector< CoordinatesMeshGrid
 

Public Member Functions

 BarycentricMesh (MeshGrid input_grid)
 Constructs the mesh. More...
 
const MeshGridget_input_grid () const
 
int get_input_size () const
 
int get_num_mesh_points () const
 
int get_num_interpolants () const
 
void get_mesh_point (int index, EigenPtr< Eigen::VectorXd > point) const
 Writes the position of a mesh point in the input space referenced by its scalar index to point. More...
 
VectorX< T > get_mesh_point (int index) const
 Returns the position of a mesh point in the input space referenced by its scalar index to point. More...
 
MatrixX< T > get_all_mesh_points () const
 Returns a matrix with all of the mesh points, one per column. More...
 
void EvalBarycentricWeights (const Eigen::Ref< const VectorX< T >> &input, EigenPtr< Eigen::VectorXi > mesh_indices, EigenPtr< VectorX< T >> weights) const
 Writes the mesh indices used for interpolation to mesh_indices, and the interpolating coefficients to weights. More...
 
void Eval (const Eigen::Ref< const MatrixX< T >> &mesh_values, const Eigen::Ref< const VectorX< T >> &input, EigenPtr< VectorX< T >> output) const
 Evaluates the function at the input values, by interpolating between the values at mesh_values. More...
 
VectorX< T > Eval (const Eigen::Ref< const MatrixX< T >> &mesh_values, const Eigen::Ref< const VectorX< T >> &input) const
 Returns the function evaluated at input. More...
 
template<typename ValueT = T>
void EvalWithMixedScalars (const Eigen::Ref< const MatrixX< ValueT >> &mesh_values, const Eigen::Ref< const VectorX< T >> &input, EigenPtr< VectorX< ValueT >> output) const
 Performs Eval, but with the possibility of the values on the mesh having a different scalar type than the values defining the mesh (symbolic::Expression containing decision variables for an optimization problem is an important example) More...
 
template<typename ValueT = T>
VectorX< ValueT > EvalWithMixedScalars (const Eigen::Ref< const MatrixX< ValueT >> &mesh_values, const Eigen::Ref< const VectorX< T >> &input) const
 Returns the function evaluated at input. More...
 
MatrixX< T > MeshValuesFrom (const std::function< VectorX< T >(const Eigen::Ref< const VectorX< T >> &)> &vector_func) const
 Evaluates vector_func at all input mesh points and extracts the mesh value matrix that should be used to approximate the function with this barycentric interpolation. More...
 
Implements CopyConstructible, CopyAssignable, MoveConstructible, MoveAssignable
 BarycentricMesh (const BarycentricMesh &)=default
 
BarycentricMeshoperator= (const BarycentricMesh &)=default
 
 BarycentricMesh (BarycentricMesh &&)=default
 
BarycentricMeshoperator= (BarycentricMesh &&)=default
 

Member Typedef Documentation

◆ Coordinates

typedef std::set<double> Coordinates

The mesh is represented by a std::set (to ensure uniqueness and provide logarithmic lookups) of coordinates in each input dimension.

Note: The values are type double, not T (We do not plan to take gradients, etc w/ respect to them).

◆ MeshGrid

typedef std::vector<Coordinates> MeshGrid

Constructor & Destructor Documentation

◆ BarycentricMesh() [1/3]

BarycentricMesh ( const BarycentricMesh< T > &  )
default

◆ BarycentricMesh() [2/3]

BarycentricMesh ( BarycentricMesh< T > &&  )
default

◆ BarycentricMesh() [3/3]

BarycentricMesh ( MeshGrid  input_grid)
explicit

Constructs the mesh.

Member Function Documentation

◆ Eval() [1/2]

void Eval ( const Eigen::Ref< const MatrixX< T >> &  mesh_values,
const Eigen::Ref< const VectorX< T >> &  input,
EigenPtr< VectorX< T >>  output 
) const

Evaluates the function at the input values, by interpolating between the values at mesh_values.

Inputs that are outside the bounding box of the input_grid are interpolated as though they were projected (elementwise) to the closest face of the defined mesh.

Note that the dimension of the output vector is completely defined by the mesh_values argument. This class does not maintain any information related to the size of the output.

Parameters
mesh_valuesis a num_outputs by get_num_mesh_points() matrix containing the points to interpolate between. The order of the columns must be consistent with the mesh indices curated by this class, as exposed by get_mesh_point().
inputmust be a vector of length get_num_inputs().
outputis the interpolated vector of length num_outputs

◆ Eval() [2/2]

VectorX<T> Eval ( const Eigen::Ref< const MatrixX< T >> &  mesh_values,
const Eigen::Ref< const VectorX< T >> &  input 
) const

Returns the function evaluated at input.

◆ EvalBarycentricWeights()

void EvalBarycentricWeights ( const Eigen::Ref< const VectorX< T >> &  input,
EigenPtr< Eigen::VectorXi >  mesh_indices,
EigenPtr< VectorX< T >>  weights 
) const

Writes the mesh indices used for interpolation to mesh_indices, and the interpolating coefficients to weights.

Inputs that are outside the bounding box of the input_grid are interpolated as though they were projected (elementwise) to the closest face of the defined mesh.

Parameters
inputmust be a vector of length get_num_inputs().
mesh_indicesis a pointer to a vector of length get_num_interpolants().
weightsis a vector of coefficients (which sum to 1) of length get_num_interpolants().

◆ EvalWithMixedScalars() [1/2]

void EvalWithMixedScalars ( const Eigen::Ref< const MatrixX< ValueT >> &  mesh_values,
const Eigen::Ref< const VectorX< T >> &  input,
EigenPtr< VectorX< ValueT >>  output 
) const

Performs Eval, but with the possibility of the values on the mesh having a different scalar type than the values defining the mesh (symbolic::Expression containing decision variables for an optimization problem is an important example)

Template Parameters
ValueTdefines the scalar type of the mesh_values and the output.
See also
Eval

◆ EvalWithMixedScalars() [2/2]

VectorX<ValueT> EvalWithMixedScalars ( const Eigen::Ref< const MatrixX< ValueT >> &  mesh_values,
const Eigen::Ref< const VectorX< T >> &  input 
) const

Returns the function evaluated at input.

◆ get_all_mesh_points()

MatrixX<T> get_all_mesh_points ( ) const

Returns a matrix with all of the mesh points, one per column.

◆ get_input_grid()

const MeshGrid& get_input_grid ( ) const

◆ get_input_size()

int get_input_size ( ) const

◆ get_mesh_point() [1/2]

void get_mesh_point ( int  index,
EigenPtr< Eigen::VectorXd >  point 
) const

Writes the position of a mesh point in the input space referenced by its scalar index to point.

Parameters
indexmust be in [0, get_num_mesh_points).
pointis set to the num_inputs-by-1 location of the mesh point.

◆ get_mesh_point() [2/2]

VectorX<T> get_mesh_point ( int  index) const

Returns the position of a mesh point in the input space referenced by its scalar index to point.

Parameters
indexmust be in [0, get_num_mesh_points).

◆ get_num_interpolants()

int get_num_interpolants ( ) const

◆ get_num_mesh_points()

int get_num_mesh_points ( ) const

◆ MeshValuesFrom()

MatrixX<T> MeshValuesFrom ( )

Evaluates vector_func at all input mesh points and extracts the mesh value matrix that should be used to approximate the function with this barycentric interpolation.

MatrixXd mesh_values = bary.MeshValuesFrom(
[](const auto& x) { return Vector1d(std::sin(x[0])); });

◆ operator=() [1/2]

BarycentricMesh& operator= ( const BarycentricMesh< T > &  )
default

◆ operator=() [2/2]

BarycentricMesh& operator= ( BarycentricMesh< T > &&  )
default

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