Skip to content

ENH: add __array_function__ protocol in polynomial #28996

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion numpy/polynomial/polynomial.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@

import numpy as np
import numpy.linalg as la
from numpy._core.overrides import array_function_dispatch
from numpy.lib.array_utils import normalize_axis_index

from . import polyutils as pu
Expand Down Expand Up @@ -841,7 +842,13 @@ def polyvalfromroots(x, r, tensor=True):
raise ValueError("x.ndim must be < r.ndim when tensor == False")
return np.prod(x - r, axis=0)

def _polyval2d_dispatcher(x, y, c):
return (x, y, c)

def _polygrid2d_dispatcher(x, y, c):
return (x, y, c)

@array_function_dispatch(_polyval2d_dispatcher)
def polyval2d(x, y, c):
"""
Evaluate a 2-D polynomial at points (x, y).
Expand Down Expand Up @@ -893,7 +900,7 @@ def polyval2d(x, y, c):
"""
return pu._valnd(polyval, c, x, y)


@array_function_dispatch(_polygrid2d_dispatcher)
def polygrid2d(x, y, c):
"""
Evaluate a 2-D polynomial on the Cartesian product of x and y.
Expand Down
22 changes: 22 additions & 0 deletions numpy/polynomial/tests/test_polynomial.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,3 +667,25 @@ def test_result_type(self):

arr = np.polydiv(1, np.float32(1))
assert_equal(arr[0].dtype, np.float64)

class ArrayFunctionInterceptor:
def __init__(self):
self.called = False

def __array_function__(self, func, types, args, kwargs):
self.called = True
return "intercepted"

def test_polyval2d_array_function_hook():
x = ArrayFunctionInterceptor()
y = ArrayFunctionInterceptor()
c = ArrayFunctionInterceptor()
result = np.polynomial.polynomial.polyval2d(x, y, c)
assert result == "intercepted"

def test_polygrid2d_array_function_hook():
x = ArrayFunctionInterceptor()
y = ArrayFunctionInterceptor()
c = ArrayFunctionInterceptor()
result = np.polynomial.polynomial.polygrid2d(x, y, c)
assert result == "intercepted"
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy