Skip to content

Commit a153fb2

Browse files
authored
BUG: Make Polynomial evaluation adhere to nep 50 (numpy#26550)
* Make sure evaluation of a Polynomial respects nep50 for scalar input: ``` import numpy as np p=np.polynomial.Polynomial(np.array([1, 2], dtype=np.float32)) print(type(p(2))) # np.float64 # correct, since the domain argument is the default which maps to [-1., 1.] w=np.array([-1,1], dtype=np.float32) p=np.polynomial.Polynomial(np.array([1, 2], dtype=np.float32), domain=w, window=w) print(type(p(2))) # np.float32 (was float64 on main) ``` * Update documentation of the various polynomial classes for the updated domain and window (was changed in numpy#24568) * Not addressed: ``` import numpy as np arr = np.polydiv(1, np.float32(1)) arr.dtype # float64 ``` The input here are polynomial coefficients, which are really an array and not a scalar. So the output type seems correct, even though `1` looks like a scalar input.
1 parent b77d2c6 commit a153fb2

File tree

9 files changed

+52
-13
lines changed

9 files changed

+52
-13
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from .common import Benchmark
2+
3+
import numpy as np
4+
5+
6+
class Polynomial(Benchmark):
7+
8+
def setup(self):
9+
self.polynomial_degree2 = np.polynomial.Polynomial(np.array([1, 2]))
10+
self.array3 = np.linspace(0, 1, 3)
11+
self.array1000 = np.linspace(0, 1, 10_000)
12+
self.float64 = np.float64(1.0)
13+
14+
def time_polynomial_evaluation_scalar(self):
15+
self.polynomial_degree2(self.float64)
16+
17+
def time_polynomial_evaluation_python_float(self):
18+
self.polynomial_degree2(1.0)
19+
20+
def time_polynomial_evaluation_array_3(self):
21+
self.polynomial_degree2(self.array3)
22+
23+
def time_polynomial_evaluation_array_1000(self):
24+
self.polynomial_degree2(self.array1000)
25+
26+
def time_polynomial_addition(self):
27+
_ = self.polynomial_degree2 + self.polynomial_degree2
28+
29+

numpy/polynomial/chebyshev.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2011,9 +2011,9 @@ class Chebyshev(ABCPolyBase):
20112011
domain : (2,) array_like, optional
20122012
Domain to use. The interval ``[domain[0], domain[1]]`` is mapped
20132013
to the interval ``[window[0], window[1]]`` by shifting and scaling.
2014-
The default value is [-1, 1].
2014+
The default value is [-1., 1.].
20152015
window : (2,) array_like, optional
2016-
Window, see `domain` for its use. The default value is [-1, 1].
2016+
Window, see `domain` for its use. The default value is [-1., 1.].
20172017
20182018
.. versionadded:: 1.6.0
20192019
symbol : str, optional

numpy/polynomial/hermite.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1756,9 +1756,9 @@ class Hermite(ABCPolyBase):
17561756
domain : (2,) array_like, optional
17571757
Domain to use. The interval ``[domain[0], domain[1]]`` is mapped
17581758
to the interval ``[window[0], window[1]]`` by shifting and scaling.
1759-
The default value is [-1, 1].
1759+
The default value is [-1., 1.].
17601760
window : (2,) array_like, optional
1761-
Window, see `domain` for its use. The default value is [-1, 1].
1761+
Window, see `domain` for its use. The default value is [-1., 1.].
17621762
17631763
.. versionadded:: 1.6.0
17641764
symbol : str, optional

numpy/polynomial/hermite_e.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1667,9 +1667,9 @@ class HermiteE(ABCPolyBase):
16671667
domain : (2,) array_like, optional
16681668
Domain to use. The interval ``[domain[0], domain[1]]`` is mapped
16691669
to the interval ``[window[0], window[1]]`` by shifting and scaling.
1670-
The default value is [-1, 1].
1670+
The default value is [-1., 1.].
16711671
window : (2,) array_like, optional
1672-
Window, see `domain` for its use. The default value is [-1, 1].
1672+
Window, see `domain` for its use. The default value is [-1., 1.].
16731673
16741674
.. versionadded:: 1.6.0
16751675
symbol : str, optional

numpy/polynomial/laguerre.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1688,9 +1688,9 @@ class Laguerre(ABCPolyBase):
16881688
domain : (2,) array_like, optional
16891689
Domain to use. The interval ``[domain[0], domain[1]]`` is mapped
16901690
to the interval ``[window[0], window[1]]`` by shifting and scaling.
1691-
The default value is [0, 1].
1691+
The default value is [0., 1.].
16921692
window : (2,) array_like, optional
1693-
Window, see `domain` for its use. The default value is [0, 1].
1693+
Window, see `domain` for its use. The default value is [0., 1.].
16941694
16951695
.. versionadded:: 1.6.0
16961696
symbol : str, optional

numpy/polynomial/legendre.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1632,9 +1632,9 @@ class Legendre(ABCPolyBase):
16321632
domain : (2,) array_like, optional
16331633
Domain to use. The interval ``[domain[0], domain[1]]`` is mapped
16341634
to the interval ``[window[0], window[1]]`` by shifting and scaling.
1635-
The default value is [-1, 1].
1635+
The default value is [-1., 1.].
16361636
window : (2,) array_like, optional
1637-
Window, see `domain` for its use. The default value is [-1, 1].
1637+
Window, see `domain` for its use. The default value is [-1., 1.].
16381638
16391639
.. versionadded:: 1.6.0
16401640
symbol : str, optional

numpy/polynomial/polynomial.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,9 +1601,9 @@ class Polynomial(ABCPolyBase):
16011601
domain : (2,) array_like, optional
16021602
Domain to use. The interval ``[domain[0], domain[1]]`` is mapped
16031603
to the interval ``[window[0], window[1]]`` by shifting and scaling.
1604-
The default value is [-1, 1].
1604+
The default value is [-1., 1.].
16051605
window : (2,) array_like, optional
1606-
Window, see `domain` for its use. The default value is [-1, 1].
1606+
Window, see `domain` for its use. The default value is [-1., 1.].
16071607
16081608
.. versionadded:: 1.6.0
16091609
symbol : str, optional

numpy/polynomial/polyutils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,8 @@ def mapdomain(x, old, new):
346346
array([-1.0+1.j , -0.6+0.6j, -0.2+0.2j, 0.2-0.2j, 0.6-0.6j, 1.0-1.j ]) # may vary
347347
348348
"""
349-
x = np.asanyarray(x)
349+
if type(x) not in (int, float, complex) and not isinstance(x, np.generic):
350+
x = np.asanyarray(x)
350351
off, scl = mapparms(old, new)
351352
return off + scl*x
352353

numpy/polynomial/tests/test_polynomial.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,3 +627,12 @@ def test_polyline(self):
627627

628628
def test_polyline_zero(self):
629629
assert_equal(poly.polyline(3, 0), [3])
630+
631+
def test_result_type(self):
632+
w = np.array([-1, 1], dtype=np.float32)
633+
p = np.polynomial.Polynomial(w, domain=w, window=w)
634+
v = p(2)
635+
assert_equal(v.dtype, np.float32)
636+
637+
arr = np.polydiv(1, np.float32(1))
638+
assert_equal(arr[0].dtype, np.float64)

0 commit comments

Comments
 (0)