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

get_variables order #1182

Open
schillic opened this issue Jun 27, 2024 · 4 comments
Open

get_variables order #1182

schillic opened this issue Jun 27, 2024 · 4 comments

Comments

@schillic
Copy link

A recent change about sorting in SymbolicUtils changed the behavior of get_variables here. Is there a contract about the order in which the variables are returned? The documentation does not say anything about that, but I have the impression that they were sorted before, so for me this was a breaking change. More confusingly, the function now returns a different result when receiving a parsed expression object or an expression for parsing:

Old behavior
pkg> add SymbolicUtils@2.0

julia> using Symbolics

julia> vars = @variables x y
2-element Vector{Num}:
 x
 y

julia> Symbolics.get_variables(x - y)
2-element Vector{Any}:
 x
 y

julia> expr = x - y
x - y

julia> Symbolics.get_variables(expr)
2-element Vector{Any}:
 x
 y
New behavior
pkg> add SymbolicUtils@2.1, Symbolics@5.31

julia> using Symbolics

julia> vars = @variables x y
2-element Vector{Num}:
 x
 y

julia> Symbolics.get_variables(x - y)  # order swapped here
2-element Vector{Any}:
 y
 x

julia> expr = x - y
x - y

julia> Symbolics.get_variables(expr)  # order not swapped here
2-element Vector{Any}:
 x
 y

Could you please confirm that this is expected behavior, and clarify how I can obtain a consistent order of the variables?

@ChrisRackauckas
Copy link
Member

Yes, there was no guarantee on the sorting order, and a recent change removes a previously enforced sorting which greatly improves performance. @bowenszhu is there a sorted version of this function?

@bowenszhu
Copy link
Member

Symbolics.get_variables doesn't offer a built-in way to obtain a sorted list of variables (as of Symbolics v5.32.0).

@ChrisRackauckas Currently, we have arguments and sorted_arguments. To enhance flexibility, how about we remove sorted_arguments, modify arguments(x) to accept a sort keyword argument arguments(x; sort = false) and add a kwargs... argument to relevant function signatures? This allows users to explicitly control sorting behavior.

@ChrisRackauckas
Copy link
Member

I think that was being discussed with @0x0f0f0f ? I think that's a good idea.

@0x0f0f0f
Copy link
Member

@bowenszhu Can't users already control this? We discussed (It took us 3 years) to avoid keyword arguments because they make implementing TermInterface.jl unstraightforward. It would be a bit of a PITA to change TermInterface again and go through another major release cycle. This is expected and I've warned about this in JuliaSymbolics/SymbolicUtils.jl#609

What about we just change get_variables to use sorted_arguments? Was there any guarantee in what order should get_variables have returned the variables? In order of appearance? Lexicographically ordered?

I guess the latter, with <\_e would be ok

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

4 participants