2

Where is the problem?

enter image description here

Subscript[k, 0] := 1;
Subscript[k, 1] := 2;
ClearAll["Global`*"]
Subscript[k, 0]
Subscript[k, 1]
Clear[Subscript[k, 0]]
Clear[Subscript[k, 1]]

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
RF_1
  • 672
  • 2
  • 7
  • 5
    As a general rule I recommend that everyone avoid subscripts and use indexed variables instead, e.g., k[0] and k[1]. After assigning values the subscripted form would not appear in the output anyway. Prior to assigning values, if you want the subscripted form displayed, use Format[k[n_]] := Subscript[k, n] to control the subsequent display. – Bob Hanlon Jun 21 '21 at 18:18
  • 1
    @BobHanlon's advice is good advice. I wish subscripts were easier to deal with--they can be so darn useful sometimes. $vars = Indexed[x, #] & /@ Range[5]$ $Grad[f[vars], vars]$ – Craig Carter Jun 21 '21 at 19:07
  • 2
    Please use a descriptive title in the future. "Where is the problem?" gives no hint about the contents of your question. – Szabolcs Jun 22 '21 at 09:40

3 Answers3

5

The problem is that your definitions are associated with Subscript not k.

Subscript is in the System context. I suspect that

(* ClearAll["System`*"] *)

would be a bad idea.

You could modify the definition of your symbols, so that they are associated with k

k /: Subscript[k, 0] := 1;
k /: Subscript[k, 1] := 2;

{Subscript[k, 0], Subscript[k, 1]} (* {1, 2} *)

These can then be cleared

ClearAll["Global`*"]
{Subscript[k, 0], Subscript[k, 1]}
(* {Subscript[k, 0], Subscript[k, 1]} *)

Less drastically

Clear[k]

also works (don't specify the subscript, because the results isn't a symbol).

mikado
  • 16,741
  • 2
  • 20
  • 54
  • I got it Subscript is in the System context. Thank you. How to show all subscripts in a notebook? Is there any code such as ?Global`* and ?System`* – RF_1 Jun 21 '21 at 20:13
  • 1
    ?Subscript will report subscripts - though I think you will need to click to expand the box containing the symbol information. – mikado Jun 21 '21 at 20:24
2

There is a very useful answer here: StackExchange 2017 by Michael E2 (I shouldn't get credit for this answer...)

Subscript[symbol, 1] = 1
Subscript[symbol, 2] = 2

Now you see it.

Subscript[symbol, 1]

Then you clear it by only retaining downvalues that don't depend on symbol

DownValues[Subscript] = 
 Cases[DownValues[Subscript], dv_ /; FreeQ[dv, symbol]]

Now you don't.

Subscript[symbol, 1]
Craig Carter
  • 4,416
  • 16
  • 28
0

One more way if you want to clear individual values.

Subscript[k, 0] := 1
Subscript[k, 1] := 2

You can clear one at a time this way:

Subscript[k, 0] =.

which clears the first one but leaves the second one alone.

Subscript[k, 0]
(*Subscript[k, 0]*)

Subscript[k, 1] (2)

Bill Watts
  • 8,217
  • 1
  • 11
  • 28