Expose gauss_legendre_tbl publicly as gauss_legendre_roots

This commit is contained in:
Dag Sverre Seljebotn 2015-04-24 09:06:20 +02:00
parent 765831ea2b
commit 7ecd1ddc93
7 changed files with 152 additions and 65 deletions

View file

@ -1,5 +1,7 @@
import numpy as np
__all__ = ['legendre_transform', 'legendre_roots']
cdef extern from "sharp.h":
ctypedef long ptrdiff_t
@ -9,6 +11,7 @@ cdef extern from "sharp.h":
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)
void sharp_legendre_roots(int n, double *x, double *w)
def legendre_transform(x, bl, out=None):
@ -41,3 +44,13 @@ def _legendre_transform_s(float[::1] x, float[::1] bl, float[::1] out):
sharp_legendre_transform_s(&bl[0], NULL, bl.shape[0] - 1, &x[0], &out[0], x.shape[0])
return np.asarray(out)
def legendre_roots(n):
x = np.empty(n, np.double)
w = np.empty(n, np.double)
cdef double[::1] x_buf = x, w_buf = w
if not (x_buf.shape[0] == w_buf.shape[0] == n):
raise AssertionError()
if n > 0:
sharp_legendre_roots(n, &x_buf[0], &w_buf[0])
return x, w

View file

@ -1,5 +1,6 @@
import numpy as np
from scipy.special import legendre
from scipy.special import p_roots
import libsharp
from numpy.testing import assert_allclose
@ -39,3 +40,20 @@ def test_legendre_transform():
for ntheta in nthetas_to_try:
for lmax in [0, 1, 2, 3, 20] + list(np.random.randint(50, size=4)):
yield check_legendre_transform, lmax, ntheta
def check_legendre_roots(n):
xs, ws = ([], []) if n == 0 else p_roots(n) # from SciPy
xl, wl = libsharp.legendre_roots(n)
assert_allclose(xs, xl)
assert_allclose(ws, wl)
def test_legendre_roots():
"""
Test the Legendre root-finding algorithm from libsharp by comparing it with
the SciPy version.
"""
yield check_legendre_roots, 0
yield check_legendre_roots, 1
yield check_legendre_roots, 32
yield check_legendre_roots, 33
yield check_legendre_roots, 128