44
$\begingroup$

Here's a small listing where I've used EscqEsc to typeset θ in the notebook:

Clear[ f, θ, Subscript[x, r] ]
Subscript[x, r] := 3
f[θ_] := Subscript[x, r] Cos[θ]

Plot[f[θ], {θ, 0, Pi}]

(in my notebook this looked like $x_r$, not Subscript[x, r] for example).

This produces a message from Clear of the form:

Clear::ssym : x_r is not a symbol or a string

What is curious is that I appear to be able to assign to this variable $x_r$ without any trouble, yet it is apparently treated differently than my other symbols f and θ.

How exactly does Mathematica define a symbol. Why can I use $x_r$ like a variable, yet it does not have this symbol characterization?

$\endgroup$
3

5 Answers 5

30
$\begingroup$

Your code reveals exactly why Clear complains: Subscript[x, r] is not a Symbol nor a String. When you assign a value to it, you're setting a DownValue not an OwnValue; in other words, you're setting the value of a function not a variable. To use $x_r$ as a symbol, use the Notation` package's function, Symbolize. I'd recommend using it from the palette directly, as it has all of the intricacies already set up for you.

$\endgroup$
4
  • 6
    $\begingroup$ ...and you can use ClearNotations to clear your Symbolized symbols. $\endgroup$ Feb 23, 2012 at 23:28
  • 3
    $\begingroup$ rcollyer wrote: "Subscript[x, r] is not a Symbol nor a String" ........... That rather evades the point. Why should Clear be artificially restricted to operate on Symbols or Strings? I cannot think of any reason why Clear[$x_1$] should not be able to work. In my view, this is a blatant user-interface failing of Mma, and one that has been unnecessarily unresolved for over 20 years. $\endgroup$
    – wolfies
    Sep 5, 2013 at 14:04
  • 2
    $\begingroup$ @wolfies I find that you frustrate me. It is apparent that you either did not read the rest of the text I wrote or deliberately ignored it so that you could express your frustration. Unset works, but it is unclear what Clear would operate on, Subscript, x, 1? What about the case where both arguments to Subscript are symbols, what should happen then? Without Notation` , Subset[x,1] is not atomic! So, if you have a better way, submit an answer. $\endgroup$
    – rcollyer
    Sep 5, 2013 at 14:40
  • 1
    $\begingroup$ As you are perfectly aware, Unset[$x_1$] works ... in fact, it is required, since Clear fails to provide a uniform clearing interface. And since Unset does the job, there is plainly no inherent reason that Clear cannot be made to operate in the same manner. Clear doesn't have to be atomic. That is the point. As for your 'frustrations' ... ? $\endgroup$
    – wolfies
    Sep 6, 2013 at 19:53
28
$\begingroup$

As the error message indicates Clear does not work that way. There are several assignment forms that automatically create a definition to something other than a raw symbol:

x[5] = 1;
Subscript[x, 1] = 2;
x /: Subscript[x, 2] = 3;
N[x] = 3.14159;

DownValues[x]
DownValues[Subscript]
UpValues[x]
NValues[x]
{HoldPattern[x[5]] :> 1}

{HoldPattern[Subscript[x, 1]] :> 2}

{HoldPattern[Subscript[x, 2]] :> 3}

{HoldPattern[N[x, {MachinePrecision, MachinePrecision}]] :> 3.14159}

(See documentation for: DownValues, UpValues. NValues is not directly documented.)

You cannot clear any of these forms in the fashion you attempted:

(* Failure

Clear[ x[5] ]
Clear[ Subscript[x, 1] ]
Clear[ Subscript[x, 2] ]
Clear[ N[x] ]

*)

You can clear some of them with Unset (short form =.):

x[5] =.
Subscript[x, 1] =.
N[x]  =.

Unset[Subscript[x, 2]] does not work because it was created with TagSet (/:), and for that you need TagUnset:

x /: Subscript[x, 2] =.
$\endgroup$
3
  • 1
    $\begingroup$ I thought about mentioning Unset, but didn't. So, you get the +1. $\endgroup$
    – rcollyer
    Jan 20, 2012 at 16:38
  • $\begingroup$ What is NValues? I haven't seen it elsewhere. $\endgroup$
    – ions me
    Feb 2, 2021 at 6:02
  • $\begingroup$ @IonSme NValues gives the values explicitly assigned to N[x] for symbol x. This is like an UpValue but is specific to N and happens automatically without use of ^=. $\endgroup$
    – Mr.Wizard
    Mar 6, 2021 at 19:02
16
$\begingroup$

You get that error because Mathematica does not recognize $x_r$ as a symbol. To instruct it to treat symbols with subscripts also as symbols, you'll need to use the Notations` package like so:

<< Notation`
Symbolize[ParsedBoxWrapper[SubscriptBox["_", "_"]]]

Now you should have no problems in clearing your subscripted symbol.

$\endgroup$
8
$\begingroup$

That is because the value is set to a DownValue of Subscript..

DownValues[Subscript]
$\endgroup$
6
$\begingroup$

If you want to clear all subscript variables, similarly to Clear["Global'*"] (the ' should be a backtick) I have always used the following with good results:

Clear["Subscript"]

Which seems to clear all variables of the form:

Subscript[r,1]
$\endgroup$
1
  • $\begingroup$ It's a nice idea, but it only works with definitions that are attached to the Symbol Subscript. See my answer for examples of other definitions. $\endgroup$
    – Mr.Wizard
    Jan 13, 2014 at 8:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.