Message Boards Message Boards

0
|
6496 Views
|
11 Replies
|
8 Total Likes
View groups...
Share
Share this post:

Create variables with subscripts automatically?

Posted 8 years ago

I'm currently working on a project and I have created by hand 20 variables with the form X [subscript i] [subscript j], and it is very tedious. I don't want to do this 80 or 100 times by hand. Is there any way easier than this? Thanks in advance and sorry for the bad English.

11 Replies
Posted 8 years ago

I am curious.

Is the seemingly often reported troublesome behavior of subscripted variables perhaps only an unexpected consequence of a design decision of convenience?

Or are there deep fundamental theoretical reasons that might be explained why it is just not possible for subscribed variables to behave like "ordinary" variables?

If it was just a decision of convenience, since giving people subscripts seems to compel them to use them and this behavior has for years caused confusion and unexpected problems, would there be any chance this design decision might be reconsidered and perhaps this problem could just go away? Would it break anything not relatively easily repaired if this were changed?

This is perhaps similar to the various "Form"s, which people seem compelled to use and are then confused when functions and operators do not then operate on the contained data exactly as if the Form were not present.

I vaguely remember something about a "principle of least surprise" from long long ago.

POSTED BY: Bill Simpson

I think the main problem is that Subscript[x,1] or so does not have Symbol as a Head, while a normal variable has, so it is not recognised and breaks everything... I think changing it will be (very) bad idea, as it will break everything. But introducing a new function IndexedVariable or so could work....

POSTED BY: Sander Huisman

It is meanwhile routine on this platform that questions come up on the use of, or problems (i.e. "unexpected behavior") with, Subscript. Sanders description of those as a "typographical construct" is just perfect!

Because in the beginning I had problems myself in proper understanding, I would like to give a little explanation:

If you have a variable (say a) and you make an assignment, that means that the variable gets an "ownvalue":

a = 3;
OwnValues[a]
(*  Out:  {HoldPattern[a]\[RuleDelayed]3}  *)

Whereas when you make the assignments:

enter image description here

there are no OwnValues at all! The point rather is that in a way you changed the properties of Subscript:

enter image description here

This is pretty much the same as if you write:

f[1] = 3;

There is no problem in doing this, but one would not want to call f[1] a "variable".

POSTED BY: Henrik Schachner

"Typographical construct" might indeed be a good addition to the documentation...

POSTED BY: Sander Huisman

If you want to do anything with the variables, indeed do not use Subscript, it will generally be very confusing and frustrating...

The new function

Indexed

might be useful as well...

POSTED BY: Sander Huisman

Thanks, will look into it. Didnt know there would be such problems with subscripts.

Not really a problem, more of a misconception. One should see Subscript merely as a typographical construct, nothing else. If one adheres to those confines generally there is no `problem'...

POSTED BY: Sander Huisman

If you use your variables for real computations (as opposed to math typesetting) the I would consider labeling variables differently, as @Bill Simpson recommended too. The easiest:

Array[a, {3, 3}]

{{a[1, 1], a[1, 2], a[1, 3]}, {a[2, 1], a[2, 2], a[2, 3]}, {a[3, 1], a[3, 2], a[3, 3]}}

then an example of computation is:

Solve[a[1, 2]^3 - a[1, 2] + 1 == 0, a[1, 2]];
TraditionalForm[Column[%]]

enter image description here

POSTED BY: Vitaliy Kaurov

Thanks for the answer. Didnt know there would be such problems as I am merely starting

Posted 8 years ago

Perhaps

Table[Subscript[X, i, j], {i, 1, 10}, {j, 1, 10}]

and then scrape-n-paste as needed?

Be careful with subscripted variables because they are different from "ordinary" variables in some situations.

POSTED BY: Bill Simpson

Thank you so much for answering so quickly. Your answer was the one I needed!

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract