Source code for ddg.math.functions

"""Basic functions and math expressions.
"""

import mpmath
import scipy as sp


[docs]def qgamma(z, q): """A generalised variant of :func:`mpmath.qgamma`. Unlike :func:`mpmath.qgamma`, this function returns infinity instead of raising a `ZeroDivisionError`. Parameters ---------- z : complex or mpmath.mpf q : complex or mpmath.mpf Returns ------- mpmath.mpf or mpmath.mpc If the result is complex, mpmath.mpc will be returned. Otherwise mpmath.mpf. Examples -------- >>> from ddg.math.functions import qgamma >>> qgamma(4, 0.75) mpf('4.046875') >>> qgamma(6, 6) mpf('121226245.0') >>> qgamma(3 + 4j, 0.5j) mpc(real='0.16630823822551999', imag='0.019524745760259529') >>> qgamma(0, 1) mpf('+inf') """ try: g = mpmath.qgamma(z, q) except ZeroDivisionError: g = mpmath.mpf("inf") return g
[docs]def double_factorial(n): """The double factorial function. .. math :: \\text{double_factorial}(n) = \\begin{cases} n \\cdot \\text{double_factorial}(n - 2) &\\text{ if } n \\ge 2 \\\\ 1 &\\text{ otherwise}. \\end{cases} Parameters ---------- n : int Returns ------- int Examples -------- >>> from ddg.math.functions import double_factorial >>> double_factorial(4) 8 >>> double_factorial(10) 3840 >>> double_factorial(0) 1 """ if n < 2: return 1 return n * double_factorial(n - 2)
[docs]def sn(u, k): return sp.special.ellipj(u, k**2)[0]
[docs]def cn(u, k): return sp.special.ellipj(u, k**2)[1]
[docs]def dn(u, k): return sp.special.ellipj(u, k**2)[2]
[docs]def ns(u, k): return 1 / sn(u, k)
[docs]def nc(u, k): return 1 / cn(u, k)
[docs]def nd(u, k): return 1 / dn(u, k)
[docs]def sc(u, k): return sn(u, k) / cn(u, k)
[docs]def sd(u, k): return sn(u, k) / dn(u, k)
[docs]def dc(u, k): return dn(u, k) / cn(u, k)
[docs]def ds(u, k): return dn(u, k) / sn(u, k)
[docs]def cs(u, k): return cn(u, k) / sn(u, k)
[docs]def cd(u, k): return cn(u, k) / dn(u, k)
[docs]def K(k): return sp.special.ellipk(k**2)