From 85fbdb25e20aa6dd809a3ce1b3de10c7fff8c6d4 Mon Sep 17 00:00:00 2001 From: Shashi Gowda Date: Mon, 4 Sep 2023 08:55:07 -0400 Subject: [PATCH] when variable is indexed, sort by variable name and then by index --- src/ordering.jl | 7 +++++-- test/basics.jl | 6 +++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/ordering.jl b/src/ordering.jl index 1806aed4..81d64a9b 100644 --- a/src/ordering.jl +++ b/src/ordering.jl @@ -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) @@ -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 () diff --git a/test/basics.jl b/test/basics.jl index 2ba325c2..28d27013 100644 --- a/test/basics.jl +++ b/test/basics.jl @@ -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