|
| 1 | +@doc doc""" |
| 2 | +
|
| 3 | +Defines an integral problem. |
| 4 | +Documentation Page: https://docs.sciml.ai/Integrals/stable/ |
| 5 | +
|
| 6 | +## Mathematical Specification of an Integral Problem |
| 7 | +
|
| 8 | +Integral problems are multi-dimensional integrals defined as: |
| 9 | +
|
| 10 | +```math |
| 11 | +\int_{lb}^{ub} f(u,p) du |
| 12 | +``` |
| 13 | +
|
| 14 | +where `p` are parameters. `u` is a `Number` or `AbstractVector` |
| 15 | +whose geometry matches the space being integrated. |
| 16 | +This space is bounded by the lowerbound `lb` and upperbound `ub`, |
| 17 | +which are `Number`s or `AbstractVector`s with the same geometry as `u`. |
| 18 | +
|
| 19 | +## Problem Type |
| 20 | +
|
| 21 | +### Constructors |
| 22 | +
|
| 23 | +``` |
| 24 | +IntegralProblem(f::AbstractIntegralFunction,domain,p=NullParameters(); kwargs...) |
| 25 | +IntegralProblem(f::AbstractIntegralFunction,lb,ub,p=NullParameters(); kwargs...) |
| 26 | +IntegralProblem(f,domain,p=NullParameters(); nout=nothing, batch=nothing, kwargs...) |
| 27 | +IntegralProblem(f,lb,ub,p=NullParameters(); nout=nothing, batch=nothing, kwargs...) |
| 28 | +``` |
| 29 | +
|
| 30 | +- f: the integrand, callable function `y = f(u,p)` for out-of-place (default) or an |
| 31 | + `IntegralFunction` or `BatchIntegralFunction` for inplace and batching optimizations. |
| 32 | +- domain: an object representing an integration domain, i.e. the tuple `(lb, ub)`. |
| 33 | +- lb: DEPRECATED: Either a number or vector of lower bounds. |
| 34 | +- ub: DEPRECATED: Either a number or vector of upper bounds. |
| 35 | +- p: The parameters associated with the problem. |
| 36 | +- nout: DEPRECATED (see `IntegralFunction`): length of the vector output of the integrand |
| 37 | + (by default the integrand is assumed to be scalar) |
| 38 | +- batch: DEPRECATED (see `BatchIntegralFunction`): number of points the integrand can |
| 39 | + evaluate simultaneously (by default there is no batching) |
| 40 | +- kwargs: Keyword arguments copied to the solvers. |
| 41 | +
|
| 42 | +Additionally, we can supply iip like IntegralProblem{iip}(...) as true or false to declare at |
| 43 | +compile time whether the integrator function is in-place. |
| 44 | +
|
| 45 | +### Fields |
| 46 | +
|
| 47 | +The fields match the names of the constructor arguments. |
| 48 | +""" |
| 49 | +struct IntegralProblem{isinplace, P, F, T, K} <: AbstractIntegralProblem{isinplace} |
| 50 | + f::F |
| 51 | + domain::T |
| 52 | + p::P |
| 53 | + kwargs::K |
| 54 | + @add_kwonly function IntegralProblem{iip}(f::AbstractIntegralFunction{iip}, domain, |
| 55 | + p = NullParameters(); nout = nothing, batch = nothing, |
| 56 | + kwargs...) where {iip} |
| 57 | + warn_paramtype(p) |
| 58 | + new{iip, typeof(p), typeof(f), typeof(domain), typeof(kwargs)}(f, |
| 59 | + domain, p, kwargs) |
| 60 | + end |
| 61 | +end |
| 62 | + |
| 63 | +function IntegralProblem(f::AbstractIntegralFunction, |
| 64 | + domain, |
| 65 | + p = NullParameters(); |
| 66 | + kwargs...) |
| 67 | + IntegralProblem{isinplace(f)}(f, domain, p; kwargs...) |
| 68 | +end |
| 69 | + |
| 70 | +@deprecate IntegralProblem{iip}(f::AbstractIntegralFunction, |
| 71 | + lb::Union{Number, AbstractVector{<:Number}}, |
| 72 | + ub::Union{Number, AbstractVector{<:Number}}, |
| 73 | + p = NullParameters(); kwargs...) where {iip} IntegralProblem{iip}( |
| 74 | + f, (lb, ub), p; kwargs...) |
| 75 | + |
| 76 | +function IntegralProblem(f, args...; kwargs...) |
| 77 | + IntegralProblem{isinplace(f, 3)}(f, args...; kwargs...) |
| 78 | +end |
| 79 | +function IntegralProblem{iip}( |
| 80 | + f, args...; nout = nothing, batch = nothing, kwargs...) where {iip} |
| 81 | + if nout !== nothing || batch !== nothing |
| 82 | + @warn "`nout` and `batch` keywords are deprecated in favor of inplace `IntegralFunction`s or `BatchIntegralFunction`s. See the updated Integrals.jl documentation for details." |
| 83 | + end |
| 84 | + |
| 85 | + g = if iip |
| 86 | + if batch === nothing |
| 87 | + output_prototype = nout === nothing ? Array{Float64, 0}(undef) : |
| 88 | + Vector{Float64}(undef, nout) |
| 89 | + IntegralFunction(f, output_prototype) |
| 90 | + else |
| 91 | + output_prototype = nout === nothing ? Float64[] : |
| 92 | + Matrix{Float64}(undef, nout, 0) |
| 93 | + BatchIntegralFunction(f, output_prototype, max_batch = batch) |
| 94 | + end |
| 95 | + else |
| 96 | + if batch === nothing |
| 97 | + IntegralFunction(f) |
| 98 | + else |
| 99 | + BatchIntegralFunction(f, max_batch = batch) |
| 100 | + end |
| 101 | + end |
| 102 | + IntegralProblem(g, args...; kwargs...) |
| 103 | +end |
| 104 | + |
| 105 | +function Base.getproperty(prob::IntegralProblem, name::Symbol) |
| 106 | + if name === :lb |
| 107 | + domain = getfield(prob, :domain) |
| 108 | + lb, ub = domain |
| 109 | + return lb |
| 110 | + elseif name === :ub |
| 111 | + domain = getfield(prob, :domain) |
| 112 | + lb, ub = domain |
| 113 | + return ub |
| 114 | + elseif name === :ps |
| 115 | + return ParameterIndexingProxy(prob) |
| 116 | + end |
| 117 | + return Base.getfield(prob, name) |
| 118 | +end |
| 119 | + |
| 120 | +struct QuadratureProblem end |
| 121 | +@deprecate QuadratureProblem(args...; kwargs...) IntegralProblem(args...; kwargs...) |
| 122 | + |
| 123 | +@doc doc""" |
| 124 | +
|
| 125 | +Defines a integral problem over pre-sampled data. |
| 126 | +Documentation Page: https://docs.sciml.ai/Integrals/stable/ |
| 127 | +
|
| 128 | +## Mathematical Specification of a data Integral Problem |
| 129 | +
|
| 130 | +Sampled integral problems are defined as: |
| 131 | +
|
| 132 | +```math |
| 133 | +\sum_i w_i y_i |
| 134 | +``` |
| 135 | +where `y_i` are sampled values of the integrand, and `w_i` are weights |
| 136 | +assigned by a quadrature rule, which depend on sampling points `x`. |
| 137 | +
|
| 138 | +## Problem Type |
| 139 | +
|
| 140 | +### Constructors |
| 141 | +
|
| 142 | +``` |
| 143 | +SampledIntegralProblem(y::AbstractArray, x::AbstractVector; dim=ndims(y), kwargs...) |
| 144 | +``` |
| 145 | +- y: The sampled integrand, must be a subtype of `AbstractArray`. |
| 146 | + It is assumed that the values of `y` along dimension `dim` |
| 147 | + correspond to the integrand evaluated at sampling points `x` |
| 148 | +- x: Sampling points, must be a subtype of `AbstractVector`. |
| 149 | +- dim: Dimension along which to integrate. Defaults to the last dimension of `y`. |
| 150 | +- kwargs: Keyword arguments copied to the solvers. |
| 151 | +
|
| 152 | +### Fields |
| 153 | +
|
| 154 | +The fields match the names of the constructor arguments. |
| 155 | +""" |
| 156 | +struct SampledIntegralProblem{Y, X, K} <: AbstractIntegralProblem{false} |
| 157 | + y::Y |
| 158 | + x::X |
| 159 | + dim::Int |
| 160 | + kwargs::K |
| 161 | + @add_kwonly function SampledIntegralProblem(y::AbstractArray, x::AbstractVector; |
| 162 | + dim = ndims(y), |
| 163 | + kwargs...) |
| 164 | + @assert dim<=ndims(y) "The integration dimension `dim` is larger than the number of dimensions of the integrand `y`" |
| 165 | + @assert length(x)==size(y, dim) "The integrand `y` must have the same length as the sampling points `x` along the integrated dimension." |
| 166 | + @assert axes(x, 1)==axes(y, dim) "The integrand `y` must obey the same indexing as the sampling points `x` along the integrated dimension." |
| 167 | + new{typeof(y), typeof(x), typeof(kwargs)}(y, x, dim, kwargs) |
| 168 | + end |
| 169 | +end |
0 commit comments