Provides CodeGen
functions which generate C99 code to evaluate symbolic expressions and matrices.
#include
directives while it may use math functions defined in <math.h>
such as sin
, cos
, exp
, and log
. A user of generated code is responsible to include <math.h>
if needed to compile generated code. Functions | |
std::string | CodeGen (const std::string &function_name, const std::vector< Variable > ¶meters, const Expression &e) |
For a given symbolic expression e , generates two C functions, <function_name> and <function_name>_meta . More... | |
template<typename Derived > | |
std::string | CodeGen (const std::string &function_name, const std::vector< Variable > ¶meters, const Eigen::PlainObjectBase< Derived > &M) |
For a given symbolic dense matrix M , generates two C functions, <function_name> and <function_name>_meta . More... | |
std::string | CodeGen (const std::string &function_name, const std::vector< Variable > ¶meters, const Eigen::Ref< const Eigen::SparseMatrix< Expression, Eigen::ColMajor >> &M) |
For a given symbolic column-major sparse matrix M , generates two C functions, <function_name> and <function_name>_meta . More... | |
std::string drake::symbolic::CodeGen | ( | const std::string & | function_name, |
const std::vector< Variable > & | parameters, | ||
const Expression & | e | ||
) |
For a given symbolic expression e
, generates two C functions, <function_name>
and <function_name>_meta
.
The generated <function_name>
function takes an array of doubles for parameters and returns an evaluation result. <function_name>_meta
returns a nested struct from which a caller can obtain the following information:
.p.size
: the size of input parameters.[in] | function_name | Name of the generated C function. |
[in] | parameters | Vector of variables provide the ordering of symbolic variables. |
[in] | e | Symbolic expression to codegen. |
For example, Codegen("f", {x, y}, 1 + sin(x) + cos(y))
generates the following string.
Note that in this example x
and y
are mapped to p[0]
and p[1]
respectively because we passed {x, y}
to Codegen
.
std::string drake::symbolic::CodeGen | ( | const std::string & | function_name, |
const std::vector< Variable > & | parameters, | ||
const Eigen::PlainObjectBase< Derived > & | M | ||
) |
For a given symbolic dense matrix M
, generates two C functions, <function_name>
and <function_name>_meta
.
The generated <function_name>
takes two parameters:
<function_name>_meta()
returns a nested struct from which a caller can obtain the following information:
.p.size
: the size of input parameters..m.rows
: the number of rows in the matrix..m.cols
: the number of columns in the matrix.Please consider the following example:
When executed, the last line of the above example generates the following code:
Note that in this example, the matrix M
is stored in column-major order and the CodeGen
function respects the storage order in the generated code. If M
were stored in row-major order, CodeGen
would return the following:
std::string drake::symbolic::CodeGen | ( | const std::string & | function_name, |
const std::vector< Variable > & | parameters, | ||
const Eigen::Ref< const Eigen::SparseMatrix< Expression, Eigen::ColMajor >> & | M | ||
) |
For a given symbolic column-major sparse matrix M
, generates two C functions, <function_name>
and <function_name>_meta
.
The generated <function_name>
is used to construct a sparse matrix of double which stores the evaluation result of the symbolic matrix M
for a given double-precision floating-point assignment for the symbolic variables in M
. <function_name>
takes one input parameter p
and three output parameters (outer_indicies
, inner_indices
, and values
).
The three outputs, (outer_indices
, inner_indices
, values
), represent a sparse matrix in the widely-used Compressed Column Storage (CCS) scheme. For more information about the CCS scheme, please read https://eigen.tuxfamily.org/dox/group__TutorialSparse.html.
<function_name>_meta()
returns a nested struct from which a caller can obtain the following information:
.p.size
: the size of input parameters..m.rows
: the number of rows in the matrix..m.cols
: the number of columns in the matrix..m.non_zeros
: the number of non-zero elements in the matrix..m.outer_indices
: the length of the outer_indices..m.inner_indices
: the length of the inner_indices.std::exception | if M is not compressed. Please consider the following example which generates code for a 3x6 sparse matrix. |
When executed, the last line of the above example generates the following code:
In the following example, we show how to use the generated function to evaluate the symbolic matrix and construct a sparse matrix of double using Eigen::Map
.