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 it. See type_annotations for built-in types that can be used to annotate functions. The function should return either:

Parameters:
  • func – A symbolic 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 | PythonGenerator | BaseGenerator, name: str | None = None, optimization_params: OptimizationParams | None = None, convert_ternaries: bool = True) 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 symbolic input arguments. Next, it is invoked and the symbolic outputs are recorded.

  2. The expression tree is flattened and optimized by wrenfold.code_generation.transpile(). Duplicate operations are eliminated during this step, and conditionals are converted to control flow. The simplified output is converted to a syntax tree.

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

Tip

For examples of the types of functions that wrenfold can generate, see the wrenfold repo.

Parameters:
  • func – A symbolic 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.

  • convert_ternaries – Whether to convert ternary wrenfold.sym.where() statements to if-else control flow. Defaults to true. You likely want to set this to False when targeting python frameworks that need to trace control-flow, for example PyTorch or JAX.

Returns:

  • A string of 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.generate_python(func: ~typing.Callable[[...], ~wrenfold.sym.Expr | ~wrenfold.sym.MatrixExpr | ~typing.Sequence[~wrenfold.code_generation.ReturnValue | ~wrenfold.code_generation.OutputArg]], target: ~wrenfold.gen.PythonGeneratorTarget, convert_ternaries: bool | None = None, context: ~typing.Dict[str, ~typing.Any] | None = None, import_target_module: bool = True, generator_type: ~typing.Callable[[~wrenfold.gen.PythonGeneratorTarget], ~wrenfold.gen.BaseGenerator] = <class 'wrenfold.gen.PythonGenerator'>) Tuple[Callable, str]

Code-generate a symbolic function as python code, then exec the code and return a python function that implements the symbolic function numerically.

Parameters:
  • func – A symbolic python function with type-annotated arguments. See wrenfold.code_generation.create_function_description() for notes on the expected signature.

  • target – Which Python API to target (ie. NumPy, PyTorch, etc).

  • convert_ternaries – Whether to convert wrenfold.sym.where() expressions to Python control flow. For frameworks like PyTorch and JAX, we need to leave conditionals in a traceable format (ie. th.where calls). By default, if convert_ternaries=None, wrenfold will not convert sym.where calls to if-else statements when targeting PyTorch and JAX. This allows generated functions to be batched and JIT compiled.

  • context – Dict of key-value pairs that will be passed to exec in the globals arg.

  • import_target_module – If true (the default), import the target API. See the warning below.

  • generator_type – By default this is wrenfold.code_generation.PythonGenerator. You may specify a different function to call to construct the code generator.

Returns:

  • A callable python function that implements func numerically.

  • A string containing the corresponding python code.

Warning

By default, wrenfold will automatically import the appropriate framework specified by target:

  • If the target is NumPy, numpy will be imported as np.

  • If the target is JAX, jax.numpy will be imported as jnp.

  • If the target is PyTorch, torch will be imported as th.

To suppress the default import behavior, specify import_target_module=False. You will then need to pass your own import in the context dict.

Tip

Code-generation is performed using the wrenfold.code_generation.PythonGenerator class. Because python lacks formal “output arguments”, any symbolic outputs tagged as wrenfold.code_generation.OutputArg will instead be returned from the generated function in a dict of key-value pairs. The example listing below illustrates this behavior.

Additionally, remember that your target framework may not be able to reason about your custom types. For example, jax.jit only operates on Jax arrays and standard python types (tuples, lists, dict, etc).

Example

>>> import numpy as np
>>> import jax
>>>
>>> from wrenfold import code_generation, sym
>>> from wrenfold.type_annotations import Vector3
>>>
>>> def foo(x: Vector3, y: Vector3):
>>>     # A simple test function, with one return value and one output argument.
>>>     # In-practice, you would probably be generating something more complicated than this.
>>>     dot, = x.T * y
>>>     f = sym.tanh(dot)
>>>     J = sym.jacobian([f], x)
>>>     return [code_generation.ReturnValue(f), code_generation.OutputArg(J, "J")]
>>>
>>> # Generate python code:
>>> func, code = code_generation.generate_python(
>>>   foo, target=code_generation.PythonGeneratorTarget.JAX)
>>> print(code) # See python listing below.
>>>
>>> # Generate a batched and JIT compiled version of our function using JAX.
>>> # Here we batch over both `x` and `y`.
>>> batched_func = jax.vmap(func, in_axes=(0, 0), out_axes=0)
>>> compiled_func = jax.jit(batched_func)
>>>
>>> # Execute the function on NumPy tensors.
>>> # `output1` contains the return value, while `output2` contains all the output arguments.
>>> x = np.random.uniform(size=(10, 3))
>>> y = np.random.uniform(size=(10, 3))
>>> output1, output2 = compiled_func(x, y)
>>>
>>> print(output1) # produces: [0.73317385 0.45288894, ...]
 1# The generated code for `foo`:
 2def foo(x: jnp.ndarray, y: jnp.ndarray) -> T.Tuple[jnp.ndarray, T.Dict[str, jnp.ndarray]]:
 3    x = x.reshape(3, 1)
 4    y = y.reshape(3, 1)
 5    v009 = y[2, 0]
 6    v008 = x[2, 0]
 7    v006 = y[1, 0]
 8    v005 = x[1, 0]
 9    v003 = x[0, 0]
10    v000 = y[0, 0]
11    v012 = jnp.tanh(v000 * v003 + v005 * v006 + v008 * v009)
12    v016 = jnp.asarray(1, dtype=jnp.float32) + -(v012 * v012)
13    J = jnp.array([
14        v000 * v016,
15        v006 * v016,
16        v009 * v016]).reshape(1, 3)
17    return (
18        v012,
19        dict(J=J)
20    )
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], optimization_params: Optional[wrenfold.gen.OptimizationParams] = None, convert_ternaries: bool = True) -> list[wrenfold.ast.FunctionDefinition]

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

  1. transpile(desc: wrenfold.gen.FunctionDescription, optimization_params: Optional[wrenfold.gen.OptimizationParams] = None, convert_ternaries: bool = True) -> 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)
create_symbolic_input(self: wrenfold.gen.Argument) wrenfold.sym.Expr | wrenfold.sym.MatrixExpr | wrenfold.sym.CompoundExpr

Create corresponding symbolic input expressions for this argument.

property direction

How the argument is used by the function.

property is_input

True if the function is an input argument.

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.

  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.Ternary) -> str

Format type wrenfold.ast.Ternary.

  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.Ternary) -> str

Format type wrenfold.ast.Ternary. 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

Generates 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.Ternary) -> str

Format type wrenfold.ast.Ternary.

  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.Ternary) -> str

Format type wrenfold.ast.Ternary. 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 arguments

Arguments to the function.

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.PythonGenerator

Generates Python code. Can target NumPy, PyTorch, or JAX.

__init__(self: wrenfold.gen.PythonGenerator, target: wrenfold.gen.PythonGeneratorTarget, float_width: wrenfold.gen.PythonGeneratorFloatWidth = <PythonGeneratorFloatWidth.Float32: 0>, indentation: int = 2) None
property float_width

Float precision applied to all NumPy arrays and tensors.

format(*args, **kwargs)

Overloaded function.

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

Format type wrenfold.ast.Add.

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

Format type wrenfold.ast.AssignTemporary.

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

Format type wrenfold.ast.AssignOutputMatrix.

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

Format type wrenfold.ast.AssignOutputScalar.

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

Format type wrenfold.ast.AssignOutputStruct.

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

Format type wrenfold.ast.BooleanLiteral.

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

Format type wrenfold.ast.Branch.

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

Format type wrenfold.ast.CallExternalFunction.

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

Format type wrenfold.ast.CallStdFunction.

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

Format type wrenfold.ast.Cast.

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

Format type wrenfold.ast.Comment.

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

Format type wrenfold.ast.Compare.

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

Format type wrenfold.ast.ConstructCustomType.

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

Format type wrenfold.ast.ConstructMatrix.

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

Format type wrenfold.ast.Declaration.

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

Format type wrenfold.ast.Divide.

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

Format type wrenfold.ast.FloatLiteral.

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

Format type wrenfold.ast.GetArgument.

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

Format type wrenfold.ast.GetField.

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

Format type wrenfold.ast.GetMatrixElement.

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

Format type wrenfold.ast.IntegerLiteral.

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

Format type wrenfold.ast.Multiply.

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

Format type wrenfold.ast.Negate.

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

Format type wrenfold.ast.OptionalOutputBranch.

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

Format type wrenfold.ast.Parenthetical.

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

Format type wrenfold.ast.SpecialConstant.

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

Format type wrenfold.ast.ReturnObject.

  1. format(self: wrenfold.gen.PythonGenerator, element: wrenfold.ast.Ternary) -> str

Format type wrenfold.ast.Ternary.

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

Format type wrenfold.ast.VariableRef.

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

Format type wrenfold.ast.FunctionSignature.

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

Format type wrenfold.type_info.ScalarType.

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

Format type wrenfold.type_info.MatrixType.

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

Format type wrenfold.type_info.CustomType.

generate(*args, **kwargs)

Overloaded function.

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

Generate code for the provided definition.

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

Generate code for multiple definitions.

property indentation

Amount of spaces used to indent nested scopes.

super_format(*args, **kwargs)

Overloaded function.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  1. super_format(self: wrenfold.gen.PythonGenerator, element: wrenfold.ast.Ternary) -> str

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

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

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

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

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

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

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

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

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

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

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

property target

The API that the python generator targets.

class wrenfold.code_generation.PythonGeneratorFloatWidth

Members:

Float32 : Float arrays/tensors will be interpreted as float32.

Float64 : Float arrays/tensors will be interpreted as float64.

__eq__(self: object, other: object) bool
__hash__(self: object) int
__index__(self: wrenfold.gen.PythonGeneratorFloatWidth) int
__init__(self: wrenfold.gen.PythonGeneratorFloatWidth, value: int) None
__int__(self: wrenfold.gen.PythonGeneratorFloatWidth) int
__ne__(self: object, other: object) bool
__str__(self: object) str
property name
class wrenfold.code_generation.PythonGeneratorTarget

Members:

NumPy : Target the NumPy API.

PyTorch : Target the PyTorch API.

JAX : Target the JAX API.

__eq__(self: object, other: object) bool
__hash__(self: object) int
__index__(self: wrenfold.gen.PythonGeneratorTarget) int
__init__(self: wrenfold.gen.PythonGeneratorTarget, value: int) None
__int__(self: wrenfold.gen.PythonGeneratorTarget) int
__ne__(self: object, other: object) bool
__str__(self: object) str
property name
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

Generates 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.Ternary) -> str

Format type wrenfold.ast.Ternary.

  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.Ternary) -> str

Format type wrenfold.ast.Ternary. 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.