Symbolic math with Rust export?


I have a complex function of many variables, for which I want to take all kinds of derivatives and then implement them in Rust. To do this by hand is lots of tedious and error-prone work, so I'm wondering if there are any tools available?

The function itself consists of basic functions like exp() with basic math operations.


Best, Oliver

Does it have to be Rust? Julia has the Zygote.jl package, which supports really cool automatic differentiation, even in the complex realm.

I'm not very familiar with complex differentiation, I only remember that it becomes really complex (pun intended) for non-holomorphic functions (like f(z) = z + |z| for example), where you don't have a single analytical derivative when deriving for df/dz, but two partial derivatives, one for z and one for the conjugate |z| (the wirtinger derivatives).

I got stuck there when I was trying to generalize the interior distance rendering algorithm (requiring us to compute the derivative df/dz) from the mandelbrot set z^2 + c to other fractals, like the tricorn |z|^2 + c (again not holomorphic).

1 Like

Sorry, I wasn't clear: I meant complex as in complicated, not as in complex numbers.

That's what I suspected.

This is called automatic differentiation, and there are quite a few libraries for it. A search on shows some promising, mature-looking results:

  • autodiff
  • dfdx
  • gad

Thank you for finding these crates! I don't think they offer the export to plain Rust, though.

What I'm looking for is this: for automatic differentiation, you usually need fat expressions, i.e. lots of enums or dyns to cover the different types of operations. I want to be able to get the expressions as plain code I can compile.

In this case, you'll probably have to roll your own. It's not difficult though, if all you want is that functionality.