TransWikia.com

Using standard notation for vector magnitude

Mathematica Asked by kungfooman on December 5, 2020

The mathematical notation for the magnitude of a vector is $vert x vert$, but this does not work:

∥ {1, 2, 3} ∥

Is there any way to overload the DoubleVerticalBar symbol so my Mathematica code would look like a textbook formula?

2 Answers

If you only want to have the "nice" format for input, you could assign Norm to DoubleBracketingBar (which is equivalent to [LeftDoubleBracketingBar] # [RightDoubleBracketingBar] &)

DoubleBracketingBar := Norm
DoubleBracketingBar[x]
(* Norm[x] *)

Alternatively, and more usable, you could use the Notation package

Needs["Notation`"]
Notation[ParsedBoxWrapper[
RowBox[{"DoubleBracketingBar", "[", "x_", 
     "]"}]] [DoubleLongLeftRightArrow] ParsedBoxWrapper[
RowBox[{"Norm", "[", "x_", "]"}]]]
Notation[ParsedBoxWrapper[
RowBox[{"[LeftDoubleBracketingBar]", "x_", 
     "[RightDoubleBracketingBar]"}]] [DoubleLongLeftRightArrow] 
  ParsedBoxWrapper[
RowBox[{"Norm", "[", "x_", "]"}]]]

Which is displayed more readable as

The way the input is displayed in the notebook

The usage is then as expected:

DoubleBracketingBar[x] // FullForm
(* Norm[x] *)
[LeftDoubleBracketingBar]x[RightDoubleBracketingBar] 
// FullForm
(* Norm[x] *)
Norm[x]
(* [LeftDoubleBracketingBar]x[RightDoubleBracketingBar] *)

Edit: By the way, the reason I did not use [DoubleVerticalBar] is because it is built in as an infix operator.

Correct answer by Natas on December 5, 2020

You could create an input alias for this:

CurrentValue[EvaluationNotebook[], {InputAliases, "norm"}] = TemplateBox[
    {"[SelectionPlaceholder]"},
    "Norm"
]

The only downside is you have to tab to the placeholder.

Answered by Carl Woll on December 5, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP