Day_08: Filing and tracking issues in GitHub

Today I ran into some strange behavior when I tried to generalize my Lagrangian equations of motion. It boils down to something happening with Symbolic Vectors

Take for example

using Symbolics

@variables t x[1:2](t) # independent and dependent variables
@variables k # parameters

Dx1 = Differential(x[1])
Dx2 = Differential(x[2])

V = 1/2*k*(x[2]- x[1])^2
\[\begin{equation} 0.5 \left( - \mathrm{x_1}\left( t \right) + \mathrm{x_2}\left( t \right) \right)^{2} k \end{equation}\]

I define a symbolic function V that is

\(\frac{1}{2}\left(x_2 - x_1\right)^2\).

Now, I take a derivative with respect to \(x_1\)

\(\frac{\partial L}{\partial x_1} = -k(x_2 - x_1)\)

but instead I get

Symbolics.expand_derivatives(Dx1(V))
\[\begin{equation} 0 \end{equation}\]

Weird!

Expanding the polynomial there are 3 terms

  • \(1/2 kx_1^2\)

  • \(1/2 kx_2^2\)

  • \(-kx_1x_2\)

So, I’ll take derivatives of these terms and see what I get

Vterms = [1/2*k*x[1]^2,1/2*k*x[2]^2, -k*x[1]*x[2]]
\[\begin{split}\begin{equation} \left[ \begin{array}{c} 0.5 \left( \mathrm{x_1}\left( t \right) \right)^{2} k \\ 0.5 \left( \mathrm{x_2}\left( t \right) \right)^{2} k \\ - k \mathrm{x_1}\left( t \right) \mathrm{x_2}\left( t \right) \\ \end{array} \right] \end{equation}\end{split}\]
Symbolics.expand_derivatives.(Dx1.(Vterms))
\[\begin{split}\begin{equation} \left[ \begin{array}{c} k \mathrm{x_1}\left( t \right) \\ k \mathrm{x_2}\left( t \right) \\ - k \mathrm{x_1}\left( t \right) - k \mathrm{x_2}\left( t \right) \\ \end{array} \right] \end{equation}\end{split}\]

Here is the root of my problem, for some reason the partial derivative Differential(x[1]) is treating x[1] and x[2] as the same variable. The result should have been

  • \(kx_1\)

  • \(0\)

  • \(-kx_2\)

Unindexed variables work

Now, I can compare this to an unindexed set of variables

@variables t y1(t) y2(t) # independent and dependent variables
@variables k # parameters

Dy1 = Differential(y1)
Dy2 = Differential(y2)

V = 1/2*k*(y2- y1)^2
\[\begin{equation} 0.5 \left( - \mathrm{y1}\left( t \right) + \mathrm{y2}\left( t \right) \right)^{2} k \end{equation}\]
Symbolics.expand_derivatives(Dy1(V))
\[\begin{equation} 0.5 k \left( 2 \mathrm{y1}\left( t \right) - 2 \mathrm{y2}\left( t \right) \right) \end{equation}\]

This returns the correct derivative,

\(\frac{\partial V}{\partial y_1} = -k(y_2 - y_1)\)

@variables z_1(t)
\[\begin{split}\begin{equation} \left[ \begin{array}{c} \mathrm{z\_1}\left( t \right) \\ \end{array} \right] \end{equation}\end{split}\]
D = Differential(t)
(::Differential) (generic function with 2 methods)
D.(x)
(broadcast(Differential(t), map(Symbolics.CallWith((t,)), x)))[1:2]
D.([y1,y2])
\[\begin{split}\begin{equation} \left[ \begin{array}{c} \frac{dy1(t)}{dt} \\ \frac{dy2(t)}{dt} \\ \end{array} \right] \end{equation}\end{split}\]

Symbolics still adding support for arrays

The Symbolics.jl package is incredible. Its still working on supporting differentiation with arrays. I filed this behavior on github.com/Symbolics.jl issue #571. From what I can see, the main driver behind symbolic differentiation is in the diff.jl file.

I was getting the error from line 58

    if symtype(expr) <: AbstractArray
        error("Differentiation of expressions involving arrays and array variables is not yet supported.")
    end

but @shashi added some great exceptions that remove this error in #570 and #568. There is always more work to be done on projects like this and because Julia is open source, I can dig into the code, offer help and try to add my experiences to the solution.

Wrapping up

Today, I found some strange behavior in the Symbolics.jl package. I’ll continue to update/add to the Julia projects as issues or hopefully merge some improvements. It would be great to document the process of a PR merge from start-to-finish.

For now, the work-around appears to be that jacobian and Differential only support scalar values. So I can create a Vector of individual values as such

@variables x1(t) x2(t)
x = [x1, x2]

The development behind Julia and Symbolics.jl is active and inspiring, so I’m sure this work-around won’t need to exist much longer.