Skip to main content

Symbolic differentiation and Rust code generation library.

Project description

cgapp logo

CI Docs site unsafe forbidden

Gradgen: what it does

Gradgen overview

Gradgen is a Python library for symbolic differentiation and (embedded) Rust code generation.

Code generation example

Read the docs

Here is an example where we will define the function

$$f(x, u) = \Vert x \Vert_2^2 + u \sin(x_1) + x_2 x_3,$$

for a three-dimensional input $x$ and scalar $u$.

The goal is to generate Rust code for the functions $f$, $Jf$ (the Jacobian matrix of $f$).

Furthermore, we want to generate a Rust function that computes simultaneous $f$ and $\nabla_x f$. This often is computationally more efficient compared to computing $f(x, u)$ and $\nabla_x f(x, u)$ in separate functions (look for FunctionBundle below).

from gradgen import CodeGenerationBuilder, Function, RustBackendConfig, SXVector, sin

# Define the symbolic inputs.
x = SXVector.sym("x", 3)
u = SXVector.sym("u", 1)

# Build a simple scalar-valued function of x and u
# f(x, u) = ||x||_2^2 + u_1 * sin(x_1) + x_2 * x_3
f_expr = x.norm2sq() + u[0] * sin(x[0]) + x[1] * x[2]

# Define a Function object
f = Function(
    "energy",
    [x, u],
    [f_expr],
    input_names=["x", "u"],
    output_names=["energy"],
)

# (Optional) Evaluate f in Python
x_value = [1.0, 2.0, -0.5]
u_value = [3.0]
print("f(x, u) =", f(x_value, u_value))

# Generate code
project = (
    CodeGenerationBuilder()
    .with_backend_config(
        RustBackendConfig()
        .with_crate_name("my_kernel")
        .with_backend_mode("no_std")
        .with_scalar_type("f64")
    )
    .for_function(f)
        .add_primal()
        .add_jacobian()
        .add_joint(
            FunctionBundle()
            .add_f()
            .add_jf(wrt=0)
        )
        .with_simplification("medium")
        .done()
    .build(Path(__file__).resolve().parent / "codegen_kernel")
)

See the demos and this more complete tutorial.

Special case: optimal control

See tutorial

In applications such as optimal control, the generated code can become too large very easily. However, the problem structure can be exploited to generate code with complexity that doesn't increase with the prediction horizon.

Instead of completely unrolled code, Gradgen exploits the problem structure to create high-performance, human-readable embeddable Rust code.

See this complete tutorial for details.

Unique features

  • Truly embdedable safe Rust code with optional #[no_std], no dynamic memory allocation, no panic!s
  • Specialised code generation tools for optimal control problems (docs)
  • Very efficient code generation thanks to modular code generation using map, zip, repeat, and chain high-order functions.
  • Supports both single (f32) and double (f64) precision arithmetic

Where to go next?

See the demos and this more complete documentation for details.

Show us some love!

If you find Gradgen useful, give us a star on GitHub!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gradgen-0.5.1a1.tar.gz (193.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gradgen-0.5.1a1-py3-none-any.whl (145.2 kB view details)

Uploaded Python 3

File details

Details for the file gradgen-0.5.1a1.tar.gz.

File metadata

  • Download URL: gradgen-0.5.1a1.tar.gz
  • Upload date:
  • Size: 193.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for gradgen-0.5.1a1.tar.gz
Algorithm Hash digest
SHA256 55244db46bc5a87cc3b3d87167cc9b612af04ff92a96bd9f2f3f97a6be2e26f5
MD5 a26376f62762c1545a440f3a5b7a3bcb
BLAKE2b-256 3ca92e98a2dc6512af47b542651b94e81e3599e4212b189e7e2416b7cf400e61

See more details on using hashes here.

File details

Details for the file gradgen-0.5.1a1-py3-none-any.whl.

File metadata

  • Download URL: gradgen-0.5.1a1-py3-none-any.whl
  • Upload date:
  • Size: 145.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for gradgen-0.5.1a1-py3-none-any.whl
Algorithm Hash digest
SHA256 bff19276bc567593956e8c57d3133edb35a5bf29e080660eea4e1e6fddccfc46
MD5 afffb383e027f27f0c03014acae57187
BLAKE2b-256 0faeaee6a7fdb4165ced5bbea5bf63434a2f5e7c65ac484cc37843227df2db9e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page