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

Suppose I have the following operation

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!

The documented examples do not work for you?

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

OK, i tried starting with this

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

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

So it seems we are out of luck here.

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

Did you mean:

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

?

1 Like

Thanks! Very nice, we get something like

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

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…