Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/infinity_handling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@ end
# without batching point container type should match the inputs
substitute_v(v2ujac, v, lb::Number, ub::Number) = v2ujac(only(v), lb, ub)
function substitute_v(v2ujac, v, lb::AbstractVector, ub::AbstractVector)
xjac = map((l, u, v) -> v2ujac(v, l, u), lb, ub, v) # ordering may influence container type
# Immutable eval point + mutable domain: map via `v`, then one similar(lb) copy.
if !ArrayInterface.ismutable(v) && ArrayInterface.ismutable(lb)
xjac = map((vi, l, u) -> v2ujac(vi, l, u), v, lb, ub)
xs = map(first, xjac)
jac = prod(last, xjac)
x = similar(lb, eltype(xs))
copyto!(x, xs)
return x, jac
end
xjac = map((l, u, vi) -> v2ujac(vi, l, u), lb, ub, v) # ordering may influence container type
x = map(first, xjac)
jac = prod(last, xjac)
return x, jac
Expand Down
27 changes: 27 additions & 0 deletions test/inf_integral_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,30 @@ end
@test sol.prob == IntegralProblem(f, domain)
@test sol.alg == alg
end

@testset "substitute_v matches domain container type" begin
v = @SVector [0.25, 0.5]
lb = [-1.0, 0.0]
ub = [1.0, 2.0]
x, jac = Integrals.substitute_v(Integrals.t2ujac, v, lb, ub)
@test x isa Vector{Float64}
@test jac isa Real
xs, _ = Integrals.substitute_v(
Integrals.t2ujac, v, (@SVector[-1.0, 0.0]), (@SVector[1.0, 2.0])
)
@test xs isa SVector{2, Float64}

# Vector-domain HCubatureJL must keep Vector points (broadcast / typed integrands)
sol = solve(
IntegralProblem((x, p) -> x .^ 2, (zeros(2), ones(2))), HCubatureJL();
abstol, reltol
)
@test sol.u isa Vector
@test sol.u ≈ [1 / 3, 1 / 3] atol = max(abstol, reltol)

f_gauss(x, p) = pdf(MvNormal([0.0, 0.0], [0.4 0.0; 0.0 0.4]), x)
for domain in (([-Inf, -Inf], [Inf, Inf]), (@SVector[-Inf, -Inf], @SVector[Inf, Inf]))
sol = solve(IntegralProblem(f_gauss, domain), HCubatureJL(); abstol, reltol)
@test abs(only(sol) - 1.0) < max(abstol, reltol)
end
end
Loading