4
$\begingroup$

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?

$\endgroup$

2 Answers 2

6
$\begingroup$

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"]
$\endgroup$
4
  • $\begingroup$ 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. $\endgroup$
    – ibeatty
    Aug 22, 2015 at 20:49
  • 2
    $\begingroup$ Yeah, it's not a particularly intuitive package. The docs tutorial under Notation/tutorial/NotationSymbolizeAndInfixNotation is pretty good, though. $\endgroup$ Aug 22, 2015 at 20:50
  • 1
    $\begingroup$ Wow, that solved my problem far more easily than I had expected. Thanks! $\endgroup$
    – ibeatty
    Aug 22, 2015 at 20:59
  • $\begingroup$ 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? $\endgroup$
    – ibeatty
    Nov 5, 2015 at 16:12
1
$\begingroup$

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]

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.