Open baggepinnen opened 1 year ago
This code doesn't run on my machine. SA is not defined. Can you post the rest of the code that defines SA?
Sorry for that, I edited the original post, it was missing using StaticArrays
This appears to be an error in tagging reachable roots and variables in the graph factorization code. May take a while to track down.
Out of curiosity, I ran the example above on the latest v0.3.14. It still errors, though the error is different:
AssertionError: Should only be one path from root 1 to variable 2. Instead have 3 children from node 122 on the path
I also narrowed it down to a simpler MWE by printing out the c
expression that's causing the error, removing as much of it as possible (~98%) and setting as many variables to zero as possible while still getting an error. Also, I split the remaining expression into two ~equal parts, each of which differentiates just fine, but the sum of which results in an error.
import FastDifferentiation as fad
fad.@variables x02 x04 u1
vars = [x02, x04, u1]
c1 = -(cos((x02 + (x04 + ((-(cos(x02)) * (((-(x04) * sin(x02)) * x04) - u1)) + sin(x02))))))
c2 = ((-((x04 + (-(cos((x02 + x04))) * (((-((x04 + (-(cos(x02)) * (((-(x04) * sin(x02)) * x04) - u1)))) * sin((x02 + x04))) * x04) - u1)))) * sin(x02)) * x04)
fad.jacobian([c1], vars) # This is fine
fad.jacobian([c2], vars) # This is fine
fad.jacobian([c1+c2], vars) # This errors
The error and stacktrace is
ERROR: AssertionError: Should only be one path from root 1 to variable 3. Instead have 2 children from node 31 on the path
Stacktrace:
[1] follow_path(a::FastDifferentiation.DerivativeGraph{Int64}, root_index::Int64, var_index::Int64)
@ FastDifferentiation ~/.julia/packages/FastDifferentiation/dkSCb/src/Factoring.jl:525
[2] evaluate_path
@ ~/.julia/packages/FastDifferentiation/dkSCb/src/Factoring.jl:555 [inlined]
[3] _symbolic_jacobian!(graph::FastDifferentiation.DerivativeGraph{…}, partial_variables::Vector{…})
@ FastDifferentiation ~/.julia/packages/FastDifferentiation/dkSCb/src/Jacobian.jl:47
[4] _symbolic_jacobian
@ ~/.julia/packages/FastDifferentiation/dkSCb/src/Jacobian.jl:61 [inlined]
[5] jacobian(terms::Vector{FastDifferentiation.Node}, partial_variables::Vector{FastDifferentiation.Node})
@ FastDifferentiation ~/.julia/packages/FastDifferentiation/dkSCb/src/Jacobian.jl:90
[6] top-level scope
@ ~/FastDifferentiation_experiments/issue23simplified.jl:12
Some type information was truncated. Use `show(err)` to see complete types.
Looks likely related to #65. Maybe the snippet above could be a good test case for #66.
Another clue: I noticed that there are a few terms like
((-(x04) * sin(x02)) * x04)
The error goes away if I replace that by either
((-(x04^2) * sin(x02)))
or
-(((x04) * sin(x02)) * x04)
in one place or another, but not both.
Hopefully this bug will be fixed by #65. Thanks for making the small MWE. These make it much easier to debug and verify the fix.
The following example generates a
ERROR: KeyError: key 228 not found
. The example forms the dynamics of a simple mechanical system and integrates it a few steps using a hand-written RK4 integrator. The failing call occurs when callingsparse_hessian