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:
A sequence of
wrenfold.code_generation.ReturnValue
orwrenfold.code_generation.OutputArg
objects, ORA single
wrenfold.sym.Expr
orwrenfold.sym.MatrixExpr
object, which will be interpreted as aReturnValue
.
- 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:
desc –
wrenfold.code_generation.FunctionDescription
object.params – Optional parameters to control subexpression elimination.
- 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: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
.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.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:
func – A python function with type-annotated arguments. See
wrenfold.code_generation.create_function_description()
for notes on the expected signature.generator – Instance of a code generator, eg.
wrenfold.code_generation.CppGenerator
.name – Name of the function. If unspecified,
func.__name__
will be used.optimization_params – Parameters governing simplifications/optimizations applied to the output code.
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.
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.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:The symbolic expression tree is converted to a flat intermediate representation. Common subexpression elimination is performed to minimize duplicated operations.
A control flow graph (CFG) is generated.
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:
desc –
wrenfold.code_generation.FunctionDescription
object.params – Optional parameters to control subexpression elimination.
- 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__(self: object) 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.
format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.Add) -> str
Format type
wrenfold.ast.Add
.format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.AssignTemporary) -> str
Format type
wrenfold.ast.AssignTemporary
.format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.AssignOutputMatrix) -> str
Format type
wrenfold.ast.AssignOutputMatrix
.format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.AssignOutputScalar) -> str
Format type
wrenfold.ast.AssignOutputScalar
.format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.AssignOutputStruct) -> str
Format type
wrenfold.ast.AssignOutputStruct
.format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.BooleanLiteral) -> str
Format type
wrenfold.ast.BooleanLiteral
.format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.Branch) -> str
Format type
wrenfold.ast.Branch
.format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.CallExternalFunction) -> str
Format type
wrenfold.ast.CallExternalFunction
.format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.CallStdFunction) -> str
Format type
wrenfold.ast.CallStdFunction
.format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.Cast) -> str
Format type
wrenfold.ast.Cast
.format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.Comment) -> str
Format type
wrenfold.ast.Comment
.format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.Compare) -> str
Format type
wrenfold.ast.Compare
.format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.ConstructCustomType) -> str
Format type
wrenfold.ast.ConstructCustomType
.format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.ConstructMatrix) -> str
Format type
wrenfold.ast.ConstructMatrix
.format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.Declaration) -> str
Format type
wrenfold.ast.Declaration
.format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.Divide) -> str
Format type
wrenfold.ast.Divide
.format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.FloatLiteral) -> str
Format type
wrenfold.ast.FloatLiteral
.format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.GetArgument) -> str
Format type
wrenfold.ast.GetArgument
.format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.GetField) -> str
Format type
wrenfold.ast.GetField
.format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.GetMatrixElement) -> str
Format type
wrenfold.ast.GetMatrixElement
.format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.IntegerLiteral) -> str
Format type
wrenfold.ast.IntegerLiteral
.format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.Multiply) -> str
Format type
wrenfold.ast.Multiply
.format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.Negate) -> str
Format type
wrenfold.ast.Negate
.format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.OptionalOutputBranch) -> str
Format type
wrenfold.ast.OptionalOutputBranch
.format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.Parenthetical) -> str
Format type
wrenfold.ast.Parenthetical
.format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.SpecialConstant) -> str
Format type
wrenfold.ast.SpecialConstant
.format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.ReturnObject) -> str
Format type
wrenfold.ast.ReturnObject
.format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.VariableRef) -> str
Format type
wrenfold.ast.VariableRef
.format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.FunctionSignature) -> str
Format type
wrenfold.ast.FunctionSignature
.format(self: wrenfold.gen.BaseGenerator, element: wrenfold.type_info.ScalarType) -> str
Format type
wrenfold.type_info.ScalarType
.format(self: wrenfold.gen.BaseGenerator, element: wrenfold.type_info.MatrixType) -> str
Format type
wrenfold.type_info.MatrixType
.format(self: wrenfold.gen.BaseGenerator, element: wrenfold.type_info.CustomType) -> str
Format type
wrenfold.type_info.CustomType
.
- generate(*args, **kwargs)¶
Overloaded function.
generate(self: wrenfold.gen.BaseGenerator, definition: wrenfold.ast.FunctionDefinition) -> str
Generate code for the provided definition.
generate(self: wrenfold.gen.BaseGenerator, definition: list[wrenfold.ast.FunctionDefinition]) -> str
Generate code for multiple definitions.
- super_format(*args, **kwargs)¶
Overloaded function.
super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.Add) -> str
Format type
wrenfold.ast.Add
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.AssignTemporary) -> str
Format type
wrenfold.ast.AssignTemporary
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.AssignOutputMatrix) -> str
Format type
wrenfold.ast.AssignOutputMatrix
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.AssignOutputScalar) -> str
Format type
wrenfold.ast.AssignOutputScalar
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.AssignOutputStruct) -> str
Format type
wrenfold.ast.AssignOutputStruct
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.BooleanLiteral) -> str
Format type
wrenfold.ast.BooleanLiteral
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.Branch) -> str
Format type
wrenfold.ast.Branch
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.CallExternalFunction) -> str
Format type
wrenfold.ast.CallExternalFunction
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.CallStdFunction) -> str
Format type
wrenfold.ast.CallStdFunction
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.Cast) -> str
Format type
wrenfold.ast.Cast
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.Comment) -> str
Format type
wrenfold.ast.Comment
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.Compare) -> str
Format type
wrenfold.ast.Compare
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.ConstructCustomType) -> str
Format type
wrenfold.ast.ConstructCustomType
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.ConstructMatrix) -> str
Format type
wrenfold.ast.ConstructMatrix
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.Declaration) -> str
Format type
wrenfold.ast.Declaration
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.Divide) -> str
Format type
wrenfold.ast.Divide
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.FloatLiteral) -> str
Format type
wrenfold.ast.FloatLiteral
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.GetArgument) -> str
Format type
wrenfold.ast.GetArgument
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.GetField) -> str
Format type
wrenfold.ast.GetField
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.GetMatrixElement) -> str
Format type
wrenfold.ast.GetMatrixElement
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.IntegerLiteral) -> str
Format type
wrenfold.ast.IntegerLiteral
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.Multiply) -> str
Format type
wrenfold.ast.Multiply
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.Negate) -> str
Format type
wrenfold.ast.Negate
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.OptionalOutputBranch) -> str
Format type
wrenfold.ast.OptionalOutputBranch
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.Parenthetical) -> str
Format type
wrenfold.ast.Parenthetical
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.SpecialConstant) -> str
Format type
wrenfold.ast.SpecialConstant
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.ReturnObject) -> str
Format type
wrenfold.ast.ReturnObject
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.VariableRef) -> str
Format type
wrenfold.ast.VariableRef
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.ast.FunctionSignature) -> str
Format type
wrenfold.ast.FunctionSignature
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.type_info.ScalarType) -> str
Format type
wrenfold.type_info.ScalarType
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.BaseGenerator, element: wrenfold.type_info.MatrixType) -> str
Format type
wrenfold.type_info.MatrixType
. Invokes the wrapped base class implementation.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.
format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.Add) -> str
Format type
wrenfold.ast.Add
.format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.AssignTemporary) -> str
Format type
wrenfold.ast.AssignTemporary
.format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.AssignOutputMatrix) -> str
Format type
wrenfold.ast.AssignOutputMatrix
.format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.AssignOutputScalar) -> str
Format type
wrenfold.ast.AssignOutputScalar
.format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.AssignOutputStruct) -> str
Format type
wrenfold.ast.AssignOutputStruct
.format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.BooleanLiteral) -> str
Format type
wrenfold.ast.BooleanLiteral
.format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.Branch) -> str
Format type
wrenfold.ast.Branch
.format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.CallExternalFunction) -> str
Format type
wrenfold.ast.CallExternalFunction
.format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.CallStdFunction) -> str
Format type
wrenfold.ast.CallStdFunction
.format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.Cast) -> str
Format type
wrenfold.ast.Cast
.format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.Comment) -> str
Format type
wrenfold.ast.Comment
.format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.Compare) -> str
Format type
wrenfold.ast.Compare
.format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.ConstructCustomType) -> str
Format type
wrenfold.ast.ConstructCustomType
.format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.ConstructMatrix) -> str
Format type
wrenfold.ast.ConstructMatrix
.format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.Declaration) -> str
Format type
wrenfold.ast.Declaration
.format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.Divide) -> str
Format type
wrenfold.ast.Divide
.format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.FloatLiteral) -> str
Format type
wrenfold.ast.FloatLiteral
.format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.GetArgument) -> str
Format type
wrenfold.ast.GetArgument
.format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.GetField) -> str
Format type
wrenfold.ast.GetField
.format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.GetMatrixElement) -> str
Format type
wrenfold.ast.GetMatrixElement
.format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.IntegerLiteral) -> str
Format type
wrenfold.ast.IntegerLiteral
.format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.Multiply) -> str
Format type
wrenfold.ast.Multiply
.format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.Negate) -> str
Format type
wrenfold.ast.Negate
.format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.OptionalOutputBranch) -> str
Format type
wrenfold.ast.OptionalOutputBranch
.format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.Parenthetical) -> str
Format type
wrenfold.ast.Parenthetical
.format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.SpecialConstant) -> str
Format type
wrenfold.ast.SpecialConstant
.format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.ReturnObject) -> str
Format type
wrenfold.ast.ReturnObject
.format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.VariableRef) -> str
Format type
wrenfold.ast.VariableRef
.format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.FunctionSignature) -> str
Format type
wrenfold.ast.FunctionSignature
.format(self: wrenfold.gen.CppGenerator, element: wrenfold.type_info.ScalarType) -> str
Format type
wrenfold.type_info.ScalarType
.format(self: wrenfold.gen.CppGenerator, element: wrenfold.type_info.MatrixType) -> str
Format type
wrenfold.type_info.MatrixType
.format(self: wrenfold.gen.CppGenerator, element: wrenfold.type_info.CustomType) -> str
Format type
wrenfold.type_info.CustomType
.
- generate(*args, **kwargs)¶
Overloaded function.
generate(self: wrenfold.gen.CppGenerator, definition: wrenfold.ast.FunctionDefinition) -> str
Generate code for the provided definition.
generate(self: wrenfold.gen.CppGenerator, definition: list[wrenfold.ast.FunctionDefinition]) -> str
Generate code for multiple definitions.
- super_format(*args, **kwargs)¶
Overloaded function.
super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.Add) -> str
Format type
wrenfold.ast.Add
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.AssignTemporary) -> str
Format type
wrenfold.ast.AssignTemporary
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.AssignOutputMatrix) -> str
Format type
wrenfold.ast.AssignOutputMatrix
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.AssignOutputScalar) -> str
Format type
wrenfold.ast.AssignOutputScalar
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.AssignOutputStruct) -> str
Format type
wrenfold.ast.AssignOutputStruct
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.BooleanLiteral) -> str
Format type
wrenfold.ast.BooleanLiteral
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.Branch) -> str
Format type
wrenfold.ast.Branch
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.CallExternalFunction) -> str
Format type
wrenfold.ast.CallExternalFunction
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.CallStdFunction) -> str
Format type
wrenfold.ast.CallStdFunction
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.Cast) -> str
Format type
wrenfold.ast.Cast
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.Comment) -> str
Format type
wrenfold.ast.Comment
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.Compare) -> str
Format type
wrenfold.ast.Compare
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.ConstructCustomType) -> str
Format type
wrenfold.ast.ConstructCustomType
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.ConstructMatrix) -> str
Format type
wrenfold.ast.ConstructMatrix
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.Declaration) -> str
Format type
wrenfold.ast.Declaration
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.Divide) -> str
Format type
wrenfold.ast.Divide
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.FloatLiteral) -> str
Format type
wrenfold.ast.FloatLiteral
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.GetArgument) -> str
Format type
wrenfold.ast.GetArgument
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.GetField) -> str
Format type
wrenfold.ast.GetField
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.GetMatrixElement) -> str
Format type
wrenfold.ast.GetMatrixElement
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.IntegerLiteral) -> str
Format type
wrenfold.ast.IntegerLiteral
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.Multiply) -> str
Format type
wrenfold.ast.Multiply
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.Negate) -> str
Format type
wrenfold.ast.Negate
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.OptionalOutputBranch) -> str
Format type
wrenfold.ast.OptionalOutputBranch
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.Parenthetical) -> str
Format type
wrenfold.ast.Parenthetical
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.SpecialConstant) -> str
Format type
wrenfold.ast.SpecialConstant
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.ReturnObject) -> str
Format type
wrenfold.ast.ReturnObject
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.VariableRef) -> str
Format type
wrenfold.ast.VariableRef
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.ast.FunctionSignature) -> str
Format type
wrenfold.ast.FunctionSignature
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.type_info.ScalarType) -> str
Format type
wrenfold.type_info.ScalarType
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.CppGenerator, element: wrenfold.type_info.MatrixType) -> str
Format type
wrenfold.type_info.MatrixType
. Invokes the wrapped base class implementation.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 towrenfold.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.
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.
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.
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.
add_output_argument(self: wrenfold.gen.FunctionDescription, name: str, is_optional: bool, value: wrenfold.sym.Expr) -> None
add_output_argument(self: wrenfold.gen.FunctionDescription, name: str, is_optional: bool, value: wrenfold.sym.MatrixExpr) -> None
Record an output argument of matrix type.
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.
set_return_value(self: wrenfold.gen.FunctionDescription, value: wrenfold.sym.Expr) -> None
set_return_value(self: wrenfold.gen.FunctionDescription, value: wrenfold.sym.MatrixExpr) -> None
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:
- 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:
- __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.
format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.Add) -> str
Format type
wrenfold.ast.Add
.format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.AssignTemporary) -> str
Format type
wrenfold.ast.AssignTemporary
.format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.AssignOutputMatrix) -> str
Format type
wrenfold.ast.AssignOutputMatrix
.format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.AssignOutputScalar) -> str
Format type
wrenfold.ast.AssignOutputScalar
.format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.AssignOutputStruct) -> str
Format type
wrenfold.ast.AssignOutputStruct
.format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.BooleanLiteral) -> str
Format type
wrenfold.ast.BooleanLiteral
.format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.Branch) -> str
Format type
wrenfold.ast.Branch
.format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.CallExternalFunction) -> str
Format type
wrenfold.ast.CallExternalFunction
.format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.CallStdFunction) -> str
Format type
wrenfold.ast.CallStdFunction
.format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.Cast) -> str
Format type
wrenfold.ast.Cast
.format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.Comment) -> str
Format type
wrenfold.ast.Comment
.format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.Compare) -> str
Format type
wrenfold.ast.Compare
.format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.ConstructCustomType) -> str
Format type
wrenfold.ast.ConstructCustomType
.format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.ConstructMatrix) -> str
Format type
wrenfold.ast.ConstructMatrix
.format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.Declaration) -> str
Format type
wrenfold.ast.Declaration
.format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.Divide) -> str
Format type
wrenfold.ast.Divide
.format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.FloatLiteral) -> str
Format type
wrenfold.ast.FloatLiteral
.format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.GetArgument) -> str
Format type
wrenfold.ast.GetArgument
.format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.GetField) -> str
Format type
wrenfold.ast.GetField
.format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.GetMatrixElement) -> str
Format type
wrenfold.ast.GetMatrixElement
.format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.IntegerLiteral) -> str
Format type
wrenfold.ast.IntegerLiteral
.format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.Multiply) -> str
Format type
wrenfold.ast.Multiply
.format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.Negate) -> str
Format type
wrenfold.ast.Negate
.format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.OptionalOutputBranch) -> str
Format type
wrenfold.ast.OptionalOutputBranch
.format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.Parenthetical) -> str
Format type
wrenfold.ast.Parenthetical
.format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.SpecialConstant) -> str
Format type
wrenfold.ast.SpecialConstant
.format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.ReturnObject) -> str
Format type
wrenfold.ast.ReturnObject
.format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.VariableRef) -> str
Format type
wrenfold.ast.VariableRef
.format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.FunctionSignature) -> str
Format type
wrenfold.ast.FunctionSignature
.format(self: wrenfold.gen.RustGenerator, element: wrenfold.type_info.ScalarType) -> str
Format type
wrenfold.type_info.ScalarType
.format(self: wrenfold.gen.RustGenerator, element: wrenfold.type_info.MatrixType) -> str
Format type
wrenfold.type_info.MatrixType
.format(self: wrenfold.gen.RustGenerator, element: wrenfold.type_info.CustomType) -> str
Format type
wrenfold.type_info.CustomType
.
- generate(*args, **kwargs)¶
Overloaded function.
generate(self: wrenfold.gen.RustGenerator, definition: wrenfold.ast.FunctionDefinition) -> str
Generate code for the provided definition.
generate(self: wrenfold.gen.RustGenerator, definition: list[wrenfold.ast.FunctionDefinition]) -> str
Generate code for multiple definitions.
- super_format(*args, **kwargs)¶
Overloaded function.
super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.Add) -> str
Format type
wrenfold.ast.Add
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.AssignTemporary) -> str
Format type
wrenfold.ast.AssignTemporary
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.AssignOutputMatrix) -> str
Format type
wrenfold.ast.AssignOutputMatrix
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.AssignOutputScalar) -> str
Format type
wrenfold.ast.AssignOutputScalar
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.AssignOutputStruct) -> str
Format type
wrenfold.ast.AssignOutputStruct
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.BooleanLiteral) -> str
Format type
wrenfold.ast.BooleanLiteral
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.Branch) -> str
Format type
wrenfold.ast.Branch
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.CallExternalFunction) -> str
Format type
wrenfold.ast.CallExternalFunction
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.CallStdFunction) -> str
Format type
wrenfold.ast.CallStdFunction
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.Cast) -> str
Format type
wrenfold.ast.Cast
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.Comment) -> str
Format type
wrenfold.ast.Comment
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.Compare) -> str
Format type
wrenfold.ast.Compare
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.ConstructCustomType) -> str
Format type
wrenfold.ast.ConstructCustomType
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.ConstructMatrix) -> str
Format type
wrenfold.ast.ConstructMatrix
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.Declaration) -> str
Format type
wrenfold.ast.Declaration
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.Divide) -> str
Format type
wrenfold.ast.Divide
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.FloatLiteral) -> str
Format type
wrenfold.ast.FloatLiteral
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.GetArgument) -> str
Format type
wrenfold.ast.GetArgument
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.GetField) -> str
Format type
wrenfold.ast.GetField
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.GetMatrixElement) -> str
Format type
wrenfold.ast.GetMatrixElement
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.IntegerLiteral) -> str
Format type
wrenfold.ast.IntegerLiteral
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.Multiply) -> str
Format type
wrenfold.ast.Multiply
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.Negate) -> str
Format type
wrenfold.ast.Negate
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.OptionalOutputBranch) -> str
Format type
wrenfold.ast.OptionalOutputBranch
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.Parenthetical) -> str
Format type
wrenfold.ast.Parenthetical
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.SpecialConstant) -> str
Format type
wrenfold.ast.SpecialConstant
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.ReturnObject) -> str
Format type
wrenfold.ast.ReturnObject
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.VariableRef) -> str
Format type
wrenfold.ast.VariableRef
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.ast.FunctionSignature) -> str
Format type
wrenfold.ast.FunctionSignature
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.type_info.ScalarType) -> str
Format type
wrenfold.type_info.ScalarType
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.type_info.MatrixType) -> str
Format type
wrenfold.type_info.MatrixType
. Invokes the wrapped base class implementation.super_format(self: wrenfold.gen.RustGenerator, element: wrenfold.type_info.CustomType) -> str
Format type
wrenfold.type_info.CustomType
. Invokes the wrapped base class implementation.