1

I want to trace a tracking function known as the Riesz function in honor of the creator:

enter image description here

after a search I found this mpmath library that can help me with the plot function, zeta function and euler's number but I don't know how to put it all together.
This is what i did

sum = lambda n,z : gamma(n)/n**2 * (euler**(-z/n**2) - 1)
Riez = lambda z: z * (6/np.pi**2*sum)

cplot(Riez)

The answer provided below involving a Sympy expression is too slow (takes 15 minutes to run). So I ask, how would we evaluate the Riesz function using Numpy (? or some other fast library) which should be between 100-1000 times faster than evaluating the Sympy expression.

MathCrackExchange
  • 595
  • 1
  • 6
  • 25
larous25
  • 149
  • 7
  • 1
    Why are you wanting to evaluate this function? – MathCrackExchange Nov 14 '21 at 21:40
  • Okay, the mpmath / scipy functions will only be fast if you're using them as themselves. If you try to compose them in an infinite series (using a Python for-loop) term compuation, you'll get speeds similar if not worse than the Sympy code. I just tried this on my machine. Therefore, the next option is Numpy. There are probably more options such as Shedskin and Cython, but those are tricky to debug your app with, so I would first try out a Numpy solution. – MathCrackExchange Nov 14 '21 at 22:23

1 Answers1

1

The following code is hacked together from various sources on the web. It is by no means optimal or pretty, but it should get you started.

First you need to pip install: mpmath, sympy, numpy, matplotlib, scipy.

If you run into trouble installing those, you have to install an earlier version of Python and try installing again (I do not recommend virtual environment systems such as conda). I found that Python 3.7.9 installs all of the above, without any problem (on Windows 10).

from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import pylab
import numpy as np
import mpmath
from sympy import (I, oo, Sum, exp, pi, factorial, zeta, im, re)
from sympy.abc import n, x, k

mpmath.dps = 5

#B = oo  # infinity
B = 10
bounds = (k,1,B)

Riesz_expr = Sum(
    ((-1)**(k+1) * (-x)**k)
    / (factorial(k-1)*zeta(2*k)), bounds)

Riesz = lambda z: Riesz_expr.evalf(subs={'x':z})

Max = 2
fig = pylab.figure()
ax = Axes3D(fig)
X = np.arange(-Max, Max, 0.125)
Y = np.arange(-Max, Max, 0.125)
X, Y = np.meshgrid(X, Y)
xn, yn = X.shape
W = X*0
for xk in range(xn):
    for yk in range(yn):
        try:
            z = X[xk,yk] + I*Y[xk,yk]
            w = Riesz(z)
            w = im(w)
            if w != w:
                raise ValueError
            W[xk,yk] = w
        except (ValueError, TypeError, ZeroDivisionError) as exc:
            # can handle special values here
            raise exc
    print (xk, " of ", xn)

# can comment out one of these
ax.plot_surface(X, Y, W, rstride=1, cstride=1, cmap=cm.jet)
ax.plot_wireframe(X, Y, W, rstride=5, cstride=5)

pylab.show()

This took 10-20 minutes to run! Even though I set B = 10 and Max = 2 (the range end points on the 2D domain plane). It produces:

enter image description here

which is the imaginary part of the Riesz function up to some accuracy and within some domain rectangle defined by Max in the code.


What we can conclude is if you want to evaluate the Riesz function and plot it with any sort of speed, you're going to have to do this purely in Numpy or simply switch over to C++ :) I don't know if Numpy has a zeta function or infinite series avaialable, but if not, you have to be creative in how you construct an evaluator for your function using Numpy.

Have fun!

MathCrackExchange
  • 595
  • 1
  • 6
  • 25
  • 1
    Thank you very much for your answers and the time that you took answering, the answer you gave solves it, at this moment I am trying to paint with the "color in domain" technique. – larous25 Nov 14 '21 at 22:21
  • @larous25 how does this solve it? Isn't it too slow to work with? Let's come up with a Numpy solution – MathCrackExchange Nov 14 '21 at 22:24
  • @larous25 I am unfamiliar with that technique - I just know Python and a little bit of complex analysis :) – MathCrackExchange Nov 14 '21 at 22:25
  • It solves it although it is not what I am looking for, but it really helps me because it gives me an idea of what I should do, again thank you very much, although I am not quite sure how to graph it, the technique I am using is used by this function: https://mpmath.org/doc/current/plotting.html#complex-function-plots – larous25 Nov 14 '21 at 23:02
  • @larous25 basically, for numpy you have to think vectorially. For example a vector of (-1)^(k + 1), you then simply multiply componentwise two numpy arrays and complete the rest of the formula. When you're done, you sum all elements in the array. The number of elements in the array equals the number of terms, so you have to decide what accuracy is right for your app. After all the evaluation in Sympy is not magic, they probably also essentially evaluate a certain number of terms and call it good once it's within some proven epsilon of the actual (I mean when `B = (k, 0, oo)` & `oo = infty` – MathCrackExchange Nov 14 '21 at 23:08
  • @larous25 by "sum up" I mean also using some numpy function. In fact all of your formula's arithmetic must be done using numpy operations, no looping allowed! Or you've re-introduced the same bottleneck. – MathCrackExchange Nov 14 '21 at 23:16