# Differentiation of expressions involving arrays

**URL:** <https://discourse.julialang.org/t/differentiation-of-expressions-involving-arrays/78826>\
**Category:** Modelling & Simulations\
**Tags:** modelingtoolkit\
**Created:** [March 31, 2022, 4:27pm UTC](https://discourse.julialang.org/t/differentiation-of-expressions-involving-arrays/78826 "2022-03-31T16:27:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![natema](https://avatars.discourse-cdn.com/v4/letter/n/ba9def/32.png) [@natema](https://discourse.julialang.org/u/natema)\
**Post date:** [March 31, 2022, 4:27pm UTC](https://discourse.julialang.org/t/differentiation-of-expressions-involving-arrays/78826/1 "2022-03-31T16:27:15Z")

</div>

Hi. I’m a ModellingToolkit newbie (and DE in general).  
I wrote some code last December which now throws the error `Differentiation of expressions involving arrays and array variables is not yet supported.`  
I saw that raising the error has been introduced in [this pull request 530](https://github.com/JuliaSymbolics/Symbolics.jl/pull/530), and I have a couple of simple ways to avoid it… but I don’t have much understanding of what’s going on, so I would like some feedback.

Here is what I want to do.  
I have some arrays of various lengths v\_1, ..., v\_n, and I want to define an ODE system whose equations are of the form \frac d{dt} f\_i(t)= g\_i(f\_i(t)) such that g\_i(x) = f(x, v\_i) for some f.  
Back in December it seemed that I could indeed register only f and achieve my goal as in this MWE, but now it raises the error mentioned above.

```julia
using ModelingToolkit, DifferentialEquations

function f(x, y)
    sum(y)*x
	return x
end
@register f(x, y::AbstractVector)
@variables t h(t) w(t) k(t)

a = rand(10) 
b = rand(5)
D = Differential(t)
eqs = [
    D(h) ~ f(w, a)
    D(w) ~ f(k, b)
    D(k) ~ 1
]
@named sys = ODESystem(eqs)
sys = structural_simplify(sys)
u0 = [h => 1, w => 1, k => 1]
tspan = (0.0, 1.0)
prob = ODEProblem(sys, u0, tspan, jac=true)
sol = solve(prob)

```

No error is issued if either

- `AbstractVector` is replaced with `NTuple` or
- We remove the arg `jac=true` from `ODEProblem`.

Are both solutions fine?  
As a side question (probably worth a separate thread?), I also had an hard time finding information about what `jac=true` means in the documentation. The most relevant page seems to be [this one](https://diffeq.sciml.ai/stable/features/performance_overloads/#ODEFunction), and it seems to be that `jac=false` is equivalent to `jac=nothing` but I don’t get what `jac=true` means.

---

<div class="post-metadata">

**Author:** ![RobertS](https://avatars.discourse-cdn.com/v4/letter/r/ee7513/32.png) [@RobertS](https://discourse.julialang.org/u/RobertS)\
**Post date:** [March 31, 2022, 8:42pm UTC](https://discourse.julialang.org/t/differentiation-of-expressions-involving-arrays/78826/2 "2022-03-31T20:42:47Z")

</div>

I also just updated my packages including Symbolics and now get the same error on similar code (`ODEProblem` construction from a `ODESystem` using `jac=true`). I can confirm that leaving out `jac=true` makes the error disappear.

Regarding your question: As far as I know (which is not much, just starting with all of this), `jac = true` will trigger symbolic construction of the Jacobian and hence is quite essential for largeish problems.

Is the error appearing since the Symbolics.jl update a regression or a desired assertion? My code too ran fine (as far as I can tell) before that update.

---

<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:** [March 31, 2022, 11:23pm UTC](https://discourse.julialang.org/t/differentiation-of-expressions-involving-arrays/78826/3 "2022-03-31T23:23:44Z")

</div>

@shashi knows about this and is working on it. The issue is that differentiation with arrays of variables had some correctness issues, but the error message may have been placed in a way that generates too many false positives.

---

<div class="post-metadata">

**Author:** ![RobertS](https://avatars.discourse-cdn.com/v4/letter/r/ee7513/32.png) [@RobertS](https://discourse.julialang.org/u/RobertS)\
**Post date:** [April 7, 2022, 2:15pm UTC](https://discourse.julialang.org/t/differentiation-of-expressions-involving-arrays/78826/4 "2022-04-07T14:15:41Z")

</div>

There is an according [issue](https://github.com/JuliaSymbolics/Symbolics.jl/issues/567) on `Symbolics.jl`.

---

<div class="post-metadata">

**Author:** ![shashi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shashi/32/1824_2.png) [@shashi](https://discourse.julialang.org/u/shashi)\
**Post date:** [April 7, 2022, 8:42pm UTC](https://discourse.julialang.org/t/differentiation-of-expressions-involving-arrays/78826/5 "2022-04-07T20:42:46Z")

</div>

Should be fixed now.
