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

Can't use subscripted variables in function definition?

319 views
Skip to first unread message

Chris Young

unread,
Jan 7, 2012, 5:29:51 AM1/7/12
to
What is the status of subscripted variables in Mathematica now? Can't
they finally be used as regular variables? Or is there some workaround
so that they can be? This is time-honored, very intuitive mathematical
notation that should be available, IMO.

Chris Young
cy...@comcast.net


In[1449]:= a = 1; b = 1; c = Sqrt[2];
{Subscript[x, 1], Subscript[x, 2], Subscript[x, 3]} = {0, 1, 0};
{Subscript[y, 1], Subscript[y, 2], Subscript[y, 3]} = {0, 0, 1};

In[1455]:=
P = {{Subscript[x, 1], Subscript[y, 1]}, {Subscript[x, 2], Subscript[
y, 2]}, {Subscript[x, 3], Subscript[y, 3]}};

In[1452]:=
inCtr = {{a, b, c}.{Subscript[x, 1], Subscript[x, 2], Subscript[x,
3]}/(a + b +
c), {a, b, c}.{Subscript[y, 1], Subscript[y, 2], Subscript[y,
3]}/(a + b + c)};

shrinkToInCtr[s_, Polygon[P_List, opts___]] :=
Module[
{
a, b, c,
Subscript[x, 1], Subscript[x, 2], Subscript[x, 3],
Subscript[y, 1], Subscript[y, 2], Subscript[y, 3],
inCtr
},

a = Norm[P[[3]] - P[[2]]];
b = Norm[P[[1]] - P[[3]]];
c = Norm[P[[2]] - P[[1]]];

{Subscript[x, 1], Subscript[x, 2], Subscript[x, 3]} = {P[[1, 1]],
P[[2, 1]], P[[3, 1]]};
{Subscript[y, 1], Subscript[y, 2], Subscript[y, 3]} = {P[[1, 2]],
P[[2, 2]], P[[3, 2]]};

inCtr = {{a, b, c}.{Subscript[x, 1], Subscript[x, 2], Subscript[x,
3]}/(a + b +
c), {a, b, c}.{Subscript[y, 1], Subscript[y, 2], Subscript[y,
3]}/(a + b + c)};

Polygon[(1 - s) (# - inCtr) + inCtr & /@ P, opts]
]

In[1456]:= shrinkToInCtr[s_, Polygon[P_List, opts___]]

In[1457]:= shrinkToInCtr[0.1, Polygon[P]]

During evaluation of In[1457]:=
Module::lvsym : "Local variable specification \[NoBreak]{a$, b$, c$, \
\!\(\*SubscriptBox[\(x\), \(1\)]\), \!\(\*SubscriptBox[\(x\), \
\(2\)]\), \!\(\*SubscriptBox[\(x\), \(3\)]\), \
\!\(\*SubscriptBox[\(y\), \(1\)]\), \!\(\*SubscriptBox[\(y\), \
\(2\)]\), \!\(\*SubscriptBox[\(y\), \(3\)]\), inCtr$}\[NoBreak] \
contains \[NoBreak]\!\(\*SubscriptBox[\(x\), \(1\)]\)\[NoBreak], \
which is not a symbol or an assignment to a symbol. \!\(\*ButtonBox["\
\[RightSkeleton]",
Appearance->{Automatic, None},
BaseStyle->"Link",
ButtonData:>"paclet:ref/message/Module/lvsym",
ButtonNote->"Module::lvsym"]\)"

Out[1457]= Module[{a$, b$, c$, Subscript[x, 1], Subscript[x, 2],
Subscript[x, 3], Subscript[y, 1], Subscript[y, 2], Subscript[y, 3],
inCtr$}, a$ =
Norm[{{0, 0}, {1, 0}, {0, 1}}[[3]] - {{0, 0}, {1, 0}, {0, 1}}[[2]]];
b$ = Norm[{{0, 0}, {1, 0}, {0, 1}}[[1]] - {{0, 0}, {1, 0}, {0, 1}}[[
3]]]; c$ =
Norm[{{0, 0}, {1, 0}, {0, 1}}[[2]] - {{0, 0}, {1, 0}, {0, 1}}[[
1]]]; {Subscript[x, 1], Subscript[x, 2], Subscript[x,
3]} = {{{0, 0}, {1, 0}, {0, 1}}[[1, 1]], {{0, 0}, {1, 0}, {0, 1}}[[
2, 1]], {{0, 0}, {1, 0}, {0, 1}}[[3, 1]]}; {Subscript[y, 1],
Subscript[y, 2], Subscript[y,
3]} = {{{0, 0}, {1, 0}, {0, 1}}[[1, 2]], {{0, 0}, {1, 0}, {0, 1}}[[
2, 2]], {{0, 0}, {1, 0}, {0, 1}}[[3, 2]]};
inCtr$ = {{a$, b$, c$}.{Subscript[x, 1], Subscript[x, 2], Subscript[
x, 3]}/(a$ + b$ +
c$), {a$, b$, c$}.{Subscript[y, 1], Subscript[y, 2], Subscript[y,
3]}/(a$ + b$ + c$)};
Polygon[((1 - 0.1) (#1 - inCtr$) + inCtr$ &) /@ {{0, 0}, {1, 0}, {0,
1}}]]


Bill Rowe

unread,
Jan 8, 2012, 4:26:54 AM1/8/12
to
On 1/7/12 at 5:25 AM, cy...@comcast.net (Chris Young) wrote:

>What is the status of subscripted variables in Mathematica now?

It has not changed in version 8.

>Can't they finally be used as regular variables?

Yes, but not by default. You can use subscripted variables like
ordinary variables using the Notation package. However, from my
perspective this is more effort than it is worth.

>Or is there some workaround so that they can be? This is time-honored,
>very intuitive mathematical notation that should be available, IMO.

Yes, it would be useful to have subscripted variables fully
integrated into Mathematica. And, that may well happen in some
future version. But for now, it seems to me this is simply one
of the limitations Mathematica has.


Armand Tamzarian

unread,
Jan 9, 2012, 3:20:21 AM1/9/12
to
On Jan 8, 8:26 pm, Bill Rowe <readn...@sbcglobal.net> wrote:
I recently ported 90 pages of code from a competitor product -- mainly
used by engineers -- to Mathematica for someone. The code was heavily
subscripted because subscripted variables in the competitor product
were pretty easy and essentially worked out of the box the way you
would expect without a learning curve or any other issues. The were
other reasons to justify the port to Mathematica but it was quite enlightening
to see code in which subscripted symbolic variables were so natural
and frankly made me wonder why Mathematica is behind the curve on this. Lets
hope V9 offers this.

Mike


Ralph Dratman

unread,
Jan 10, 2012, 5:56:36 AM1/10/12
to
Is this just a discussion about how things look on the screen -- or is
there some deeper difference between Mathematica's "array" indices and
properly-implemented subscripts?

Ralph

On Mon, Jan 9, 2012 at 3:16 AM, Armand Tamzarian
<mike.hon...@gmail.com> wrote:
> On Jan 8, 8:26 pm, Bill Rowe <readn...@sbcglobal.net> wrote:

Chris Young

unread,
Jan 13, 2012, 4:54:44 AM1/13/12
to
On 2012-01-10 10:56:36 +0000, Ralph Dratman said:

> Is this just a discussion about how things look on the screen -- or is
> there some deeper difference between Mathematica's "array" indices and
> properly-implemented subscripts?

I was looking for subscripted variables to conveniently extend notation
for things such as points determining a polynomial, and so on. Formulas
to do this kind of thing appear in all the math refererences with
subscripted variables, and I think it's the most natural notation.
We're not really interested in an array of points, just the individual
points, in this case. It's simply a more uncluttered notation.

Chris


DrMajorBob

unread,
Jan 14, 2012, 2:55:53 AM1/14/12
to
a[i] is less cluttered than Subscript[a,i], it works better as a variable,
and it's easier to enter and access.

If you want a[i] to LOOK like Subscript[a,i] in displayed results, you can
execute:

subFunction[
a_Symbol] := (MakeExpression[SubscriptBox[ToString@a, i_], f_] :=
MakeExpression[RowBox[{ToString@a, "[", i, "]"}]];
MakeBoxes[a[i_], f_] :=
SubscriptBox[MakeBoxes[a, f], MakeBoxes[i, f]])

subFunction[a]
Array[a, 10]

{Subscript[a, 1], Subscript[a, 2], Subscript[a, 3], Subscript[a, 4], \
Subscript[a, 5], Subscript[a, 6], Subscript[a, 7], Subscript[a, 8], \
Subscript[a, 9], Subscript[a, 10]}

You'll see subscripts only when a value is not defined, but that's true
for subscripted variables, too. For instance:

a[2] = 3;
Subscript[a, 5] = 12;
Array[a, 10]

{Subscript[a, 1], 3, Subscript[a, 3], Subscript[a, 4], 12, \
Subscript[a, 6], Subscript[a, 7], Subscript[a, 8], Subscript[a, 9], \
Subscript[a, 10]}

and also

Table[Subscript[a, n], {n, 10}]

{Subscript[a, 1], 3, Subscript[a, 3], Subscript[a, 4], 12, \
Subscript[a, 6], Subscript[a, 7], Subscript[a, 8], Subscript[a, 9], \
Subscript[a, 10]}

Bobby
--
DrMaj...@yahoo.com

Armand Tamzarian

unread,
Jan 14, 2012, 2:58:58 AM1/14/12
to
For me it is not about looks because I think Mathematica typesetting/
rendering is miles ahead of competitor products. It is about ease of
use. There is Symbolize in the Notations package which can get you
closer to something that works better but the best analogy i can think
of of how the other product works is if instead of Subscript you had a
function SubscriptVariable in which the subscript was a different
context e.g. SubscriptVariable[Global`x, NewContext`i] rendered as x
subscript i.

Mike

On Jan 10, 9:56 pm, Ralph Dratman <ralph.drat...@gmail.com> wrote:
> Is this just a discussion about how things look on the screen -- or is
> there some deeper difference between Mathematica's "array" indices and
> properly-implemented subscripts?
>
> Ralph
>
> On Mon, Jan 9, 2012 at 3:16 AM, Armand Tamzarian
>
>
>
>
>
>
>
> <mike.honeychu...@gmail.com> wrote:
> > On Jan 8, 8:26 pm, Bill Rowe <readn...@sbcglobal.net> wrote:
> >> On 1/7/12 at 5:25 AM, c...@comcast.net (Chris Young) wrote:
>
> >> >What is the status of subscripted variables in Mathematica now?
>
> >> It has not changed in version 8.
>
> >> >Can't they finally be used as regular variables?
>
> >> Yes, but not by default. You can use subscripted variables like
> >> ordinary variables using the Notation package. However, from my
> >> perspective this is more effort than it is worth.
>
> >> >Or is there some workaround so that they can be? This is time-honored=
,
> >> >very intuitive mathematical notation that should be available, IMO.
>
> >> Yes, it would be useful to have subscripted variables fully
> >> integrated into Mathematica. And, that may well happen in some
> >> future version. But for now, it seems to me this is simply one
> >> of the limitations Mathematica has.
>
> > I recently ported 90 pages of code from a competitor product -- mainly
> > used by engineers -- to Mathematica for someone. The code was heavily
> > subscripted because subscripted variables in the competitor product
> > were pretty easy and essentially worked out of the box the way you
> > would expect without a learning curve or any other issues. The were
> > other reasons to justify the port to Mathematica but it was quite enlig=
htening
> > to see code in which subscripted symbolic variables were so natural
> > and frankly made me wonder why Mathematica is behind the curve on this.=

Kevin J. McCann

unread,
Jan 14, 2012, 3:04:04 AM1/14/12
to
Subscripted variables are still a bit tricky as they have been from the
beginning, i.e. Mathematica 8 has not improved upon this.

To use a subscripted variable in a function definition, you need a colon
after the symbol, e.g.

f[xsub1:_]=...

However, you need to use the Notation package and Symbolize all your
subscripted variables. BTW, you have to use the Symbolize button on the
popup palette, not just type Symbolize (I don't know why).

If you like, I could send you a short nb with an example.

In spite of the overhead of using "nice" notation, I find that the
readability, especially after some time has gone by, to be more than
worth it.

Kevin
> Polygon[(1 - s) (# - inCtr) + inCtr& /@ P, opts]
> Polygon[((1 - 0.1) (#1 - inCtr$) + inCtr$&) /@ {{0, 0}, {1, 0}, {0,
> 1}}]]
>
>


Bill Rowe

unread,
Jan 14, 2012, 5:16:11 PM1/14/12
to
On 1/14/12 at 2:53 AM, btr...@austin.rr.com (DrMajorBob) wrote:

>a[i] is less cluttered than Subscript[a,i], it works better as a
>variable, and it's easier to enter and access.

On my Mac, there is no real difference between entering

a cntrl- i and

a[i]

Both require 4 key strokes

And since I have the front end set to display Traditional
Notation, using true subscripts is less cluttered than a[i].

But your point about access (which I interpret as actually using
subscripts in Mathematica computations) is quite valid which is
why I use subscripts very rarely.


Armand Tamzarian

unread,
Jan 15, 2012, 4:50:09 AM1/15/12
to
I should clarify that a bit because it was written in haste. Firstly I
meant SubscriptVariable[NewContext`x, NewContext`i] but this isn't
really a proper solution but reason I mentioned it was because if you
have x_j and you want that to be a single symbol you run into problems
if x or j or both are set to some other global value. With x_j and
then x=2 you don't want 2_j you still want x_j returned. Symbolize
addresses this but the point of the original comment is that is it
nowhere near as straight forward as the other product to work with
subscripts.

Mike

On Jan 14, 6:58 pm, Armand Tamzarian <mike.honeychu...@gmail.com>
wrote:
> For me it is not about looks because I think Mathematica typesetting/
> rendering is miles ahead of competitor products. It is about ease of
> use. There is Symbolize in the Notations package which can get you
> closer to something that works better but the best analogy i can think
> of of how the other product works is if instead of Subscript you had a
> function SubscriptVariable in which the subscript was a different
> context e.g. SubscriptVariable[Global`x, NewContext`i] rendered as x
> subscript i.
>
> Mike
>
> On Jan 10, 9:56 pm, Ralph Dratman <ralph.drat...@gmail.com> wrote:
>
>
>
>
>
>
>
> > Is this just a discussion about how things look on the screen -- or is
> > there some deeper difference between Mathematica's "array" indices and
> > properly-implemented subscripts?
>
> > Ralph
>
> > On Mon, Jan 9, 2012 at 3:16 AM, Armand Tamzarian
>
> > <mike.honeychu...@gmail.com> wrote:
> > > On Jan 8, 8:26 pm, Bill Rowe <readn...@sbcglobal.net> wrote:
> > >> On 1/7/12 at 5:25 AM, c...@comcast.net (Chris Young) wrote:
>
> > >> >What is the status of subscripted variables in Mathematica now?
>
> > >> It has not changed in version 8.
>
> > >> >Can't they finally be used as regular variables?
>
> > >> Yes, but not by default. You can use subscripted variables like
> > >> ordinary variables using the Notation package. However, from my
> > >> perspective this is more effort than it is worth.
>
> > >> >Or is there some workaround so that they can be? This is time-honor=
ed=
> ,
> > >> >very intuitive mathematical notation that should be available, IMO.
>
> > >> Yes, it would be useful to have subscripted variables fully
> > >> integrated into Mathematica. And, that may well happen in some
> > >> future version. But for now, it seems to me this is simply one
> > >> of the limitations Mathematica has.
>
> > > I recently ported 90 pages of code from a competitor product -- mainl=
y
> > > used by engineers -- to Mathematica for someone. The code was heavily
> > > subscripted because subscripted variables in the competitor product
> > > were pretty easy and essentially worked out of the box the way you
> > > would expect without a learning curve or any other issues. The were
> > > other reasons to justify the port to Mathematica but it was quite enl=
ig=
> htening
> > > to see code in which subscripted symbolic variables were so natural
> > > and frankly made me wonder why Mathematica is behind the curve on thi=
0 new messages