1
1
Fork
You've already forked zymbol
0
Symbolic Automatic Differentiation for Zig https://arrufat.github.io/zymbol/
  • Zig 100%
Find a file
Adrià Arrufat ec4b6d48e1 style(example/wasm): standardize html formatting
Improve consistency by adjusting indentation and using self-closing tags in the example HTML file.
2025年10月25日 22:00:02 +09:00
.github/workflows ci: add github pages deployment workflow 2025年10月17日 22:49:53 +09:00
examples/wasm style(example/wasm): standardize html formatting 2025年10月25日 22:00:02 +09:00
src refactor(simplifier): partition addition terms 2025年10月25日 21:16:17 +09:00
.gitignore chore: enhance gitignore with editor files and zig artifacts 2025年10月17日 22:47:33 +09:00
build.zig refactor!: use graph-based symbolic AD and update API 2025年10月17日 23:20:09 +09:00
build.zig.zon feat: implement core symbolic differentiation library 2025年09月30日 21:23:04 +09:00
LICENSE Add MIT License to the project 2025年10月17日 22:45:13 +09:00
README.md feat: add support for hyperbolic functions 2025年10月25日 20:30:01 +09:00

Zymbol

Symbolic Automatic Differentiation for Zig

conststd=@import("std");constzymbol=@import("zymbol");varregistry=zymbol.Registry.init(allocator);deferregistry.deinit();tryzymbol.registerBuiltins(&registry);varexpr=tryzymbol.Expression.parse(allocator,&registry,"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, and tanh; 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.