Skip to content

Commit

Permalink
when variable is indexed, sort by variable name and then by index
Browse files Browse the repository at this point in the history
  • Loading branch information
shashi committed Sep 4, 2023
1 parent dccce50 commit 85fbdb2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/ordering.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# find symbols and their corresponding degrees
function get_degrees(expr)
if issym(expr)
(nameof(expr) => 1,)
((Symbol(expr),) => 1,)
elseif istree(expr)
op = operation(expr)
args = arguments(expr)
Expand All @@ -34,8 +34,11 @@ function get_degrees(expr)
ds = map(get_degrees, args)
_, idx = findmax(x->sum(last.(x), init=0), ds)
return ds[idx]
elseif operation(expr) == (getindex)
args = arguments(expr)
return ((Symbol.(args)...,) => 1,)
else
return (Symbol("zzzzzzz", hash(expr)) => 1,)
return ((Symbol("zzzzzzz", hash(expr)),) => 1,)
end
else
return ()
Expand Down
6 changes: 5 additions & 1 deletion test/basics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,15 @@ end
end

@testset "polynomial printing" begin
@syms a b c
@syms a b c x[1:3]
@test repr(b+a) == "a + b"
@test repr(b-a) == "-a + b"
@test repr(2a+1+3a^2) == "1 + 2a + 3(a^2)"
@test repr(2a+1+3a^2+2b+3b^2+4a*b) == "1 + 2a + 2b + 3(a^2) + 4a*b + 3(b^2)"

@syms a b[1:3] c d[1:3]
@test repr(a + b[3] + b[1] + d[2] + c) == "a + b[1] + b[3] + c + d[2]"
@test repr(expand((c + b[3] - d[1])^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)"
end

@testset "inspect" begin
Expand Down

0 comments on commit 85fbdb2

Please sign in to comment.