Skip to content

Commit

Permalink
import numpy legval rather than copy code
Browse files Browse the repository at this point in the history
  • Loading branch information
sbailey committed May 23, 2018
1 parent 3867402 commit 195db5c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 35 deletions.
2 changes: 1 addition & 1 deletion doc/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ specter change log
0.8.6 (unreleased)
------------------

* No changes yet.
* Added numba-ized legval for ~20% overall ex2d speedup (PR #61).

0.8.5 (2018-05-10)
------------------
Expand Down
38 changes: 4 additions & 34 deletions py/specter/util/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,8 @@ def outer(x, y, out):
return np.multiply(x[:, None], y[None, :], out)


#have faster numba version of legval if numba is available
# have faster numba version of legval if numba is available
try:
#this is the faster, flexible, most readable version
if 'NUMBA_DISABLE_JIT' in os.environ:
raise ImportError
import numba
Expand All @@ -248,38 +247,9 @@ def legval_numba(x, c):
return c0 + c1*x

except ImportError:
#in the event there is no numba, revert to the orig legval
#this is an exact copy of numpy's legval
def legval_numba(x, c, tensor=True):
c = np.array(c, ndmin=1, copy=0)
if c.dtype.char in '?bBhHiIlLqQpP':
c = c.astype(np.double)
if isinstance(x, (tuple, list)):
x = np.asarray(x)
if isinstance(x, np.ndarray) and tensor:
c = c.reshape(c.shape + (1,)*x.ndim)

if len(c) == 1:
c0 = c[0]
c1 = 0
elif len(c) == 2:
c0 = c[0]
c1 = c[1]
else:
nd = len(c)
c0 = c[-2]
c1 = c[-1]
for i in range(3, len(c) + 1):
tmp = c0
nd = nd - 1
c0 = c[-i] - (c1*(nd - 1))/nd
c1 = tmp + (c1*x*(2*nd - 1))/nd
return c0 + c1*x





# in the event there is no numba, revert to the orig legval while
# still calling it legval_numba so that they can be used interchangeably
from numpy.polynomial.legendre import legval as legval_numba



0 comments on commit 195db5c

Please sign in to comment.