# How to extract a differential operator from an expression involving symbolic derivatives (using Symbolics.jl)?

**URL:** https://discourse.julialang.org/t/how-to-extract-a-differential-operator-from-an-expression-involving-symbolic-derivatives-using-symbolics-jl/76449
**Category:** Numerics
**Tags:** symbolics
**Created:** [February 14, 2022, 5:53pm UTC](https://discourse.julialang.org/t/how-to-extract-a-differential-operator-from-an-expression-involving-symbolic-derivatives-using-symbolics-jl/76449 "2022-02-14T17:53:58Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)
#### Post date: [February 14, 2022, 5:53pm UTC](https://discourse.julialang.org/t/how-to-extract-a-differential-operator-from-an-expression-involving-symbolic-derivatives-using-symbolics-jl/76449/1 "2022-02-14T17:53:58Z")

</div>

Suppose I have the following operation

```julia
julia> @variables t x(t)
2-element Vector{Num}:
    t
 x(t)

julia> Dt = Differential(t)
(::Differential) (generic function with 2 methods)

julia> ex = expand_derivatives(Dt(t^2*x))
(t^2)*Differential(t)(x(t)) + 2t*x(t)

```

I would like to somehow extract the operator `t^2*Dt + 2t` from `ex` and represent this as a matrix (assuming that `x(t)` is an arbitrary vector, and I have the matrices `t` and `Dt`). Is there a way to achieve this? Thanks in advance!

---

<div class="post-metadata">

### Author: ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)
#### Post date: [February 14, 2022, 7:11pm UTC](https://discourse.julialang.org/t/how-to-extract-a-differential-operator-from-an-expression-involving-symbolic-derivatives-using-symbolics-jl/76449/2 "2022-02-14T19:11:56Z")

</div>

The [documented](https://symbolics.juliasymbolics.org/stable/tutorials/symbolic_functions/) examples do not work for you?

---

<div class="post-metadata">

### Author: ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)
#### Post date: [April 7, 2022, 6:42am UTC](https://discourse.julialang.org/t/how-to-extract-a-differential-operator-from-an-expression-involving-symbolic-derivatives-using-symbolics-jl/76449/3 "2022-04-07T06:42:39Z")

</div>

I’m afraid not, and I’m not experienced enough with the ecosystem to get this to work by myself

---

<div class="post-metadata">

### Author: ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)
#### Post date: [April 7, 2022, 8:13am UTC](https://discourse.julialang.org/t/how-to-extract-a-differential-operator-from-an-expression-involving-symbolic-derivatives-using-symbolics-jl/76449/4 "2022-04-07T08:13:17Z")

</div>

OK, i tried starting with this

```julia
using Symbolics
@variables t x1(t) x2(t)
@show x = [x1 x2]
@show Dt = Differential(t)
@show ex = expand_derivatives(Dt(t^2*x))

```

and got

```julia
ERROR: Differentiation of expressions involving arrays and array variables is not yet supported.

```

So it seems we are out of luck here.

---

<div class="post-metadata">

### Author: ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)
#### Post date: [April 7, 2022, 8:26am UTC](https://discourse.julialang.org/t/how-to-extract-a-differential-operator-from-an-expression-involving-symbolic-derivatives-using-symbolics-jl/76449/5 "2022-04-07T08:26:46Z")

</div>

Thanks for your time! I’ll leave this open in case someone manages to find a solution

---

<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: [April 7, 2022, 11:52am UTC](https://discourse.julialang.org/t/how-to-extract-a-differential-operator-from-an-expression-involving-symbolic-derivatives-using-symbolics-jl/76449/6 "2022-04-07T11:52:29Z")

</div>

Did you mean:

```julia
julia> ex = expand_derivatives(Dt.(t^2 * x))
1×2 Matrix{Num}:
 Differential(t)((t^2)*x1(t)) Differential(t)((t^2)*x2(t))

```

?

---

<div class="post-metadata">

### Author: ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)
#### Post date: [April 7, 2022, 12:04pm UTC](https://discourse.julialang.org/t/how-to-extract-a-differential-operator-from-an-expression-involving-symbolic-derivatives-using-symbolics-jl/76449/7 "2022-04-07T12:04:34Z")

</div>

Thanks! Very nice, we get something like

```julia
using Symbolics
@variables t x1(t) x2(t)

@show Dt = Differential(t)
@show ex = expand_derivatives(Dt(t^2*sin(t)))

@show Dt = Differential(t)
@show ex = expand_derivatives(Dt(t^2*cos(t)))

@show x = [sin(t) cos(t)]
@show Dt = Differential(t)
@show ex = expand_derivatives.(Dt.(t^2*[sin(t) cos(t)]))

@show x = [x1 x2]
@show Dt = Differential(t)
@show ex = expand_derivatives.(Dt.(t^2*x))

```

yielding

```julia
Dt = Differential(t) = Differential(t)
ex = expand_derivatives(Dt(t ^ 2 * sin(t))) = (t^2)*cos(t) + 2t*sin(t)
Dt = Differential(t) = Differential(t)
ex = expand_derivatives(Dt(t ^ 2 * cos(t))) = 2t*cos(t) - (t^2)*sin(t)
x = [sin(t) cos(t)] = Num[sin(t) cos(t)]
Dt = Differential(t) = Differential(t)  
ex = expand_derivatives.(Dt.(t ^ 2 * [sin(t) cos(t)])) = Num[(t^2)*cos(t) + 2t*sin(t) 2t*cos(t) - (t^2)*sin(t)]
x = [x1 x2] = Num[x1(t) x2(t)]        
Dt = Differential(t) = Differential(t)
ex = expand_derivatives.(Dt.(t ^ 2 * x)) = Num[(t^2)*Differential(t)(x1(t)) + 2t*x1(t) (t^2)*Differential(t)(x2(t)) + 2t*x2(t)]

```

That should be pretty near to what OP wants…
