You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a need for a function polyvalnd which is just like polyval, polyval2d, or polyval3d, but that handles arbitrary dimensions. Surprisingly this function does not already exist in Numpy. It is trivial to implement with existing non-public utilities in Numpy, i.e.,
fromnumpy.polynomialimportpolyutilsaspufromnumpy.polynomial.polynomialimportpolyvaldefpolyvalnd(x, c):
"""Evaluate an arbitrary multivariate polynomial."""returnpu._valnd(polyval, c, *x)
Of course there is a decision to make about the signature, because with polyval2d and polyval3d the arguments are passed like this:
polyval2d(x, y, c)
polyval3d(x, y, c)
So we would have to decide which of these we prefer:
polyvalnd(p, q, ..., x, y, z, c) # ordinates firstpolyvalnd(c, p, q, ..., x, y, z) # ordinates lastpolyvalnd([p, q, ..., x, y, z], c) # ordinates first, in an list
This has been suggested before in #6071, but my needs are much narrower: I don't need N-D Chebyshev polynomials, etc.
My application is that I need some multivariate polynomial test data for a multivariate interpolation scheme that I am working on with a student.
Regarding the signature, using polyvalnd(c, *ordinates) or polyvalnd(*ordinates, c) gets messy with Python's argument parsing. I propose packing the coordinates into an iterable as the first argument: polyvalnd(pts, c). Since the current 1D/2D/3D functions put coordinates first, polyvalnd(pts, c) feels like the most natural drop-in API.
I noticed in the historical #6071 PR that the maintainers prefer to keep all 6 polynomial classes in sync. To keep this PR scoped and manageable, I will strictly add the N-D evaluation wrappers (valnd and maybe gridnd) across the base polynomial module and its siblings (chebyshev, legendre, hermite, hermite_e, laguerre).
Generally we're very conservative about adding new functions to NumPy's API. Is there a reason why this needs to live in NumPy and not in an external library?
Also API additions like this need a mailing list ping as well as positive responses before we add them.
Generally we're very conservative about adding new functions to NumPy's API. Is there a reason why this needs to live in NumPy and not in an external library?
It is trivial to implement using internal, undocumented functions within Numpy, but a pain to implement using only the public Numpy API.
Given that the internal details of the polynomial evaluation machinery do support arbitrary numbers of dimensions, it also seems like an obvious omission.
Yes, since you seem the most engaged and interested. Normally maintainers don't champion additions to the NumPy API proposed by people on github issues.
Yes, since you seem the most engaged and interested. Normally maintainers don't champion additions to the NumPy API proposed by people on github issues.
I am happy to support higher dimensions. I did 2d-3d as I thought they would be the most used and would illustrate the way to do it. I needed 2-d myself, which is always a consideration :) @lpsinger I am curious as to your application.
I am working on an astronomy research project with a student that requires interpolating a dataset in many dimensions. It is a sky background spectrum model for earth-based optical observations. I suggested the student to try multivariate piecewise polynomial interpolation using RegularGridInterpolator from Scipy. Surprisingly, this method was too slow for our application because the size of the linear system that it has to solve scales poorly with the number of dimensions. The student is now working on implementing a multi-dimensional Catmull-Rom spline interpolator which should have only O(N) scaling in the number of dimensions N. I wrote a test case for her that generates input values by sampling a high-order cubic polynomial, for which her interpolator should be exact.
Activity
Rishabh-git10 commentedon Feb 23, 2026
I can take this up!
Regarding the signature, using
polyvalnd(c, *ordinates)orpolyvalnd(*ordinates, c)gets messy with Python's argument parsing. I propose packing the coordinates into an iterable as the first argument:polyvalnd(pts, c). Since the current 1D/2D/3D functions put coordinates first,polyvalnd(pts, c)feels like the most natural drop-in API.I noticed in the historical #6071 PR that the maintainers prefer to keep all 6 polynomial classes in sync. To keep this PR scoped and manageable, I will strictly add the N-D evaluation wrappers (
valndand maybegridnd) across the base polynomial module and its siblings (chebyshev,legendre,hermite,hermite_e,laguerre).ngoldbaum commentedon Feb 24, 2026
Generally we're very conservative about adding new functions to NumPy's API. Is there a reason why this needs to live in NumPy and not in an external library?
Also API additions like this need a mailing list ping as well as positive responses before we add them.
lpsinger commentedon Feb 24, 2026
It is trivial to implement using internal, undocumented functions within Numpy, but a pain to implement using only the public Numpy API.
Given that the internal details of the polynomial evaluation machinery do support arbitrary numbers of dimensions, it also seems like an obvious omission.
ngoldbaum commentedon Feb 24, 2026
Would you mind pinging the mailing list with a link to this issue?
lpsinger commentedon Feb 24, 2026
Who, me?
ngoldbaum commentedon Feb 24, 2026
Yes, since you seem the most engaged and interested. Normally maintainers don't champion additions to the NumPy API proposed by people on github issues.
lpsinger commentedon Feb 24, 2026
Done!
charris commentedon Feb 24, 2026
I am happy to support higher dimensions. I did 2d-3d as I thought they would be the most used and would illustrate the way to do it. I needed 2-d myself, which is always a consideration :) @lpsinger I am curious as to your application.
lpsinger commentedon Feb 25, 2026
I am working on an astronomy research project with a student that requires interpolating a dataset in many dimensions. It is a sky background spectrum model for earth-based optical observations. I suggested the student to try multivariate piecewise polynomial interpolation using RegularGridInterpolator from Scipy. Surprisingly, this method was too slow for our application because the size of the linear system that it has to solve scales poorly with the number of dimensions. The student is now working on implementing a multi-dimensional Catmull-Rom spline interpolator which should have only O(N) scaling in the number of dimensions N. I wrote a test case for her that generates input values by sampling a high-order cubic polynomial, for which her interpolator should be exact.
Here's that test case: https://github.com/m4opt/m4opt/blob/71c83585fde2a2ab363d1de8ca481f9a4462bda2/m4opt/utils/tests/test_interp.py
And as you can see it contains an implementation of
polyvalndthat relies on internal Numpy functions:https://github.com/m4opt/m4opt/blob/71c83585fde2a2ab363d1de8ca481f9a4462bda2/m4opt/utils/tests/test_interp.py#L13-L16
charris commentedon Feb 25, 2026
I have just noticed #20529, which fell through the cracks. Sounds like a good fit for your project.,