Symbolic Automatic Differentiation for Zig
https://arrufat.github.io/zymbol/
- Zig 100%
|
Adrià Arrufat
ec4b6d48e1
style(example/wasm): standardize html formatting
Improve consistency by adjusting indentation and using self-closing tags in the example HTML file. |
||
|---|---|---|
| .github/workflows | ci: add github pages deployment workflow | |
| examples/wasm | style(example/wasm): standardize html formatting | |
| src | refactor(simplifier): partition addition terms | |
| .gitignore | chore: enhance gitignore with editor files and zig artifacts | |
| build.zig | refactor!: use graph-based symbolic AD and update API | |
| build.zig.zon | feat: implement core symbolic differentiation library | |
| LICENSE | Add MIT License to the project | |
| README.md | feat: add support for hyperbolic functions | |
Zymbol
Symbolic Automatic Differentiation for Zig
conststd=@import("std");constzymbol=@import("zymbol");varregistry=zymbol.Registry.init(allocator);deferregistry.deinit();tryzymbol.registerBuiltins(®istry);varexpr=tryzymbol.Expression.parse(allocator,®istry,"x^2 + sin(x)");deferexpr.deinit();varinputs=std.StringHashMap(f32).init(allocator);deferinputs.deinit();tryinputs.put("x",1.5);constvalue=tryexpr.evaluate(inputs);std.debug.print("f(1.5) = {d}\n",.{value});vargrad_expr=tryexpr.symbolicGradient("x");defergrad_expr.deinit();constgrad_value=trygrad_expr.evaluate(inputs);std.debug.print("df/dx(1.5) = {d}\n",.{grad_value});Features:
- Symbolic differentiation - generates derivative expressions at compile time
- Composable -
grad(grad(f))for higher-order derivatives - Extensible - register custom operations without modifying the library
- Inspectable - convert graphs back to human-readable strings
- Simplified output - algebraic pass folds constants, collapses duplicate terms, and removes trivial factors (e.g.
d/dx x^3->3 * (x ^ 2),x + x->2 * x) - Zero runtime overhead - all parsing happens at compile time (when used with comptime)
- Robust gradients - gracefully handles edge cases like non-positive power bases to avoid NaNs
Expression Syntax
- Identifiers map to input variables (e.g.
x,foo_bar). - Literals support integers and floating-point values (
2,3.14). - Binary operators:
+,-,*,/,^with standard precedence. - Unary prefixes
+and-are supported (-x,-(x + 1)). - Parentheses group sub-expressions.
- Function calls invoke registered operations (
sin(x),max(x, y), etc.). - Built-in math functions include
log,exp,sin,cos,tan,sinh,cosh, andtanh; additional ops can be registered at runtime.
WebAssembly Playground
Build an interactive derivative playground that runs entirely in the browser:
zig build wasm-example
cd zig-out/wasm
python -m http.server 8080
Then browse to http://localhost:8080. The page lets you type an expression, choose the differentiation variable, toggle simplification, and calls the compiled WASM module to print either the raw or simplified symbolic derivative. The build enables rdynamic so every exported Zig function stays visible to JavaScript.
Changes pushed to master are deployed automatically to https://arrufat.github.io/zymbol via the GitHub Actions workflow in .github/workflows/pages.yml, which builds with the latest Zig master toolchain.