1+ using FastTransforms, FillArrays, Test
2+ import FastTransforms: clenshaw, clenshaw!, forwardrecurrence!, forwardrecurrence
3+
4+ @testset " clenshaw" begin
5+ @testset " Chebyshev T" begin
6+ c = [1 ,2 ,3 ]
7+ cf = float (c)
8+ @test @inferred (clenshaw (c,1 )) ≡ 1 + 2 + 3
9+ @test @inferred (clenshaw (c,0 )) ≡ 1 + 0 - 3
10+ @test @inferred (clenshaw (c,0.1 )) == 1 + 2 * 0.1 + 3 * cos (2 acos (0.1 ))
11+ @test @inferred (clenshaw (c,[- 1 ,0 ,1 ])) == clenshaw! (c,[- 1 ,0 ,1 ]) == [2 ,- 2 ,6 ]
12+ @test clenshaw (c,[- 1 ,0 ,1 ]) isa Vector{Int}
13+ @test @inferred (clenshaw (Float64[],1 )) ≡ 0.0
14+
15+ x = [1 ,0 ,0.1 ]
16+ @test @inferred (clenshaw (c,x)) ≈ @inferred (clenshaw! (c,copy (x))) ≈
17+ @inferred (clenshaw! (c,x,similar (x))) ≈
18+ @inferred (clenshaw (cf,x)) ≈ @inferred (clenshaw! (cf,copy (x))) ≈
19+ @inferred (clenshaw! (cf,x,similar (x))) ≈ [6 ,- 2 ,- 1.74 ]
20+ end
21+
22+ @testset " Chebyshev U" begin
23+ N = 5
24+ A, B, C = Fill (2 ,N- 1 ), Zeros {Int} (N- 1 ), Ones {Int} (N)
25+ @testset " forwardrecurrence!" begin
26+ @test @inferred (forwardrecurrence (N, A, B, C, 1 )) == @inferred (forwardrecurrence! (Vector {Int} (undef,N), A, B, C, 1 )) == 1 : N
27+ @test forwardrecurrence! (Vector {Int} (undef,N), A, B, C, - 1 ) == (- 1 ) .^ (0 : N- 1 ) .* (1 : N)
28+ @test forwardrecurrence (N, A, B, C, 0.1 ) ≈ forwardrecurrence! (Vector {Float64} (undef,N), A, B, C, 0.1 ) ≈
29+ sin .((1 : N) .* acos (0.1 )) ./ sqrt (1 - 0.1 ^ 2 )
30+ end
31+
32+ c = [1 ,2 ,3 ]
33+ @test c' forwardrecurrence (3 , A, B, C, 0.1 ) ≈ clenshaw ([1 ,2 ,3 ], A, B, C, 0.1 ) ≈
34+ 1 + (2 sin (2 acos (0.1 )) + 3 sin (3 acos (0.1 )))/ sqrt (1 - 0.1 ^ 2 )
35+ end
36+
37+ @testset " Chebyshev-as-general" begin
38+ @testset " forwardrecurrence!" begin
39+ N = 5
40+ A, B, C = [1 ; fill (2 ,N- 2 )], fill (0 ,N- 1 ), fill (1 ,N)
41+ Af, Bf, Cf = float (A), float (B), float (C)
42+ @test forwardrecurrence (N, A, B, C, 1 ) == forwardrecurrence! (Vector {Int} (undef,N), A, B, C, 1 ) == ones (Int,N)
43+ @test forwardrecurrence! (Vector {Int} (undef,N), A, B, C, - 1 ) == (- 1 ) .^ (0 : N- 1 )
44+ @test forwardrecurrence (N, A, B, C, 0.1 ) ≈ forwardrecurrence! (Vector {Float64} (undef,N), A, B, C, 0.1 ) ≈ cos .((0 : N- 1 ) .* acos (0.1 ))
45+ end
46+
47+ c, A, B, C = [1 ,2 ,3 ], [1 ,2 ,2 ], fill (0 ,3 ), fill (1 ,4 )
48+ cf, Af, Bf, Cf = float (c), float (A), float (B), float (C)
49+ @test @inferred (clenshaw (c, A, B, C, 1 )) ≡ 6
50+ @test @inferred (clenshaw (c, A, B, C, 0.1 )) ≡ - 1.74
51+ @test @inferred (clenshaw ([1 ,2 ,3 ], A, B, C, [- 1 ,0 ,1 ])) == clenshaw! ([1 ,2 ,3 ],A, B, C, [- 1 ,0 ,1 ]) == [2 ,- 2 ,6 ]
52+ @test clenshaw (c, A, B, C, [- 1 ,0 ,1 ]) isa Vector{Int}
53+ @test @inferred (clenshaw (Float64[], A, B, C, 1 )) ≡ 0.0
54+
55+ x = [1 ,0 ,0.1 ]
56+ @test @inferred (clenshaw (c, A, B, C, x)) ≈ @inferred (clenshaw! (c, A, B, C, copy (x))) ≈
57+ @inferred (clenshaw! (c, A, B, C, x, one .(x), similar (x))) ≈
58+ @inferred (clenshaw! (cf, Af, Bf, Cf, x, one .(x),similar (x))) ≈
59+ @inferred (clenshaw ([1. ,2 ,3 ], A, B, C, x)) ≈
60+ @inferred (clenshaw! ([1. ,2 ,3 ], A, B, C, copy (x))) ≈ [6 ,- 2 ,- 1.74 ]
61+ end
62+
63+ @testset " Legendre" begin
64+ @testset " Float64" begin
65+ N = 5
66+ n = 0 : N- 1
67+ A = (2 n .+ 1 ) ./ (n .+ 1 )
68+ B = zeros (N)
69+ C = n ./ (n .+ 1 )
70+ v_1 = forwardrecurrence (N, A, B, C, 1 )
71+ v_f = forwardrecurrence (N, A, B, C, 0.1 )
72+ @test v_1 ≈ ones (N)
73+ @test forwardrecurrence (N, A, B, C, - 1 ) ≈ (- 1 ) .^ (0 : N- 1 )
74+ @test v_f ≈ [1 ,0.1 ,- 0.485 ,- 0.1475 ,0.3379375 ]
75+
76+ n = 0 : N # need extra entry for C in Clenshaw
77+ C = n ./ (n .+ 1 )
78+ for j = 1 : N
79+ c = [zeros (j- 1 ); 1 ]
80+ @test clenshaw (c, A, B, C, 1 ) ≈ v_1[j] # Julia code
81+ @test clenshaw (c, A, B, C, 0.1 ) ≈ v_f[j] # Julia code
82+ @test clenshaw! (c, A, B, C, [1.0 ,0.1 ], [1.0 ,1.0 ], [0.0 ,0.0 ]) ≈ [v_1[j],v_f[j]] # libfasttransforms
83+ end
84+ end
85+
86+ @testset " BigFloat" begin
87+ N = 5
88+ n = BigFloat (0 ): N- 1
89+ A = (2 n .+ 1 ) ./ (n .+ 1 )
90+ B = zeros (N)
91+ C = n ./ (n .+ 1 )
92+ @test forwardrecurrence (N, A, B, C, parse (BigFloat," 0.1" )) ≈ [1 ,big " 0.1" ,big " -0.485" ,big " -0.1475" ,big " 0.3379375" ]
93+ end
94+ end
95+
96+ @testset " Int" begin
97+ N = 10 ; A = 1 : 10 ; B = 2 : 11 ; C = range (3 ; step= 2 , length= N+ 1 )
98+ v_i = forwardrecurrence (N, A, B, C, 1 )
99+ v_f = forwardrecurrence (N, A, B, C, 0.1 )
100+ @test v_i isa Vector{Int}
101+ @test v_f isa Vector{Float64}
102+
103+ j = 3
104+ clenshaw ([zeros (Int,j- 1 ); 1 ; zeros (Int,N- j)], A, B, C, 1 ) == v_i[j]
105+ end
106+ end
0 commit comments