Gradgen: what it does
Gradgen is a Python library for symbolic differentiation and (embedded) Rust code generation.
Code generation example
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
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, nopanic!s - Specialised code generation tools for optimal control problems (docs)
- Very efficient code generation thanks to modular code generation using
map,zip,repeat, andchainhigh-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!
Metadata
Release files for gradgen 0.5.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| gradgen-0.5.2.tar.gz | 200.4 kB | Without provenance | Details for gradgen-0.5.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| gradgen-0.5.2-py3-none-any.whl | Python 3 | none | any | Details for gradgen-0.5.2-py3-none-any.whl |
Total release size: 348.6 kB
Release files / gradgen-0.5.2.tar.gz
| Download URL | gradgen-0.5.2.tar.gz |
|---|---|
| Size | 200.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
321af9cb1d5d3cb9e52bc83588854cb558fa3345d18c4aaa5d4e7d46948d72fb
|
|
BLAKE2b-256 checksum How to use checksums |
7e7b36b854149020ccb412dd3bbe20996451a1b0a606c78bc9fd5e89bddae812
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.3
|
Release files / gradgen-0.5.2-py3-none-any.whl
| Download URL | gradgen-0.5.2-py3-none-any.whl |
|---|---|
| Size | 148.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
86c09418606c82e0b3a72718f8c4d379c08c027de12161a197954de59a0a5c7b
|
|
BLAKE2b-256 checksum How to use checksums |
cb4baecf8b3442dc38f8f04dfbcc1384e5b56b56aa7281a64a9ee45d89b642ad
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.3
|
Security for gradgen 0.5.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.