Skip to content

Commit fffc615

Browse files
committed
fix bug in term ordering for small terms
1 parent 3fbbcc9 commit fffc615

File tree

4 files changed

+16
-8
lines changed

4 files changed

+16
-8
lines changed

src/simplify.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,14 @@ function <ₑ(a::Term, b::Term)
115115
return a <operation(b)
116116
end
117117

118+
na = nameof(operation(a))
119+
nb = nameof(operation(b))
120+
118121
if 0 < arglength(a) <= 2 && 0 < arglength(b) <= 2
119122
# e.g. a < sin(a) < b ^ 2 < b
120123
@goto compare_args
121124
end
122-
na = nameof(operation(a))
123-
nb = nameof(operation(b))
125+
124126
if na !== nb
125127
return na <ₑ nb
126128
elseif arglength(a) != arglength(b)
@@ -144,9 +146,11 @@ function <ₑ(a::Term, b::Term)
144146
# compare the numbers
145147
nums = zip(Iterators.filter(isnumber, aa),
146148
Iterators.filter(isnumber, ab))
147-
return any(a <ₑ b for (a, b) in nums)
149+
if any(a <ₑ b for (a, b) in nums)
150+
return true
151+
end
148152
end
149-
return na <ₑ nb
153+
return na <ₑ nb # all args are equal, compare the name
150154
end
151155
end
152156

test/basics.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,5 @@ end
9292
@syms a b
9393
@test repr(a+b) == "a + b"
9494
@test repr(-a) == "-a"
95-
@test repr(-a + 3) == "3 + (-a)"
95+
@test repr(-a + 3) == "(-a) + 3"
9696
end

test/order.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,19 @@ end
4747
@test !(b <f(2,b))
4848
for j in i+1:length(fs)
4949
g = fs[j]
50-
@test !(g(a, b) <f(a, b)) && !(f(a, b) <g(a, b))
50+
@test g(a, b) <f(a, b) && !(f(a, b) <g(a, b))
5151
@test istotal(f(a, b), g(a, b))
5252
end
5353
end
5454
end
5555

56-
@testset "0-arg variable calls ordering" begin
56+
@testset "callable variable order" begin
5757
@syms z() ρ()
5858

5959
@test -1z() <ρ()
6060
@test !(ρ() <-1z())
61+
62+
@syms a(t) b(t) t
63+
@test a(t) <b(t)
64+
@test !(b(t) <a(t))
6165
end

test/rulesets.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ end
2222
@syms a::Integer b c d x::Real y::Number
2323
@test simplify(x - y) == x + -1*y
2424
@test simplify(x - sin(y)) == x + -1*sin(y)
25-
@test simplify(-sin(x)) == -sin(x)
25+
@test simplify(-sin(x)) == -1*sin(x)
2626
@test simplify(1 * x * 2) == 2 * x
2727
@test simplify(1 + x + 2) == 3 + x
2828
@test simplify(b*b) == b^2 # tests merge_repeats

0 commit comments

Comments
 (0)