-
Notifications
You must be signed in to change notification settings - Fork 0
/
chebychev.jl
181 lines (165 loc) · 6.86 KB
/
chebychev.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
using Distributions
using MultivariateChebyshev
using Test
include("test_functions.jl")
@testset "Chebyshev methods." begin
@testset "Test chebyshev_points." begin
degrees = [ 1, 2, 3 ]
points = MultivariateChebyshev.chebyshev_points(degrees)
@test size(points)==(3,)
@test points[1]==[1.0, -1.0]
@test isapprox(points[2], [1.0, 0.0, -1.0], atol=1.0e-15)
@test isapprox(points[3], [1.0, 0.5, -0.5, -1.0], atol=1.0e-15)
# println(points)
end
@testset "Test chebyshev_multi_points." begin
degrees = [ 1, 2]
points = MultivariateChebyshev.chebyshev_multi_points(degrees)
points_ref = [
1.0 1.0;
-1.0 1.0;
1.0 0.0;
-1.0 0.0;
1.0 -1.0;
-1.0 -1.0
]
@test isapprox(points, points_ref, atol=1.0e-16)
end
@testset "Test chebyshev_polynomials." begin
polys = MultivariateChebyshev.chebyshev_polynomials(0.5, 3)
# println(size(polys))
# println(polys)
@test size(polys)==(1,4)
@test polys==[1.0 0.5 -0.5 -1.0]
#
x = collect(range(-1.0, 1.0, 5))
polys = MultivariateChebyshev.chebyshev_polynomials(x, 3)
# println(size(polys))
# println(polys)
polys_ref = [
1.0 -1.0 1.0 -1.0;
1.0 -0.5 -0.5 1.0;
1.0 0.0 -1.0 -0.0;
1.0 0.5 -0.5 -1.0;
1.0 1.0 1.0 1.0
]
@test size(polys)==(5,4)
@test polys==polys_ref
end
@testset "Test chebyshev_batch_call." begin
C = ones(2, 3, 4)
x = ones(5, 3)
y = MultivariateChebyshev.chebyshev_batch_call(C, x)
# println(size(y))
# println(y)
@test y==24.0*ones(5)
y = MultivariateChebyshev.chebyshev_batch_call(C, x, MultivariateChebyshev.matmul)
@test y==24.0*ones(5)
end
@testset "Test chebyshev_coefficients with batchmul." begin
degrees = [ 2, 3, 4 ]
multi_points = MultivariateChebyshev.chebyshev_multi_points(degrees)
values = ones(size(multi_points, 1))
coeff = MultivariateChebyshev.chebyshev_coefficients(degrees, multi_points, values)
coeff_ref = zeros(size(coeff))
coeff_ref[(1 for d in degrees)...] = 1.0
# println(coeff)
@test isapprox(coeff, coeff_ref, atol=5.0e-16)
#
a = [ 0.5, 0.01, -1.0 ]
b = [ 2.0, 0.50, +1.0 ]
degrees = [ 3, 4, 5 ]
#
multi_points = MultivariateChebyshev.chebyshev_multi_points(degrees)
Y = MultivariateChebyshev.chebyshev_transform(multi_points, a, b)
values = [ BlackOverK(Y[i,:]) for i in 1:size(Y,1) ]
C = MultivariateChebyshev.chebyshev_coefficients(degrees, multi_points, values)
# println(size(C))
@test size(C)==(4,5,6)
end
@testset "Test chebyshev_coefficients with matmul" begin
degrees = [ 2, 3, 4 ]
multi_points = MultivariateChebyshev.chebyshev_multi_points(degrees)
values = ones(size(multi_points, 1))
coeff = MultivariateChebyshev.chebyshev_coefficients(degrees, multi_points, values, MultivariateChebyshev.matmul)
coeff_ref = zeros(size(coeff))
coeff_ref[(1 for d in degrees)...] = 1.0
# println(coeff)
@test isapprox(coeff, coeff_ref, atol=5.0e-16)
#
a = [ 0.5, 0.01, -1.0 ]
b = [ 2.0, 0.50, +1.0 ]
degrees = [ 3, 4, 5 ]
#
multi_points = MultivariateChebyshev.chebyshev_multi_points(degrees)
Y = MultivariateChebyshev.chebyshev_transform(multi_points, a, b)
values = [ BlackOverK(Y[i,:]) for i in 1:size(Y,1) ]
C = MultivariateChebyshev.chebyshev_coefficients(degrees, multi_points, values, MultivariateChebyshev.matmul)
# println(size(C))
@test size(C)==(4,5,6)
end
@testset "Test Black formula chebyshev_points with batchmul" begin
a = [ 0.5, 0.01, -1.0 ]
b = [ 2.0, 0.50, +1.0 ]
degrees = [ 3, 4, 5 ]
#
multi_points = MultivariateChebyshev.chebyshev_multi_points(degrees)
Y = MultivariateChebyshev.chebyshev_transform(multi_points, a, b)
values = [ BlackOverK(Y[i,:]) for i in 1:size(Y,1) ]
C = MultivariateChebyshev.chebyshev_coefficients(degrees, multi_points, values)
#
z = MultivariateChebyshev.chebyshev_interpolation(Y, C, a, b)
# println(max(abs.(z - values)...))
@test isapprox(z, values, atol=1.0e-14)
end
@testset "Test Black formula chebyshev_points with matmul" begin
a = [ 0.5, 0.01, -1.0 ]
b = [ 2.0, 0.50, +1.0 ]
degrees = [ 3, 4, 5 ]
#
multi_points = MultivariateChebyshev.chebyshev_multi_points(degrees)
Y = MultivariateChebyshev.chebyshev_transform(multi_points, a, b)
values = [ BlackOverK(Y[i,:]) for i in 1:size(Y,1) ]
C = MultivariateChebyshev.chebyshev_coefficients(degrees, multi_points, values, MultivariateChebyshev.matmul)
#
z = MultivariateChebyshev.chebyshev_interpolation(Y, C, a, b, MultivariateChebyshev.matmul)
# println(max(abs.(z - values)...))
@test isapprox(z, values, atol=1.0e-14)
end
@testset "Teest Black formula with random points and batchmul." begin
a = [ 0.5, 0.50, -1.0 ]
b = [ 2.0, 2.50, +1.0 ]
degrees = [ 5, 5, 5 ]
#
multi_points = MultivariateChebyshev.chebyshev_multi_points(degrees)
Y = MultivariateChebyshev.chebyshev_transform(multi_points, a, b)
values = [ BlackOverK(Y[i,:]) for i in 1:size(Y,1) ]
C = MultivariateChebyshev.chebyshev_coefficients(degrees, multi_points, values)
#
base2 = 13
y = a' .+ rand(2^base2, 3) .* (b.-a)'
z = MultivariateChebyshev.chebyshev_interpolation(y, C, a, b)
z_ref = [ BlackOverK(y[i,:]) for i in 1:size(y,1) ]
# println(max(abs.(z - z_ref)...))
@test isapprox(z, z_ref, atol=1.0e-1)
@test max(abs.(z - z_ref)...) < 7.5e-3
end
@testset "Teest Black formula with random points and matmul" begin
a = [ 0.5, 0.50, -1.0 ]
b = [ 2.0, 2.50, +1.0 ]
degrees = [ 5, 5, 5 ]
#
multi_points = MultivariateChebyshev.chebyshev_multi_points(degrees)
Y = MultivariateChebyshev.chebyshev_transform(multi_points, a, b)
values = [ BlackOverK(Y[i,:]) for i in 1:size(Y,1) ]
C = MultivariateChebyshev.chebyshev_coefficients(degrees, multi_points, values, MultivariateChebyshev.matmul)
#
base2 = 13
y = a' .+ rand(2^base2, 3) .* (b.-a)'
z = MultivariateChebyshev.chebyshev_interpolation(y, C, a, b, MultivariateChebyshev.matmul)
z_ref = [ BlackOverK(y[i,:]) for i in 1:size(y,1) ]
# println(max(abs.(z - z_ref)...))
@test isapprox(z, z_ref, atol=1.0e-1)
@test max(abs.(z - z_ref)...) < 7.5e-3
end
end