Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Assigning to superscripted variables

180 views
Skip to first unread message

Dave Snead

unread,
May 13, 2002, 6:10:44 AM5/13/02
to
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

Wolf, Hartmut

unread,
May 14, 2002, 4:15:55 AM5/14/02
to
Dave,

better use the Notation package (see Help > Add-ons > Notation Package):

In[1]:= << Utilities`Notation`

In[2]:= \!\(a\^5 = 25\)
>From In[2]:=
\!\(Set::"write" \(\(:\)\(\ \)\)
"Tag \!\(Power\) in \!\(a\^5\) is Protected."\)
Out[2]= 25

so just entering a^5 still means Power[a,5], but _first_ symbolizing a^5
does it:

In[4]:=
\!\(\*
RowBox[{"Symbolize", "[",
TagBox[\(a\^5\),
NotationBoxTag,
TagStyle->"NotationTemplateStyle"], "]"}]\)

You enter this through clicking at Symbolize[#] at the Notation Palette, and
then expanding # to what you want. Then you may treat that as a symbol

In[5]:= \!\(a\^5 = 25\)
Out[5]= 25

and you may do funny things

In[6]:= \!\(\@\(a\^5\)\%5\)
Out[6]= \!\(5\^\(2/5\)\)

The fifth root of our symbol a^5 now is 5^(2/5) (Power is still available).

However, you can't use Symbolize within an expression; here I tried to do it
within an assignment.

In[7]:=
\!\(\*
RowBox[{
RowBox[{"Evaluate", "[",
RowBox[{"Symbolize", "[",
TagBox[\(b\^2\),
NotationBoxTag,
TagStyle->"NotationTemplateStyle"], "]"}], "]"}], "=", "3"}]\)
>From In[7]:=
Set::"wrsym": "Symbol \!\(Null\) is Protected."
Out[7]= 3

In[8]:= Names["Global`*"]
Out[12]=
{"a", "a\[UnderBracket]Superscript\[UnderBracket]5", \
"b\[UnderBracket]Superscript\[UnderBracket]2", "CapitalRomanNumeral", \
"$NotationDebug"}

You see what Symbolize has done: even b^2 has been symbolized, but didn't
get its desired value after the error occurred.

In[9]:= \!\(b\^2\)
Out[9]= \!\(b\^2\)

so symbolize in a prior line.

--
Hartmut


Louis M. Pecora

unread,
May 14, 2002, 4:25:10 AM5/14/02
to
In article <abo3f4$5dt$1...@smc.vnet.net>, Dave Snead
<dsn...@charter.net> wrote:

> I've been trying to assign values to superscripted variable, ex,
> a^i = 5
> but I get a message that Tag Power is protected.

?? You're assigning to a to the ith power. Is that what you want?
That's not a subscripted variable AFAIK.

Unprotecting Power can be dangerous.

--
-- Lou Pecora

- My views are my own.

David Park

unread,
May 14, 2002, 4:28:13 AM5/14/02
to
Dave,

It is not totally clear what you are trying to do. I assume you are looking
for a tensor type index on a variable, i.e. you want a superscript to be
treated like a subscript is treated. But do you really want to throw away
the ability to have powers?

I am interested in what answers you will get. I would say that if you want
super indexed variables you have to define a special form for them and then
give an output formatting. I also alter the output formatting of Power. It
puts parentheses around the quantity that is being raised to a power.

An upper indexed variable will be represented and entered as super[x,i].

Format[super[x_, i_Integer]] := Superscript[x, i]

Unprotect[Power];
Format[ Power[A_, sup_]] := Superscript[SequenceForm["", A, ""], sup];
Protect[Power];

Then

super[a, 2]
% // FullForm
(superscript form)
super[a, 2]

a^2
% // FullForm
(power with parentheses)
Power[a, 2]

super[a, 2]^2
% // FullForm
(power of a superscript variable)
Power[super[a, 2], 2]

Now you can assign values to the indexed variable.

super[a, 2] := 5
super[a, 2]^2
25

David Park
dj...@earthlink.net
http://home.earthlink.net/~djmp/

> From: Dave Snead [mailto:dsn...@charter.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.

Jens-Peer Kuska

unread,
May 14, 2002, 4:40:29 AM5/14/02
to
Hi,

what you are doing is realy dangerous, because
Power[] is a very a basic build-in function and
allmost all other functions have rules for Power[].

To use super scripted index values, traditional mathematics
use a^{(b)} with round brackets. If you are using the Frontend
you should try

MakeBoxes[SuperIndex[a_, b_], fmt_:StandardForm] :=
SuperscriptBox[MakeBoxes[a,fmt], RowBox[{"(", MakeBoxes[b,fmt], ")"}]]

MakeExpression[SuperscriptBox[a_, RowBox[{"(", b_, ")"}]],
fmt_:StandardForm] :=
MakeExpression[RowBox[{"SuperIndex", "[", a, ",", b, "]"}]]


and use SuperIndex[a,i] or enter the typesetted form

a <Crtl>^ ( i )

where <Crtl>^ means th Ctrl-Key on your keyboard.

Regards
Jens

Instanton

unread,
May 15, 2002, 3:34:00 AM5/15/02
to
Hi,

Mathematica internally understands "superscripted variables" as the
power function of its Head, i.e.

A^i

is understood as the variable A raised to the power of i. So, when
you're assigning values to a superscripted variable, you are actually
modifying the definition of the function Power. This is quite danguous
because if you assigned
a^i = 5 by unprotecting Power, the system would use this rule in all
subsequent calculations of any expression involving Power and may give
rise to wrong results. So, please do not use this kind of assignment.

Now I guess that your motivation of doing the above assignment should
be one of the following:

i) you wished to define a variable whose name is a^i and whose value
is 5;
ii) you wished to define the variable a whose i-th power is evaluated
to 5;
iii) you wished to define the variable i which satisfies Power[A,i]=5.

In the case i), I suggest that you change the superscript into a
subscript; for the case ii) or iii), I would preferr to define a or i
as

a:=5^(1/i)

or

i=Log[5]/Log[a].

--------------------------------------------------------------------

"Dave Snead" <dsn...@charter.net> wrote in message news:<abo3f4$5dt$1...@smc.vnet.net>...

Dave Snead

unread,
May 15, 2002, 3:38:03 AM5/15/02
to
Hi all,

Thanks for the suggestions. I'm trying to keep the look and feel of a
contravariant vector (or tensor),
so I prefer not to throw parentheses around the superscript.
Using
a <Crtl>^ i = 5
works fine as long as I avoid assigning to "a" directly and keep the
Protected attribute off of Power. And I've yet to encounter
anything "dangerous" about unprotecting Power. The use of Power with
expressions other than "a^i" works exactly as before.
Since my initial posting I've discovered my problem was due to Simplify--
it's first use in a new session restores the Protected attribute of Power.
This looks like a bug in Mathematica, since Simplify should not be altering
the attributes of Power (and it's inconsistent about it too!). So if I
just use Simplify before my use of Unprotect[Power] my problems disappear.
The following is a new session showing the attribute altering behavior of
Simplify:

In[1]:=
Attributes[Power]
Out[1]=
{Listable,NumericFunction,OneIdentity,Protected}

In[2]:=
Unprotect[Power];

In[3]:=
Attributes[Power]

Out[3]=
{Listable,NumericFunction,OneIdentity}

In[4]:=
Simplify[0];

In[5]:=
Attributes[Power]

Out[5]=
{Listable,NumericFunction,OneIdentity,Protected}

In[6]:=
Unprotect[Power];

In[7]:=
Attributes[Power]

Out[7]=
{Listable,NumericFunction,OneIdentity}

In[8]:=
Simplify[0];

In[9]:=
Attributes[Power]

Out[9]=
{Listable,NumericFunction,OneIdentity}

Thanks,
-- Dave Snead

P.S.


In[1]:=<< "Utilities`Notation`"

In[2]:=Symbolize[a <Crtl>^ i]
In[3]:=a <Crtl>^ i = 5
seems to work too, although Symbolize must be entered from the Notation
palette
and the expression in In[4] must be copied and pasted from In[2] since
although it looks like
a <Crtl>^ i
it's really become
a\[UnderBracket]Superscript\[UnderBracket]i

"Wolf, Hartmut" <Hartmu...@t-systems.com> wrote in message
news:abqh3r$9pf$1...@smc.vnet.net...

Carl K. Woll

unread,
May 17, 2002, 6:53:18 AM5/17/02
to
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" <dsn...@charter.net> wrote in message
news:abo3f4$5dt$1...@smc.vnet.net...

0 new messages