Skip to content

ENH: polyvalnd, N-dimensional polynomial evaluation #30857

Closed

Description

@lpsinger

Proposed new feature or change:

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.,

from numpy.polynomial import polyutils as pu
from numpy.polynomial.polynomial import polyval


def polyvalnd(x, c):
    """Evaluate an arbitrary multivariate polynomial."""
    return pu._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 first
polyvalnd(c, p, q, ..., x, y, z)  # ordinates last
polyvalnd([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.

Activity

  1. Rishabh-git10 commented on Feb 23, 2026

    @Rishabh-git10
    Contributor

    I can take this up!

    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).

  2. ngoldbaum commented on Feb 24, 2026

    @ngoldbaum
    Member

    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.

  3. lpsinger commented on Feb 24, 2026

    @lpsinger
    ContributorAuthor

    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.

  4. ngoldbaum commented on Feb 24, 2026

    @ngoldbaum
    Member

    Would you mind pinging the mailing list with a link to this issue?

  5. lpsinger commented on Feb 24, 2026

    @lpsinger
    ContributorAuthor

    Who, me?

  6. ngoldbaum commented on Feb 24, 2026

    @ngoldbaum
    Member

    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.

  7. lpsinger commented on Feb 24, 2026

    @lpsinger
    ContributorAuthor

    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.

    Done!

  8. charris commented on Feb 24, 2026

    @charris
    Member

    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.

  9. lpsinger commented on Feb 25, 2026

    @lpsinger
    ContributorAuthor

    @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.

    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 polyvalnd that relies on internal Numpy functions:
    https://github.com/m4opt/m4opt/blob/71c83585fde2a2ab363d1de8ca481f9a4462bda2/m4opt/utils/tests/test_interp.py#L13-L16

  10. charris commented on Feb 25, 2026

    @charris
    Member

    I have just noticed #20529, which fell through the cracks. Sounds like a good fit for your project.,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions