Hello,
I have the following MWE, where I cannot remove the type instability that appears from the call on the last line.
abstract type FrictionLaw end
struct Cubic{T <: Real} <: FrictionLaw
γ::T
end
g(x, law::Cubic) = @. law.γ * x^3
function finite_diff_jac(f, x)
xᵢ₊₁ = similar(x)
fᵢ = f(x)
fᵢ₊₁ = similar(fᵢ)
J = similar(x, length(fᵢ), length(x))
h = 1e-8
for j in eachindex(x)
xᵢ₊₁ .= x
xᵢ₊₁[j] += h
fᵢ₊₁ .= f(xᵢ₊₁)
@. J[:, j] = (fᵢ₊₁ - fᵢ) / h
end
return J
end
function perform_aft(E, Eᴴ, X, friction_law)
return Eᴴ * g(E * X, friction_law)
end
function friction_force_fourier(Xⱼ, xₚ, gₚ, friction_law, E, Eᴴ)
Gⱼ = perform_aft(E, Eᴴ, [Xⱼ[1] + 2xₚ; Xⱼ[2:end]], friction_law)
Gⱼ[1] -= 2gₚ
return Gⱼ
end
H = 10
@code_warntype finite_diff_jac(u -> friction_force_fourier(u,
0.1,
0.1,
Cubic(1.0),
rand(2H + 1, 2H + 1),
rand(2H + 1, 2H + 1)),
ones(2H + 1))
Upon executing the code, I get
MethodInstance for finite_diff_jac(::var"#17#18", ::Vector{Float64})
from finite_diff_jac(f, x) @ Main ~/ITP_Friction/HBMContinuation.jl/scripts/new/mwe_typeinst.jl:9
Arguments
#self#::Core.Const(finite_diff_jac)
f::Core.Const(var"#17#18"())
x::Vector{Float64}
Locals
@_4::Union{Nothing, Tuple{Int64, Int64}}
h::Float64
J::Union{Vector, Matrix{Float64}}
fᵢ₊₁::Any
fᵢ::Any
xᵢ₊₁::Vector{Float64}
j::Int64
Body::Union{Vector, Matrix{Float64}}
where the function vectors are of type Any, although I do not understand where this instability is coming from. Help will be appreaciated.