-
Notifications
You must be signed in to change notification settings - Fork 0
/
math_tools.py
77 lines (50 loc) · 1.93 KB
/
math_tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import math
import numpy as np
def lapl_cart(mat, dx, dy):
"""
Compute the laplacian using `numpy.gradient` twice.
"""
grad_y, grad_x = np.gradient(mat, dy, dx)
grad_xx = np.gradient(grad_x, dx, axis=1)
grad_yy = np.gradient(grad_y, dy, axis=0)
return (grad_xx, grad_yy)
def lapl_pol(f: np.ndarray, r: np.ndarray, theta: np.ndarray) -> np.ndarray:
"""
Compute the laplacian in polar coordinates using numpy.gradient twice.
theta corresponds to axis=0
r corresponds to axis=1
"""
grad_th, grad_r = np.gradient(f, theta, r)
grad_rr = np.gradient(grad_r, r, axis=1)
grad_thth = np.gradient(grad_th, theta, axis=0)
return grad_rr + 1 / r * grad_r + 1 / r**2 * grad_thth
def abs2(x):
return x.real**2 + x.imag**2
def pol2cart(r, theta):
x = r * np.cos(theta)
y = r * np.sin(theta)
return (x, y)
def sph2cart(r, theta, phi):
x = r * np.sin(theta) * np.cos(phi)
y = r * np.sin(theta) * np.sin(phi)
z = r * np.cos(theta)
return (x, y, z)
def cart2sph(x, y, z):
r = np.sqrt(x**2 + y**2 + z**2)
theta = np.arctan2(np.sqrt(x**2 + y**2), z)
phi = np.arctan2(y, x)
return (r, theta, phi)
def hammer_ang2cart(theta: np.ndarray, phi: np.ndarray) -> tuple:
x = math.sqrt(8) * np.sin(theta) * np.sin(phi / 2) / np.sqrt(1 + np.sin(theta) * np.cos(phi / 2))
y = math.sqrt(2) * np.cos(theta) / np.sqrt(1 + np.sin(theta) * np.cos(phi / 2))
return (x, y)
# def explicit_hermite(n, x):
# # Hermite polynomials explicit representation is faster than SciPy's algorithm
# sum = 0
# for k in range(n//2 + 1):
# sum += (-1)**k / (math.factorial(k) * math.factorial(n - 2*k)) * (2*x)**(n - 2*k)
# return math.factorial(n) * sum
# def scipy_genlaguerre(n, alpha, x):
# # SciPy's generalized Laguerre algorithm is faster than the explicit representation
# L = special.genlaguerre(n, alpha)
# return L(x)