8000 Merge pull request #3676 from charris/pep8-polynomial-tests · juliantaylor/numpy@b9b0ea5 · GitHub
[go: up one dir, main page]

Skip to content

Commit b9b0ea5

Browse files
committed
Merge pull request numpy#3676 from charris/pep8-polynomial-tests
STY: Make numpy/polynomial/tests/*.py PEP8 compliant.
2 parents f1e49d7 + 8255df3 commit b9b0ea5

File tree

9 files changed

+504
-523
lines changed

9 files changed

+504
-523
lines changed

numpy/polynomial/tests/test_chebyshev.py

Lines changed: 82 additions & 85 deletions
< 10000 td data-grid-cell-id="diff-eb5d89eb4ce1e3105406835ad91bb5a4f63c753cbea2bba0d026ae2154c51db7-355-354-1" data-selected="false" role="gridcell" style="background-color:var(--bgColor-default);text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative diff-line-number-neutral left-side">354
Original file line numberDiff line numberDiff line change
@@ -7,73 +7,74 @@
77
import numpy.polynomial.chebyshev as cheb
88
from numpy.polynomial.polynomial import polyval
99
from numpy.testing import (
10-
TestCase, assert_almost_equal, assert_raises,
11-
assert_equal, assert_, run_module_suite)
10+
TestCase, assert_almost_equal, assert_raises,
11+
assert_equal, assert_, run_module_suite)
1212

13-
def trim(x) :
13+
14+
def trim(x):
1415
return cheb.chebtrim(x, tol=1e-6)
1516

16-
T0 = [ 1]
17-
T1 = [ 0, 1]
18-
T2 = [-1, 0, 2]
19-
T3 = [ 0, -3, 0, 4]
20-
T4 = [ 1, 0, -8, 0, 8]
21-
T5 = [ 0, 5, 0, -20, 0, 16]
22-
T6 = [-1, 0, 18, 0, -48, 0, 32]
23-
T7 = [ 0, -7, 0, 56, 0, -112, 0, 64]
24-
T8 = [ 1, 0, -32, 0, 160, 0, -256, 0, 128]
25-
T9 = [ 0, 9, 0, -120, 0, 432, 0, -576, 0, 256]
17+
T0 = [1]
18+
T1 = [0, 1]
19+
T2 = [-1, 0, 2]
20+
T3 = [0, -3, 0, 4]
21+
T4 = [1, 0, -8, 0, 8]
22+
T5 = [0, 5, 0, -20, 0, 16]
23+
T6 = [-1, 0, 18, 0, -48, 0, 32]
24+
T7 = [0, -7, 0, 56, 0, -112, 0, 64]
25+
T8 = [1, 0, -32, 0, 160, 0, -256, 0, 128]
26+
T9 = [0, 9, 0, -120, 0, 432, 0, -576, 0, 256]
2627

2728
Tlist = [T0, T1, T2, T3, T4, T5, T6, T7, T8, T9]
2829

2930

30-
class TestPrivate(TestCase) :
31+
class TestPrivate(TestCase):
3132

32-
def test__cseries_to_zseries(self) :
33-
for i in range(5) :
33+
def test__cseries_to_zseries(self):
34+
for i in range(5):
3435
inp = np.array([2] + [1]*i, np.double)
3536
tgt = np.array([.5]*i + [2] + [.5]*i, np.double)
3637
res = cheb._cseries_to_zseries(inp)
3738
assert_equal(res, tgt)
3839

39-
def test__zseries_to_cseries(self) :
40-
for i in range(5) :
40+
def test__zseries_to_cseries(self):
41+
for i in range(5):
4142
inp = np.array([.5]*i + [2] + [.5]*i, np.double)
4243
tgt = np.array([2] + [1]*i, np.double)
4344
res = cheb._zseries_to_cseries(inp)
4445
assert_equal(res, tgt)
4546

4647

47-
class TestConstants(TestCase) :
48+
class TestConstants(TestCase):
4849

49-
def test_chebdomain(self) :
50+
def test_chebdomain(self):
5051
assert_equal(cheb.chebdomain, [-1, 1])
5152

52-
def test_chebzero(self) :
53+
def test_chebzero(self):
5354
assert_equal(cheb.chebzero, [0])
5455

55-
def test_chebone(self) :
56+
def test_chebone(self):
5657
assert_equal(cheb.chebone, [1])
5758

58-
def test_chebx(self) :
59+
def test_chebx(self):
5960
assert_equal(cheb.chebx, [0, 1])
6061

6162

62-
class TestArithmetic(TestCase) :
63+
class TestArithmetic(TestCase):
6364

64-
def test_chebadd(self) :
65-
for i in range(5) :
66-
for j in range(5) :
65+
def test_chebadd(self):
66+
for i in range(5):
67+
for j in range(5):
6768
msg = "At i=%d, j=%d" % (i, j)
6869
tgt = np.zeros(max(i, j) + 1)
6970
tgt[i] += 1
7071
tgt[j] += 1
7172
res = cheb.chebadd([0]*i + [1], [0]*j + [1])
7273
assert_equal(trim(res), trim(tgt), err_msg=msg)
7374

74-
def test_chebsub(self) :
75-
for i in range(5) :
76-
for j in range(5) :
75+
def test_chebsub(self):
76+
for i in range(5):
77+
for j in range(5):
7778
msg = "At i=%d, j=%d" % (i, j)
7879
tgt = np.zeros(max(i, j) + 1)
7980
tgt[i] += 1
@@ -89,19 +90,19 @@ def test_chebmulx(self):
8990
tgt = [0]*(i - 1) + [.5, 0, .5]
9091
assert_equal(cheb.chebmulx(ser), tgt)
9192

92-
def test_chebmul(self) :
93-
for i in range(5) :
94-
for j in range(5) :
93+
def test_chebmul(self):
94+
for i in range(5):
95+
for j in range(5):
9596
msg = "At i=%d, j=%d" % (i, j)
9697
tgt = np.zeros(i + j + 1)
9798
tgt[i + j] += .5
9899
tgt[abs(i - j)] += .5
99100
res = cheb.chebmul([0]*i + [1], [0]*j + [1])
100101
assert_equal(trim(res), trim(tgt), err_msg=msg)
101102

102-
def test_chebdiv(self) :
103-
for i in range(5) :
104-
for j in range(5) :
103+
def test_chebdiv(self):
104+
for i in range(5):
105+
for j in range(5):
105106
msg = "At i=%d, j=%d" % (i, j)
106107
ci = [0]*i + [1]
107108
cj = [0]*j + [1]
@@ -121,22 +122,21 @@ class TestEvaluation(TestCase):
121122
x = np.random.random((3, 5))*2 - 1
122123
y = polyval(x, [1., 2., 3.])
123124

124-
125-
def test_chebval(self) :
125+
def test_chebval(self):
126126
#check empty input
127127
assert_equal(cheb.chebval([], [1]).size, 0)
128128

129129
#check normal input)
130130
x = np.linspace(-1, 1)
131131
y = [polyval(x, c) for c in Tlist]
132-
for i in range(10) :
132+
for i in range(10):
133133
msg = "At i=%d" % i
134134
tgt = y[i]
135135
res = cheb.chebval(x, [0]*i + [1])
136136
assert_almost_equal(res, tgt, err_msg=msg)
137137

138138
#check that shape is preserved
139-
for i in range(3) :
139+
for i in range(3):
140140
dims = [2]*i
141141
x = np.zeros(dims)
142142
assert_equal(cheb.chebval(x, [1]).shape, dims)
@@ -206,9 +206,9 @@ def test_chebgrid3d(self):
206206
assert_(res.shape == (2, 3)*3)
207207

208208

209-
class TestIntegral(TestCase) :
209+
class TestIntegral(TestCase):
210210

211-
def test_chebint(self) :
211+
def test_chebint(self):
212212
# check exceptions
213213
assert_raises(ValueError, cheb.chebint, [0], .5)
214214
assert_raises(ValueError, cheb.chebint, [0], -1)
@@ -221,7 +221,7 @@ def test_chebint(self) :
221221
assert_almost_equal(res, [0, 1])
222222

223223
# check single integration with integration constant
224-
for i in range(5) :
224+
for i in range(5):
225225
scl = i + 1
226226
pol = [0]*i + [1]
227227
tgt = [i] + [0]*i + [1/scl]
@@ -231,15 +231,15 @@ def test_chebint(self) :
231231
assert_almost_equal(trim(res), trim(tgt))
232232

233233
# check single integration with integration constant and lbnd
234-
for i in range(5) :
234+
for i in range(5):
235235
scl = i + 1
236236
pol = [0]*i + [1]
237237
chebpol = cheb.poly2cheb(pol)
238238
chebint = cheb.chebint(chebpol, m=1, k=[i], lbnd=-1)
239239
assert_almost_equal(cheb.chebval(-1, chebint), i)
240240

241241
# check single integration with integration constant and scaling
242-
for i in range(5) :
242+
for i in range(5):
243243
scl = i + 1
244244
pol = [0]*i + [1]
245245
tgt = [i] + [0]*i + [2/scl]
@@ -249,41 +249,41 @@ def test_chebint(self) :
249249
assert_almost_equal(trim(res), trim(tgt))
250250

251251
# check multiple integrations with default k
252-
for i in range(5) :
253-
for j in range(2, 5) :
252+
for i in range(5):
253+
for j in range(2, 5):
254254
pol = [0]*i + [1]
255255
tgt = pol[:]
256-
for k in range(j) :
256+
for k in range(j):
257257
tgt = cheb.chebint(tgt, m=1)
258258
res = cheb.chebint(pol, m=j)
259259
assert_almost_equal(trim(res), trim(tgt))
260260

261261
# check multiple integrations with defined k
262-
for i in range(5) :
263-
for j in range(2, 5) :
262+
for i in range(5):
263+
for j in range(2, 5):
264264
pol = [0]*i + [1]
265265
tgt = pol[:]
266-
for k in range(j) :
266+
for k in range(j):
267267
tgt = cheb.chebint(tgt, m=1, k=[k])
268268
res = cheb.chebint(pol, m=j, k=list(range(j)))
269269
assert_almost_equal(trim(res), trim(tgt))
270270

271271
# check multiple integrations with lbnd
272-
for i in range(5) :
273-
for j in range(2, 5) :
272+
for i in range(5):
273+
for j in range(2, 5):
274274
pol = [0]*i + [1]
275275
tgt = pol[:]
276-
for k in range(j) :
276+
for k in range(j):
277277
tgt = cheb.chebint(tgt, m=1, k=[k], lbnd=-1)
278278
res = cheb.chebint(pol, m=j, k=list(range(j)), lbnd=-1)
279279
assert_almost_equal(trim(res), trim(tgt))
280280

281281
# check multiple integrations with scaling
282-
for i in range(5) :
283-
for j in range(2, 5) :
282+
for i in range(5):
283+
for j in range(2, 5):
284284
pol = [0]*i + [1]
285285
tgt = pol[:]
286-
for k in range(j) :
286+
for k in range(j):
287287
tgt = cheb.chebint(tgt, m=1, k=[k], scl=2)
288288
res = cheb.chebint(pol, m=j, k=list(range(j)), scl=2)
289289
assert_almost_equal(trim(res), trim(tgt))
@@ -305,29 +305,29 @@ def test_chebint_axis(self):
305305
assert_almost_equal(res, tgt)
306306

307307

308-
class TestDerivative(TestCase) :
308+
class TestDerivative(TestCase):
309309

310-
def test_chebder(self) :
310+
def test_chebder(self):
311311
# check exceptions
312312
assert_raises(ValueError, cheb.chebder, [0], .5)
313313
assert_raises(ValueError, cheb.chebder, [0], -1)
314314

315315
# check that zeroth deriviative does nothing
316-
for i in range(5) :
316+
for i in range(5):
317317
tgt = [0]*i + [1]
318318
res = cheb.chebder(tgt, m=0)
319319
assert_equal(trim(res), trim(tgt))
320320

321321
# check that derivation is the inverse of integration
322-
for i in range(5) :
323-
for j in range(2, 5) :
322+
for i in range(5):
323+
for j in range(2, 5):
324324
tgt = [0]*i + [1]
325325
res = cheb.chebder(cheb.chebint(tgt, m=j), m=j)
326326
assert_almost_equal(trim(res), trim(tgt))
327327

328328
# check derivation with scaling
329-
for i in range(5) :
330-
for j in range(2, 5) :
329+
for i in range(5):
330+
for j in range(2, 5):
331331
tgt = [0]*i + [1]
332332
res = cheb.chebder(cheb.chebint(tgt, m=j, scl=2), m=j, scl=.5)
333333
assert_almost_equal(trim(res), trim(tgt))
@@ -349,25 +349,24 @@ class TestVander(TestCase):
349349
# some random values in [-1, 1)
350350
x = np.random.random((3, 5))*2 - 1
351351

352-
353-
def test_chebvander(self) :
352+
def test_chebvander(self):
354353
# check for 1d x
355
x = np.arange(3)
356355
v = cheb.chebvander(x, 3)
357356
assert_(v.shape == (3, 4))
358-
for i in range(4) :
357+
for i in range(4):
359358
coef = [0]*i + [1]
360359
assert_almost_equal(v[..., i], cheb.chebval(x, coef))
361360

362361
# check for 2d x
363362
x = np.array([[1, 2], [3, 4], [5, 6]])
364363
v = cheb.chebvander(x, 3)
365364
assert_(v.shape == (3, 2, 4))
366-
for i in range(4) :
365+
for i in range(4):
367366
coef = [0]*i + [1]
368367
assert_almost_equal(v[..., i], cheb.chebval(x, coef))
369368

370-
def test_chebvander2d(self) :
369+
def test_chebvander2d(self):
371370
# also tests chebval2d for non-square coefficient array
372371
x1, x2, x3 = self.x
373372
c = np.random.random((2, 3))
@@ -380,8 +379,7 @@ def test_chebvander2d(self) :
380379
van = cheb.chebvander2d([x1], [x2], [1, 2])
381380
assert_(van.shape == (1, 5, 6))
382381

383-
384-
def test_chebvander3d(self) :
382+
def test_chebvander3d(self):
385383
# also tests chebval3d for non-square coefficient array
386384
x1, x2, x3 = self.x
387385
c = np.random.random((2, 3, 4))
@@ -397,8 +395,8 @@ def test_chebvander3d(self) :
397395

398396
class TestFitting(TestCase):
399397

400-
def test_chebfit(self) :
401-
def f(x) :
398+
def test_chebfit(self):
399+
def f(x):
402400
return x*(x - 1)*(x - 2)
403401

404402
# Test exceptions
@@ -475,26 +473,26 @@ def test_100(self):
475473
assert_almost_equal(w.sum(), tgt)
476474

477475

478-
class TestMisc(TestCase) :
476+
class TestMisc(TestCase):
479477

480-
def test_chebfromroots(self) :
478+
def test_chebfromroots(self):
481479
res = cheb.chebfromroots([])
482480
assert_almost_equal(trim(res), [1])
483-
for i in range(1, 5) :
481+
for i in range(1, 5):
484482
roots = np.cos(np.linspace(-np.pi, 0, 2*i + 1)[1::2])
485483
tgt = [0]*i + [1]
486484
res = cheb.chebfromroots(roots)*2**(i-1)
487485
assert_almost_equal(trim(res), trim(tgt))
488486

489-
def test_chebroots(self) :
487+
def test_chebroots(self):
490488
assert_almost_equal(cheb.chebroots([1]), [])
491489
assert_almost_equal(cheb.chebroots([1, 2]), [-.5])
492-
for i in range(2, 5) :
490+
for i in range(2, 5):
493491
tgt = np.linspace(-1, 1, i)
494492
res = cheb.chebroots(cheb.chebfromroots(tgt))
495493
assert_almost_equal(trim(res), trim(tgt))
496494

497-
def test_chebtrim(self) :
495+
def test_chebtrim(self):
498496
coef = [2, -1, 1, 0]
499497

500498
# Test exceptions
@@ -505,15 +503,15 @@ def test_chebtrim(self) :
505503
assert_equal(cheb.chebtrim(coef, 1), coef[:-3])
506504
assert_equal(cheb.chebtrim(coef, 2), [0])
507505

508-
def test_chebline(self) :
506+
def test_chebline(self):
509507
assert_equal(cheb.chebline(3, 4), [3, 4])
510508

511-
def test_cheb2poly(self) :
512-
for i in range(10) :
509+
def test_cheb2poly(self):
510+
for i in range(10):
513511
assert_almost_equal(cheb.cheb2poly([0]*i + [1]), Tlist[i])
514512

515-
def test_poly2cheb(self) :
516-
for i in range(10) :
513+
def test_poly2cheb(self):
514+
for i in range(10):
517515
assert_almost_equal(cheb.poly2cheb(Tlist[i]), [0]*i + [1])
518516

519517
def test_weight(self):
@@ -537,7 +535,6 @@ def test_chebpts1(self):
537535
tgt = [-0.9238795325, -0.3826834323, 0.3826834323, 0.9238795325]
538536
assert_almost_equal(cheb.chebpts1(4), tgt)
539537

540-
541538
def test_chebpts2(self):
542539
#test exceptions
543540
assert_raises(ValueError, cheb.chebpts2, 1.5)

0 commit comments

Comments
 (0)
0