Skip to content

ENH: Reduce overhead of numpy.polynomial #24480

@eendebakpt

Description

@eendebakpt

Proposed new feature or change:

The numpy.polynomial classes have quite some overhead in the calculations. This is a collection of ideas to reduce the overhead

from numpy.polynomial import Polynomial
import timeit

p = Polynomial([1,2,3])
q = Polynomial([3,2,1])

dt=timeit.timeit('p + p - q', globals={'p': p, 'q': q}, number=40_000)
print(dt)

the timings are

2.51 seconds (main)
2.35 seconds (np.array_equal)
2.2 seconds (.all() )

The fastest option does assume the domain already is a numpy array. Current main and the np.array_equal option cast (with asarray). #24499

  • The numpy.polynomial.polyutils.as_series is called in many places. Also in internal methods where many of the input checks (e.g. on input dimensions) are not required. We can add a flag internal : bool to as_series than will skip several of the checks. Draft PR: MAINT: Improve performance of polynomial operations (2) #24531

Profiling results on main (2023-8-22) for p * q with p=Polynomial([1,2,3]); q=Polynomial([3,0,3]):
np_polynomial_mul

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