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

symbolic_solve fails for time-dependent variables #1301

Open
adhalanay opened this issue Oct 9, 2024 · 3 comments
Open

symbolic_solve fails for time-dependent variables #1301

adhalanay opened this issue Oct 9, 2024 · 3 comments
Assignees

Comments

@adhalanay
Copy link

As the title suggests symbolic_solve fails with an error if one tries to solve an algebraic equations involving time-dependent variables. An MWE is :
@variables t S(t) R(t)
expr=[S+R-2,2*S-3*R-6]
symbolic_solve(expr,[S,R])

which fails with

KeyError: key R not found

Stacktrace:
[1] getindex
@ ./iddict.jl:98 [inlined]
[2] linearity_1
@ ~/.julia/packages/Symbolics/rtkf9/src/linearity.jl:57 [inlined]
[3] mark_vars(expr::SymbolicUtils.BasicSymbolic{Real}, vars::OrderedCollections.OrderedSet{Vector{Num}})
@ Symbolics ~/.julia/packages/Symbolics/rtkf9/src/semipoly.jl:208
[4] (::Base.Fix2{typeof(Symbolics.mark_vars), OrderedCollections.OrderedSet{Vector{Num}}})(y::SymbolicUtils.BasicSymbolic{Real})
@ Base ./operators.jl:1144
[5] iterate
@ ./generator.jl:48 [inlined]
[6] collect_to!(dest::Vector{Symbolics.SemiMonomial}, itr::Base.Generator{Vector{Any}, Base.Fix2{typeof(Symbolics.mark_vars), OrderedCollections.OrderedSet{Vector{Num}}}}, offs::Int64, st::Int64)
@ Base ./array.jl:838
[7] collect_to_with_first!
@ ./array.jl:816 [inlined]
[8] _collect(c::Vector{Any}, itr::Base.Generator{Vector{Any}, Base.Fix2{typeof(Symbolics.mark_vars), OrderedCollections.OrderedSet{Vector{Num}}}}, ::Base.EltypeUnknown, isz::Base.HasShape{1})
@ Base ./array.jl:810
[9] collect_similar
@ ./array.jl:709 [inlined]
[10] map
@ ./abstractarray.jl:3371 [inlined]
[11] mark_vars(expr::SymbolicUtils.BasicSymbolic{Real}, vars::OrderedCollections.OrderedSet{Vector{Num}})
@ Symbolics ~/.julia/packages/Symbolics/rtkf9/src/semipoly.jl:204
[12] mark_and_exponentiate(expr::SymbolicUtils.BasicSymbolic{Real}, vars::OrderedCollections.OrderedSet{Vector{Num}})
@ Symbolics ~/.julia/packages/Symbolics/rtkf9/src/semipoly.jl:144
[13] semipolyform_terms(expr::SymbolicUtils.BasicSymbolic{Real}, vars::OrderedCollections.OrderedSet{Vector{Num}})
@ Symbolics ~/.julia/packages/Symbolics/rtkf9/src/semipoly.jl:160

[14] semipolynomial_form(expr::SymbolicUtils.BasicSymbolic{Real}, vars::Vector{Vector{Num}}, degree::Float64; consts::Bool)
@ Symbolics ~/.julia/packages/Symbolics/rtkf9/src/semipoly.jl:264
[15] semipolynomial_form
@ ~/.julia/packages/Symbolics/rtkf9/src/semipoly.jl:257 [inlined]
[16] polynomial_coeffs(expr::SymbolicUtils.BasicSymbolic{Real}, vars::Vector{Vector{Num}})
@ Symbolics ~/.julia/packages/Symbolics/rtkf9/src/semipoly.jl:301
[17] check_poly_inunivar(poly::Num, var::Vector{Num})
@ Symbolics ~/.julia/packages/Symbolics/rtkf9/src/solver/solve_helpers.jl:97
[18] symbolic_solve(expr::Vector{Num}, x::Vector{Num}; dropmultiplicity::Bool, warns::Bool)
@ Symbolics ~/.julia/packages/Symbolics/rtkf9/src/solver/main.jl:168
[19] symbolic_solve(expr::Vector{Num}, x::Vector{Num})
@ Symbolics ~/.julia/packages/Symbolics/rtkf9/src/solver/main.jl:145
[20] top-level scope
@ In[18]:3

@ChrisRackauckas
Copy link
Member

@n0rbed can you take a look? This is a very important case to handle and the matching seems like it's taking too many liberties with the dependence

@n0rbed
Copy link
Member

n0rbed commented Oct 14, 2024

Hi @ChrisRackauckas , the error stems from lines 262-264 in ./src/solver/preprocess.jl
Here is the problem:

julia> some_expr = unwrap(1 + S)
1 + S(t)

julia> args = Symbolics.arguments(some_expr)
2-element Vector{Any}:
 1
  S(t)

julia> new_expr = Symbolics.term(+, args...)
1 + S(t)

julia> polynomial_coeffs(new_expr, S)
ERROR: MethodError: no method matching iterate(::SymbolicUtils.BasicSymbolic{Real})

julia> polynomial_coeffs(wrap(new_expr), S)
ERROR: MethodError: no method matching iterate(::SymbolicUtils.BasicSymbolic{Real})

There was one small mistake causing the "key R not Found" but thats easily fixed, this is the root problem. Let me know if you have any clues, not sure why this "reconstruction" of the poly is breaking stuff.

@n0rbed
Copy link
Member

n0rbed commented Oct 25, 2024

This reconstruction above can be removed actually. And i have a branch that does so, last problem is now #1323 ; I'll make a PR after that's fixed

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

3 participants