Skip to content

Commit dccce50

Browse files
committed
more fixes to total order
1 parent 3dc1fe1 commit dccce50

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/ordering.jl

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
<(a::Symbolic, b::Number) = false
99
<(a::Number, b::Symbolic) = true
1010

11+
<(a::Function, b::Function) = nameof(a) <nameof(b)
12+
13+
<(a::Type, b::Type) = nameof(a) <nameof(b)
14+
<(a::T, b::S) where{T,S} = T<S
15+
<(a::T, b::T) where{T} = a < b
16+
17+
1118
###### A variation on degree lexicographic order ########
1219
# find symbols and their corresponding degrees
1320
function get_degrees(expr)
@@ -53,13 +60,25 @@ function lexlt(degs1, degs2)
5360
end
5461

5562
_arglen(a) = istree(a) ? length(unsorted_arguments(a)) : 0
63+
64+
function <(a::Tuple, b::Tuple)
65+
for (x, y) in zip(a, b)
66+
if x <ₑ y
67+
return true
68+
elseif y <ₑ x
69+
return false
70+
end
71+
end
72+
return length(a) < length(b)
73+
end
74+
5675
function <(a::BasicSymbolic, b::BasicSymbolic)
5776
da, db = get_degrees(a), get_degrees(b)
5877
fw = monomial_lt(da, db)
5978
bw = monomial_lt(db, da)
6079
if fw === bw && !isequal(a, b)
6180
if _arglen(a) == _arglen(b)
62-
return hash(a) < hash(b)
81+
return (operation(a), arguments(a)...,) <ₑ (operation(b), arguments(b)...,)
6382
else
6483
return _arglen(a) < _arglen(b)
6584
end

test/order.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,29 @@ end
3030
@test Term(^, [1,-1]) <ₑ a
3131
@test istotal(a, Term(^, [1,-1]))
3232

33+
@testset "operator order" begin
34+
fs = (*, -, +)
35+
for i in 1:length(fs)
36+
f = fs[i]
37+
@test f(a, b) <f(b, c)
38+
@test istotal(f(a, b), f(b, c))
39+
@test !(f(b, b) <f(b, b))
40+
@test istotal(f(b, b), f(b, b))
41+
@test !(f(b, c) <f(a, b))
42+
@test istotal(f(b, c), f(a, b))
43+
44+
@test f(1, b) <f(2, b)
45+
@test !(f(2, b) <f(1, b))
46+
@test istotal(f(1, b), f(2, b))
47+
@test istotal(f(2, b), f(1, b))
48+
@test b <f(2,b) && !(f(2,b) <ₑ b)
49+
50+
for j in i+1:length(fs)
51+
g = fs[j]
52+
@test istotal(f(a, b), g(a, b))
53+
end
54+
end
55+
end
3356
@testset "callable variable order" begin
3457
@syms z() ρ()
3558

@@ -61,6 +84,7 @@ end
6184
# issue #160
6285
@syms σ x y z
6386
expr = σ*sin(x + -1y)*(sin(z)^2)*(-1x + y)
87+
# Note that arguments(expr) no more uses <ₑ
6488
args = sort(arguments(expr), lt=<ₑ)
6589
@test all(((a, b), )->a <ₑ b, combinations(args, 2))
6690
end

0 commit comments

Comments
 (0)