# Why can't I define vector v as having subscripted elements v_1, v_2, v_3…?

I'm a very beginner in Mathematica 8 programming. While trying to play a little with tensor analysis I encountered a problem. How to define for example vector in such manner:

v={$v_1,v_2,v_3$}

For now I get the recursive definition, due to the situation in which the v variable is on the right-hand and left-hand sides.

-
Related question: mathematica.stackexchange.com/questions/1004/… –  Szabolcs Mar 15 '12 at 11:59
This is recursive even in standard maths notation (i.e. completely independent of Mathematica) if the left hand side v would be the same symbol as the $v$ in any $v_i$ on the right hand side. –  István Zachar Mar 15 '12 at 12:45
@IstvánZachar How so? I'm curious -- could you explain what you mean a bit more? –  Andrew MacFie Mar 15 '12 at 15:52
@AndrewMacFie: if you assume that the subscript operator is a function, which takes $v$ as an argument (like Part, $sin$, or any function $f$), then writing $v = v_i$ equals writing $v = f(v)$ which is recursive definition (if $=$ stands for assignment and not for testing). Just like @Helen said it. –  István Zachar Mar 15 '12 at 16:19

You could also use something like OverVector, since

OverVector[v] = {Subscript[v,1],Subscript[v,2],Subscript[v,3]}


isn't recursive, and so won't cause problems.

This takes a bit more effort to input

Type Ctrl+& and then Esc vec Esc to enter directly

but if you're interested in the typeset forms it might be worth it.

-
That is exactly what I have been looking for. Thanks a lot. –  Misery Mar 16 '12 at 6:59

You can't define an unassigned symbolic variable throught itself. You are trying to do something like that:

x = F[x]


This is not right for symbolic computations, because x evaluates to itself as a pure symbolic value.

Equal[v, List[Subscript[v, 1], Subscript[v, 2], Subscript[v, 3]]]


So, you get the recursion.

Try different symbol for your vector:

W={$v_1,v_2,v_3$}

-
...or don't use subscripts. They're used for typesetting, but they're not idiomatic in Mathematica programming. –  Andrew MacFie Mar 15 '12 at 15:51
But subscripts, in the manner desired by @Misery, are idiomatic in mathematics! –  murray Mar 15 '12 at 16:15
@murray for sure –  Andrew MacFie Mar 15 '12 at 16:32

Update

Actually, I decided the OverVector solution by @Brett Champion is not a good idea after all. The reason is that it deceives you into thinking you can safely distinguish between the vector variable $\vec{v}$ and the variable v. This is not the case: set v = 7 after defining OverVector[v] with its three components, and you lose the vector definition because now OverVector[v] will return OverVector[7].

So my conclusion is that I would definitely stick with the following approach which has worked well for me in the past (the rest is a slightly edited version of my original post):

If you're doing tensor analysis and need symbolic manipulations where the vector components are functions such as $v = \{v_1(x,y,z), v_2(x,y,z), v_3(x,y,z)\}$, you could also make use of the following:

v = Through[Array[Subscript["v", #] &, 3][x, y, z]]


The output is $\{v_1[x,y,z],v_2[x,y,z],v_3[x,y,z]\}$. These can be used in symbolic manipulations: You can now do operations on the components of v, such as D[v[[1]], x], or on the vector v as a whole, such as D[v,x]. In tensor analysis, these symbolically defined components are what you need most. But you can also assign specific functions or constants to these components:

If I were to assign, say, Subscript["v", 1][x, y, z] = x^2, this would define SubValues for the Subscript operator. The latter is similar to OverVector in that it has no pre-defined meaning in Mathematica, see "Operators without Built-in Meanings" in the documentation. The recursion problem is avoided by making the component names into strings, whereas the vector name is a symbol.

To see what other subscripted variables are defined, type SubValues[Subscript] to get variables indexed by [x,y,z] as above, or DownValues[Subscript] to find subscripted variables such as Subscript["v",0] that aren't functions of x,y,z.

Comparing the above suggestion to the definition

OverVector[w] = {Subscript[w,1][x,y,z],Subscript[w,2][x,y,z],Subscript[w,3][x,y,z]}


my recipe allows you to retain simple notation for your vectors (assuming that you'll mostly be typing their names, not the names of the components).

Edit

In response to the comment by @murray let me add that if you don't like the output with its verbose component form for your vector v, it is always possible to add rules that format the results in a wide variety of ways. For example, assuming my above template for defining vectors, you could define the following rule:

nice = {Through[Array[Subscript[name_String, #] &, 3][x, y, z]] :> name}


Then compare the output of v and v/.nice. The latter just replaces the list of components by the string name which all components have in common (which is also the name of the vector symbol v). For more on how to work with these objects, you may want to look at this MathGroup post.

-
There's one trouble with the OverVector solution: you cannot then directly have the stacked notation of the v with the right-vector arrow in it in an evaluated result; rather, that will automatically be replaced by the list of subscripted elements. What's missing, it seems, is an easy-to-use way to have your cake and eat it too: to use the over-vector notation freely, yet get coordinates for the vectors as needed. But I'm not sure what the design should look like overall. –  murray Mar 15 '12 at 21:33
If I understand your comment correctly, that's independent of the subscripts issue. It is a question of defining a format or rule such that the component-form output is printed as a single variable if possible. I'll add a remark on this to my answer. –  Jens Mar 16 '12 at 0:23

The Notations package seems to overcome this:

Needs["Notation"]
Symbolize[ParsedBoxWrapper[SubscriptBox["v", "_"]]]
v = {Subscript[v, 1], Subscript[v, 2], Subscript[v, 3]}


Edit

The symbolized symbol is a symbol with a subscript. In order to make this apply to all subscripts with this symbol "v" I use a pattern:

-
I don't understand this use of Notation. If I evaluate the three lines as shown in the block of code I get an infinite recursion. If I do it one line at a time, using the Notation Palette to enter the Symbolize... expression, I get an error that the pattern v_ is too general. –  murray Mar 15 '12 at 21:38
What version and system do you have. Ever since I first used Symbolize (~10 years ago?) I have always used subscript underscore precisely to make it general. When you say you evaluate the 3 lines in the code block it could be that the process of cutting and pasting has introduced some errors. Generally to use Symbolize you need to use the palette to ensure that it works. –  Mike Honeychurch Mar 15 '12 at 21:42
I'm using Mathematica 8.0.4 under Mac OS X. I $did$ try the three inputs you show as In[1], In[2], and In[3] precisely because I'm wary of using the Notation package in any way except by means of its palette. And for the input Symbolize[v_] I get the error message: Symbolize::ptogen: "The pattern v_ appearing in the symbolize statement Symbolize[\!$$NotationPrivateidentityForm[\"v_\"]$$] is too general to be used." –  murray Mar 16 '12 at 3:32
@murray it is subscript underscore, not underscore. i.e. the underscore is a subscript, in other words it is a subscript pattern. Enter "v" then "Control _", this will create the subscript template, now enter _ as the subscript. Alternatively you could paste Symbolize[ParsedBoxWrapper[SubscriptBox["v", "_"]]] into a cell and Convert To > StandardForm. –  Mike Honeychurch Mar 16 '12 at 4:45
Got it now, thanks @Mike Honeychurch. But I sympathize with the original poster: this seems like a roundabout (if necessary) way to do this in Mathematica, given the basic design of the language. –  murray Mar 16 '12 at 14:28

I hesitated to post this as low-level monkeying can break the system in unexpected, serious, and subtle ways. Nevertheless, since other options have been presented, and I believe that $PreRead is fairly safe as it does not apply to internal operations, I shall. We can use $PreRead to transform user input of the form

Symbolsubscript
into
HoldForm[Symbol]subscript
thereby preventing evaluation of Symbol:

\$PreRead =
(# /. SubscriptBox[x_String, ss_] :>
SubscriptBox[TagBox[x, HoldForm], ss] ) &;


This allows:

v = {Subscript[v, 1], Subscript[v, 2], Subscript[v, 3]};

v

{5, 7, 13}

{Subscript[v, 1], Subscript[v, 2], Subscript[v, 3]} = {5, 7, 13};

v

Subscript[v, 3]

{5, 7, 13}

13


FrontEnd screenshot:

-

Well, if our intent is only to see the notation over vector we could do so:

Clear[v]

OverVector[v] = {Subscript[v, 1], Subscript[v, 2], Subscript[v, 3]}


where output is

{Subscript[v, 1], Subscript[v, 2], Subscript[v, 3]}


so

OverVector[v] gives

{Subscript[v, 1], Subscript[v, 2], Subscript[v, 3]}


but if we use the built-in Defer

Defer[OverVector[v]]


we get

\!$$\*OverscriptBox[\(v$$, $$\[RightVector]$$]\)
`
-