diff --git a/src/infinity_handling.jl b/src/infinity_handling.jl index 07364218..80baaf65 100644 --- a/src/infinity_handling.jl +++ b/src/infinity_handling.jl @@ -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 diff --git a/test/inf_integral_tests.jl b/test/inf_integral_tests.jl index dd9779dd..bdcfa1a7 100644 --- a/test/inf_integral_tests.jl +++ b/test/inf_integral_tests.jl @@ -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