-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
common.jl
179 lines (156 loc) · 4.92 KB
/
common.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
mutable struct IntegralCache{iip, F, D, P, PK, A, S, K, Tc}
iip::Val{iip}
f::F
domain::D
p::P
prob_kwargs::PK
alg::A
sensealg::S
kwargs::K
cacheval::Tc # store alg cache here
end
SciMLBase.isinplace(::IntegralCache{iip}) where {iip} = iip
init_cacheval(::SciMLBase.AbstractIntegralAlgorithm, args...) = nothing
function SciMLBase.init(prob::IntegralProblem{iip},
alg::SciMLBase.AbstractIntegralAlgorithm;
sensealg = ReCallVJP(ZygoteVJP()),
do_inf_transformation = nothing, kwargs...) where {iip}
checkkwargs(kwargs...)
prob = transformation_if_inf(prob, do_inf_transformation)
cacheval = init_cacheval(alg, prob)
IntegralCache{iip,
typeof(prob.f),
typeof(prob.domain),
typeof(prob.p),
typeof(prob.kwargs),
typeof(alg),
typeof(sensealg),
typeof(kwargs),
typeof(cacheval)}(Val(iip),
prob.f,
prob.domain,
prob.p,
prob.kwargs,
alg,
sensealg,
kwargs,
cacheval)
end
function Base.getproperty(cache::IntegralCache, name::Symbol)
if name === :lb
domain = getfield(cache, :domain)
lb, ub = domain
return lb
elseif name === :ub
domain = getfield(cache, :domain)
lb, ub = domain
return ub
end
return getfield(cache, name)
end
function Base.setproperty!(cache::IntegralCache, name::Symbol, x)
if name === :lb
@warn "updating lb is deprecated by replacing domain"
lb, ub = cache.domain
setfield!(cache, :domain, (oftype(lb, x), ub))
return x
elseif name === :ub
@warn "updating ub is deprecated by replacing domain"
lb, ub = cache.domain
setfield!(cache, :domain, (lb, oftype(ub, x)))
return x
end
return setfield!(cache, name, x)
end
# Throw error if alg is not provided, as defaults are not implemented.
function SciMLBase.solve(::IntegralProblem; kwargs...)
checkkwargs(kwargs...)
throw(ArgumentError("""
No integration algorithm `alg` was supplied as the second positional argument.
Recommended integration algorithms are:
For scalar functions: QuadGKJL()
For ≤ 8 dimensional vector functions: HCubatureJL()
For > 8 dimensional vector functions: MonteCarloIntegration.vegas(f, st, en, kwargs...)
See the docstrings of the different algorithms for more detail.
"""))
end
"""
```julia
solve(prob::IntegralProblem, alg::SciMLBase.AbstractIntegralAlgorithm; kwargs...)
```
## Keyword Arguments
The arguments to `solve` are common across all of the quadrature methods.
These common arguments are:
- `maxiters` (the maximum number of iterations)
- `abstol` (absolute tolerance in changes of the objective value)
- `reltol` (relative tolerance in changes of the objective value)
"""
function SciMLBase.solve(prob::IntegralProblem,
alg::SciMLBase.AbstractIntegralAlgorithm;
kwargs...)
solve!(init(prob, alg; kwargs...))
end
function SciMLBase.solve!(cache::IntegralCache)
__solvebp(cache, cache.alg, cache.sensealg, cache.domain, cache.p;
cache.kwargs...)
end
function build_problem(cache::IntegralCache{iip}) where {iip}
IntegralProblem{iip}(cache.f, cache.domain, cache.p; cache.prob_kwargs...)
end
# fallback method for existing algorithms which use no cache
function __solvebp_call(cache::IntegralCache, args...; kwargs...)
__solvebp_call(build_problem(cache), args...; kwargs...)
end
mutable struct SampledIntegralCache{Y, X, D, PK, A, K, Tc}
y::Y
x::X
dim::D
prob_kwargs::PK
alg::A
kwargs::K
isfresh::Bool # state of whether weights have been calculated
cacheval::Tc # store alg weights here
end
function Base.setproperty!(cache::SampledIntegralCache, name::Symbol, x)
if name === :x
setfield!(cache, :isfresh, true)
end
setfield!(cache, name, x)
end
function SciMLBase.init(prob::SampledIntegralProblem,
alg::SciMLBase.AbstractIntegralAlgorithm;
kwargs...)
NamedTuple(kwargs) == NamedTuple() ||
throw(ArgumentError("There are no keyword arguments allowed to `solve`"))
cacheval = init_cacheval(alg, prob)
isfresh = true
SampledIntegralCache(prob.y,
prob.x,
prob.dim,
prob.kwargs,
alg,
kwargs,
isfresh,
cacheval)
end
"""
```julia
solve(prob::SampledIntegralProblem, alg::SciMLBase.AbstractIntegralAlgorithm; kwargs...)
```
## Keyword Arguments
There are no keyword arguments used to solve `SampledIntegralProblem`s
"""
function SciMLBase.solve(prob::SampledIntegralProblem,
alg::SciMLBase.AbstractIntegralAlgorithm;
kwargs...)
solve!(init(prob, alg; kwargs...))
end
function SciMLBase.solve!(cache::SampledIntegralCache)
__solvebp(cache, cache.alg; cache.kwargs...)
end
function build_problem(cache::SampledIntegralCache)
SampledIntegralProblem(cache.y,
cache.x;
dim = dimension(cache.dim),
cache.prob_kwargs...)
end