Skip to content

Commit

Permalink
add Number * VirtualTally, VirtualTally == VirtualTally, expressions …
Browse files Browse the repository at this point in the history
…fixes
  • Loading branch information
pzinn committed Aug 23, 2023
1 parent 56c9f60 commit 5a145cf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
13 changes: 8 additions & 5 deletions M2/Macaulay2/m2/expressions.m2
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,8 @@ scan(assocList, opClass -> (
if opClass#?unit then (
installMethod(opClass#operator,Expression,opClass#unit,(x,y) -> x);
installMethod(opClass#operator,opClass#unit,Expression,(x,y) -> y);
installMethod(opClass#operator,opClass,opClass#unit,(x,y) -> x);
installMethod(opClass#operator,opClass#unit,opClass,(x,y) -> y);
)
))
- ZeroExpression := identity
Expand Down Expand Up @@ -552,6 +554,7 @@ matrixOpts := m -> ( -- helper function
)
expressionValue MatrixExpression := x -> (
(opts,m) := matrixOpts x;
if #m === 0 then return map(ZZ^0,ZZ^0,0); -- not great but best one can do
m = (if opts.MutableMatrix then mutableMatrix else matrix) applyTable(m,expressionValue);
-- TODO: keep track of blocks too
if opts.Degrees === null then m else (
Expand All @@ -566,7 +569,7 @@ toString'(Function, MatrixExpression) := (fmt,x) -> concatenate(
-----------------------------------------------------------------------------
VectorExpression = new HeaderType of Expression
VectorExpression.synonym = "vector expression"
expressionValue VectorExpression := x -> vector apply(toList x,expressionValue)
expressionValue VectorExpression := x -> if #x===0 then vector(map(ZZ^0,ZZ^1,0)) else vector apply(toList x,expressionValue)
toString'(Function,VectorExpression) := (fmt,v) -> concatenate(
"vector {",
between(", ",apply(toList v,fmt)),
Expand Down Expand Up @@ -612,10 +615,10 @@ keywordTexMath = new HashTable from { -- both unary and binary keywords
symbol #? => "\\#?",
symbol % => "\\%",
symbol & => "\\&",
symbol ^ => "\\wedge",
symbol ^^ => "\\wedge\\wedge",
symbol <| => "\\langle",
symbol |> => "\\rangle",
symbol ^ => "\\wedge ",
symbol ^^ => "\\wedge\\wedge ",
symbol <| => "\\langle ",
symbol |> => "\\rangle ",
symbol _* => "{}_*", -- temporary solution to KaTeX issue https://github.com/KaTeX/KaTeX/issues/3576
symbol ^* => "{}^*" -- temporary solution to KaTeX issue https://github.com/KaTeX/KaTeX/issues/3576
}
Expand Down
24 changes: 15 additions & 9 deletions M2/Macaulay2/m2/set.m2
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,31 @@ Tally + Tally := Tally => (x,y) -> merge(x,y,plus) -- no need to check for zero
Tally - Tally := Tally => (x,y) -> select(merge(x,applyValues(y,minus),plus),i -> i > 0) -- sadly, can't use continueIfNonPositive

VirtualTally ? VirtualTally := (x,y) -> (
w := values ((new VirtualTally from x) - (new VirtualTally from y));
if #w === 0 then symbol ==
else if all(w,i -> i>0) then symbol >
else if all(w,i -> i<0) then symbol <
else incomparable)
flag:=symbol ==;
for k in keys x|keys y do
if ((cmp := x_k?y_k) =!= symbol ==) and (cmp === incomparable or (flag =!= cmp and first(flag =!= symbol ==,flag = cmp))) then return incomparable;
flag
)

zeroVirtualTally := new VirtualTally from {}
toVirtualTally := i -> if i === 0 then zeroVirtualTally else error "comparison of a virtual tally with a nonzero integer"
VirtualTally == ZZ := (x,i) -> x === toVirtualTally i
ZZ == VirtualTally := (i,x) -> toVirtualTally i === x
VirtualTally == ZZ := (x,i) -> x == toVirtualTally i
ZZ == VirtualTally := (i,x) -> toVirtualTally i == x
VirtualTally ? ZZ := (x,i) -> x ? toVirtualTally i
ZZ ? VirtualTally := (i,x) -> toVirtualTally i ? x
VirtualTally == VirtualTally := (x,y) -> (x ? y) === symbol ==

-*
zeroTally := new Tally from {}
toTally := i -> if i === 0 then zeroTally else error "comparison of a tally with a nonzero integer"
Tally == ZZ := (x,i) -> x === toTally i
ZZ == Tally := (i,x) -> toTally i === x
Tally == ZZ := (x,i) -> x == toTally i
ZZ == Tally := (i,x) -> toTally i == x
Tally ? ZZ := (x,i) -> x ? toTally i
ZZ ? Tally := (i,x) -> toTally i ? x
*-

RingElement * VirtualTally := Number * VirtualTally := (i,v) -> if i==0 then new class v from {} else applyValues(v,y->y*i)
Number * Tally := (i,v) -> if i<=0 then new class v from {} else applyValues(v,y->y*i)

sum VirtualTally := (w) -> sum(pairs w, (k,v) -> v * k)
product VirtualTally := (w) -> product(pairs w, (k,v) -> k^v)
Expand Down

0 comments on commit 5a145cf

Please sign in to comment.