Elliptic theta functions

Are elliptic theta functions implemented in Julia? They exist in Mathematica as EllipticTheta, but I need them for heavy numerical calculations.

Googling “julia theta function” gives a bunch of results, did you try them?

I tried very much all of them. They are mostly related to Riemann theta functions, not elliptic theta functions.

I don’t know if that is acceptable for your use case, but there is Nemo.jl, which wraps the arbitrary-precision interval arithmetic C library Arb. It has the method jacobi_theta, which calculates the four Jacobi theta functions all at once.

Note that the definitions are slightly different from what is implemented in Mathematica as EllipticTheta (compare this MathWorld entry to Eq. 1.10 in this reference).
Specifically, jacobi_theta is in terms of \tau, while EllipticTheta is in terms of q, where q = \mathrm{e}^{\mathrm{i}\pi \tau}. Also, the z argument differs by a factor of \pi.

For example, the basic example given in the Wolfram documentation is EllipticTheta[1, 2., 1/3], which should give 1.42788. To replicate this, one could do:

julia> using Nemo;

julia> CC = ComplexField(64)
Complex Field with 64 bits of precision and error bounds

julia> Ď‘ = jacobi_theta(CC(2/Ď€), CC(0, log(3)/Ď€));

julia> Ď‘[1]
[1.42787634002218211 +/- 5.93e-18]

If needed, this could also be converted back to a floating point number (possibly already taking into account that we know the result is purely real, as in this case):

julia> convert(ComplexF64, Ď‘[1])
1.4278763400221821 + 0.0im

julia> convert(Float64, real(Ď‘[1]))

There is a Julia binding to the GSL library elliptic functions at JuliaHub which might also be useful.

Thanks for your reply! This library definitely works, but is slow. I am indeed using it right now, but it would be better if a direct numerical implementation of elliptic thetas of type 3 existed… Especially without relying on an algebraic package.

Unfortunately I don’t know how to express elliptic theta functions in terms of elliptic sines and cosines efficiently… In fact, it might even be impossible to invert them.

Yes, when you said “heavy numerical calculations” I already figured this solution would probably be too slow for your use case. Unfortunately I wasn’t able to find any other Julia package that provides ready-made access to the elliptic theta functions, as you said it seems to be mostly either Riemann theta functions or the elliptic sines and cosines mentioned above.

As an alternative, you could also try an implementation from a different programming language and call that from within Julia. Boost seems to have it, and I believe the currently recommended way of interfacing C++ and Julia is to use GitHub - JuliaInterop/CxxWrap.jl: Package to make C++ libraries available in Julia . I haven’t used it myself so far, so I cannot say how much additional work that would be. Maybe it isn’t too bad if you only need that one function anyway.

1 Like

The Boost implementation does not support complex numbers. There is a Fortran implementation here. It works fine, and it is not hard to translate the code to Julia.

Here is a Julia implementation of the third Jacobi theta function. This is a translation of the Fortran code.

Note that there are two definitions of Jacobi theta functions. To match the Python mpmath implementation, or the Wolfram implementation, you have to divide the variable by pi:

julia> jtheta3((1+1im)/pi, 1im)
0.8645618493544178 - 0.28488586703507296im
>>> from mpmath import jtheta, qfrom

>>> jtheta(3, 1+1j, qfrom(tau=1j))
Out[3]: mpc(real='0.86456184935441778', imag='-0.28488586703507289')

I will continue the implementation (e.g. the other Jacobi theta functions).

Finally I changed the implementation; now you don’t have to divide by pi. In this way this directly matches Python and Wolfram.

If it is fast enough, it would be great of you could wrap it in a package, add some tests and submit it to the General Registry!

The package is here.

One can derive other special functions from the Jacobi theta functions:

  • Klein j-invariant function
  • modular lambda function
  • Dedekind eta function
  • Weierstrass p-function and its first three derivatives
  • Weierstrass sigma-function
  • Weierstrass zeta-function

Thanks for the great package! However, note that there is already a registered Julia package of the same name “Jacobi” here. Please consider renaming to something like “JacobiTheta” or somesuch, and please consider registering your package to make it easily available to all Julia users :grinning:

Argh!.. Thanks for letting me know.

Thanks a lot for solving this for us all!