Open
Description
openedon Nov 14, 2023
Inference fails for the following on 1.9 and 1.11.0-DEV.894.
struct Quadrature{T}
x::Vector{T}
w::Vector{T}
end
function cubature(f::F, quadrature::Quadrature, B::NTuple{N}) where {F,N}
(; x, w) = quadrature
function _f(_ι)
ι = Tuple(_ι)
z = map((i, b) -> getindex(x, i) * b, ι, B)
ω = prod(i -> getindex(w, i), ι)
f(z) * ω
end
K = length(x)
sum(_f, CartesianIndices(ntuple(_ -> K, Val(N)))) * prod(B)
end
Q = Quadrature(randn(10), randn(10)) # just a mockup
@code_warntype cubature(sum, Q, (1.0, 2.0))
but it is fixed when prod
is moved outside, as in
@noinline prod_outside(w, ι) = prod(i -> getindex(w, i), ι)
function cubature(f::F, quadrature::Quadrature, B::NTuple{N}) where {F,N}
(; x, w) = quadrature
function _f(_ι)
ι = Tuple(_ι)
z = map((i, b) -> getindex(x, i) * b, ι, B)
ω = prod_outside(w, ι)
f(z) * ω
end
K = length(x)
sum(_f, CartesianIndices(ntuple(_ -> K, Val(N))))
end
Cf this discussion.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment