core.backends

A CasADi function as plain Python: source for numpy or PyTorch, generated from its graph.

The models of the library are CasADi functions. source writes one as a straight-line Python function over numpy arrays or PyTorch tensors, one statement per operation of its expression graph, and translate runs that source. The text imports numpy (or torch) and nothing else, so a model can leave CasADi behind: a controller on a machine without it, a network that is trained through a model. The operations are the same ones in the same order, so the values agree with CasADi to rounding error, and PyTorch differentiates the model like any other function.

Module Attributes

UNARY

Operations of one argument, by CasADi's name, and the numpy name of the function.

EXPRESSIONS

Operations of two arguments (and the few others), as the expression that computes them.

PRELUDE

the helpers that give arguments and results their CasADi shapes.

Functions

dense_index(sparsity)

Where each nonzero of sparsity sits in the dense column-major vector.

expanded(function)

The function as a graph of scalar operations (a CasADi SXFunction).

export(functions, backend)

The functions of a model, by name, as callables of backend (None stays None), each with its source.

literal(value)

A float as Python source, exactly.

source(function[, backend, name])

Python source of function over numpy arrays or torch tensors.

translate(function[, backend, name])

function as a Python callable over numpy arrays or torch tensors, from its source (kept as the callable's source attribute).

wrapped(items)

Comma-separated items in lines of about 90 characters, to go inside brackets.