symforce.util module

symbolic_eval(func)[source]

Build symbolic arguments for a function, and return the function evaluated on those arguments.

Useful for easily visualizing what expressions a symbolic function produces

Parameters:

func (Callable[[...], _T]) – A callable; args should have type annotations, and those types should be constructible automatically with symforce.ops.storage_ops.StorageOps.symbolic()

Returns:

The outputs of ``func`` evaluated on the constructed symbolic args

Return type:

_T

lambdify(f_or_args, expr=None, *, use_numba=False)[source]

Convert a symbolic function, or expression(s), to a numerical function.

This is a thin wrapper around Codegen.lambdify provided for convenience.

See symforce_util_test for examples.

Can be called either with a symbolic function, like Codegen.function:

sf.lambdify(my_symbolic_function)

Or with separate arguments and expression(s), like sympy.lambdify():

sf.lambdify([x, y], x**2 + y**2)
Parameters:
  • use_numba (bool) – If True, use Numba to compile the generated function. This can be much faster, but has some limitations - see codegen.PythonConfig for details

  • f_or_args (T.Callable | T.Sequence[T.Element]) –

  • expr (T.Element | T.Sequence[T.Element] | None) –

Returns:

A numerical function equivalent to ``f``

Return type:

T.Callable

numbify(f_or_args, expr=None)[source]

Shorthand for lambdify(..., use_numba=True)

See also

lambdify()

Parameters:
  • f_or_args (T.Callable | T.Sequence[T.Element]) –

  • expr (T.Element | T.Sequence[T.Element] | None) –

Return type:

T.Callable

specialize_types(f, type_replacements)[source]

Specialize the type annotations on the given function, replacing any types in type_replacements

For example, this can be used to take a symbolic function that accepts a generic type and generate it for several concrete types:

def f(x: sf.CameraCal) -> sf.Scalar:
    ...

Codegen.function(specialize_types(f, {sf.CameraCal: sf.LinearCameraCal}), ...)
Codegen.function(specialize_types(f, {sf.CameraCal: sf.PolynomialCameraCal}), ...)
Parameters:
Return type:

SymbolicFunction

specialize_args(f, arg_replacements)[source]

Specialize the type annotations on the given function, replacing types for any arguments in arg_replacements

For example, this can be used to take a symbolic function that accepts a generic type and generate it for several concrete types:

def f(x: sf.CameraCal, y: sf.CameraCal) -> sf.Scalar:
    ...

Codegen.function(
    specialize_types(f, {"x": sf.LinearCameraCal, "y": sf.PolynomialCameraCal}), ...
)
Parameters:
Return type:

SymbolicFunction