Symbolics.jacobian_sparsity detection errors with PreallocationTools

When I try to do jacobian sparsity detection, I get error that ERROR: MethodError: no method matching zero(::Type{Any}).
The array returned from PreallocationsTools.jl for array of Symbolics.Num datatype, is of type Vector{Any}. So when I do some mathematical operations on it, it errors. Here is a MWE

using Symbolics, PreallocationTools, SparseArrays
import LinearAlgebra as LA

function f!(du::AbstractArray{T}, u::AbstractArray{T}, p) where {T}
    ∇, vbd, intp, Γd, tmp, v = p
    v = get_tmp(v, first(u))
    tmp = get_tmp(tmp, first(u))
    v[Γd] .= vbd
    v[intp] .= u
    @show typeof(v)
    LA.mul!(tmp, ∇, v)
    tmp - sin.(v)
    du .= tmp[intp]

function main()
    n = 1000
    Γd = rand(1:n, 10)
    v = rand(n)
    vcache = DiffCache(v[:])
    tmp = DiffCache(v[:])
    vbd = v[Γd]
    intp = setdiff(1:n, Γd)
    ∇ = sprand(n, n, 0.3)
    p = (∇, vbd, intp, Γd, tmp, vcache)
    u = v[intp]
    du = similar(u)
    f1!(du, u) = f!(du, u, p)
    jac_sp = Symbolics.jacobian_sparsity(f1!, du, u)
    jac_sp = Float64.(jac_sp)
    return jac_sp

jac_sp = main()

It seems I’m missing something and there should be an easy fix for this. Thanks in advance.

You might need special handling of it, like v = v isa Vector{Any} ? typeof(first(v)).(v) : v to concretize it. Since that would only allocate in the symbolics run, it won’t really impact performance in what you need but should fix the symbolics tracing.

That doesn’t quite for this example, after this change it says

ERROR: UndefRefError: access to undefined reference

It seems, preallocationtools hasn’t yet allocated memory for this datatype.
But this idea works if we manually allocate the memory for vector for type T.

    v = v isa Vector{Any} ? Vector{T}(undef, length(v)) : v

Thanks a lot Chris.