Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chain rule not working as expected with Symbolic array elements #1383

Open
willrae42 opened this issue Dec 12, 2024 · 1 comment
Open

Chain rule not working as expected with Symbolic array elements #1383

willrae42 opened this issue Dec 12, 2024 · 1 comment

Comments

@willrae42
Copy link

willrae42 commented Dec 12, 2024

I'm attempting to use Symbolics to solve some perturbation problems involving using symbolic array variables to generate series. However, no matter what I try, I've been unable to get the chain rule to apply the elements of the array. I understand that array differentiation is currently supported, but here I'm only working with elements of the array so it seems different.

Below is an example of the problem I've been running into:

using Symbolics
@variables t T(t) y(T)[1:3]
Dt = Differential(t)
expand_derivatives(Dt(y[1]))

$$ \begin{equation} \frac{\mathrm{d} y\left( T\left( t \right) \right)_{1}}{\mathrm{d}t} \end{equation} $$

Instead, I would expect it to behave more like the following:

@variables x(T)
expand_derivatives(Dt(x))

$$ \begin{equation} \frac{\mathrm{d} x\left( T\left( t \right) \right)}{T\left( t \right)} \frac{\mathrm{d} T\left( t \right)}{\mathrm{d}t} \end{equation} $$

I've been able to get this to work by inserting the following condition into the expand_derivatives function in diff.jl, but I'm not certain this wouldn't break other things or would be useful for anyone else.

        elseif op == getindex
            inner_args = arguments(arguments(arg)[1])
            c = 0
            for a in inner_args
                if isequal(a,D.x)
                    return D(arg)
                else
                    c += Differential(a)(arg)*D(a)
                end
            end
            return expand_derivatives(c)

This change results in:

expand_derivatives(Dt(y[1]))

$$ \begin{equation} \frac{\mathrm{d} y\left( T\left( t \right) \right)_{1}}{T\left( t \right)} \frac{\mathrm{d} T\left( t \right)}{\mathrm{d}t} \end{equation} $$

Original Discourse link: https://discourse.julialang.org/t/chain-rule-not-working-as-expected-with-symbolic-series/123598

@ChrisRackauckas
Copy link
Member

That extension seems correct

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants