from mpmath import * import os try: import psyco psyco.full() except ImportError: pass fp = open("gallery.html", "w") fp.write("Gallery of mathematical functions") fp.write("

Gallery of mathematical functions

") fp.write('''

Generated with mpmath and matplotlib by this script.

The complex plots show the magnitude as brightness (0 = black, ∞ = white) and the phase as hue (positive real = red, imaginary = lime, negative real = cyan, negative imaginary = violet).

''') def plotgroup(name, fs, descs, xlim=[-5, 5], ylim=None, repoints=500, relim=[-5,5], imlim=[-5, 5], singularities=[], complex_only=False): print name, "..." fp.write('

%s

' % name) f1name = '%s.png' % name fnname = name + ' C%i.png' if not (complex_only or os.path.exists(f1name)): plot(fs, xlim=xlim, ylim=ylim, file=f1name, points=repoints, dpi=40, singularities=singularities) for k, f in enumerate(fs): if not os.path.exists(fnname % k): cplot(f, file=(fnname % k), re=relim, im=imlim, points=35000, dpi=40, verbose=1) fp.write('') if not complex_only: fp.write('' % f1name) for k in range(len(fs)): fp.write('' % (fnname % k)) fp.write('') desc = ",
".join(descs) desc = desc[0].upper() + desc[1:] if not complex_only: fp.write('' % desc) for k, d in enumerate(descs): fp.write('' % descs[k]) fp.write('
%sComplex %s
') fp.flush() plotgroup("Polynomials", [lambda x: x, lambda x: 0.1*x**3 - x + 1], ["linear function f(x) = x", "cubic, 0.1 x3 - x + 1"]) plotgroup("Rational functions", [lambda x: 1/x, lambda x: (x+4)/(x**5-3j*x**3+2)], ["inverse 1/x", ("rational function " \ "(x+4)/(x5-3ix3+2)")], ylim=[-5,5]) plotgroup("Nonanalytic functions", [abs, floor], ["absolute value |x|", "floor function ⌊x⌋"], singularities=[-4,-3,-2,-1,0,1,2,3,4]) plotgroup("Roots", [sqrt, cbrt], ["square root x1/2", "cube root x1/3"]) plotgroup("Exponentiation", [exp, ln], ["exponential function exp(x)", "natural logarithm ln(x)"], ylim=[-5,5]) def hypexp(x): z = x**(x**x) # Avoid float overflow in matplotlib if abs(z) > 100: return sign(z)*1000 return z plotgroup("Hyper-exponentiation", [lambda x: x**x, hypexp], ["2-tetration xx", "3-tetration x(xx)"], ylim=[-5, 5]) plotgroup("Lambert W function", [lambertw, lambda x: lambertw(x, -1)], ["W0(x) (branch 0)", "W−1(x) (branch −1)"], ylim=[-5, 5]) plotgroup("Trigonometric functions 1", [sin, cos], ["sine", "cosine"]) plotgroup("Trigonometric functions 2", [tan, cot], ["tangent", "cotangent"], ylim=[-5, 5], singularities=[-3*pi/2,-pi,-pi/2,pi/2,pi,3*pi/2]) plotgroup("Trigonometric functions 3", [sec, csc], ["secant", "cosecant"], ylim=[-5, 5], singularities=[-3*pi/2,-pi,-pi/2,pi/2,pi,3*pi/2]) plotgroup("Inverse trigonometric functions 1", [asin, acos], ["inverse sine", "inverse cosine"]) plotgroup("Inverse trigonometric functions 2", [atan, acot], ["inverse tangent", "inverse cotangent"]) plotgroup("Inverse trigonometric functions 3", [asec, acsc], ["inverse secant", "inverse cosecant"]) plotgroup("Hyperbolic functions 1", [sinh, cosh], ["hyperbolic sine", "hyperbolic cosine"], ylim=[-5, 5]) plotgroup("Hyperbolic functions 2", [tanh, coth], ["hyperbolic tangent", "hyperbolic cotangent"], ylim=[-5, 5]) plotgroup("Hyperbolic functions 3", [sech, csch], ["hyperbolic secant", "hyperbolic cosecant"], ylim=[-5, 5]) plotgroup("Inverse hyperbolic functions 1", [asinh, acosh], ["inverse hyperbolic sine", "inverse hyperbolic cosine"]) plotgroup("Inverse hyperbolic functions 2", [atanh, acoth], ["inverse hyperbolic tangent", "inverse hyperbolic cotangent"]) plotgroup("Inverse hyperbolic functions 3", [asech, acsch], ["inverse hyperbolic secant", "inverse hyperbolic cosecant"]) plotgroup("Error function", [lambda x: exp(-x**2), erf], ["Gaussian, exp(−x2)", "error function erf(x)"]) plotgroup("Fresnel integrals", [fresnels, fresnelc], ["Fresnel integral S(x)", "Fresnel integral C(x)"]) plotgroup("Exponential integrals", [ei, li], ["exponential integral Ei(x)", "Logarithmic integral li(x)"], ylim=[-5, 5]) plotgroup("Trigonometric integrals", [si, ci], ["sine integral Si(x)", "cosine integral Ci(x)"]) plotgroup("Hyperbolic integrals", [shi, chi], ["hyperbolic sine integral Shi(x)", "hyperbolic cosine integral Chi(x)"], ylim=[-10, 10]) plotgroup("Airy functions", [airyai, airybi], ["Airy function Ai(x)", "Airy function Bi(x)"], xlim=[-8, 1], ylim=[-1, 1]) plotgroup("Bessel function of the first kind", [j0, j1], ["Bessel function J0(x)", "Bessel function J1(x)"], xlim=[-15, 15], relim=[-15,15], imlim=[-15,15]) plotgroup("Gamma function", [gamma], ["gamma function Γ(x)"], ylim=[-5, 5], repoints=1500) plotgroup("Polygamma functions", [digamma, trigamma], ["digamma function ψ(0)(x)", "trigamma function ψ(1)(x)"], ylim=[-20, 20]) plotgroup("Riemann zeta function", [zeta], ["Riemann zeta function ζ(x)"], ylim=[-5, 5], relim=[-20, 20], imlim=[-30, 30]) plotgroup("Jacobi theta functions 1-2", [lambda x: jtheta(1,x,0.5), lambda x: jtheta(2,x,0.5)], ["θ1(x,1/2)", "θ2(x,1/2)"]) plotgroup("Jacobi theta functions 3-4", [lambda x: jtheta(3,x,0.5), lambda x: jtheta(4,x,0.5)], ["θ3(x,1/2)", "θ4(x,1/2)"]) plotgroup("Derivative of Jacobi theta functions 1-2", [lambda x: djtheta(1,x,0.5), lambda x: djtheta(2,x,0.5)], ["θ'1(x,1/2)", "θ'2(x,1/2)"]) plotgroup("Derivative of Jacobi theta functions 3-4", [lambda x: djtheta(3,x,0.5), lambda x: djtheta(4,x,0.5)], ["θ'3(x,1/2)", "θ'4(x,1/2)"]) plotgroup("Jacobi theta functions 1-2, variable nome", [lambda q: jtheta(1,(1+j)/3,q), lambda q: jtheta(2,(1+j)/3,q)], ["θ1((1+i)/3,q)", "θ2((1+i)/3,q)"], xlim=[-1,1], ylim=[-1, 1], relim=[-1,1], imlim=[-1,1], complex_only=True) plotgroup("Jacobi theta functions 3-4, variable nome", [lambda q: jtheta(3,(1+j)/3,q), lambda q: jtheta(4,(1+j)/3,q)], ["θ3((1+i)/3,q)", "θ4((1+i)/3,q)"], xlim=[-1,1], ylim=[-1, 1], relim=[-1,1], imlim=[-1,1], complex_only=True) plotgroup("Derivative of Jacobi theta functions 1-2, variable nome", [lambda q: djtheta(1,(1+j)/3,q), lambda q: djtheta(2,(1+j)/3,q)], ["θ1((1+i)/3,q)", "θ2((1+i)/3,q)"], xlim=[-1,1], ylim=[-1, 1], relim=[-1,1], imlim=[-1,1], complex_only=True) plotgroup("Derivative of Jacobi theta functions 3-4, variable nome", [lambda q: djtheta(3,(1+j)/3,q), lambda q: djtheta(4,(1+j)/3,q)], ["θ'3((1+i)/3,q)", "θ'4((1+i)/3,q)"], xlim=[-1,1], ylim=[-1, 1], relim=[-1,1], imlim=[-1,1], complex_only=True) fp.write("") fp.close()