MathGroup Archive 2002

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Assigning to superscripted variables

  • To: mathgroup at smc.vnet.net
  • Subject: [mg34384] Re: Assigning to superscripted variables
  • From: "Carl K. Woll" <carlw at u.washington.edu>
  • Date: Fri, 17 May 2002 06:31:20 -0400 (EDT)
  • Organization: University of Washington
  • References: <abo3f4$5dt$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Hi Dave,

I thought about this problem a while ago when I was taking GR. I came up
with what I think is a reasonable solution, although it may be more
complicated then you might like. I will give a brief overview of my approach
below, and then provide the code at the end.

I am in agreement with some of the other posters that modifying Power is not
a good solution. On the other hand, I didn't want to change the standard
notation used for tensors. This means that one has to go into the guts of
MakeExpression to change the way Mathematica handles SuperscriptBoxes before
Mathematica turns them in to powers. The problem then is to come up with a
way to have SuperscriptBoxes turn into tensors when we want them to, and
have them turn into powers otherwise. I solved this problem by defining a
list of what objects are tensors, that is, what are the symbols s that when
given a superscript should be a tensor.

The next problem that arises is the internal representation of a tensor. I
made the decision that a tensor is represented by a Tensor[] object, where
the first argument is the tensor symbol, and the remaining arguments are the
indices. With this approach, we need a different way to indicate that an
index is contravariant or covariant. My approach was to attach a symbol to
each index representing what kind of index it is. So, with ui (upper index)
denoting a contravariant index and li (lower index) denoting a covariant
index, a tensor T_{\mu \nu}^\lambda (in latex notation) would be internally
represented as
Tensor[T, li[mu,nu], ui[lambda] ]

Now for the code, with some remarks. The code needs more work, and ought to
be put into a package.
When I looked at this problem, I also wanted to be able to define a tensor
as a matrix, and to extract from the matrix the value corresponding to the
indices when they were integers. For this reason I gave Tensor the HoldFirst
attribute.

Clear[Tensor]
SetAttributes[Tensor,HoldFirst]

We want to override the default handling of superscripts when the base
object is a tensor. For this purpose we need to first define which symbols
are tensors. This is what the functions ClearTensor and MakeTensor do. Since
we need to be able to check for tensors before the default Power code
happens, we need to know whether an object is a tensor during the
MakeExpression phase. Hence, we store the tensors as strings and not
symbols. The TensorQ function is used during the MakeExpression phase, so it
does not need to hold any arguments.

ClearAll[ClearTensor,MakeTensor]
SetAttributes[ClearTensor,HoldAll]
SetAttributes[MakeTensor,HoldAll]
ClearTensor[]:=Module[{},
    tensors={};
    ]
ClearTensor[a__Symbol]:=Module[{},
    tensors=Complement[tensors,List@@ToString/@Unevaluated/@Hold[a]];]
MakeTensor[a__Symbol]:=Module[{},
    tensors=Union[tensors,List@@ToString/@Unevaluated/@Hold[a]];
    ]
tensors={};
TensorQ[f_?(MemberQ[tensors,#]&)]:=True
TensorQ[SuperscriptBox[f_,_]]:=TensorQ[f]
TensorQ[SubscriptBox[f_,_]]:=TensorQ[f]

Here we define the rules for handling SuperscriptBoxes and SubscriptBoxes
during the MakeExpression phase when a tensor is involved. Basically, we
strip out the superscripts and subscripts, replacing them with ui[] and li[]
as appropriate. Note that I made the decision that indices are separated by
a single space, so that commas and semicolons can be interpreted as
derivatives in the usual way. I have not included derivatives below, but it
is straightforward to do so. Finally, when a subscript follows a
superscript, Mathematica automatically inserts a parenthesis around the
superscripted tensor. This must be because superscripts have a different
precedence than subscripts. My code does not handle this situation properly.
There are several solutions. First, every time Mathematica puts in a
"spurious" parenthesis, delete it. This was the solution I followed. If this
is too cumbersome, then you will need to include additional code below to
handle these parentheses, or you will need to figure out how Mathematica
inserts the parenthesis and change that behaviour. Including code to handle
the parentheses shouldn't be too difficult. Changing Mathematica's behavior
is more difficult, if not impossible, and while I believe that it is
possible to change, I never spent the time trying to figure it out.

Clear[MakeExpression]
MakeExpression[SuperscriptBox[f_?(TensorQ[#]&),h_],g_]:=Module[{},
  MakeExpression[
      RowBox[{"Tensor[",Stripscript[f],",",ToIndices["ui[",h],"]"}],g]]
MakeExpression[SubscriptBox[f_?(TensorQ[#]&),h_],g_]:=Module[{},
  MakeExpression[
      RowBox[{"Tensor[",Stripscript[f],",",ToIndices["li[",h],"]"}],g]]

Clear[Stripscript]
Stripscript[SubscriptBox[f_,h_]]:=
  Sequence[Stripscript[f],",",ToIndices["li[",h]]
Stripscript[SuperscriptBox[f_,h_]]:=
  Sequence[Stripscript[f],",",ToIndices["ui[",h]]
Stripscript[f_]:=f

Clear[ToIndices,MoreIndices]
ToIndices[s_,a_]:=RowBox[{s,a,"]"}];
ToIndices[s_,RowBox[{a_," ",b__}]]:=
    RowBox[{s,a,",",MoreIndices[b],"]"}]
MoreIndices[a_," ",b__]:=Sequence[a,",",MoreIndices[b]]
MoreIndices[a_]:=a

Finally, we need to provide rules to MakeBoxes, so as to convert the
internal form of a tensor into the standard representation with raised and
lowered indices.

Clear[MakeBoxes]
MakeBoxes[Tensor[a__,b_li],f_]:=
  SubscriptBox[MakeBoxes[Tensor[a],f],MakeBoxes[b,f]]
MakeBoxes[Tensor[a__,b_ui],f_]:=
  SuperscriptBox[MakeBoxes[Tensor[a],f],MakeBoxes[b,f]]
MakeBoxes[Tensor[a_],f_]:=MakeBoxes[a,f]

MakeBoxes[ui[a__],f_]:=RowBox[{a}]
MakeBoxes[li[a__],f_]:=RowBox[{a}]

Some examples:

In[36]:=
\!\(\(T\^2\)\_3\^1 // FullForm\)
Out[36]//FullForm=
Tensor[T,ui[2],li[3],ui[1]]

In[37]:=
Tensor[T, ui[a], li[b], ui[c]]
Out[37]=
\!\(\(T\^a\)\_b\^c\)

If the above looks like an approach you want to check out, I would be happy
to to help out further.

Carl Woll
Physics Dept
U of Washington

"Dave Snead" <dsnead6 at charter.net> wrote in message
news:abo3f4$5dt$1 at smc.vnet.net...
> Hi,
>
> I've been trying to assign values to superscripted variable, ex,
> a^i = 5
> but I get a message that Tag Power is protected.
> I can
> Unprotect[Power]
> first and then it works fine.
> However after a few more expressions, Power somehow gets reprotected!
> Does anyone know what causes this?
> How can I keep Power unprotected for the remainder of the session?
>
> Thanks in advance,
> Dave Snead
>
>
>




  • Prev by Date: RE: solving vector equations in mathematica
  • Next by Date: A possible bug in Lists
  • Previous by thread: Re: Assigning to superscripted variables
  • Next by thread: Conditional evaluation