7
7
import numpy .polynomial .chebyshev as cheb
8
8
from numpy .polynomial .polynomial import polyval
9
9
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 )
12
12
13
- def trim (x ) :
13
+
14
+ def trim (x ):
14
15
return cheb .chebtrim (x , tol = 1e-6 )
15
16
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 ]
26
27
27
28
Tlist = [T0 , T1 , T2 , T3 , T4 , T5 , T6 , T7 , T8 , T9 ]
28
29
29
30
30
- class TestPrivate (TestCase ) :
31
+ class TestPrivate (TestCase ):
31
32
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 ):
34
35
inp = np .array ([2 ] + [1 ]* i , np .double )
35
36
tgt = np .array ([.5 ]* i + [2 ] + [.5 ]* i , np .double )
36
37
res = cheb ._cseries_to_zseries (inp )
37
38
assert_equal (res , tgt )
38
39
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 ):
41
42
inp = np .array ([.5 ]* i + [2 ] + [.5 ]* i , np .double )
42
43
tgt = np .array ([2 ] + [1 ]* i , np .double )
43
44
res = cheb ._zseries_to_cseries (inp )
44
45
assert_equal (res , tgt )
45
46
46
47
47
- class TestConstants (TestCase ) :
48
+ class TestConstants (TestCase ):
48
49
49
- def test_chebdomain (self ) :
50
+ def test_chebdomain (self ):
50
51
assert_equal (cheb .chebdomain , [- 1 , 1 ])
51
52
52
- def test_chebzero (self ) :
53
+ def test_chebzero (self ):
53
54
assert_equal (cheb .chebzero , [0 ])
54
55
55
- def test_chebone (self ) :
56
+ def test_chebone (self ):
56
57
assert_equal (cheb .chebone , [1 ])
57
58
58
- def test_chebx (self ) :
59
+ def test_chebx (self ):
59
60
assert_equal (cheb .chebx , [0 , 1 ])
60
61
61
62
62
- class TestArithmetic (TestCase ) :
63
+ class TestArithmetic (TestCase ):
63
64
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 ):
67
68
msg = "At i=%d, j=%d" % (i , j )
68
69
tgt = np .zeros (max (i , j ) + 1 )
69
70
tgt [i ] += 1
70
71
tgt [j ] += 1
71
72
res = cheb .chebadd ([0 ]* i + [1 ], [0 ]* j + [1 ])
72
73
assert_equal (trim (res ), trim (tgt ), err_msg = msg )
73
74
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 ):
77
78
msg = "At i=%d, j=%d" % (i , j )
78
79
tgt = np .zeros (max (i , j ) + 1 )
79
80
tgt [i ] += 1
@@ -89,19 +90,19 @@ def test_chebmulx(self):
89
90
tgt = [0 ]* (i - 1 ) + [.5 , 0 , .5 ]
90
91
assert_equal (cheb .chebmulx (ser ), tgt )
91
92
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 ):
95
96
msg = "At i=%d, j=%d" % (i , j )
96
97
tgt = np .zeros (i + j + 1 )
97
98
tgt [i + j ] += .5
98
99
tgt [abs (i - j )] += .5
99
100
res = cheb .chebmul ([0 ]* i + [1 ], [0 ]* j + [1 ])
100
101
assert_equal (trim (res ), trim (tgt ), err_msg = msg )
101
102
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 ):
105
106
msg = "At i=%d, j=%d" % (i , j )
106
107
ci = [0 ]* i + [1 ]
107
108
cj = [0 ]* j + [1 ]
@@ -121,22 +122,21 @@ class TestEvaluation(TestCase):
121
122
x = np .random .random ((3 , 5 ))* 2 - 1
122
123
y = polyval (x , [1. , 2. , 3. ])
123
124
124
-
125
- def test_chebval (self ) :
125
+ def test_chebval (self ):
126
126
#check empty input
127
127
assert_equal (cheb .chebval ([], [1 ]).size , 0 )
128
128
129
129
#check normal input)
130
130
x = np .linspace (- 1 , 1 )
131
131
y = [polyval (x , c ) for c in Tlist ]
132
- for i in range (10 ) :
132
+ for i in range (10 ):
133
133
msg = "At i=%d" % i
134
134
tgt = y [i ]
135
135
res = cheb .chebval (x , [0 ]* i + [1 ])
136
136
assert_almost_equal (res , tgt , err_msg = msg )
137
137
138
138
#check that shape is preserved
139
- for i in range (3 ) :
139
+ for i in range (3 ):
140
140
dims = [2 ]* i
141
141
x = np .zeros (dims )
142
142
assert_equal (cheb .chebval (x , [1 ]).shape , dims )
@@ -206,9 +206,9 @@ def test_chebgrid3d(self):
206
206
assert_ (res .shape == (2 , 3 )* 3 )
207
207
208
208
209
- class TestIntegral (TestCase ) :
209
+ class TestIntegral (TestCase ):
210
210
211
- def test_chebint (self ) :
211
+ def test_chebint (self ):
212
212
# check exceptions
213
213
assert_raises (ValueError , cheb .chebint , [0 ], .5 )
214
214
assert_raises (ValueError , cheb .chebint , [0 ], - 1 )
@@ -221,7 +221,7 @@ def test_chebint(self) :
221
221
assert_almost_equal (res , [0 , 1 ])
222
222
223
223
# check single integration with integration constant
224
- for i in range (5 ) :
224
+ for i in range (5 ):
225
225
scl = i + 1
226
226
pol = [0 ]* i + [1 ]
227
227
tgt = [i ] + [0 ]* i + [1 / scl ]
@@ -231,15 +231,15 @@ def test_chebint(self) :
231
231
assert_almost_equal (trim (res ), trim (tgt ))
232
232
233
233
# check single integration with integration constant and lbnd
234
- for i in range (5 ) :
234
+ for i in range (5 ):
235
235
scl = i + 1
236
236
pol = [0 ]* i + [1 ]
237
237
chebpol = cheb .poly2cheb (pol )
238
238
chebint = cheb .chebint (chebpol , m = 1 , k = [i ], lbnd = - 1 )
239
239
assert_almost_equal (cheb .chebval (- 1 , chebint ), i )
240
240
241
241
# check single integration with integration constant and scaling
242
- for i in range (5 ) :
242
+ for i in range (5 ):
243
243
scl = i + 1
244
244
pol = [0 ]* i + [1 ]
245
245
tgt = [i ] + [0 ]* i + [2 / scl ]
@@ -249,41 +249,41 @@ def test_chebint(self) :
249
249
assert_almost_equal (trim (res ), trim (tgt ))
250
250
251
251
# 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 ):
254
254
pol = [0 ]* i + [1 ]
255
255
tgt = pol [:]
256
- for k in range (j ) :
256
+ for k in range (j ):
257
257
tgt = cheb .chebint (tgt , m = 1 )
258
258
res = cheb .chebint (pol , m = j )
259
259
assert_almost_equal (trim (res ), trim (tgt ))
260
260
261
261
# 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 ):
264
264
pol = [0 ]* i + [1 ]
265
265
tgt = pol [:]
266
- for k in range (j ) :
266
+ for k in range (j ):
267
267
tgt = cheb .chebint (tgt , m = 1 , k = [k ])
268
268
res = cheb .chebint (pol , m = j , k = list (range (j )))
269
269
assert_almost_equal (trim (res ), trim (tgt ))
270
270
271
271
# 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 ):
274
274
pol = [0 ]* i + [1 ]
275
275
tgt = pol [:]
276
- for k in range (j ) :
276
+ for k in range (j ):
277
277
tgt = cheb .chebint (tgt , m = 1 , k = [k ], lbnd = - 1 )
278
278
res = cheb .chebint (pol , m = j , k = list (range (j )), lbnd = - 1 )
279
279
assert_almost_equal (trim (res ), trim (tgt ))
280
280
281
281
# 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 ):
284
284
pol = [0 ]* i + [1 ]
285
285
tgt = pol [:]
286
- for k in range (j ) :
286
+ for k in range (j ):
287
287
tgt = cheb .chebint (tgt , m = 1 , k = [k ], scl = 2 )
288
288
res = cheb .chebint (pol , m = j , k = list (range (j )), scl = 2 )
289
289
assert_almost_equal (trim (res ), trim (tgt ))
@@ -305,29 +305,29 @@ def test_chebint_axis(self):
305
305
assert_almost_equal (res , tgt )
306
306
307
307
308
- class TestDerivative (TestCase ) :
308
+ class TestDerivative (TestCase ):
309
309
310
- def test_chebder (self ) :
310
+ def test_chebder (self ):
311
311
# check exceptions
312
312
assert_raises (ValueError , cheb .chebder , [0 ], .5 )
313
313
assert_raises (ValueError , cheb .chebder , [0 ], - 1 )
314
314
315
315
# check that zeroth deriviative does nothing
316
- for i in range (5 ) :
316
+ for i in range (5 ):
317
317
tgt = [0 ]* i + [1 ]
318
318
res = cheb .chebder (tgt , m = 0 )
319
319
assert_equal (trim (res ), trim (tgt ))
320
320
321
321
# 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 ):
324
324
tgt = [0 ]* i + [1 ]
325
325
res = cheb .chebder (cheb .chebint (tgt , m = j ), m = j )
326
326
assert_almost_equal (trim (res ), trim (tgt ))
327
327
328
328
# 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 ):
331
331
tgt = [0 ]* i + [1 ]
332
332
res = cheb .chebder (cheb .chebint (tgt , m = j , scl = 2 ), m = j , scl = .5 )
333
333
assert_almost_equal (trim (res ), trim (tgt ))
@@ -349,25 +349,24 @@ class TestVander(TestCase):
349
349
# some random values in [-1, 1)
350
350
x = np .random .random ((3 , 5 ))* 2 - 1
351
351
352
-
353
- def test_chebvander (self ) :
352
+ def test_chebvander (self ):
354
353
# check for 1d x
355
<
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
x = np .arange (3 )
356
355
v = cheb .chebvander (x , 3 )
357
356
assert_ (v .shape == (3 , 4 ))
358
- for i in range (4 ) :
357
+ for i in range (4 ):
359
358
coef = [0 ]* i + [1 ]
360
359
assert_almost_equal (v [..., i ], cheb .chebval (x , coef ))
361
360
362
361
# check for 2d x
363
362
x = np .array ([[1 , 2 ], [3 , 4 ], [5 , 6 ]])
364
363
v = cheb .chebvander (x , 3 )
365
364
assert_ (v .shape == (3 , 2 , 4 ))
366
- for i in range (4 ) :
365
+ for i in range (4 ):
367
366
coef = [0 ]* i + [1 ]
368
367
assert_almost_equal (v [..., i ], cheb .chebval (x , coef ))
369
368
370
- def test_chebvander2d (self ) :
369
+ def test_chebvander2d (self ):
371
370
# also tests chebval2d for non-square coefficient array
372
371
x1 , x2 , x3 = self .x
373
372
c = np .random .random ((2 , 3 ))
@@ -380,8 +379,7 @@ def test_chebvander2d(self) :
380
379
van = cheb .chebvander2d ([x1 ], [x2 ], [1 , 2 ])
381
380
assert_ (van .shape == (1 , 5 , 6 ))
382
381
383
-
384
- def test_chebvander3d (self ) :
382
+ def test_chebvander3d (self ):
385
383
# also tests chebval3d for non-square coefficient array
386
384
x1 , x2 , x3 = self .x
387
385
c = np .random .random ((2 , 3 , 4 ))
@@ -397,8 +395,8 @@ def test_chebvander3d(self) :
397
395
398
396
class TestFitting (TestCase ):
399
397
400
- def test_chebfit (self ) :
401
- def f (x ) :
398
+ def test_chebfit (self ):
399
+ def f (x ):
402
400
return x * (x - 1 )* (x - 2 )
403
401
404
402
# Test exceptions
@@ -475,26 +473,26 @@ def test_100(self):
475
473
assert_almost_equal (w .sum (), tgt )
476
474
477
475
478
- class TestMisc (TestCase ) :
476
+ class TestMisc (TestCase ):
479
477
480
- def test_chebfromroots (self ) :
478
+ def test_chebfromroots (self ):
481
479
res = cheb .chebfromroots ([])
482
480
assert_almost_equal (trim (res ), [1 ])
483
- for i in range (1 , 5 ) :
481
+ for i in range (1 , 5 ):
484
482
roots = np .cos (np .linspace (- np .pi , 0 , 2 * i + 1 )[1 ::2 ])
485
483
tgt = [0 ]* i + [1 ]
486
484
res = cheb .chebfromroots (roots )* 2 ** (i - 1 )
487
485
assert_almost_equal (trim (res ), trim (tgt ))
488
486
489
- def test_chebroots (self ) :
487
+ def test_chebroots (self ):
490
488
assert_almost_equal (cheb .chebroots ([1 ]), [])
491
489
assert_almost_equal (cheb .chebroots ([1 , 2 ]), [- .5 ])
492
- for i in range (2 , 5 ) :
490
+ for i in range (2 , 5 ):
493
491
tgt = np .linspace (- 1 , 1 , i )
494
492
res = cheb .chebroots (cheb .chebfromroots (tgt ))
495
493
assert_almost_equal (trim (res ), trim (tgt ))
496
494
497
- def test_chebtrim (self ) :
495
+ def test_chebtrim (self ):
498
496
coef = [2 , - 1 , 1 , 0 ]
499
497
500
498
# Test exceptions
@@ -505,15 +503,15 @@ def test_chebtrim(self) :
505
503
assert_equal (cheb .chebtrim (coef , 1 ), coef [:- 3 ])
506
504
assert_equal (cheb .chebtrim (coef , 2 ), [0 ])
507
505
508
- def test_chebline (self ) :
506
+ def test_chebline (self ):
509
507
assert_equal (cheb .chebline (3 , 4 ), [3 , 4 ])
510
508
511
- def test_cheb2poly (self ) :
512
- for i in range (10 ) :
509
+ def test_cheb2poly (self ):
510
+ for i in range (10 ):
513
511
assert_almost_equal (cheb .cheb2poly ([0 ]* i + [1 ]), Tlist [i ])
514
512
515
- def test_poly2cheb (self ) :
516
- for i in range (10 ) :
513
+ def test_poly2cheb (self ):
514
+ for i in range (10 ):
517
515
assert_almost_equal (cheb .poly2cheb (Tlist [i ]), [0 ]* i + [1 ])
518
516
519
517
def test_weight (self ):
@@ -537,7 +535,6 @@ def test_chebpts1(self):
537
535
tgt = [- 0.9238795325 , - 0.3826834323 , 0.3826834323 , 0.9238795325 ]
538
536
assert_almost_equal (cheb .chebpts1 (4 ), tgt )
539
537
540
-
541
538
def test_chebpts2 (self ):
542
539
#test exceptions
543
540
assert_raises (ValueError , cheb .chebpts2 , 1.5 )
0 commit comments