4

Is there a way to make <a,b,c> behave like {a,b,c}, but remain displayed as <a,b,c> (short of setting up new rules for every operation involving such a list that I care about)?

Note: I've typed "<" and ">" here for readability, but really mean \[LeftAngleBracket] and \[RightAngleBracket]. That, way, I could write expressions that look like the normal vector-of-components notation that my intro physics students see in their textbook, but Mathematica would treat as the three-element lists that are the normal way to represent vectors (e.g., to distribute scalar multiplication, calculate dot and cross products, etc.).

I don't know where to start. The kinds of rules I've learned to define seem designed to replace one symbol with its equivalent, rather than simply treating it as its equivalent. I have a feeling this is easy, but it's a side of Mathematica I haven't learned about yet. Can anyone point me in the right direction?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
ibeatty
  • 2,533
  • 11
  • 22

2 Answers2

6

The Notation package is built for this.

Needs["Notation`"];
Notation[ParsedBoxWrapper[RowBox[{"\[LeftAngleBracket]", "x___", "\[RightAngleBracket]"}]] \[DoubleLongLeftRightArrow] 
ParsedBoxWrapper[RowBox[{"{", "x___", "}"}]]]

(It looks better when input via the Notation palette. Don't be frightened by the box manipulation - I used the palette to construct that expression.)

If you want to see exactly how it looks in the frontend, a cell which contains the expression above looks like:

Cell[BoxData[
 RowBox[{"Notation", "[", 
  RowBox[{
   TemplateBox[{RowBox[{"\[LeftAngleBracket]", "x___", "\[RightAngleBracket]"}]},
    "NotationTemplateTag"], " ", "\[DoubleLongLeftRightArrow]", " ", 
    TemplateBox[{RowBox[{"{", "x___", "}"}]},
    "NotationTemplateTag"]}], "]"}]], "Input"]
Patrick Stevens
  • 6,107
  • 1
  • 16
  • 42
  • Aah, okay. It seemed like a common enough thing to want to do. When I took my first look at the Notation Package doc page, though the immediate reference to "constructing explicit MakeExpression and MakeBoxes rules" scared me away. – ibeatty Aug 22 '15 at 20:49
  • 2
    Yeah, it's not a particularly intuitive package. The docs tutorial under Notation/tutorial/NotationSymbolizeAndInfixNotation is pretty good, though. – Patrick Stevens Aug 22 '15 at 20:50
  • 1
    Wow, that solved my problem far more easily than I had expected. Thanks! – ibeatty Aug 22 '15 at 20:59
  • Unfortunately, this has an undesired side effect: It make Mathematica use angle brackets instead of curly-braces for ALL lists. I'd like it to leave curly-braces as curly-braces, and angle-brackets as angle-brackets, but evaluate expressions involving angle-brackets as if they were curly-braces. Is that possible? – ibeatty Nov 05 '15 at 16:12
1

One hacky possibility is the following:

f_[a___, AngleBracket[b__], c___] ^:= With[{r = f[a, List[b], c]},
    If[VectorQ[r],
        AngleBracket @@ r,
        r
    ]
]

Examples:

AngleBracket[1,2,3]^2
RandomInteger[1, {3, 3}] . AngleBracket[a, b, c]

\[LeftAngleBracket]1, 4, 9\[RightAngleBracket]

\[LeftAngleBracket]a + b, b, a + b + c\[RightAngleBracket]

Carl Woll
  • 130,679
  • 6
  • 243
  • 355