Skip to content

Instantly share code, notes, and snippets.

@bartolsthoorn
Created April 14, 2020 14:29
Show Gist options
  • Save bartolsthoorn/cc5e05b0417d76f5072cb6f07b2dfe3b to your computer and use it in GitHub Desktop.
Save bartolsthoorn/cc5e05b0417d76f5072cb6f07b2dfe3b to your computer and use it in GitHub Desktop.
Hypergeometric2F1 with complex arguments in Python by evaluating the power series
from numba import njit
import numpy as np
@njit('c8(c8,u8)')
def rising_poch(x, n):
p = 1+0j
for k in range(n):
p *= (x+k)
return p
@njit('u8(u8)')
def factorial(n):
p = 1
n = n + 1
for k in range(1, n):
p *= k
return p
@njit('c8(c8,c8,c8,c8,u8)')
def hypergeometric_2F1(a, b, c, z, n):
if np.abs(z) > 1:
return np.nan
s = 0+0j
for k in range(n):
s += (rising_poch(a, k)*rising_poch(b, k)/rising_poch(c, k)) * np.power(z,k) / factorial(k)
return s
hypergeometric_2F1(0 + 2j, 3., 1., 0.25, 20)
# => (-0.13342943787574768+1.6351008415222168j)
# Mathematica result: -0.133429 + 1.6351 I
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment