Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
DOC: Add polynomial evaluation examples and improve polyval docs
- Add evaluation examples to polynomial quick reference table
- Enhance polyval docstring with clearer examples
- Add warning about domain/window scaling with fitted polynomials
- Show comparison between legacy and new polynomial APIs

Fixes #25587"
  • Loading branch information
olivvius committed Aug 17, 2025
commit 721904422a8b51a4729f502976403f76defd7dab
34 changes: 19 additions & 15 deletions doc/source/reference/routines.polynomials.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,27 @@ The `~numpy.polynomial.polynomial.Polynomial` class is imported for brevity::
from numpy.polynomial import Polynomial


+------------------------+------------------------------+---------------------------------------+
| **How to...** | Legacy (`numpy.poly1d`) | `numpy.polynomial` |
+------------------------+------------------------------+---------------------------------------+
| Create a | ``p = np.poly1d([1, 2, 3])`` | ``p = Polynomial([3, 2, 1])`` |
| polynomial object | | |
| from coefficients [1]_ | | |
+------------------------+------------------------------+---------------------------------------+
| Create a polynomial | ``r = np.poly([-1, 1])`` | ``p = Polynomial.fromroots([-1, 1])`` |
| object from roots | ``p = np.poly1d(r)`` | |
+------------------------+------------------------------+---------------------------------------+
| Fit a polynomial of | | |
| degree ``deg`` to data | ``np.polyfit(x, y, deg)`` | ``Polynomial.fit(x, y, deg)`` |
+------------------------+------------------------------+---------------------------------------+

+------------------------+----------------------------------------+---------------------------------------+
| **How to...** | Legacy (`numpy.poly1d`) | `numpy.polynomial` |
+------------------------+----------------------------------------+---------------------------------------+
| Create a | ``p = np.poly1d([1, 2, 3])`` | ``p = Polynomial([3, 2, 1])`` |
| polynomial object | | |
| from coefficients [1]_ | | |
+------------------------+----------------------------------------+---------------------------------------+
| Create a polynomial | ``r = np.poly([-1, 1])`` | ``p = Polynomial.fromroots([-1, 1])`` |
| object from roots | ``p = np.poly1d(r)`` | |
+------------------------+----------------------------------------+---------------------------------------+
| Fit a polynomial of | | |
| degree ``deg`` to data | ``np.polyfit(x, y, deg)`` | ``Polynomial.fit(x, y, deg)`` |
+------------------------+----------------------------------------+---------------------------------------+
| Evaluate a polynomial | ``p(2.0)`` or | ``p(2.0)`` or ``polyval(2.0, p.coef)``|
| at a point [2]_ | ``np.polyval([1, 2, 3], 2.0)`` | (use ``p.convert().coef`` after fit) |
+------------------------+----------------------------------------+---------------------------------------+

.. [1] Note the reversed ordering of the coefficients

.. [2] When evaluating polynomials created with ``fit()``, use ``p(x)`` or ``polyval(x, p.convert().coef)`` to handle domain/window scaling correctly.

Transition Guide
~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -187,4 +191,4 @@ Documentation for legacy polynomials
.. toctree::
:maxdepth: 2

routines.polynomials.poly1d
routines.polynomials.poly1d
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The trailing newline is missing

5 changes: 5 additions & 0 deletions numpy/polynomial/polynomial.py
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,11 @@ def polyval(x, c, tensor=True):
Notes
-----
The evaluation uses Horner's method.

.. warning::
When using coefficients from polynomials created with ``Polynomial.fit()``,
use ``p(x)`` or ``polyval(x, p.convert().coef)`` to handle domain/window
scaling correctly, not ``polyval(x, p.coef)``.

Examples
--------
Expand Down
Loading