-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
inf_integral_tests.jl
224 lines (209 loc) · 8.48 KB
/
inf_integral_tests.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
using Integrals, Distributions, Test, Cubature, FastGaussQuadrature, StaticArrays
reltol = 0.0
abstol = 1e-3
# not all quadratures are compatible with infinities if they evaluate the endpoints
alg_req = Dict(
QuadratureRule(gausslegendre, n = 50) => (
nout = Inf, min_dim = 1, max_dim = 1, allows_batch = false,
allows_iip = false, allows_inf = true),
QuadGKJL() => (nout = Inf, allows_batch = true, min_dim = 1, max_dim = 1,
allows_iip = true, allows_inf = true),
HCubatureJL() => (nout = Inf, allows_batch = false, min_dim = 1,
max_dim = Inf, allows_iip = true, allows_inf = true),
CubatureJLh() => (nout = Inf, allows_batch = true, min_dim = 1,
max_dim = Inf, allows_iip = true, allows_inf = true)
)
# GaussLegendre(n=50) => (nout = Inf, min_dim = 1, max_dim = 1, allows_batch = false,
# allows_iip = false, allows_inf=true),
# VEGAS() => (nout = 1, allows_batch = true, min_dim = 2, max_dim = Inf,
# allows_iip = true),
# CubatureJLp() => (nout = Inf, allows_batch = true, min_dim = 1,
# max_dim = Inf, allows_iip = true, allows_inf=false),
# CubaVegas() => (nout = Inf, allows_batch = true, min_dim = 1, max_dim = Inf,
# allows_iip = true),
# CubaSUAVE() => (nout = Inf, allows_batch = true, min_dim = 1, max_dim = Inf,
# allows_iip = true),
# CubaDivonne() => (nout = Inf, allows_batch = true, min_dim = 2,
# max_dim = Inf, allows_iip = true),
# CubaCuhre() => (nout = Inf, allows_batch = true, min_dim = 2, max_dim = Inf,
# allows_iip = true),
problems = (
(; # 1. multi-variate infinite limits: Gaussian
f = (x, p) -> pdf(MvNormal([0.00, 0.00], [0.4 0.0; 0.00 0.4]), x),
domain = (@SVector[-Inf, -Inf], @SVector[Inf, Inf]),
solution = 1.00
),
(; # 2. multi-variate flipped infinite limits: Gaussian
f = (x, p) -> pdf(MvNormal([0.00, 0.00], [0.4 0.0; 0.00 0.4]), x),
domain = (@SVector[Inf, Inf], @SVector[-Inf, -Inf]),
solution = 1.00
),
(; # 3. multi-variate mixed infinite/semi-infinite upper limit: Gaussian
f = (x, p) -> pdf(MvNormal([0.00, 0.00], [0.4 0.0; 0.00 0.4]), x),
domain = (@SVector[-Inf, 0], @SVector[Inf, Inf]),
solution = 0.5
),
(; # 4. multi-variate mixed infinite/semi-infinite lower limit: Gaussian
f = (x, p) -> pdf(MvNormal([0.00, 0.00], [0.4 0.0; 0.00 0.4]), x),
domain = (@SVector[-Inf, -Inf], @SVector[Inf, 0]),
solution = 0.5
),
(; # 5. multi-variate mixed infinite/finite: Gaussian * quadratic
f = (x, p) -> pdf(Normal(0.00, 1.00), x[1]) * x[2]^2,
domain = (@SVector[-Inf, 1], @SVector[Inf, 4]),
solution = 21.0
),
(; # 6. multi-variate mixed semi-infinite lower limit/finite: Gaussian * quadratic
f = (x, p) -> pdf(Normal(0.00, 1.00), x[1]) * x[2]^2,
domain = (@SVector[0.00, 1], @SVector[Inf, 4]),
solution = 10.5
),
(; # 7. multi-variate mixed semi-infinite upper limit/finite: Gaussian * quadratic
f = (x, p) -> pdf(Normal(0.00, 1.00), x[1]) * x[2]^2,
domain = (@SVector[-Inf, 1], @SVector[0.00, 4]),
solution = 10.5
),
(; # 8. single-variable infinite limit: Gaussian
f = (x, p) -> pdf(Normal(0.00, 1.00), x),
domain = (-Inf, Inf),
solution = 1.0
),
(; # 9. single-variable flipped infinite limit: Gaussian
f = (x, p) -> pdf(Normal(0.00, 1.00), x),
domain = (Inf, -Inf),
solution = -1.0
),
(; # 10. single-variable semi-infinite upper limit: Gaussian
f = (x, p) -> pdf(Normal(0.00, 1.00), x),
domain = (0.00, Inf),
solution = 0.5
),
(; # 11. single-variable flipped, semi-infinite upper limit: Gaussian
f = (x, p) -> pdf(Normal(0.00, 1.00), x),
domain = (0.00, -Inf),
solution = -0.5
),
(; # 12. single-variable semi-infinite lower limit: Gaussian
f = (x, p) -> pdf(Normal(0.00, 1.00), x),
domain = (-Inf, 0.00),
solution = 0.5
),
(; # 13. single-variable flipped, semi-infinite lower limit: Gaussian
f = (x, p) -> pdf(Normal(0.00, 1.00), x),
domain = (Inf, 0.00),
solution = -0.5
),
(; # 14. single-variable infinite limit: Lorentzian
f = (x, p) -> 1 / (x^2 + 1),
domain = (-Inf, Inf),
solution = pi / 1
),
(; # 15. single-variable shifted, semi-infinite lower limit: Lorentzian
f = (x, p) -> 1 / ((x - 2)^2 + 1),
domain = (-Inf, 2),
solution = pi / 2
),
(; # 16. single-variable shifted, semi-infinite upper limit: Lorentzian
f = (x, p) -> 1 / ((x - 2)^2 + 1),
domain = (2, Inf),
solution = pi / 2
),
(; # 17. single-variable flipped, shifted, semi-infinite lower limit: Lorentzian
f = (x, p) -> 1 / ((x - 2)^2 + 1),
domain = (Inf, 2),
solution = -pi / 2
),
(; # 18. single-variable flipped, shifted, semi-infinite upper limit: Lorentzian
f = (x, p) -> 1 / ((x - 2)^2 + 1),
domain = (2, -Inf),
solution = -pi / 2
),
(; # 19. single-variable finite limits: quadratic
f = (x, p) -> x^2,
domain = (1, 4),
solution = 21
),
(; # 20. single-variable flipped, finite limits: quadratic
f = (x, p) -> x^2,
domain = (4, 1),
solution = -21
)
)
function f_helper!(f, y, x, p)
y[] = f(x, p)
return
end
function batch_helper(f, x, p)
map(i -> f(x[axes(x)[begin:(end - 1)]..., i], p), axes(x)[end])
end
function batch_helper!(f, y, x, p)
y .= batch_helper(f, x, p)
return
end
do_tests = function (; f, domain, alg, abstol, reltol, solution)
prob = IntegralProblem(f, domain)
sol = solve(prob, alg; reltol, abstol)
@test abs(only(sol) - solution) < max(abstol, reltol * abs(solution))
cache = @test_nowarn @inferred init(prob, alg)
@test_nowarn @inferred solve!(cache)
@test_nowarn @inferred solve(prob, alg)
end
# IntegralFunction{false}
for (alg, req) in pairs(alg_req), (j, (; f, domain, solution)) in enumerate(problems)
req.allows_inf || continue
req.nout >= length(solution) || continue
req.min_dim <= length(first(domain)) <= req.max_dim || continue
@info "oop infinity test" alg=nameof(typeof(alg)) problem=j
do_tests(; f, domain, solution, alg, abstol, reltol)
end
# IntegralFunction{true}
for (alg, req) in pairs(alg_req), (j, (; f, domain, solution)) in enumerate(problems)
req.allows_inf || continue
req.nout >= length(solution) || continue
req.allows_iip || continue
req.min_dim <= length(first(domain)) <= req.max_dim || continue
@info "iip infinity test" alg=nameof(typeof(alg)) problem=j
fiip = IntegralFunction((y, x, p) -> f_helper!(f, y, x, p), zeros(size(solution)))
do_tests(; f = fiip, domain, solution, alg, abstol, reltol)
end
# BatchIntegralFunction{false}
for (alg, req) in pairs(alg_req), (j, (; f, domain, solution)) in enumerate(problems)
req.allows_inf || continue
req.nout >= length(solution) || continue
req.allows_batch || continue
req.min_dim <= length(first(domain)) <= req.max_dim || continue
@info "Batched, oop infinity test" alg=nameof(typeof(alg)) problem=j
bf = BatchIntegralFunction((x, p) -> batch_helper(f, x, p))
do_tests(; f = bf, domain, solution, alg, abstol, reltol)
end
# BatchIntegralFunction{true}
for (alg, req) in pairs(alg_req), (j, (; f, domain, solution)) in enumerate(problems)
req.allows_inf || continue
req.nout >= length(solution) || continue
req.allows_batch || continue
req.allows_iip || continue
req.min_dim <= length(first(domain)) <= req.max_dim || continue
@info "Batched, iip infinity test" alg=nameof(typeof(alg)) problem=j
bfiip = BatchIntegralFunction((y, x, p) -> batch_helper!(f, y, x, p), zeros(0))
do_tests(; f = bfiip, domain, solution, alg, abstol, reltol)
end
@testset "Caching interface" begin
# two distinct semi-infinite transformations should still work as expected
f = (x, p) -> pdf(Normal(0.00, 1.00), x)
domain = (0.0, -Inf)
solution = -0.5
prob = IntegralProblem(f, domain)
alg = QuadGKJL()
cache = init(prob, alg; abstol, reltol)
sol = solve!(cache)
@test abs(only(sol.u) - solution) < max(abstol, reltol * abs(solution))
@test sol.prob == IntegralProblem(f, domain)
@test sol.alg == alg
domain = (-Inf, 0.0)
solution = 0.5
cache.domain = domain
sol = solve!(cache)
@test abs(only(sol.u) - solution) < max(abstol, reltol * abs(solution))
@test sol.prob == IntegralProblem(f, domain)
@test sol.alg == alg
end