Skip to content

Document unicode math operators #3737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions M2/Macaulay2/d/lex.d
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,8 @@ gettoken1(file:PosFile,sawNewline:bool):Token := (
else if ismathoperator(peek2(file)) then (
for i from 1 to utf8charlength(char(ch))
do tokenbuf << char(getc(file));
if peek(file) == int('=') -- augmented assignment operator
then tokenbuf << char(getc(file));
return Token(makeUniqueWord(takestring(tokenbuf), parseWORD),
newPosition(file, line, column), globalDictionary, dummySymbol, sawNewline))
else if isalpha(ch) then ( -- valid symbols are an alpha (letters, any unicode) followed by any number of alphanum (alpha, digit, dollar, prime)
Expand Down
19 changes: 18 additions & 1 deletion M2/Macaulay2/m2/help.m2
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,15 @@ getSource(Symbol, Package) := (S, pkg) -> (
)

-- Handling operators

-- for each unicode operator, we give a pair:
-- * unicode name
-- * TeX name (for emacs TeX input mode -- null if not available)
unicodeOperators := hashTable {
symbol · => ("middle dot", "\\cdot"),
symbol ⊠ => ("squared times", "\\boxtimes"),
symbol ⧢ => ("shuffle product",)}

-- e.g. symbol +
getOperator := key -> if operator#?key then (
op := toString key;
Expand Down Expand Up @@ -328,7 +337,15 @@ getOperator := key -> if operator#?key then (
PARA {"This operator may be used as a binary operator in an expression like ", TT ("x" | op | "y"), ". ",
"The user may ", TO2{ "Macaulay2Doc :: :=", "install a method" }, " for handling such expressions with code such as"},
PRE (" X "|op|" (x,y) -> ..."),
PARA {"where ", TT "X", " is the class of ", TT "x", "."}}
PARA {"where ", TT "X", " is the class of ", TT "x", "."}},
if unicodeOperators#?key then {
PARA {"To insert this character in Emacs, you may press ", KBD "C-x 8 RET", " or ", KBD "M-x insert-char",
" and then enter ", format unicodeOperators#key#0, " in the minibuffer."},
if (texcmd := unicodeOperators#key#1) =!= null
then PARA {"Alternatively, you may press ", KBD "C-x RET C-\\", " or ", KBD "M-x set-input-method",
" and then enter \"TeX\" in the minibuffer. Afterwards, typing \"", texcmd,
"\" will input the character. You may then toggle the input method using ", KBD "C-\\", " or ",
KBD "M-x toggle-input-method"}},
))

-- TODO: expand this
Expand Down
10 changes: 10 additions & 0 deletions M2/Macaulay2/packages/Macaulay2Doc/operators/tensor.m2
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,13 @@ Node
Example
{1, 2} ** {10, 20, 30}
///

document {
Key => symbol ⊠,
Headline => "a binary operator, usually used for exterior product",
}

document {
Key => symbol ⧢,
Headline => "a binary operator, usually used for shuffle product",
}
5 changes: 5 additions & 0 deletions M2/Macaulay2/packages/Macaulay2Doc/operators/times.m2
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,8 @@ document {
},
SeeAlso => {(degree,Matrix),degrees}
}

document {
Key => symbol ·,
Headline => "a binary operator, usually used for dot product",
}
3 changes: 3 additions & 0 deletions M2/Macaulay2/packages/Macaulay2Doc/ov_language.m2
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,7 @@ document {
TO symbol + ,
TO symbol - ,
TO symbol * ,
TO symbol · ,
TO symbol / ,
TO symbol // ,
TO symbol % ,
Expand All @@ -1078,6 +1079,8 @@ document {
TO symbol ++ ,
TO symbol ** ,
TO symbol ^** ,
TO symbol ⊠ ,
TO symbol ⧢ ,
TO symbol ~ ,
TO symbol (*) ,
TO symbol : ,
Expand Down
9 changes: 9 additions & 0 deletions M2/Macaulay2/tests/normal/augmented-assignment.m2
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ assert Equation(x#0, 28)
Foo @ Foo := (x, y) -> Foo(4*x#0 - 5*y#0)
x @= Foo 7
assert Equation(x#0, 77)
Foo · Foo := (x,y) -> Foo(6*x#0 * 7*y#0)
x ·= Foo 8
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks awkward, and I'm not convinced that it is useful enough to justify supporting it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My original thought was that every flexible binary operator (as long as it didn't introduce ambiguity when appending an extra =, like == -> ===) should support augmented assignment out of the box.

While it's certainly true it would probably be a bad idea to use ·= for an augmented dot product (First v is a vector, but after v ·= w, it's an element of the ring...), we might as well make it available for some other context where it might make sense.

assert Equation(x#0, 25872)
Foo ⊠ Foo := (x,y) -> Foo((8*x#0) // (9*y#0))
x ⊠= Foo 9
assert Equation(x#0, 2555)
Foo ⧢ Foo := (x,y) -> Foo(10*x#0 + 11*y#0)
x ⧢= Foo 10
assert Equation(x#0, 25660)

-- local variable
x := 2
Expand Down
Loading