Clear all the values associated with a subscripted variable

See my following code snippet:

In[764]:= xTM
N[x] =.
xTM

Out[764]= {{Subscript[x, 1, 1], Subscript[x, 1, 2], Subscript[x, 1, 
  3]}, {Subscript[x, 2, 1], Subscript[x, 2, 2], Subscript[x, 2, 
  3]}, {Subscript[x, 3, 1], Subscript[x, 3, 2], Subscript[x, 3, 3]}}

During evaluation of In[764]:= Unset::norep: Assignment on x for N[x,{MachinePrecision,MachinePrecision}] not found.

Out[765]= $Failed

Out[766]= {{Subscript[x, 1, 1], Subscript[x, 1, 2], Subscript[x, 1, 
  3]}, {Subscript[x, 2, 1], Subscript[x, 2, 2], Subscript[x, 2, 
  3]}, {Subscript[x, 3, 1], Subscript[x, 3, 2], Subscript[x, 3, 3]}}

I want to clear all the values associated with the subscripted variable, in this case, x. But as you can see, my method failed to do the trick. Any tips for achieving this goal?

Regards,
Zhao

I don’t understand your code. What is xTM?

If you want to use subscripted variables, instead of Subscript[x,1,2] and the like you should better use Indexed and write:

Clear[x];
{Indexed[x, 1], Indexed[x, {2, 1}]}
(* ==> {x subscripted with 1, x substripted with 12 *)

… assign values:

{x[1], x[1, 2]} = {1, 21};
{x[1], x[1, 2]}
(* ==> {1, 21} *)

… and clear them all:

Clear[x];
{x[1], x[1, 2]}
(* ==> {x[1], x[1, 2]} *)

What is xTM?

It means The transformation matrix defined here. And my code is aimed to solve the linear equation to find the corresponding transformation matrix.

OK. Not such important for the Subscript/Indexed point.

What do you want to solve?
Given some point coordinates before and after transformation, find the transformation matrix and shift vector?

This could be done by Solve. See the attached notebook.

CalcTrans.nb (25.1 KB)

Zhao, it would be easier to help if you provided clear and complete examples. Putting x and xTM in your question but not showing how they were defined causes confusion. The title of your post was “Clear all the values associated with a subscripted variable”, but the only clearing shown is N[x]=.. I’m not sure why you expect that to work–it looks very strange to me since N is a built in symbol.

However, ignoring your code and trying to answer your question in general…

Subscript is a kind of weird symbol and it can lead to headaches if you try to use it for variables. It isn’t protected, so you can do things like Subscript[x,1] = 7, but that creates a defintion for Subscript, not for x. If you later do something like x = 5, then you’ve basically “lost” your definition for the subscripted x. However, you could just do ClearAll[Subscript] to clear all “subscripted variables”.

Somewhat better is to use Indexed. Indexed is protected, so you can’t use it for assignments, but if you have a matrix variable, then there are interesting uses for Indexed. Clearing all “subscripted variables” is just clearing the matrix variable. Without some more context, I don’t know whether Indexed is actually what you need.

I find that Subscript and Indexed introduce too much complication for very little benefit. The benefit is mostly formatting. I tend to either assign an array to a variable and just use Part in the normal way or use DownValues (where the indices are arguments).

Somewhat better is to use Indexed.

Do you mean something like the following, as shown by Werner Geiger?

In[1]:= (* The unknown tarnsformation *)
Clear[t, b];
tTM = Array[Indexed[t, {#1, #2}] &, {3, 3}]
bT = Array[Indexed[b, #] &, 3]

Out[2]= {{Indexed[t, {1, 1}], Indexed[t, {1, 2}], 
  Indexed[t, {1, 3}]}, {Indexed[t, {2, 1}], Indexed[t, {2, 2}], 
  Indexed[t, {2, 3}]}, {Indexed[t, {3, 1}], Indexed[t, {3, 2}], 
  Indexed[t, {3, 3}]}}

Out[3]= {Indexed[b, {1}], Indexed[b, {2}], Indexed[b, {3}]}

but if you have a matrix variable,

Yes. That’s exactly my case. How about the following usage as a solution to this situation?

In[21]:= Array[X,{3,3}]

Out[21]= {{X[1, 1], X[1, 2], X[1, 3]}, {X[2, 1], X[2, 2], 
  X[2, 3]}, {X[3, 1], X[3, 2], X[3, 3]}}

then there are interesting uses for Indexed.

What do you mean by saying interesting uses for Indexed?

or use DownValues (where the indices are arguments).

Could you please give an example of this trick?

Regards,
Zhao

Thank you for your wonderful example. But for my case, in fact, the purpose is to identify a given space group based on its generators. In other words, I have a given affine matrix group, and I want to determine the type of space group that it belongs to, that is, using a transformation like the one you described as an isomorphic mapping, which can associate my matrix group isomorphically with a particular group of 230 space groups in three dimensions.

The difficulties here are as follows:

  1. I don’t know the correspondence between the group elements and the elements of a particular space group. If I build all possible combinations to establish the corresponding equations by enumerating, the possible combinations will be astronomical. As a result, I can’t construct the coordinate correspondence as you show.
  2. There are 230 3-dimensional space groups, and I need to narrow the range to minimize the amount of calculation.
  3. Space groups have their own specific restrictions and requirements, and only meeting the mapping between a few points does not necessarily guarantee the isomorphism between the two groups.

Zhao,
of course, Eric means that kind of code I demonstrated within my little notebook CalcTrans.nb. Did it not answer all your questions?

There is nothing special and no tricks around “Indexed”. Why don’t you just read the documentation?

Since you seem to be a beginner, you need not bother about down- and up-values for the moment.

9-26-2022
Hi Dr. Zhang,
Very interesting problems.
Among the 230 3-D space groups, how many differences ar there between any pair of space groups?
Could that formulation be applied to Mathematica, to assist the problems that you raise?

9-26-2022 Hi Dr. Zhang,

My surname is Zhao.

As for your question, this is a very complicated problem and can’t be explained clearly in such a discussion. I have been studying this problem for several months, and the progress is good so far, but it hasn’t been completely solved.

For what it’s worth, here is a simple alternative to using Subscript or Indexed:

In[153]:= m = Array[Symbol["m" <> ToString@FromDigits@{##}] &, {3, 3}]
Out[153]= {{m11, m12, m13}, {m21, m22, m23}, {m31, m32, m33}}

Individual elements can be assigned values or they can be cleared:

In[154]:= m22 = 5
Out[154]= 5

In[155]:= m
Out[155]= {{m11, m12, m13}, {m21, 5, m23}, {m31, m32, m33}}

In[156]:= Clear[m22]

In[157]:= m
Out[157]= {{m11, m12, m13}, {m21, m22, m23}, {m31, m32, m33}}

I am not familiar with space groups and unfortunately cannot help. The question goes far beyond your original question about Subscript.

I see. Never mind.

What do you mean by saying interesting uses for Indexed?

Look at the last two examples in the documentation for Indexed. So, for example, by asserting that x is an element of the region Rectangle[] one can infer that x is a 2-dimensional vector (aka a 2-element list) and so Indexed[x,1] and Indexed[x,2] can be meaningfully interpreted in the rest of the expression.

Could you please give an example of this trick?

You already started with your example Array[X,{3,3}]. You can use X[1,1] as you would have used Subscript[x,1,1] or Indexed[x,{1,1}]. If you define X[1,1] = ..., then you have DownValues for X instead of for Subscript. And so you can do ClearAll[X] to easily achieve your original task.

Hi Hans,

When I try to use the above tip, I encounter the following very strange problem:

In[174]:= ClearAll[x];
Array[Symbol["x" <> ToString@FromDigits@{##}] &, {3}]

Out[175]= {<|{{-1, -1, -1}, 
    2} -> {{-1, 0, 0}, {0, -1, 0}, {0, 0, -1}}, {{-1, -1, 1}, 
    2} -> {{2, -1, -1}, {3/2, -(3/2), -(1/2)}, {3/
     2, -(1/2), -(3/2)}}, {{-1, -I, I}, 
    4} -> {{3/2, -(1/2), -(3/2)}, {3/
     2, -(3/2), -(1/2)}, {2, -1, -1}}, {{-1, 1, 1}, 
    2} -> {{2, -1, -1}, {3/2, -(1/2), -(3/2)}, {3/
     2, -(3/2), -(1/2)}}, {{-1, (-1)^(1/3), -(-1)^(2/3)}, 
    6} -> {{1, 1, -2}, {3/2, 1/2, -(3/2)}, {1/2, 3/2, -(3/2)}}, {{-I, 
     I, 1}, 4} -> {{3/2, 1/2, -(3/2)}, {1, 1, -2}, {1/2, 3/
     2, -(3/2)}}, {{1, 1, 1}, 
    1} -> {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}, {{1, -(-1)^(1/3), (-1)^(
     2/3)}, 3} -> {{3/2, -(1/2), -(3/2)}, {2, -1, -1}, {3/
     2, -(3/2), -(1/2)}}|>, <|{{-1, -1, -1}, 
    2} -> {{-1, 0, 0}, {0, -1, 0}, {0, 0, -1}}, {{-1, -1, 1}, 
    2} -> {{1, 0, 0}, {0, -1, 0}, {0, 0, -1}}, {{-1, -I, I}, 
    4} -> {{0, 1, 0}, {-1, 0, 0}, {0, 0, -1}}, {{-1, 1, 1}, 
    2} -> {{1, 0, 0}, {0, 1, 0}, {0, 0, -1}}, {{-1, (-1)^(
     1/3), -(-1)^(2/3)}, 
    6} -> {{0, 1, 0}, {0, 0, 1}, {-1, 0, 0}}, {{-I, I, 1}, 
    4} -> {{1, 0, 0}, {0, 0, 1}, {0, -1, 0}}, {{1, 1, 1}, 
    1} -> {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}, {{1, -(-1)^(1/3), (-1)^(
     2/3)}, 3} -> {{0, 1, 0}, {0, 0, 1}, {1, 0, 0}}|>, x3}

As you can see, I’ve cleared the variable x, however, it still interferes with subsequent assignment operations.

As you can see

No, no, we can’t see! Why do you continue doing this? You need to show us the relevant code, otherwise we’re just constantly guessing about what’s going on. What happened between In[174] and Out[175]? Or for that matter, what happened before In[174]? Or more to the point, why do you even make that part of your question? Why don’t you provide a simplified version of your question so that we don’t have to understand all of your domain-specific details? Where did these Associations come from? Why should I have to parse the complicated structures you’re presenting here when your question seems to be about how to represent subscripted variables? What “subsequent assignment operations” are being interfered with? You do realize that x and x1 are completely different and independent symbols, don’t you? Just ask a clear question with clear examples in code, please.

I dont believe that. Your Code:

ClearAll[x];
Array[Symbol["x" <> ToString@FromDigits@{##}] &, {3}]
(* ==> {x1,x2,x3} *)

is correct. Maybe:

ClearAll[x];
Array[Symbol["x" <> ToString[#]] &, 3]
(* ==> {x1,x2,x3} *)

is a bit easier to read.

Anyway, as I said above, I would prefer Indexed:

ClearAll[x];
Array[Indexed[x, #] &, 3]
(* ==> {x with subscript i, i=1,..,3 *)

You can access the i-th vector elements by x[i], which is more feasible than with Hans’ approach where you have to use Symbol[“x”<>ToString[i]]. Or, if you assigned the whole vector to x: x[[i]].

Zhao, from your example code it is obvious that the symbols x1 and x2 has already been given values before you create the matrix. That’s why they displays with their contents, not their names.

In[81]:= x2 = {15, 16, 17, 18, 19};
In[82]:= Array[Symbol["x" <> ToString@FromDigits@{##}] &, {3}]
Out[82]= {x1, {15, 16, 17, 18, 19}, x3}

In[83]:= Clear[x2]
In[84]:= Array[Symbol["x" <> ToString@FromDigits@{##}] &, {3}]
Out[84]= {x1, x2, x3}

Clearing x clears the symbol x, not the symbols x1, x2 …x327 …

Werner, there is not much practical difference in using the two methods, yours or mine. See the attached edit of your CalcTrans notebook.

CalcTrans HM.nb (21 KB)

That is true of course, Hans.

I prefer the indexed version only because it is “nearer” to common mathematical notation, and it looks nicer in output.

BTW: As I now did some performance measurement I found: Your symbolic method is pretty faster than the indexed method. Namely the indexed method takes about 1.7 times the time of the symbolic method when randomly assigning one million of values to the elements of a length-1000 vector.

See the attached notebook

CalcTrans HM WG.nb (45.1 KB)