GeezerVison is showing github.com — ENH: add __array_function__ protocol in polynomial · numpy/numpy@b5737e8
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: numpy/polynomial/polynomial.py +8-1Lines changed: 8 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 83 | 83 | import numpy as np | 84 | 84 | import numpy.linalg as la | 85 | 85 | from numpy.lib.array_utils import normalize_axis_index | | 86 | +from numpy._core.overrides import array_function_dispatch | 86 | 87 |
| 87 | 88 | from . import polyutils as pu | 88 | 89 | from ._polybase import ABCPolyBase | @@ -841,7 +842,13 @@ def polyvalfromroots(x, r, tensor=True): | 841 | 842 | raise ValueError("x.ndim must be < r.ndim when tensor == False") | 842 | 843 | return np.prod(x - r, axis=0) | 843 | 844 |
| | 845 | +def _polyval2d_dispatcher(x, y, c): | | 846 | + return (x, y, c) | 844 | 847 |
| | 848 | +def _polygrid2d_dispatcher(x, y, c): | | 849 | + return (x, y, c) | | 850 | + | | 851 | +@array_function_dispatch(_polyval2d_dispatcher) | 845 | 852 | def polyval2d(x, y, c): | 846 | 853 | """ | 847 | 854 | Evaluate a 2-D polynomial at points (x, y). | @@ -893,7 +900,7 @@ def polyval2d(x, y, c): | 893 | 900 | """ | 894 | 901 | return pu._valnd(polyval, c, x, y) | 895 | 902 |
| 896 | | - | | 903 | +@array_function_dispatch(_polygrid2d_dispatcher) | 897 | 904 | def polygrid2d(x, y, c): | 898 | 905 | """ | 899 | 906 | Evaluate a 2-D polynomial on the Cartesian product of x and y. | |
Collapse file Copy file name to clipboardExpand all lines: numpy/polynomial/tests/test_polynomial.py +22Lines changed: 22 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -667,3 +667,25 @@ def test_result_type(self): | 667 | 667 |
| 668 | 668 | arr = np.polydiv(1, np.float32(1)) | 669 | 669 | assert_equal(arr[0].dtype, np.float64) | | 670 | + | | 671 | +class ArrayFunctionInterceptor: | | 672 | + def __init__(self): | | 673 | + self.called = False | | 674 | + | | 675 | + def __array_function__(self, func, types, args, kwargs): | | 676 | + self.called = True | | 677 | + return "intercepted" | | 678 | + | | 679 | +def test_polyval2d_array_function_hook(): | | 680 | + x = ArrayFunctionInterceptor() | | 681 | + y = ArrayFunctionInterceptor() | | 682 | + c = ArrayFunctionInterceptor() | | 683 | + result = np.polynomial.polynomial.polyval2d(x, y, c) | | 684 | + assert result == "intercepted" | | 685 | + | | 686 | +def test_polygrid2d_array_function_hook(): | | 687 | + x = ArrayFunctionInterceptor() | | 688 | + y = ArrayFunctionInterceptor() | | 689 | + c = ArrayFunctionInterceptor() | | 690 | + result = np.polynomial.polynomial.polygrid2d(x, y, c) | | 691 | + assert result == "intercepted" |
|