Skip to content

Commit 9810302

Browse files
Alexey Stukalovalyst
authored andcommitted
monomial/lex_lt: sort by abs degree, positive first
1 parent 50fe665 commit 9810302

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/ordering.jl

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,20 @@ end
124124
function monomial_lt(degs1, degs2)
125125
d1 = sum(last, degs1, init=0)
126126
d2 = sum(last, degs2, init=0)
127-
d1 != d2 ? d1 < d2 : lexlt(degs1, degs2)
127+
d1 != d2 ?
128+
# lower absolute degree first, or if equal, positive degree first
129+
(abs(d1) < abs(d2) || abs(d1) == abs(d2) && d1 > d2) :
130+
lexlt(degs1, degs2)
128131
end
129132

130133
function lexlt(degs1, degs2)
131-
for (a, b) in zip(degs1, degs2)
132-
if a[1] == b[1] && a[2] != b[2]
133-
return a[2] > b[2] # higher degree first
134-
elseif a[1] != b[1]
135-
return a[1] < b[1] # lexicographic order for the base
134+
for ((a_base, a_deg), (b_base, b_deg)) in zip(degs1, degs2)
135+
if a_base == b_base && a_deg != b_deg
136+
# same base, higher absolute degree first, positive degree first
137+
return abs(a_deg) > abs(b_deg) || abs(a_deg) == abs(b_deg) && a_deg > b_deg
138+
elseif a_base != b_base
139+
# lexicographic order for the base
140+
return a_base < b_base
136141
end
137142
end
138143
return false # they are equal

test/basics.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,11 @@ end
222222
@test repr(a + b3 + b1 + d2 + c) == "a + b[1] + b[3] + c + d[2]"
223223
@test repr(expand((c + b3 - d1)^3)) == "b[3]^3 + 3(b[3]^2)*c - 3(b[3]^2)*d[1] + 3b[3]*(c^2) - 6b[3]*c*d[1] + 3b[3]*(d[1]^2) + c^3 - 3(c^2)*d[1] + 3c*(d[1]^2) - (d[1]^3)"
224224
# test negative powers sorting
225-
@test repr((b3^2)^(-2) + a^(-3) + (c*d1)^(-2)) == "1 / (b[3]^4) + 1 / ((c^2)*(d[1]^2)) + 1 / (a^3)"
225+
@test repr((b3^2)^(-2) + a^(-3) + (c*d1)^(-2)) == "1 / (a^3) + 1 / (b[3]^4) + 1 / ((c^2)*(d[1]^2))"
226+
227+
# test that the "x^2 + y^-1 + sin(a)^3.5 + 2t + 1//1" expression from Symbolics.jl/build_targets.jl is properly sorted
228+
@syms x1 y1 a1 t1
229+
@test repr(x1^2 + y1^-1 + sin(a1)^3.5 + 2t1 + 1//1) == "(1//1) + 2t1 + 1 / y1 + x1^2 + sin(a1)^3.5"
226230
end
227231

228232
@testset "inspect" begin

0 commit comments

Comments
 (0)