Is there any way to force Mathematica to treat subscripted variables independently of their unsubscripted counterparts? More specifically. Say, I have the following definitions:
Subscript[b, 1] = {{1, 2}}
Subscript[b, 2] = {{3, 4}}
b = Join[Subscript[b, 1], Subscript[b, 2]]
Now when I use
Subscript[b, 1]
Mathematica will substitute it with
Subscript[{{1, 2}, {3, 4}},1]
when I want these to be three independent values, so changing b will not affect Subscript[b, ..]. Is it possible?
The symbol is
b
, and notSubscript[b, _]
.When you define:
is like defining a downvalue for any function f. Like doing:
So, what you are doing is
Which of course assigns a value to the symbol b.
and now
As expected.
So, I guess the short answer is no. At least not in a straightforward way.
Edit
While the above is true (I believe), I wasn't aware that the Notation package has a way to circumvent the default behavior. Other answers contain the details.
I studied the thread on subscripted variables in detail by pasting the various replies into a Mathematica notebook (and tried it with version 7 and 8). However, what I found out that in some cases the explicit representation of subscripted variables as
Subscript[a,b]
does not give the correct answer as contained in these answers. However, when I used explicitly the 2d notation for subscript ( a_b ) the answer was as expected. Could it be that when pasting subscripted symbols into an email they are represented asSubscript[a,b]
. (Of course, I should add that for each individual contributions I started Mathematica fresh - after usingQuit[ ]
).Here's some code I used to use to do this. It should work for you too:
With this, definitions like
are internally stored like this:
But they display as subscripts.
From a quick look I think this may be what Simon was aiming for.
If your application allows it, you may wish to consider adopting Mathematica-like naming conventions such as FullyDescriptiveCamelCase variable names instead of subscripted variables. It will make your code more portable in the end, and it does become second nature eventually.
Could use Notation from the package of same name.
Don't mind the code below, you don't figure out that RowBox structure. Just use the palette template and type Subscript[b,j_] into the left side, and, say, bb[j_], into the right. So the "actual"variables are now bb[1] etc., and you can assign safely to b.
Out[3]= {{1, 2}}
Out[4]= {{3, 4}}
Out[5]= {{1, 2}, {3, 4}}
Out[6]= {{1, 2}}
You'll probably get more accurate replies, this is the first I ever mucked with the Notation package.
Daniel Lichtblau Wolfram Research
In an answer to a previous SO question, Mathematica Notation and syntax mods, telefunkenvf14 mentioned that he was
which is essentially what this question is about.
WReach pointed out that the Notation package can do this quite simply using
Symbolize
Where (as in Daniel's answer) don't worry too much about the
Box
structure above as you can use theNotation
palette to enter this stuff in more simply.Check that it all works as wanted:
and
Note: all of the above code has been copied as Input Text, so the typeset
SubscriptBox
s have been converted to the input formSubscript
s. However, theSymbolize
works at the box level, so the tests need to be converted back to their 2d forms. To do this, select the code (or cells) and convert it to standard form by using theCell
menu or the shortcutCtrl-Shift-N
. The notebook with all the above code should look likeIf you don't want to use the
Notation
package (see Daniel's and my answers) but want to copy the behaviour ofSymbolize
, then it gets a little tricky.I had a go at doing this after I reading this SO answer but ran into troubles and gave up. I'll put the code here as community wiki so other people can try to finish it!
First you want to intercept an inputted subscript box structure and make it be interpreted as a "unique" symbol. The following code
makes an inputted
x_i
become a symbol named"$sUbsCript$x$SPLIT$i"
. Not a guaranteed unique symbol name... but it would a fairly unusual one! Notes:1) that this code will not pick up subscripts written in
FullForm
.2) this definition only fires off if both parts of the subscript are "simple" - no spaces, brackets, operators, etc...
Next, because this symbol name is so ugly, here's an optional something to make it nicer when it's asked for (this probably should be changed)
Finally, we want this subscript symbol to print out nicely. Normally we'd do this using a
MakeBoxes
definition -- but we can't in this case becauseSymbol
has the attributeLocked
:(Instead, we'll hack in a
$PrePrint
to find these crazily named symbols and write them back as subscripts:Finally the place where all of this falls down is if you try to assign something to a subscripted symbol. I haven't tried working around this yet!
Some tests - note that you'll have to convert the
Subscript
s to actual boxes for the code to work. Do this by converting to StandardForm: Ctrl-Shift-N.It doesn't work with
Set
orSetDelayed
if they generateOwnValues
and it doesn't work withInformation
It does work with definitions that produce
DownValues