EconForge / interpolation.py

BSD 2-Clause "Simplified" License
123 stars 35 forks source link

Support Complex numbers #72

Open joseabernal opened 3 years ago

joseabernal commented 3 years ago

import numpy as np

from interpolation.splines import UCGrid, CGrid, nodes

f = lambda x,y: 1j*np.sin(np.sqrt(x2+y2+0.00001))/np.sqrt(x2+y2+0.00001) grid = UCGrid((-1.0, 1.0, 10), (-1.0, 1.0, 10)) gp = nodes(grid) # 100x2 matrix values = f(gp[:,0], gp[:,1]).reshape((10,10))

from interpolation.splines import eval_linear This one works ok point = np.array([0.1,0.45]) # 1d array val = eval_linear(grid, values, point) # float

This one does not points = np.random.random((10000,2)) eval_linear(grid, values, points) # 10000 vector

Here the error

TypingError Traceback (most recent call last)

in 13 14 points = np.random.random((10000,2)) ---> 15 eval_linear(grid, values, points) # 10000 vector /DSTORE/BRICIA/jbernal_PhD/testenv/lib/python3.6/site-packages/numba/core/dispatcher.py in _compile_for_args(self, *args, **kws) 413 e.patch_message(msg) 414 --> 415 error_rewrite(e, 'typing') 416 except errors.UnsupportedError as e: 417 # Something unsupported is present in the user code, add help info /DSTORE/BRICIA/jbernal_PhD/testenv/lib/python3.6/site-packages/numba/core/dispatcher.py in error_rewrite(e, issue_type) 356 raise e 357 else: --> 358 reraise(type(e), e, None) 359 360 argtypes = [] /DSTORE/BRICIA/jbernal_PhD/testenv/lib/python3.6/site-packages/numba/core/utils.py in reraise(tp, value, tb) 78 value = tp() 79 if value.__traceback__ is not tb: ---> 80 raise value.with_traceback(tb) 81 raise value 82 TypingError: Failed in nopython mode pipeline (step: nopython frontend) No implementation of function Function() found for signature: >>> _eval_linear(UniTuple(Tuple(float64, float64, int64) x 2), array(complex128, 2d, C), array(float64, 2d, C)) There are 8 candidate implementations: - Of which 2 did not match due to: Overload in function '__eval_linear': File: interpolation/splines/eval_splines.py: Line 88. With argument(s): '(UniTuple(Tuple(float64, float64, int64) x 2), array(complex128, 2d, C), array(float64, 2d, C))': Rejected as the implementation raised a specific error: TypingError: Failed in nopython mode pipeline (step: nopython frontend) No implementation of function Function() found for signature: >>> setitem(array(float64, 1d, C), int64, complex128) There are 16 candidate implementations: - Of which 16 did not match due to: Overload of function 'setitem': File: : Line N/A. With argument(s): '(array(float64, 1d, C), int64, complex128)': No match. During: typing of setitem at (44) File "", line 44: raised from /DSTORE/BRICIA/jbernal_PhD/testenv/lib/python3.6/site-packages/numba/core/typeinfer.py:1071 - Of which 2 did not match due to: Overload in function '__eval_linear': File: interpolation/splines/eval_splines.py: Line 104. With argument(s): '(UniTuple(Tuple(float64, float64, int64) x 2), array(complex128, 2d, C), array(float64, 2d, C))': Rejected as the implementation raised a specific error: TypeError: __eval_linear() missing 1 required positional argument: 'extrap_mode' raised from /DSTORE/BRICIA/jbernal_PhD/testenv/lib/python3.6/site-packages/numba/core/typing/templates.py:710 - Of which 2 did not match due to: Overload in function '__eval_linear': File: interpolation/splines/eval_splines.py: Line 130. With argument(s): '(UniTuple(Tuple(float64, float64, int64) x 2), array(complex128, 2d, C), array(float64, 2d, C))': Rejected as the implementation raised a specific error: TypeError: __eval_linear() missing 2 required positional arguments: 'out' and 'extrap_mode' raised from /DSTORE/BRICIA/jbernal_PhD/testenv/lib/python3.6/site-packages/numba/core/typing/templates.py:710 - Of which 2 did not match due to: Overload in function '__eval_linear': File: interpolation/splines/eval_splines.py: Line 154. With argument(s): '(UniTuple(Tuple(float64, float64, int64) x 2), array(complex128, 2d, C), array(float64, 2d, C))': Rejected as the implementation raised a specific error: TypeError: __eval_linear() missing 1 required positional argument: 'out' raised from /DSTORE/BRICIA/jbernal_PhD/testenv/lib/python3.6/site-packages/numba/core/typing/templates.py:710 During: resolving callee type: Function() During: typing of call at /DSTORE/BRICIA/jbernal_PhD/testenv/lib/python3.6/site-packages/interpolation/splines/eval_splines.py (172) File "../../testenv/lib/python3.6/site-packages/interpolation/splines/eval_splines.py", line 172: def eval_linear(*args): """Do I get a docstring ?""" return _eval_linear(*args) ^
albop commented 3 years ago

This looks feasible. Simple in theory but probably a bit of code refactoring. Could be a good excuse to revisit how scalars are specified (currently everything is float64).