Message Boards Message Boards

0
|
1171 Views
|
18 Replies
|
8 Total Likes
View groups...
Share
Share this post:

How to clear a variable with subscript?

Posted 2 months ago

Hello,

The subscript symbol can be assigned but can't be cleared.

Subscript[a, 0] = 2
Clear[Subscript[a, 0]]

cause error.

enter image description here

POSTED BY: Zhenyu Zeng
18 Replies

Isn’t this where Remove is supposed to be used? That seems to get rid of a subscripted variable that’s been made into a symbol via the palette from the Notations package.

https://stackoverflow.com/questions/8512120/difference-between-clear-and-remove-in-mathematica#:~:text=Remove%5Bsymbol1%2C%20...,string%20input%20that%20ClearAll%20supports.

Posted 2 months ago

Thanks for your advice. The Remove doesn't work for Subscript.

Subscript[a, 0] = 7; Remove[Subscript[a, 0]]
(*Remove::ssym: Subscript[a, 0] is not a symbol or a valid string pattern.*)
POSTED BY: Updating Name
Posted 2 months ago

That's now how you'd use Remove. Remove can't remove an expression. It can only remove a symbol.

POSTED BY: Eric Rimbey
Posted 2 months ago

Thanks.

POSTED BY: Zhenyu Zeng
Posted 2 months ago

What is the difference between a normal expression and a symbol?

Regards.

POSTED BY: Zhenyu Zeng
Posted 2 months ago

In the WL everything is an expression. An expression is either an Atom or a normal expression. A normal expression is of the form

h[e1, e2, ...]

where h is the Head. h, e1, e2, ... are expressions - atoms or normal expressions. I don't think there are any exceptions to this structure.

POSTED BY: Rohit Namjoshi
Posted 2 months ago

Thanks. So a symbol is a atom?

POSTED BY: Zhenyu Zeng

A common refrain is not to define Subscript as a variable. For instance, Subscript[x, j] is an expression that depends on x and j, as though Subscript were a function. You cannot treat x and Subscript[x, 0] as independent variables. The following give errors:

Solve[x == Subscript[x, 0], x]

x = Subscript[x, 0]
Clear[x]

You can use Clear[Subscript] or ClearAll[Subscript] to clear the definitions, but it clears all of them. Maybe you don't want to clear all of them and lose the previous definitions of your other Subscript variables. In that case, you have to use Unset on each definition you wish to clear, which Marvin Ray Burns has shown.

This can be inconvenient for complicated function definitions.

Subscript[f, 1][x_, n_Integer, n_Integer] := 1 + x/n;
Subscript[f, 1][x_, m_Integer, n_Integer] /; m < n := 
  1 + x*Subscript[f, 1][x, m + 1, n]/m;
Subscript[f, 1][x_, n_Integer?NonNegative] := Subscript[f, 1][x, 1, n];

Subscript[f, 1][1., 17] - E
(*  0.  *) 

To clear the definitions of Subscript[f, 1], I have to unset each of the three definitions.

Subscript[f, 1][x_, n_Integer, n_Integer] =.
Subscript[f, 1][x_, m_Integer, n_Integer] /; m < n =.
Subscript[f, 1][x_, n_Integer?NonNegative] =.

The main problem here is if I make a typo, execute, fix the typo, and reexecute, I have a definition that is hidden, that I have to find in order to unset it. I can find the definition with the following:

?? Subscript

Another problem with Subscript definitions is that Attributes are attributed only to symbols, not expressions. I cannot make Subscript[f, 1] have the attributes Listable, HoldAll, etc.

POSTED BY: Michael Rogers
Posted 2 months ago

Thanks. Learned a lot.

POSTED BY: Updating Name
Posted 2 months ago

Just adding more explanation to what Marvin provided. Let's start with.

Subscript[a, 0] = 2

What has happened is that you've set DownValues for Subscript. You haven't set anything for a. One way to see this is to use Information.

Information[a]
(* You'll see that there are no definitions shown for a *)

Information[Subscript]
(* You'll see that Subscript[a,0] is set to 2 *)

Even though Subscript is a built in symbol, it has no definitions attached to it by default, and it is not Protected, which means you can set/override its definitions. So, it's effectively the same as just defining your own custom function.

MyFunction[a, 0] = 2

And if you tried to Clear that, you'd get the same message.

Clear[MyFunction[a, 0]]

So, to get rid of the Subscript[a,0] = 2 definition, you can use Unset as Marvin showed you. Alternatively, you can just clear all definitions associated with Subscript:

Clear[Subscript]

But that would get rid of any other definitions for Subscript as well.

POSTED BY: Eric Rimbey
Posted 2 months ago

Okay. So, Subscript[a,0] definite Subscript.

Subscript[a, 0] = 2
Information[Subscript]
Superscript*5

Why isn't the last result of the codes 10 but 5 Superscript? enter image description here

POSTED BY: Zhenyu Zeng
Posted 2 months ago

Here's what you posted:

Subscript[a, 0] = 2
Information[Subscript]
Superscript*5

You created a definition for Subscript, but later you used Superscript. So, let's assume that the following is what you intended:

Subscript[a, 0] = 2
Information[Subscript]
Subscript*5

You get

5 Subscript

Why? Just forget about Subscript for a minute. What if you used a custom symbol:

JustASymbol[a, 0] = 2;
JustASymbol*5
(* 5 JustASymbol *)

It's the same thing. You defined a rule for a function-like expression. You defined DownValues. If you want a symbol itself to have a definition, you need to define OwnValues.

SomeOtherSymbol = 2;
SomeOtherSymbol*5
(* 10 *)

And you can even do this with Subscript

Subscript = 77;
Subscript*5
(* 385 *)

But beware, if you have both OwnValues and DownValues for a symbol, they will produce results that you probably didn't expect.

POSTED BY: Eric Rimbey
Posted 2 months ago

Thanks. I am still a little confused. When there is a value in DownValues, multiplication should work normally.

POSTED BY: Zhenyu Zeng
Posted 2 months ago

Multiplication is working normally (as in, the way Mathematica intends for it to work).

Try

5  List
(* 5 List *)

List is a built in function, and it definitely has defined behavior when applied to arguments (aka DownValues), but when encountered in isolation, it's just a symbol. In the expression Subscript*5, Subscript appears as an isolated symbol--it's not the head of any expression. So, Mathematica looks at OwnValues, finds nothing, and leaves Subscript alone.

Try 5 Table or 5 Integrate or any other symbol that has no OwnValues. The multiplication cannot be resolved further so you just end up with 5 symbol.

When Mathematica encounters an isolated symbol, it consults OwnValues not DownValues to figure out what to do. Mathematica looks for DownValues when the symbol is the head of an expression.

POSTED BY: Eric Rimbey
Posted 2 months ago

Thanks. Benefit a lot

POSTED BY: Zhenyu Zeng

You can use

Subscript[a, 0] =.(*where the decimal point follows the equal sign.*)
POSTED BY: Marvin Ray Burns
Posted 2 months ago

Thanks. That is a good way. But why can variable be assigned but can't be cleared by Clear?

POSTED BY: Zhenyu Zeng

An error occurs because the argument is a normal expression rather than a symbol: As shown in the error message, ef/message/ClearAll/ssym :enter image description here

POSTED BY: Marvin Ray Burns
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