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

16

6

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.

– Szabolcs – 2012-03-15T11:59:50.013

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 – 2012-03-15T12:45:55.980

@IstvánZachar How so? I'm curious -- could you explain what you mean a bit more? – Andrew – 2012-03-15T15:52:31.847

@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 – 2012-03-15T16:19:21.720

11

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 – 2012-03-16T06:59:23.353

16

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$}

3...or don't use subscripts. They're used for typesetting, but they're not idiomatic in Mathematica programming. – Andrew – 2012-03-15T15:51:17.917

2But subscripts, in the manner desired by @Misery, are idiomatic in mathematics! – murray – 2012-03-15T16:15:50.960

@murray for sure – Andrew – 2012-03-15T16:32:14.713

4

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 – 2012-03-15T21:33:24.057

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 – 2012-03-16T00:23:23.297

4

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: 3 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 – 2012-03-15T21:38:35.517 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 – 2012-03-15T21:42:07.620 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 – 2012-03-16T03:32:33.907

1@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 – 2012-03-16T04:45:27.607

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 – 2012-03-16T14:28:32.090

@murray I couldn't agree more. And having used Mathcad last year for something I was shocked at how easy they make it. – Mike Honeychurch – 2012-03-16T21:33:50.863

1

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]$$]\)
`