Skip to content

Commit aabc924

Browse files
authored
Merge pull request #161 from JuliaSymbolics/myb/trans
Ensure transitivity in tree ordering
2 parents e5ac265 + 50fcaf7 commit aabc924

File tree

4 files changed

+23
-36
lines changed

4 files changed

+23
-36
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*.jl.*.cov
2+
*.jl.cov
3+
*.jl.mem
4+
Manifest.toml
5+
/docs/build/

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "SymbolicUtils"
22
uuid = "d1185830-fcd6-423d-90d6-eec64667417b"
33
authors = ["Shashi Gowda"]
4-
version = "0.7.2"
4+
version = "0.7.3"
55

66
[deps]
77
AbstractAlgebra = "c3fe647b-3220-5bb0-a1ea-a7954cac585d"

src/ordering.jl

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,13 @@ function <ₑ(a, b)
1313
if !istree(a) && !istree(b)
1414
T = typeof(a)
1515
S = typeof(b)
16-
T===S ? isless(a, b) : nameof(T) < nameof(S)
16+
return T===S ? isless(a, b) : nameof(T) < nameof(S)
1717
elseif istree(b) && !istree(a)
18-
args = arguments(b)
19-
if length(args) === 2
20-
n1, n2 = !is_literal_number(args[1]) , !is_literal_number(args[2])
21-
if n1 && n2
22-
# both subterms are terms, so it's definitely firster
23-
return true
24-
elseif n1
25-
return isequal(a, args[1]) || a <ₑ args[1]
26-
elseif n2
27-
return isequal(a, args[2]) || a <ₑ args[2]
28-
else
29-
# both arguments are not numbers
30-
# This case when a <ₑ Term(^, [1,-1])
31-
# so this term should go to the left.
32-
return false
33-
end
34-
elseif length(args) === 1
35-
# make sure a < sin(a) < b^2 < b
36-
if isequal(a, args[1])
37-
return true # e.g sin(a)*a should become a*sin(a)
38-
else
39-
return a<ₑargs[1]
40-
end
41-
else
42-
# variables to the right
43-
return false
44-
end
18+
return true
4519
elseif istree(a) && istree(b)
46-
cmp_term_term(a,b)
20+
return cmp_term_term(a,b)
4721
else
48-
!(b <ₑ a)
22+
return !(b <ₑ a)
4923
end
5024
end
5125

test/order.jl

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Test
2-
using SymbolicUtils: <
2+
using Combinatorics
3+
using SymbolicUtils: <ₑ, arguments
34
SymbolicUtils.show_simplified[] = false
45

56
@syms a b c
@@ -26,7 +27,7 @@ end
2627
@test istotal(b*a, a)
2728
@test istotal(a, b*a)
2829
@test !(b*a <ₑ b+a)
29-
@test Term(^, [1,-1]) <ₑ a
30+
@test a <Term(^, [1,-1])
3031
@test istotal(a, Term(^, [1,-1]))
3132

3233
@testset "operator order" begin
@@ -56,8 +57,7 @@ end
5657
@testset "callable variable order" begin
5758
@syms z() ρ()
5859

59-
@test -1z() <ρ()
60-
@test !(ρ() <-1z())
60+
@test istotal(ρ(), -1z())
6161

6262
@syms a(t) b(t) t
6363
@test a(t) <b(t)
@@ -72,7 +72,7 @@ end
7272
@syms x y
7373

7474
@test x <ₑ (3 + x) && !((3 + x) <ₑ x)
75-
@test x^2 <ₑ y && !(y < x^2)
75+
@test istotal(y, x^2)
7676

7777
# a nice consequence
7878
@test simplify(x/(x+3) + 3/(x+3)) == 1
@@ -84,3 +84,11 @@ end
8484
@test Term(^, [a, -1]) <ₑ (a + 2)
8585
@test !((a + 2) <Term(^, [a, -1]))
8686
end
87+
88+
@testset "transitivity" begin
89+
# issue #160
90+
@syms σ x y z
91+
expr = σ*sin(x + -1y)*(sin(z)^(-1))*(-1x + y)
92+
args = arguments(expr)
93+
@test all(((a, b), )->a <ₑ b, combinations(args, 2))
94+
end

0 commit comments

Comments
 (0)