code_generation#

wrenfold.code_generation.create_function_description(func: Callable[[...], Expr | MatrixExpr | Sequence[ReturnValue | OutputArg]], name: str | None = None) FunctionDescription#

Accept a python function that manipulates symbolic mathematical expressions, and convert it to a wrenfold.code_generation.FunctionDescription object. The provided function is invoked, and its output expressions are captured and stored, along with a signature that carries type information required to emit code.

Tip

The provided callable must be type annotated so that wrenfold can deduce the type of input expressions required to invoke. See type_annotations for built-in types that can be used to annotate functions. The function should return either:

Parameters:
  • func – A python function with type-annotated arguments.

  • name – String name of the function.

Returns:

An instance of wrenfold.code_generation.FunctionDescription.

Example

>>> from wrenfold.type_annotations import FloatScalar
>>> from wrenfold import code_generation
>>>
>>> def foo(x: FloatScalar, y: FloatScalar):
>>>     # One return value, and one output argument named `z`:
>>>     return [code_generation.ReturnValue(x * y), code_generation.OutputArg(x + y, "z")]
>>> description = code_generation.create_function_description(func=foo)
>>> print(description)
FunctionDescription('foo', 3 args)

The description can then be transpiled to a target language:

>>> definition = code_generation.transpile(description)
>>> print(definition)
FunctionDefinition('foo', <3 arguments>, <7 elements>)
>>> code = code_generation.CppGenerator().generate(definition=definition)
>>> print(code)
 1template <typename Scalar>
 2Scalar foo(const Scalar x, const Scalar y, Scalar& z)
 3{
 4    // Operation counts:
 5    // add: 1
 6    // multiply: 1
 7    // total: 2
 8
 9    const Scalar v01 = y;
10    const Scalar v00 = x;
11    const Scalar v04 = v00 + v01;
12    const Scalar v02 = v00 * v01;
13    z = v04;
14    return v02;
15}
wrenfold.code_generation.cse_function_description(desc: wrenfold.gen.FunctionDescription, params: wrenfold.gen.OptimizationParams | None = None) Tuple[Dict[wrenfold.gen.OutputKey, wrenfold.sym.Expr | wrenfold.sym.MatrixExpr | wrenfold.sym.CompoundExpr | wrenfold.sym.BooleanExpr], List[Tuple[wrenfold.sym.Expr, wrenfold.sym.Expr]]]#

Given a wrenfold.code_generation.FunctionDescription object, run the code-generation CSE and then convert the simplified result back to a list of symbolic expressions. This function will apply all the simplifications and subexpression elimination steps normally applied during code-generation.

The first returned object is a dict mapping from OutputKey to output expressions. The outputs will be expressed as a function of variables [v0, v1, ... v{N}]. The second returned object is a list of tuples of the form [(v0, <v0 expr>), (v1, <v1 expr>), ...] - these are the eliminated subexpressions required to evaluate the function.

Parameters:
Returns:

A dict mapping from OutputKey to output expressions, and a list of intermediate subexpressions required to compute the function outputs.

Example

>>> from wrenfold import sym, code_generation, type_annotations
>>> def func(x: type_annotations.FloatScalar, y: type_annotations.FloatScalar):
>>>   return sym.abs(x * y) * sym.cos(x * y)
>>> desc = code_generation.create_function_description(func)
>>> outputs, intermediate_values = code_generation.cse_function_description(desc)
>>> outputs
{OutputKey(return_value): [v5]}
>>> intermediate_values
[(v0, $arg(0, 0)),
 (v1, $arg(1, 0)),
 (v2, v0*v1),
 (v3, abs(v2)),
 (v4, cos(v2)),
 (v5, v3*v4)]
wrenfold.code_generation.generate_function(func: Callable[[...], Expr | MatrixExpr | Sequence[ReturnValue | OutputArg]], generator: CppGenerator | RustGenerator | BaseGenerator, name: str | None = None, optimization_params: OptimizationParams | None = None) str#

Accept a python function that manipulates symbolic mathematical expressions, and convert it to code in the language emitted by generator. This is a three-step process:

  1. The signature of the provided function is inspected to generate input arguments. Next, it is invoked and the symbolic outputs are captured in a wrenfold.code_generation.FunctionDescription.

  2. This description is converted to AST using wrenfold.code_generation.transpile(). Duplicate operations are eliminated during this step, and conditionals are converted to control flow.

  3. Lastly, the AST is passed to the provided generator to emit usable code.

Tip

The steps above can also be performed individually. For instance, you might wish to pass the output AST to multiple generators if many simultaneous target languages are required. See wrenfold.code_generation.create_function_description() for an example.

Parameters:

Returns: Generated code.

Example

>>> from wrenfold.type_annotations import FloatScalar
>>> from wrenfold import code_generation
>>>
>>> def foo(x: FloatScalar, y: FloatScalar):
>>>     # One return value, and one output argument named `z`:
>>>     return [code_generation.ReturnValue(x * y), code_generation.OutputArg(x + y, "z")]
>>>
>>> code = code_generation.generate_function(func=foo)
>>> print(code)
 1template <typename Scalar>
 2Scalar foo(const Scalar x, const Scalar y, Scalar& z)
 3{
 4    // Operation counts:
 5    // add: 1
 6    // multiply: 1
 7    // total: 2
 8
 9    const Scalar v01 = y;
10    const Scalar v00 = x;
11    const Scalar v04 = v00 + v01;
12    const Scalar v02 = v00 * v01;
13    z = v04;
14    return v02;
15}
wrenfold.code_generation.mkdir_and_write_file(code: str, path: str | Path) None#

Write code to the specified path. Create intermediate directories as required.

Parameters:
  • code – String containing file contents.

  • path – Path to the destination file.

wrenfold.code_generation.transpile(*args, **kwargs)#

Overloaded function.

  1. transpile(desc: List[wrenfold.gen.FunctionDescription], params: Optional[wrenfold.gen.OptimizationParams] = None) -> List[wrenfold.ast.FunctionDefinition]

Overload of wrenfold.code_generation.transpile() that operates on a sequence of functions.

  1. transpile(desc: wrenfold.gen.FunctionDescription, params: Optional[wrenfold.gen.OptimizationParams] = None) -> wrenfold.ast.FunctionDefinition

Given a wrenfold.code_generation.FunctionDescription object, convert it to an abstract syntax tree suitable for code generation. This operation incorporates three steps:

  1. The symbolic expression tree is converted to a flat intermediate representation. Common subexpression elimination is performed to minimize duplicated operations.

  2. A control flow graph (CFG) is generated.

  3. The CFG is then converted to an abstract syntax tree (AST) that can be emitted as usable code. See the ast module for a list of types used in the syntax tree.

The syntax tree can then be passed to a generator (for example, wrenfold.code_generation.CppGenerator) to emit compilable code that you can incorporate in your project.

Parameters:
Returns:

Instance of wrenfold.ast.FunctionDefinition.

class wrenfold.code_generation.Argument#

Describe an argument to a function.

__init__(*args, **kwargs)#
property direction#

How the argument is used by the function.

property is_optional#

True if the argument is optional.

property name#

String name of the argument.

property type#

Type of the argument.

class wrenfold.code_generation.ArgumentDirection#

Members:

Input : Argument is an input.

Output : Argument is an output.

OptionalOutput : Argument is an optional output.

__eq__(self: object, other: object) bool#
__hash__(self: object) int#
__index__(self: wrenfold.gen.ArgumentDirection) int#
__init__(self: wrenfold.gen.ArgumentDirection, value: int) None#
__int__(self: wrenfold.gen.ArgumentDirection) int#
__ne__(self: object, other: object) bool#
__str__()#

name(self: handle) -> str

property name#
class wrenfold.code_generation.BaseGenerator#

Abstract base class for generators. The user may inherit from this in python when writing a new generator from scratch.

__init__(self: wrenfold.gen.BaseGenerator) None#
format(*args, **kwargs)#

Overloaded function.

  1. format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.Add) -> str

Format type wrenfold.ast.Add.

  1. format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.AssignTemporary) -> str

Format type wrenfold.ast.AssignTemporary.

  1. format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.AssignOutputMatrix) -> str

Format type wrenfold.ast.AssignOutputMatrix.

  1. format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.AssignOutputScalar) -> str

Format type wrenfold.ast.AssignOutputScalar.

  1. format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.AssignOutputStruct) -> str

Format type wrenfold.ast.AssignOutputStruct.

  1. format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.BooleanLiteral) -> str

Format type wrenfold.ast.BooleanLiteral.

  1. format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.Branch) -> str

Format type wrenfold.ast.Branch.

  1. format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.CallExternalFunction) -> str

Format type wrenfold.ast.CallExternalFunction.

  1. format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.CallStdFunction) -> str

Format type wrenfold.ast.CallStdFunction.

  1. format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.Cast) -> str

Format type wrenfold.ast.Cast.

  1. format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.Comment) -> str

Format type wrenfold.ast.Comment.

  1. format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.Compare) -> str

Format type wrenfold.ast.Compare.

  1. format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.ConstructCustomType) -> str

Format type wrenfold.ast.ConstructCustomType.

  1. format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.ConstructMatrix) -> str

Format type wrenfold.ast.ConstructMatrix.

  1. format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.Declaration) -> str

Format type wrenfold.ast.Declaration.

  1. format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.Divide) -> str

Format type wrenfold.ast.Divide.

  1. format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.FloatLiteral) -> str

Format type wrenfold.ast.FloatLiteral.

  1. format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.GetArgument) -> str

Format type wrenfold.ast.GetArgument.

  1. format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.GetField) -> str

Format type wrenfold.ast.GetField.

  1. format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.GetMatrixElement) -> str

Format type wrenfold.ast.GetMatrixElement.

  1. format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.IntegerLiteral) -> str

Format type wrenfold.ast.IntegerLiteral.

  1. format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.Multiply) -> str

Format type wrenfold.ast.Multiply.

  1. format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.Negate) -> str

Format type wrenfold.ast.Negate.

  1. format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.OptionalOutputBranch) -> str

Format type wrenfold.ast.OptionalOutputBranch.

  1. format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.Parenthetical) -> str

Format type wrenfold.ast.Parenthetical.

  1. format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.SpecialConstant) -> str

Format type wrenfold.ast.SpecialConstant.

  1. format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.ReturnObject) -> str

Format type wrenfold.ast.ReturnObject.

  1. format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.VariableRef) -> str

Format type wrenfold.ast.VariableRef.

  1. format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.FunctionSignature) -> str

Format type wrenfold.ast.FunctionSignature.

  1. format(self: wrenfold.gen.BaseGenerator, element: wrenfold.type_info.ScalarType) -> str

Format type wrenfold.type_info.ScalarType.

  1. format(self: wrenfold.gen.BaseGenerator, element: wrenfold.type_info.MatrixType) -> str

Format type wrenfold.type_info.MatrixType.

  1. format(self: wrenfold.gen.BaseGenerator, element: wrenfold.type_info.CustomType) -> str

Format type wrenfold.type_info.CustomType.

generate(*args, **kwargs)#

Overloaded function.

  1. generate(self: wrenfold.gen.BaseGenerator, definition: wrenfold.ast.FunctionDefinition) -> str

Generate code for the provided definition.

  1. generate(self: wrenfold.gen.BaseGenerator, definition: List[wrenfold.ast.FunctionDefinition]) -> str

Generate code for multiple definitions.

super_format(*args, **kwargs)#

Overloaded function.

  1. super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.Add) -> str

Format type wrenfold.ast.Add. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.AssignTemporary) -> str

Format type wrenfold.ast.AssignTemporary. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.AssignOutputMatrix) -> str

Format type wrenfold.ast.AssignOutputMatrix. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.AssignOutputScalar) -> str

Format type wrenfold.ast.AssignOutputScalar. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.AssignOutputStruct) -> str

Format type wrenfold.ast.AssignOutputStruct. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.BooleanLiteral) -> str

Format type wrenfold.ast.BooleanLiteral. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.Branch) -> str

Format type wrenfold.ast.Branch. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.CallExternalFunction) -> str

Format type wrenfold.ast.CallExternalFunction. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.CallStdFunction) -> str

Format type wrenfold.ast.CallStdFunction. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.Cast) -> str

Format type wrenfold.ast.Cast. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.Comment) -> str

Format type wrenfold.ast.Comment. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.Compare) -> str

Format type wrenfold.ast.Compare. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.ConstructCustomType) -> str

Format type wrenfold.ast.ConstructCustomType. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.ConstructMatrix) -> str

Format type wrenfold.ast.ConstructMatrix. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.Declaration) -> str

Format type wrenfold.ast.Declaration. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.Divide) -> str

Format type wrenfold.ast.Divide. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.FloatLiteral) -> str

Format type wrenfold.ast.FloatLiteral. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.GetArgument) -> str

Format type wrenfold.ast.GetArgument. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.GetField) -> str

Format type wrenfold.ast.GetField. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.GetMatrixElement) -> str

Format type wrenfold.ast.GetMatrixElement. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.IntegerLiteral) -> str

Format type wrenfold.ast.IntegerLiteral. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.Multiply) -> str

Format type wrenfold.ast.Multiply. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.Negate) -> str

Format type wrenfold.ast.Negate. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.OptionalOutputBranch) -> str

Format type wrenfold.ast.OptionalOutputBranch. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.Parenthetical) -> str

Format type wrenfold.ast.Parenthetical. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.SpecialConstant) -> str

Format type wrenfold.ast.SpecialConstant. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.ReturnObject) -> str

Format type wrenfold.ast.ReturnObject. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.VariableRef) -> str

Format type wrenfold.ast.VariableRef. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.FunctionSignature) -> str

Format type wrenfold.ast.FunctionSignature. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.type_info.ScalarType) -> str

Format type wrenfold.type_info.ScalarType. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.type_info.MatrixType) -> str

Format type wrenfold.type_info.MatrixType. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.type_info.CustomType) -> str

Format type wrenfold.type_info.CustomType. Invokes the wrapped base class implementation.

class wrenfold.code_generation.CppGenerator#

Generate C++ code.

__init__(self: wrenfold.gen.CppGenerator) None#
static apply_preamble(code: str, namespace: str, imports: str = '') str#

Apply a preamble that incorporates necessary runtime includes.

format(*args, **kwargs)#

Overloaded function.

  1. format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.Add) -> str

Format type wrenfold.ast.Add.

  1. format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.AssignTemporary) -> str

Format type wrenfold.ast.AssignTemporary.

  1. format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.AssignOutputMatrix) -> str

Format type wrenfold.ast.AssignOutputMatrix.

  1. format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.AssignOutputScalar) -> str

Format type wrenfold.ast.AssignOutputScalar.

  1. format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.AssignOutputStruct) -> str

Format type wrenfold.ast.AssignOutputStruct.

  1. format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.BooleanLiteral) -> str

Format type wrenfold.ast.BooleanLiteral.

  1. format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.Branch) -> str

Format type wrenfold.ast.Branch.

  1. format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.CallExternalFunction) -> str

Format type wrenfold.ast.CallExternalFunction.

  1. format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.CallStdFunction) -> str

Format type wrenfold.ast.CallStdFunction.

  1. format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.Cast) -> str

Format type wrenfold.ast.Cast.

  1. format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.Comment) -> str

Format type wrenfold.ast.Comment.

  1. format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.Compare) -> str

Format type wrenfold.ast.Compare.

  1. format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.ConstructCustomType) -> str

Format type wrenfold.ast.ConstructCustomType.

  1. format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.ConstructMatrix) -> str

Format type wrenfold.ast.ConstructMatrix.

  1. format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.Declaration) -> str

Format type wrenfold.ast.Declaration.

  1. format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.Divide) -> str

Format type wrenfold.ast.Divide.

  1. format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.FloatLiteral) -> str

Format type wrenfold.ast.FloatLiteral.

  1. format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.GetArgument) -> str

Format type wrenfold.ast.GetArgument.

  1. format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.GetField) -> str

Format type wrenfold.ast.GetField.

  1. format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.GetMatrixElement) -> str

Format type wrenfold.ast.GetMatrixElement.

  1. format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.IntegerLiteral) -> str

Format type wrenfold.ast.IntegerLiteral.

  1. format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.Multiply) -> str

Format type wrenfold.ast.Multiply.

  1. format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.Negate) -> str

Format type wrenfold.ast.Negate.

  1. format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.OptionalOutputBranch) -> str

Format type wrenfold.ast.OptionalOutputBranch.

  1. format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.Parenthetical) -> str

Format type wrenfold.ast.Parenthetical.

  1. format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.SpecialConstant) -> str

Format type wrenfold.ast.SpecialConstant.

  1. format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.ReturnObject) -> str

Format type wrenfold.ast.ReturnObject.

  1. format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.VariableRef) -> str

Format type wrenfold.ast.VariableRef.

  1. format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.FunctionSignature) -> str

Format type wrenfold.ast.FunctionSignature.

  1. format(self: wrenfold.gen.CppGenerator, element: wrenfold.type_info.ScalarType) -> str

Format type wrenfold.type_info.ScalarType.

  1. format(self: wrenfold.gen.CppGenerator, element: wrenfold.type_info.MatrixType) -> str

Format type wrenfold.type_info.MatrixType.

  1. format(self: wrenfold.gen.CppGenerator, element: wrenfold.type_info.CustomType) -> str

Format type wrenfold.type_info.CustomType.

generate(*args, **kwargs)#

Overloaded function.

  1. generate(self: wrenfold.gen.CppGenerator, definition: wrenfold.ast.FunctionDefinition) -> str

Generate code for the provided definition.

  1. generate(self: wrenfold.gen.CppGenerator, definition: List[wrenfold.ast.FunctionDefinition]) -> str

Generate code for multiple definitions.

super_format(*args, **kwargs)#

Overloaded function.

  1. super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.Add) -> str

Format type wrenfold.ast.Add. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.AssignTemporary) -> str

Format type wrenfold.ast.AssignTemporary. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.AssignOutputMatrix) -> str

Format type wrenfold.ast.AssignOutputMatrix. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.AssignOutputScalar) -> str

Format type wrenfold.ast.AssignOutputScalar. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.AssignOutputStruct) -> str

Format type wrenfold.ast.AssignOutputStruct. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.BooleanLiteral) -> str

Format type wrenfold.ast.BooleanLiteral. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.Branch) -> str

Format type wrenfold.ast.Branch. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.CallExternalFunction) -> str

Format type wrenfold.ast.CallExternalFunction. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.CallStdFunction) -> str

Format type wrenfold.ast.CallStdFunction. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.Cast) -> str

Format type wrenfold.ast.Cast. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.Comment) -> str

Format type wrenfold.ast.Comment. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.Compare) -> str

Format type wrenfold.ast.Compare. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.ConstructCustomType) -> str

Format type wrenfold.ast.ConstructCustomType. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.ConstructMatrix) -> str

Format type wrenfold.ast.ConstructMatrix. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.Declaration) -> str

Format type wrenfold.ast.Declaration. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.Divide) -> str

Format type wrenfold.ast.Divide. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.FloatLiteral) -> str

Format type wrenfold.ast.FloatLiteral. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.GetArgument) -> str

Format type wrenfold.ast.GetArgument. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.GetField) -> str

Format type wrenfold.ast.GetField. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.GetMatrixElement) -> str

Format type wrenfold.ast.GetMatrixElement. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.IntegerLiteral) -> str

Format type wrenfold.ast.IntegerLiteral. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.Multiply) -> str

Format type wrenfold.ast.Multiply. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.Negate) -> str

Format type wrenfold.ast.Negate. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.OptionalOutputBranch) -> str

Format type wrenfold.ast.OptionalOutputBranch. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.Parenthetical) -> str

Format type wrenfold.ast.Parenthetical. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.SpecialConstant) -> str

Format type wrenfold.ast.SpecialConstant. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.ReturnObject) -> str

Format type wrenfold.ast.ReturnObject. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.VariableRef) -> str

Format type wrenfold.ast.VariableRef. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.FunctionSignature) -> str

Format type wrenfold.ast.FunctionSignature. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.type_info.ScalarType) -> str

Format type wrenfold.type_info.ScalarType. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.type_info.MatrixType) -> str

Format type wrenfold.type_info.MatrixType. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.type_info.CustomType) -> str

Format type wrenfold.type_info.CustomType. Invokes the wrapped base class implementation.

class wrenfold.code_generation.FunctionDescription#
Stores information required to emit the function signature, including:
  • The types of input and output values.

  • All the symbolic expressions required to compute the outputs.

FunctionDescription may be passed to wrenfold.code_generation.transpile() in order to create a syntax tree representation, which may then be converted into usable code.

__init__(self: wrenfold.gen.FunctionDescription, name: str) None#

Construct with function name.

add_input_argument(*args, **kwargs)#

Overloaded function.

  1. add_input_argument(self: wrenfold.gen.FunctionDescription, name: str, type: wrenfold.type_info.ScalarType) -> Union[wrenfold.sym.Expr, wrenfold.sym.MatrixExpr, wrenfold.sym.CompoundExpr]

Add a scalar input argument. Returns placeholder value to pass to the python function.

  1. add_input_argument(self: wrenfold.gen.FunctionDescription, name: str, type: wrenfold.type_info.MatrixType) -> Union[wrenfold.sym.Expr, wrenfold.sym.MatrixExpr, wrenfold.sym.CompoundExpr]

Add a matrix input argument. Returns placeholder value to pass to the python function.

  1. add_input_argument(self: wrenfold.gen.FunctionDescription, name: str, type: wrenfold.type_info.CustomType) -> Union[wrenfold.sym.Expr, wrenfold.sym.MatrixExpr, wrenfold.sym.CompoundExpr]

Add an input argument with a custom user-specified type.

add_output_argument(*args, **kwargs)#

Overloaded function.

  1. add_output_argument(self: wrenfold.gen.FunctionDescription, name: str, is_optional: bool, value: wrenfold.sym.Expr) -> None

  2. add_output_argument(self: wrenfold.gen.FunctionDescription, name: str, is_optional: bool, value: wrenfold.sym.MatrixExpr) -> None

Record an output argument of matrix type.

  1. add_output_argument(self: wrenfold.gen.FunctionDescription, name: str, is_optional: bool, custom_type: wrenfold.type_info.CustomType, expressions: List[wrenfold.sym.Expr]) -> None

Record an output argument of custom type.

property name#

Name of the function.

output_expressions(self: wrenfold.gen.FunctionDescription) Dict[wrenfold.gen.OutputKey, wrenfold.sym.Expr | wrenfold.sym.MatrixExpr | wrenfold.sym.CompoundExpr | wrenfold.sym.BooleanExpr]#

Retrieve a dict of output expressions computed by this function.

set_return_value(*args, **kwargs)#

Overloaded function.

  1. set_return_value(self: wrenfold.gen.FunctionDescription, value: wrenfold.sym.Expr) -> None

  2. set_return_value(self: wrenfold.gen.FunctionDescription, value: wrenfold.sym.MatrixExpr) -> None

  3. set_return_value(self: wrenfold.gen.FunctionDescription, custom_type: wrenfold.type_info.CustomType, expressions: List[wrenfold.sym.Expr]) -> None

class wrenfold.code_generation.OutputArg(expression: Expr | MatrixExpr | Any, name: str, is_optional: bool = False)#

Designate an output argument in the result of a symbolic function invocation.

expression#

Value of the output argument. This may be an expression, or an instance of a user-provided custom type.

Type:

wrenfold.sym.Expr | wrenfold.sym.MatrixExpr | Any

name#

Name of the argument.

Type:

str

is_optional#

Specify whether the output argument is optional or not.

Type:

bool

__eq__(other)#

Return self==value.

__hash__ = None#
__init__(expression: Expr | MatrixExpr | Any, name: str, is_optional: bool = False) None#
class wrenfold.code_generation.ReturnValue(expression: Expr | MatrixExpr | Any)#

Designate a return value in the result of symbolic function invocation.

expression#

The returned value. This may be an expression, or an instance of a user-provided custom type.

Type:

wrenfold.sym.Expr | wrenfold.sym.MatrixExpr | Any

__eq__(other)#

Return self==value.

__hash__ = None#
__init__(expression: Expr | MatrixExpr | Any) None#
class wrenfold.code_generation.RustGenerator#

Generate Rust code.

__init__(self: wrenfold.gen.RustGenerator) None#
static apply_preamble(code: str) str#

Apply a preamble to generated code.

format(*args, **kwargs)#

Overloaded function.

  1. format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.Add) -> str

Format type wrenfold.ast.Add.

  1. format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.AssignTemporary) -> str

Format type wrenfold.ast.AssignTemporary.

  1. format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.AssignOutputMatrix) -> str

Format type wrenfold.ast.AssignOutputMatrix.

  1. format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.AssignOutputScalar) -> str

Format type wrenfold.ast.AssignOutputScalar.

  1. format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.AssignOutputStruct) -> str

Format type wrenfold.ast.AssignOutputStruct.

  1. format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.BooleanLiteral) -> str

Format type wrenfold.ast.BooleanLiteral.

  1. format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.Branch) -> str

Format type wrenfold.ast.Branch.

  1. format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.CallExternalFunction) -> str

Format type wrenfold.ast.CallExternalFunction.

  1. format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.CallStdFunction) -> str

Format type wrenfold.ast.CallStdFunction.

  1. format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.Cast) -> str

Format type wrenfold.ast.Cast.

  1. format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.Comment) -> str

Format type wrenfold.ast.Comment.

  1. format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.Compare) -> str

Format type wrenfold.ast.Compare.

  1. format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.ConstructCustomType) -> str

Format type wrenfold.ast.ConstructCustomType.

  1. format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.ConstructMatrix) -> str

Format type wrenfold.ast.ConstructMatrix.

  1. format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.Declaration) -> str

Format type wrenfold.ast.Declaration.

  1. format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.Divide) -> str

Format type wrenfold.ast.Divide.

  1. format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.FloatLiteral) -> str

Format type wrenfold.ast.FloatLiteral.

  1. format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.GetArgument) -> str

Format type wrenfold.ast.GetArgument.

  1. format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.GetField) -> str

Format type wrenfold.ast.GetField.

  1. format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.GetMatrixElement) -> str

Format type wrenfold.ast.GetMatrixElement.

  1. format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.IntegerLiteral) -> str

Format type wrenfold.ast.IntegerLiteral.

  1. format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.Multiply) -> str

Format type wrenfold.ast.Multiply.

  1. format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.Negate) -> str

Format type wrenfold.ast.Negate.

  1. format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.OptionalOutputBranch) -> str

Format type wrenfold.ast.OptionalOutputBranch.

  1. format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.Parenthetical) -> str

Format type wrenfold.ast.Parenthetical.

  1. format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.SpecialConstant) -> str

Format type wrenfold.ast.SpecialConstant.

  1. format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.ReturnObject) -> str

Format type wrenfold.ast.ReturnObject.

  1. format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.VariableRef) -> str

Format type wrenfold.ast.VariableRef.

  1. format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.FunctionSignature) -> str

Format type wrenfold.ast.FunctionSignature.

  1. format(self: wrenfold.gen.RustGenerator, element: wrenfold.type_info.ScalarType) -> str

Format type wrenfold.type_info.ScalarType.

  1. format(self: wrenfold.gen.RustGenerator, element: wrenfold.type_info.MatrixType) -> str

Format type wrenfold.type_info.MatrixType.

  1. format(self: wrenfold.gen.RustGenerator, element: wrenfold.type_info.CustomType) -> str

Format type wrenfold.type_info.CustomType.

generate(*args, **kwargs)#

Overloaded function.

  1. generate(self: wrenfold.gen.RustGenerator, definition: wrenfold.ast.FunctionDefinition) -> str

Generate code for the provided definition.

  1. generate(self: wrenfold.gen.RustGenerator, definition: List[wrenfold.ast.FunctionDefinition]) -> str

Generate code for multiple definitions.

super_format(*args, **kwargs)#

Overloaded function.

  1. super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.Add) -> str

Format type wrenfold.ast.Add. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.AssignTemporary) -> str

Format type wrenfold.ast.AssignTemporary. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.AssignOutputMatrix) -> str

Format type wrenfold.ast.AssignOutputMatrix. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.AssignOutputScalar) -> str

Format type wrenfold.ast.AssignOutputScalar. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.AssignOutputStruct) -> str

Format type wrenfold.ast.AssignOutputStruct. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.BooleanLiteral) -> str

Format type wrenfold.ast.BooleanLiteral. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.Branch) -> str

Format type wrenfold.ast.Branch. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.CallExternalFunction) -> str

Format type wrenfold.ast.CallExternalFunction. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.CallStdFunction) -> str

Format type wrenfold.ast.CallStdFunction. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.Cast) -> str

Format type wrenfold.ast.Cast. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.Comment) -> str

Format type wrenfold.ast.Comment. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.Compare) -> str

Format type wrenfold.ast.Compare. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.ConstructCustomType) -> str

Format type wrenfold.ast.ConstructCustomType. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.ConstructMatrix) -> str

Format type wrenfold.ast.ConstructMatrix. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.Declaration) -> str

Format type wrenfold.ast.Declaration. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.Divide) -> str

Format type wrenfold.ast.Divide. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.FloatLiteral) -> str

Format type wrenfold.ast.FloatLiteral. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.GetArgument) -> str

Format type wrenfold.ast.GetArgument. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.GetField) -> str

Format type wrenfold.ast.GetField. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.GetMatrixElement) -> str

Format type wrenfold.ast.GetMatrixElement. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.IntegerLiteral) -> str

Format type wrenfold.ast.IntegerLiteral. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.Multiply) -> str

Format type wrenfold.ast.Multiply. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.Negate) -> str

Format type wrenfold.ast.Negate. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.OptionalOutputBranch) -> str

Format type wrenfold.ast.OptionalOutputBranch. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.Parenthetical) -> str

Format type wrenfold.ast.Parenthetical. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.SpecialConstant) -> str

Format type wrenfold.ast.SpecialConstant. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.ReturnObject) -> str

Format type wrenfold.ast.ReturnObject. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.VariableRef) -> str

Format type wrenfold.ast.VariableRef. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.FunctionSignature) -> str

Format type wrenfold.ast.FunctionSignature. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.type_info.ScalarType) -> str

Format type wrenfold.type_info.ScalarType. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.type_info.MatrixType) -> str

Format type wrenfold.type_info.MatrixType. Invokes the wrapped base class implementation.

  1. super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.type_info.CustomType) -> str

Format type wrenfold.type_info.CustomType. Invokes the wrapped base class implementation.