Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4c18530

Browse filesBrowse files
authored
Merge pull request #9536 from eric-wieser/better-polynomial-repr
ENH: Show domain and window as kwargs in repr
2 parents 9e05bc3 + 1a0e863 commit 4c18530
Copy full SHA for 4c18530

File tree

Expand file treeCollapse file tree

6 files changed

+63
-55
lines changed
Filter options
Expand file treeCollapse file tree

6 files changed

+63
-55
lines changed

‎doc/release/1.14.0-notes.rst

Copy file name to clipboardExpand all lines: doc/release/1.14.0-notes.rst
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,14 @@ Because ``np.tensordot`` uses BLAS when possible, that will speed up execution.
141141
By default, ``np.einsum`` will also attempt optimization as the overhead is
142142
small relative to the potential improvement in speed.
143143

144+
The ``repr`` of ``np.polynomial`` classes is more explicit
145+
----------------------------------------------------------
146+
It now shows the domain and window parameters as keyword arguments to make
147+
them more clear::
148+
149+
>>> np.polynomial.Polynomial(range(4))
150+
Polynomial([ 0., 1., 2., 3.], domain=[-1, 1], window=[-1, 1])
151+
144152

145153
Changes
146154
=======

‎doc/source/reference/routines.polynomials.classes.rst

Copy file name to clipboardExpand all lines: doc/source/reference/routines.polynomials.classes.rst
+24-24Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ the conventional Polynomial class because of its familiarity::
5252
>>> from numpy.polynomial import Polynomial as P
5353
>>> p = P([1,2,3])
5454
>>> p
55-
Polynomial([ 1., 2., 3.], [-1., 1.], [-1., 1.])
55+
Polynomial([ 1., 2., 3.], domain=[-1, 1], window=[-1, 1])
5656

5757
Note that there are three parts to the long version of the printout. The
5858
first is the coefficients, the second is the domain, and the third is the
@@ -77,19 +77,19 @@ we ignore them and run through the basic algebraic and arithmetic operations.
7777
Addition and Subtraction::
7878

7979
>>> p + p
80-
Polynomial([ 2., 4., 6.], [-1., 1.], [-1., 1.])
80+
Polynomial([ 2., 4., 6.], domain=[-1, 1], window=[-1, 1])
8181
>>> p - p
82-
Polynomial([ 0.], [-1., 1.], [-1., 1.])
82+
Polynomial([ 0.], domain=[-1, 1], window=[-1, 1])
8383

8484
Multiplication::
8585

8686
>>> p * p
87-
Polynomial([ 1., 4., 10., 12., 9.], [-1., 1.], [-1., 1.])
87+
Polynomial([ 1., 4., 10., 12., 9.], domain=[-1, 1], window=[-1, 1])
8888

8989
Powers::
9090

9191
>>> p**2
92-
Polynomial([ 1., 4., 10., 12., 9.], [-1., 1.], [-1., 1.])
92+
Polynomial([ 1., 4., 10., 12., 9.], domain=[-1, 1], window=[-1, 1])
9393

9494
Division:
9595

@@ -100,20 +100,20 @@ versions the '/' will only work for division by scalars. At some point it
100100
will be deprecated::
101101

102102
>>> p // P([-1, 1])
103-
Polynomial([ 5., 3.], [-1., 1.], [-1., 1.])
103+
Polynomial([ 5., 3.], domain=[-1, 1], window=[-1, 1])
104104

105105
Remainder::
106106

107107
>>> p % P([-1, 1])
108-
Polynomial([ 6.], [-1., 1.], [-1., 1.])
108+
Polynomial([ 6.], domain=[-1, 1], window=[-1, 1])
109109

110110
Divmod::
111111

112112
>>> quo, rem = divmod(p, P([-1, 1]))
113113
>>> quo
114-
Polynomial([ 5., 3.], [-1., 1.], [-1., 1.])
114+
Polynomial([ 5., 3.], domain=[-1, 1], window=[-1, 1])
115115
>>> rem
116-
Polynomial([ 6.], [-1., 1.], [-1., 1.])
116+
Polynomial([ 6.], domain=[-1, 1], window=[-1, 1])
117117

118118
Evaluation::
119119

@@ -134,7 +134,7 @@ the polynomials are regarded as functions this is composition of
134134
functions::
135135

136136
>>> p(p)
137-
Polynomial([ 6., 16., 36., 36., 27.], [-1., 1.], [-1., 1.])
137+
Polynomial([ 6., 16., 36., 36., 27.], domain=[-1, 1], window=[-1, 1])
138138

139139
Roots::
140140

@@ -148,11 +148,11 @@ tuples, lists, arrays, and scalars are automatically cast in the arithmetic
148148
operations::
149149

150150
>>> p + [1, 2, 3]
151-
Polynomial([ 2., 4., 6.], [-1., 1.], [-1., 1.])
151+
Polynomial([ 2., 4., 6.], domain=[-1, 1], window=[-1, 1])
152152
>>> [1, 2, 3] * p
153-
Polynomial([ 1., 4., 10., 12., 9.], [-1., 1.], [-1., 1.])
153+
Polynomial([ 1., 4., 10., 12., 9.], domain=[-1, 1], window=[-1, 1])
154154
>>> p / 2
155-
Polynomial([ 0.5, 1. , 1.5], [-1., 1.], [-1., 1.])
155+
Polynomial([ 0.5, 1. , 1.5], domain=[-1, 1], window=[-1, 1])
156156

157157
Polynomials that differ in domain, window, or class can't be mixed in
158158
arithmetic::
@@ -180,7 +180,7 @@ conversion of Polynomial classes among themselves is done for type, domain,
180180
and window casting::
181181

182182
>>> p(T([0, 1]))
183-
Chebyshev([ 2.5, 2. , 1.5], [-1., 1.], [-1., 1.])
183+
Chebyshev([ 2.5, 2. , 1.5], domain=[-1, 1], window=[-1, 1])
184184

185185
Which gives the polynomial `p` in Chebyshev form. This works because
186186
:math:`T_1(x) = x` and substituting :math:`x` for :math:`x` doesn't change
@@ -195,18 +195,18 @@ Polynomial instances can be integrated and differentiated.::
195195
>>> from numpy.polynomial import Polynomial as P
196196
>>> p = P([2, 6])
197197
>>> p.integ()
198-
Polynomial([ 0., 2., 3.], [-1., 1.], [-1., 1.])
198+
Polynomial([ 0., 2., 3.], domain=[-1, 1], window=[-1, 1])
199199
>>> p.integ(2)
200-
Polynomial([ 0., 0., 1., 1.], [-1., 1.], [-1., 1.])
200+
Polynomial([ 0., 0., 1., 1.], domain=[-1, 1], window=[-1, 1])
201201

202202
The first example integrates `p` once, the second example integrates it
203203
twice. By default, the lower bound of the integration and the integration
204204
constant are 0, but both can be specified.::
205205

206206
>>> p.integ(lbnd=-1)
207-
Polynomial([-1., 2., 3.], [-1., 1.], [-1., 1.])
207+
Polynomial([-1., 2., 3.], domain=[-1, 1], window=[-1, 1])
208208
>>> p.integ(lbnd=-1, k=1)
209-
Polynomial([ 0., 2., 3.], [-1., 1.], [-1., 1.])
209+
Polynomial([ 0., 2., 3.], domain=[-1, 1], window=[-1, 1])
210210

211211
In the first case the lower bound of the integration is set to -1 and the
212212
integration constant is 0. In the second the constant of integration is set
@@ -215,9 +215,9 @@ number of times the polynomial is differentiated::
215215

216216
>>> p = P([1, 2, 3])
217217
>>> p.deriv(1)
218-
Polynomial([ 2., 6.], [-1., 1.], [-1., 1.])
218+
Polynomial([ 2., 6.], domain=[-1, 1], window=[-1, 1])
219219
>>> p.deriv(2)
220-
Polynomial([ 6.], [-1., 1.], [-1., 1.])
220+
Polynomial([ 6.], domain=[-1, 1], window=[-1, 1])
221221

222222

223223
Other Polynomial Constructors
@@ -233,9 +233,9 @@ are demonstrated below::
233233
>>> from numpy.polynomial import Chebyshev as T
234234
>>> p = P.fromroots([1, 2, 3])
235235
>>> p
236-
Polynomial([ -6., 11., -6., 1.], [-1., 1.], [-1., 1.])
236+
Polynomial([ -6., 11., -6., 1.], domain=[-1, 1], window=[-1, 1])
237237
>>> p.convert(kind=T)
238-
Chebyshev([ -9. , 11.75, -3. , 0.25], [-1., 1.], [-1., 1.])
238+
Chebyshev([ -9. , 11.75, -3. , 0.25], domain=[-1, 1], window=[-1, 1])
239239

240240
The convert method can also convert domain and window::
241241

@@ -249,9 +249,9 @@ available. The cast method works like the convert method while the basis
249249
method returns the basis polynomial of given degree::
250250

251251
>>> P.basis(3)
252-
Polynomial([ 0., 0., 0., 1.], [-1., 1.], [-1., 1.])
252+
Polynomial([ 0., 0., 0., 1.], domain=[-1, 1], window=[-1, 1])
253253
>>> T.cast(p)
254-
Chebyshev([ -9. , 11.75, -3. , 0.25], [-1., 1.], [-1., 1.])
254+
Chebyshev([ -9. , 11.75, -3. , 0.25], domain=[-1, 1], window=[-1, 1])
255255

256256
Conversions between types can be useful, but it is *not* recommended
257257
for routine use. The loss of numerical precision in passing from a

‎numpy/polynomial/_polybase.py

Copy file name to clipboardExpand all lines: numpy/polynomial/_polybase.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ def __init__(self, coef, domain=None, window=None):
260260
self.window = window
261261

262262
def __repr__(self):
263-
format = "%s(%s, %s, %s)"
263+
format = "%s(%s, domain=%s, window=%s)"
264264
coef = repr(self.coef)[6:-1]
265265
domain = repr(self.domain)[6:-1]
266266
window = repr(self.window)[6:-1]

‎numpy/polynomial/chebyshev.py

Copy file name to clipboardExpand all lines: numpy/polynomial/chebyshev.py
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,10 +361,10 @@ def poly2cheb(pol):
361361
>>> from numpy import polynomial as P
362362
>>> p = P.Polynomial(range(4))
363363
>>> p
364-
Polynomial([ 0., 1., 2., 3.], [-1., 1.])
364+
Polynomial([ 0., 1., 2., 3.], domain=[-1, 1], window=[-1, 1])
365365
>>> c = p.convert(kind=P.Chebyshev)
366366
>>> c
367-
Chebyshev([ 1. , 3.25, 1. , 0.75], [-1., 1.])
367+
Chebyshev([ 1. , 3.25, 1. , 0.75], domain=[-1, 1], window=[-1, 1])
368368
>>> P.poly2cheb(range(4))
369369
array([ 1. , 3.25, 1. , 0.75])
370370

‎numpy/polynomial/legendre.py

Copy file name to clipboardExpand all lines: numpy/polynomial/legendre.py
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,10 @@ def poly2leg(pol):
136136
>>> from numpy import polynomial as P
137137
>>> p = P.Polynomial(np.arange(4))
138138
>>> p
139-
Polynomial([ 0., 1., 2., 3.], [-1., 1.])
140-
>>> c = P.Legendre(P.poly2leg(p.coef))
139+
Polynomial([ 0., 1., 2., 3.], domain=[-1, 1], window=[-1, 1])
140+
>>> c = P.Legendre(P.legendre.poly2leg(p.coef))
141141
>>> c
142-
Legendre([ 1. , 3.25, 1. , 0.75], [-1., 1.])
142+
Legendre([ 1. , 3.25, 1. , 0.75], domain=[-1, 1], window=[-1, 1])
143143
144144
"""
145145
[pol] = pu.as_series([pol])

‎numpy/polynomial/tests/test_printing.py

Copy file name to clipboardExpand all lines: numpy/polynomial/tests/test_printing.py
+25-25Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,71 @@
11
from __future__ import division, absolute_import, print_function
22

33
import numpy.polynomial as poly
4-
from numpy.testing import run_module_suite, assert_
4+
from numpy.testing import run_module_suite, assert_equal
55

66

77
class TestStr(object):
88
def test_polynomial_str(self):
99
res = str(poly.Polynomial([0, 1]))
10-
tgt = 'poly([0., 1.])'
11-
assert_(res, tgt)
10+
tgt = 'poly([ 0. 1.])'
11+
assert_equal(res, tgt)
1212

1313
def test_chebyshev_str(self):
1414
res = str(poly.Chebyshev([0, 1]))
15-
tgt = 'leg([0., 1.])'
16-
assert_(res, tgt)
15+
tgt = 'cheb([ 0. 1.])'
16+
assert_equal(res, tgt)
1717

1818
def test_legendre_str(self):
1919
res = str(poly.Legendre([0, 1]))
20-
tgt = 'leg([0., 1.])'
21-
assert_(res, tgt)
20+
tgt = 'leg([ 0. 1.])'
21+
assert_equal(res, tgt)
2222

2323
def test_hermite_str(self):
2424
res = str(poly.Hermite([0, 1]))
25-
tgt = 'herm([0., 1.])'
26-
assert_(res, tgt)
25+
tgt = 'herm([ 0. 1.])'
26+
assert_equal(res, tgt)
2727

2828
def test_hermiteE_str(self):
2929
res = str(poly.HermiteE([0, 1]))
30-
tgt = 'herme([0., 1.])'
31-
assert_(res, tgt)
30+
tgt = 'herme([ 0. 1.])'
31+
assert_equal(res, tgt)
3232

3333
def test_laguerre_str(self):
3434
res = str(poly.Laguerre([0, 1]))
35-
tgt = 'lag([0., 1.])'
36-
assert_(res, tgt)
35+
tgt = 'lag([ 0. 1.])'
36+
assert_equal(res, tgt)
3737

3838

3939
class TestRepr(object):
4040
def test_polynomial_str(self):
4141
res = repr(poly.Polynomial([0, 1]))
42-
tgt = 'Polynomial([0., 1.])'
43-
assert_(res, tgt)
42+
tgt = 'Polynomial([ 0., 1.], domain=[-1, 1], window=[-1, 1])'
43+
assert_equal(res, tgt)
4444

4545
def test_chebyshev_str(self):
4646
res = repr(poly.Chebyshev([0, 1]))
47-
tgt = 'Chebyshev([0., 1.], [-1., 1.], [-1., 1.])'
48-
assert_(res, tgt)
47+
tgt = 'Chebyshev([ 0., 1.], domain=[-1, 1], window=[-1, 1])'
48+
assert_equal(res, tgt)
4949

5050
def test_legendre_repr(self):
5151
res = repr(poly.Legendre([0, 1]))
52-
tgt = 'Legendre([0., 1.], [-1., 1.], [-1., 1.])'
53-
assert_(res, tgt)
52+
tgt = 'Legendre([ 0., 1.], domain=[-1, 1], window=[-1, 1])'
53+
assert_equal(res, tgt)
5454

5555
def test_hermite_repr(self):
5656
res = repr(poly.Hermite([0, 1]))
57-
tgt = 'Hermite([0., 1.], [-1., 1.], [-1., 1.])'
58-
assert_(res, tgt)
57+
tgt = 'Hermite([ 0., 1.], domain=[-1, 1], window=[-1, 1])'
58+
assert_equal(res, tgt)
5959

6060
def test_hermiteE_repr(self):
6161
res = repr(poly.HermiteE([0, 1]))
62-
tgt = 'HermiteE([0., 1.], [-1., 1.], [-1., 1.])'
63-
assert_(res, tgt)
62+
tgt = 'HermiteE([ 0., 1.], domain=[-1, 1], window=[-1, 1])'
63+
assert_equal(res, tgt)
6464

6565
def test_laguerre_repr(self):
6666
res = repr(poly.Laguerre([0, 1]))
67-
tgt = 'Laguerre([0., 1.], [0., 1.], [0., 1.])'
68-
assert_(res, tgt)
67+
tgt = 'Laguerre([ 0., 1.], domain=[0, 1], window=[0, 1])'
68+
assert_equal(res, tgt)
6969

7070

7171
#

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.