# Symbolics.jacobian\_sparsity detection errors with PreallocationTools

**URL:** <https://discourse.julialang.org/t/symbolics-jacobian-sparsity-detection-errors-with-preallocationtools/100715>\
**Category:** Numerics\
**Tags:** symbolics, preallocation\
**Created:** [June 22, 2023, 9:18pm UTC](https://discourse.julialang.org/t/symbolics-jacobian-sparsity-detection-errors-with-preallocationtools/100715 "2023-06-22T21:18:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sanjeev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sanjeev/32/212058_2.png) [@sanjeev](https://discourse.julialang.org/u/sanjeev)\
**Post date:** [June 22, 2023, 9:18pm UTC](https://discourse.julialang.org/t/symbolics-jacobian-sparsity-detection-errors-with-preallocationtools/100715/1 "2023-06-22T21:18:29Z")

</div>

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

```julia
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]
    return
end

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
end

jac_sp = main()

```

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

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [June 22, 2023, 9:45pm UTC](https://discourse.julialang.org/t/symbolics-jacobian-sparsity-detection-errors-with-preallocationtools/100715/2 "2023-06-22T21:45:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sanjeev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sanjeev/32/212058_2.png) [@sanjeev](https://discourse.julialang.org/u/sanjeev)\
**Post date:** [June 22, 2023, 10:02pm UTC](https://discourse.julialang.org/t/symbolics-jacobian-sparsity-detection-errors-with-preallocationtools/100715/3 "2023-06-22T22:02:52Z")

</div>

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

```julia
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.

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

```

Thanks a lot Chris.
