From 6b3e0bd256f3164a67e8d5ddd4e8c3bfc5a98104 Mon Sep 17 00:00:00 2001 From: Doug Torrance Date: Fri, 24 May 2024 08:51:39 -0400 Subject: [PATCH 1/2] Refactor dedupSymbols to allow more than two duplicates Closes: #3261 --- M2/Macaulay2/m2/monoids.m2 | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/M2/Macaulay2/m2/monoids.m2 b/M2/Macaulay2/m2/monoids.m2 index e3d85493237..aea587f6b50 100644 --- a/M2/Macaulay2/m2/monoids.m2 +++ b/M2/Macaulay2/m2/monoids.m2 @@ -474,14 +474,22 @@ checkSymbol = sym -> if instance(sym, Symbol) or lookup(symbol <-, class sym) =! -- turns {x, y, z, y} into {x, y_0, z, y_1} -- adding 'toString' in a few places will eliminate more duplications -- but makes creating temporary rings in functions more difficult. -dedupSymbols = varlist -> if 0 == repeats varlist then varlist else while 0 < repeats varlist do ( - mapping := hashTable toList pairs varlist; - -- TODO: applyPairs with collision handler, and accepting MutableHashTable - counter := new MutableHashTable from applyKeys(mapping, - i -> varlist#i, dups -> makeVars(#dups, first dups)); - varlist = values applyValues(mapping, var -> if instance(counter#(name := var), List) - then first(counter#name#0, counter#name = drop(counter#name, 1)) else var); - if 0 == repeats varlist then break varlist else varlist) +dedupSymbols = varlist -> ( + while repeats varlist > 0 do ( + counts := tally varlist; + data := hashTable apply(keys counts, + var -> ( + var, + new MutableList from { + 0, + if counts#var > 1 then ( + makeVars(counts#var, var)) + else {var}})); + varlist = apply(varlist, var -> ( + newvar := data#var#1#(data#var#0); + data#var#0 += 1; + newvar))); + varlist) -- also used in AssociativeAlgebras.m2 findSymbols = varlist -> dedupSymbols toList apply(pairs listSplice varlist, From 4ea15ca1b524f51a3541ac97e70a96110cedc4eb Mon Sep 17 00:00:00 2001 From: Doug Torrance Date: Fri, 24 May 2024 08:54:55 -0400 Subject: [PATCH 2/2] Add unit test for variable deduplication w/ 3 duplicates --- M2/Macaulay2/tests/normal/monoids.m2 | 1 + 1 file changed, 1 insertion(+) diff --git a/M2/Macaulay2/tests/normal/monoids.m2 b/M2/Macaulay2/tests/normal/monoids.m2 index 103f465e01e..29ff62a141b 100644 --- a/M2/Macaulay2/tests/normal/monoids.m2 +++ b/M2/Macaulay2/tests/normal/monoids.m2 @@ -230,6 +230,7 @@ assert(gens(B = A ** A) == {x_0,y_0,x_1,y_1}) -- TODO: eliminate the warning assert(gens(C = B ** B) == {x_(0,0),y_(0,0),x_(0,1),y_(0,1),x_(1,0),y_(1,0),x_(1,1),y_(1,1)}) assert(toString gens(A ** B) == "{x, y, x_0, y_0, x_1, y_1}") -- TODO: not yet working without toString assert(toString gens(monoid[x,y,x,z]) == "{x_0, y, x_1, z}") -- TODO: not yet working without toString +assert(toString gens(monoid[x,x,x]) == "{x_0, x_1, x_2}") -- issue #3261 -- test ^** and runLengthEncode -- TODO: what should happen for odd powers?