Python wrapper with test for Legendre transform
This commit is contained in:
parent
351180baf4
commit
0a0cad34aa
11 changed files with 165 additions and 0 deletions
1
python/libsharp/__init__.py
Normal file
1
python/libsharp/__init__.py
Normal file
|
@ -0,0 +1 @@
|
|||
from .libsharp import *
|
41
python/libsharp/libsharp.pyx
Normal file
41
python/libsharp/libsharp.pyx
Normal file
|
@ -0,0 +1,41 @@
|
|||
import numpy as np
|
||||
|
||||
cdef extern from "sharp.h":
|
||||
ctypedef long ptrdiff_t
|
||||
|
||||
void sharp_legendre_transform_s(float *bl, float *recfac, ptrdiff_t lmax, float *x,
|
||||
float *out, ptrdiff_t nx)
|
||||
void sharp_legendre_transform(double *bl, double *recfac, ptrdiff_t lmax, double *x,
|
||||
double *out, ptrdiff_t nx)
|
||||
void sharp_legendre_transform_recfac(double *r, ptrdiff_t lmax)
|
||||
void sharp_legendre_transform_recfac_s(float *r, ptrdiff_t lmax)
|
||||
|
||||
|
||||
def legendre_transform(x, bl, out=None):
|
||||
if out is None:
|
||||
out = np.empty_like(x)
|
||||
if x.dtype == np.float64:
|
||||
if bl.dtype != np.float64:
|
||||
bl = bl.astype(np.float64)
|
||||
return _legendre_transform(x, bl, out=out)
|
||||
elif x.dtype == np.float32:
|
||||
if bl.dtype != np.float32:
|
||||
bl = bl.astype(np.float32)
|
||||
return _legendre_transform_s(x, bl, out=out)
|
||||
else:
|
||||
raise ValueError("unsupported dtype")
|
||||
|
||||
|
||||
def _legendre_transform(double[::1] x, double[::1] bl, double[::1] out):
|
||||
if out.shape[0] != x.shape[0]:
|
||||
raise ValueError('x and out must have same shape')
|
||||
sharp_legendre_transform(&bl[0], NULL, bl.shape[0] - 1, &x[0], &out[0], x.shape[0])
|
||||
return np.asarray(out)
|
||||
|
||||
|
||||
def _legendre_transform_s(float[::1] x, float[::1] bl, float[::1] out):
|
||||
if out.shape[0] != x.shape[0]:
|
||||
raise ValueError('x and out must have same shape')
|
||||
sharp_legendre_transform_s(&bl[0], NULL, bl.shape[0] - 1, &x[0], &out[0], x.shape[0])
|
||||
return np.asarray(out)
|
||||
|
1
python/libsharp/tests/__init__.py
Normal file
1
python/libsharp/tests/__init__.py
Normal file
|
@ -0,0 +1 @@
|
|||
# empty
|
29
python/libsharp/tests/test_legendre.py
Normal file
29
python/libsharp/tests/test_legendre.py
Normal file
|
@ -0,0 +1,29 @@
|
|||
import numpy as np
|
||||
from scipy.special import legendre
|
||||
import libsharp
|
||||
|
||||
|
||||
def test_legendre_transform():
|
||||
lmax = 20
|
||||
ntheta = 19
|
||||
|
||||
l = np.arange(lmax + 1)
|
||||
bl = np.exp(-l*(l+1))
|
||||
bl *= (2 * l + 1)
|
||||
|
||||
theta = np.linspace(0, np.pi, ntheta, endpoint=True)
|
||||
x = np.cos(theta)
|
||||
|
||||
# Compute truth using scipy.special.legendre
|
||||
P = np.zeros((ntheta, lmax + 1))
|
||||
for l in range(lmax + 1):
|
||||
P[:, l] = legendre(l)(x)
|
||||
y0 = np.dot(P, bl)
|
||||
|
||||
# double-precision
|
||||
y = libsharp.legendre_transform(x, bl)
|
||||
assert np.max(np.abs(y - y) / np.abs(y)) < 1e-12
|
||||
|
||||
# single-precision
|
||||
y32 = libsharp.legendre_transform(x.astype(np.float32), bl)
|
||||
assert np.max(np.abs(y32 - y) / np.abs(y32)) < 1e-4
|
Loading…
Add table
Add a link
Reference in a new issue