@@ -18,7 +18,6 @@ sdict(kv...) = Dict{Any, Any}(kv...)
1818using Base: RefValue
1919const EMPTY_ARGS = []
2020const EMPTY_HASH = RefValue (UInt (0 ))
21- const NOT_SORTED = RefValue (false )
2221const EMPTY_DICT = sdict ()
2322const EMPTY_DICT_T = typeof (EMPTY_DICT)
2423const ENABLE_HASHCONSING = Ref (true )
@@ -42,15 +41,13 @@ const ENABLE_HASHCONSING = Ref(true)
4241 hash:: RefValue{UInt} = EMPTY_HASH
4342 hash2:: RefValue{UInt} = EMPTY_HASH
4443 arguments:: Vector{Any} = EMPTY_ARGS
45- issorted:: RefValue{Bool} = NOT_SORTED
4644 end
4745 mutable struct Add{T} <: BasicSymbolic{T}
4846 coeff:: Any = 0 # exp/den if Pow
4947 dict:: EMPTY_DICT_T = EMPTY_DICT
5048 hash:: RefValue{UInt} = EMPTY_HASH
5149 hash2:: RefValue{UInt} = EMPTY_HASH
5250 arguments:: Vector{Any} = EMPTY_ARGS
53- issorted:: RefValue{Bool} = NOT_SORTED
5451 end
5552 mutable struct Div{T} <: BasicSymbolic{T}
5653 num:: Any = 1
@@ -150,25 +147,19 @@ end
150147
151148@inline head (x:: BasicSymbolic ) = operation (x)
152149
153- function TermInterface. sorted_arguments (x:: BasicSymbolic )
154- args = arguments (x)
150+ @cache function TermInterface. sorted_arguments (x:: BasicSymbolic ):: Vector{Any}
151+ args = copy ( arguments (x) )
155152 @compactified x:: BasicSymbolic begin
156153 Add => @goto ADD
157154 Mul => @goto MUL
158155 _ => return args
159156 end
160157 @label MUL
161- if ! x. issorted[]
162- sort! (args, by= get_degrees)
163- x. issorted[] = true
164- end
158+ sort! (args, by= get_degrees)
165159 return args
166160
167161 @label ADD
168- if ! x. issorted[]
169- sort! (args, lt = monomial_lt, by= get_degrees)
170- x. issorted[] = true
171- end
162+ sort! (args, lt = monomial_lt, by= get_degrees)
172163 return args
173164end
174165
@@ -603,7 +594,7 @@ function Add(::Type{T}, coeff, dict; metadata=NO_METADATA, kw...) where T
603594 end
604595 end
605596
606- s = Add {T} (; coeff, dict, hash= Ref (UInt (0 )), hash2= Ref (UInt (0 )), metadata, arguments= [], issorted = RefValue ( false ), kw... )
597+ s = Add {T} (; coeff, dict, hash= Ref (UInt (0 )), hash2= Ref (UInt (0 )), metadata, arguments= [], kw... )
607598 BasicSymbolic (s)
608599end
609600
@@ -621,7 +612,7 @@ function Mul(T, a, b; metadata=NO_METADATA, kw...)
621612 else
622613 coeff = a
623614 dict = b
624- s = Mul {T} (; coeff, dict, hash= Ref (UInt (0 )), hash2= Ref (UInt (0 )), metadata, arguments= [], issorted = RefValue ( false ), kw... )
615+ s = Mul {T} (; coeff, dict, hash= Ref (UInt (0 )), hash2= Ref (UInt (0 )), metadata, arguments= [], kw... )
625616 BasicSymbolic (s)
626617 end
627618end
@@ -642,7 +633,7 @@ ratio(x::Rat,y::Rat) = x//y
642633function maybe_intcoeff (x)
643634 if ismul (x)
644635 if x. coeff isa Rational && isone (x. coeff. den)
645- Mul {symtype(x)} (; coeff= x. coeff. num, dict= x. dict, x. metadata, arguments= [], issorted = RefValue ( false ) )
636+ Mul {symtype(x)} (; coeff= x. coeff. num, dict= x. dict, x. metadata, arguments= [])
646637 else
647638 x
648639 end
@@ -1011,7 +1002,7 @@ end
10111002function remove_minus (t)
10121003 ! iscall (t) && return - t
10131004 @assert operation (t) == (* )
1014- args = arguments (t)
1005+ args = sorted_arguments (t)
10151006 @assert args[1 ] < 0
10161007 Any[- args[1 ], args[2 : end ]. .. ]
10171008end
0 commit comments