[Mathematica] Trouble understanding subscripted variables

  • Context: Mathematica 
  • Thread starter Thread starter jackmell
  • Start date Start date
  • Tags Tags
    Mathematica Variables
Click For Summary

Discussion Overview

The discussion revolves around the challenges of managing subscripted variables in Mathematica, specifically how to remove or unset values assigned to these variables without affecting others. The scope includes technical explanations and practical coding tips related to variable manipulation in Mathematica.

Discussion Character

  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant expresses confusion about how to remove values from subscripted variables after assignment, noting that attempts to use Clear or Remove result in errors or unintended consequences.
  • Another participant provides an example demonstrating the use of subscripted variables and suggests that using subscripts may lead to complications in Mathematica.
  • A different participant introduces the concept of using UNSET (represented as "=.") to remove values from subscripted variables, indicating it as a solution to the problem presented.
  • Another participant suggests using Quiet along with the UNSET operation to suppress output when unsetting a variable that may already be unset, indicating a practical improvement to their coding process.

Areas of Agreement / Disagreement

Participants share various methods for managing subscripted variables, but there is no consensus on the best approach, as opinions vary regarding the utility and complexity of using subscripts in Mathematica.

Contextual Notes

Participants mention specific commands and their effects, but there are unresolved aspects regarding the implications of using subscripted variables and the potential for errors in more complex scenarios.

Who May Find This Useful

This discussion may be useful for Mathematica users, particularly those dealing with variable management and looking for practical coding strategies related to subscripted variables.

jackmell
Messages
1,806
Reaction score
54
Hi,

I don't understand how to remove the values for these subscripted variables once I have assigned a number to them. If I try to remove the value from a_1 below, I get an error message:

Code:
Remove[a]
Subscript[a, 1] = 1; 
myf = z^3 + z*w^2
myf /. z -> Subscript[a, 1]
myf /. z -> Subscript[a, 2]
Clear[Subscript[a, 1]]

So substituting a_1 and a_2 into myf does what it's suppose to do. In the first case, the value of a_1 is substituted into myf and in the second, the variable name a_2 is substituted. Now, suppose I want to remove the value from a_1 so that when I next try to substitute into myf, the variable name a_1 is used and not the value of a_1. If I try to use Clear[a_1] or Remove[a_1] it tells me that's not a variable name and if I use Remove[a], then all the values of a are removed including the ones I want to keep.

Thanks guys,
Jack
 
Physics news on Phys.org
In[1]:= Subscript[a,1]=1; p*q+p/.p->Subscript[a,1]
Out[2]= 1+q

In[3]:= Subscript[a,1]=.; p*q+p/.p->Subscript[a,1]
Out[4]= \!\(a\_1 + q\ a\_1\)

Personal opinion: Trying to use subscripts is probably going to cause you more grief than it is worth. Your post is just the first example you will run into. The darker and darker the corners of Mathematica you start poking into the more you will find. I hope it works out for you.
 
Wow, I love learning something new. The function you're looking for, and is used above, is UNSET.

The shortcut for it is, as above, " =. "
 
Ok. I think I should use

Quiet[a_n=.];

That way, with the Quiet and the semicolon, no output is generated if the variable name already has been unset.

Wow! I am already saving so much time now with just this little change to my code. You guys are tops. You too Bill. :)
 
Last edited:

Similar threads

  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
10
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
Replies
21
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K