patrick-kidger / sympy2jax

Turn SymPy expressions into trainable JAX expressions.
Apache License 2.0
314 stars 12 forks source link

add numerical constants pi, e, Euler's gamma, and imaginary I #12

Closed denehoffman closed 11 months ago

denehoffman commented 11 months ago

Addresses #6 and adds some other nice constants. For example, you should now be able to run the following without error:

import sympy
import sympy2jax
theta, phi = sympy.symbols("theta phi")
y22 = sympy.Ynm(2, 2, theta, phi).expand(func=True)
mod = sympy2jax.SymbolicModule(y22)
mod(theta=0.1, phi=0.2)
# returns Array(0.00354597+0.00149921j, dtype=complex64, weak_type=True), which is the correct result for this spherical harmonic function
mod.sympy()
# returns: -sqrt(30)*exp(2*I*phi)*cos(theta)**2/(8*sqrt(pi)) + sqrt(30) *exp(2*I*phi)/(8*sqrt(pi))

I've also written a unit test for these constants, although I had to use jnp.isclose rather than == I believe because of some type conversions. If someone knows how to make this nicer, please do so!

google-cla[bot] commented 11 months ago

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

denehoffman commented 11 months ago

The CI failure looks a little odd, I think that's unrelated. Let me see if I can fix that.

From a quick search, I believe changing isort 5.10.1 to a newer version might fix this (source)

see also: https://github.com/PyCQA/isort/releases/tag/5.11.5

patrick-kidger commented 11 months ago

Alright, try rebasing on top of the new main and I think the CI should be fixed. :)

denehoffman commented 11 months ago

@patrick-kidger nice (love the switch to ruff and pyproject.toml btw)! Okay, I've updated my PR with the changes you mentioned, let me know how it looks.

update: oops, not sure why the pre-commit didn't do the formatting but I've run black on it now so it should pass this time.

patrick-kidger commented 11 months ago

Marvellous! Just merged. I'll do a new release with this now.

denehoffman commented 11 months ago

Thank you!