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

BoundsError when using expand_derivatives on a particular expression in y, but not the same expression in x #1126

Open
krsteffen opened this issue Apr 26, 2024 · 1 comment

Comments

@krsteffen
Copy link

MWE (Julia Version 1.10.2, Symbolics v5.28.0, SymbolicUtils v1.5.1):

using Symbolics

expr_gen = (var, fun) -> Differential(var)(Differential(var)(((-Differential(var)(Differential(var)(fun))) / g(var))))

@syms y f(y) g(y) h(y)

expr = expr_gen(y, f(y))
expand_derivatives(expr)
expr = expr_gen(y, g(y))
expand_derivatives(expr) # Error
expr = expr_gen(y, h(y))
expand_derivatives(expr) # Error

The error message:

> show(err)
1-element ExceptionStack:
BoundsError: attempt to access 1-element Vector{Any} at index [2]
Stacktrace:
 [1] getindex(A::Vector{Any}, i1::Int64)
   @ Base ./essentials.jl:13
 [2] expand_derivatives(O::SymbolicUtils.BasicSymbolic{Number}, simplify::Bool; occurrences::SymbolicUtils.BasicSymbolic{Real}) (repeats 2 times)
   @ Symbolics ~/.julia/packages/Symbolics/Eas9m/src/diff.jl:245
 [3] expand_derivatives(O::SymbolicUtils.BasicSymbolic{Number}, simplify::Bool; occurrences::Nothing)
   @ Symbolics ~/.julia/packages/Symbolics/Eas9m/src/diff.jl:245
 [4] expand_derivatives
   @ ~/.julia/packages/Symbolics/Eas9m/src/diff.jl:171 [inlined]
 [5] expand_derivatives(O::SymbolicUtils.BasicSymbolic{Number})
   @ Symbolics ~/.julia/packages/Symbolics/Eas9m/src/diff.jl:171
 [6] top-level scope
   @ REPL[9]:1

For some reason all of the following run successfully. The only difference is replacing y with x.

using Symbolics

expr_gen = (var, fun) -> Differential(var)(Differential(var)(((-Differential(var)(Differential(var)(fun))) / g(var))))

@syms x f(x) g(x) h(x)

expr = expr_gen(x, f(x))
expand_derivatives(expr)
expr = expr_gen(x, g(x))
expand_derivatives(expr)
expr = expr_gen(x, h(x))
expand_derivatives(expr)
@bowenszhu
Copy link
Member

This is still a problem of ordering.

As a result, inner_args[i] and arguments(occurrences)[i] sometimes does not actually match inside expand_derivatives.

Symbolics.jl/src/diff.jl

Lines 239 to 245 in 45de9b8

inner_args = arguments(arg)
l = length(inner_args)
exprs = []
c = 0
for i in 1:l
t2 = expand_derivatives(D(inner_args[i]),false, occurrences=arguments(occurrences)[i])

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