JuliaSymbolics / SymbolicUtils.jl

Symbolic expressions, rewriting and simplification
https://docs.sciml.ai/SymbolicUtils/stable/
Other
524 stars 99 forks source link

`<ₑ` not fully reliable #483

Open bowenszhu opened 1 year ago

bowenszhu commented 1 year ago

<ₑ is normally used for sorting arguments.

sort!(v; lt=isless, ...) requires that only one of lt(x,y) and lt(y,x) can return true.

Here is an example where both return true.

using SymbolicUtils
using SymbolicUtils: <ₑ
@syms x
a = 28(x^14)
b = 44(x^7)
a <ₑ b # true
b <ₑ a # true

We got strange results sometimes.

using SymbolicUtils
using SymbolicUtils: <ₑ, unsorted_arguments
@syms x
n = 4
expr = sum((n + 1 - i) * x^i for i in 1:n)
args = unsorted_arguments(expr)
julia> sort(args, lt = <ₑ)
4-element Vector{Any}:
 x^4
 2(x^3)
 4x
 3(x^2)

julia> sort(reverse(args), lt = <ₑ)
4-element Vector{Any}:
 x^4
 3(x^2)
 4x
 2(x^3)

Note that the two above are different.

bowenszhu commented 1 year ago

describes a bug caused by this issue.

And it is also important to take

into account. Sometimes symbolic software are used in math classes for educational purpose. Printing results in an appropriate order is important to avoid making bad first impression.

bowenszhu commented 1 year ago
bowenszhu commented 1 year ago

Maple's sort: https://www.maplesoft.com/support/help/maple/view.aspx?path=sort