Group Abstract Group Abstract

Message Boards Message Boards

0
|
15.5K Views
|
46 Replies
|
20 Total Likes
View groups...
Share
Share this post:

Clear all the values associated with a subscripted variable

Posted 3 years ago

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

POSTED BY: Hongyi Zhao
46 Replies
Posted 3 years ago

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.

POSTED BY: Eric Rimbey
Posted 3 years ago

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}}
POSTED BY: Hans Milton
Posted 3 years ago
POSTED BY: Eric Rimbey
Posted 3 years ago

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.

Attachments:
POSTED BY: Werner Geiger
POSTED BY: Paul Abbott
Posted 3 years ago
POSTED BY: Hongyi Zhao
Posted 3 years ago

POSTED BY: Werner Geiger
Posted 3 years ago

Werner, I will try to show what is happening when assigning values to the list elements:

POSTED BY: Hans Milton
Posted 3 years ago

Okay Werner. The "syntax problems" were actually the effect of the post editors dysfunctional rendering of code output. But when output formatting is important it can be better to include a notebook directly in the post:

That is "Add Notebook" to the left in the top toolbar. As opposed to the button "Add a file" at the bottom.

POSTED BY: Hans Milton
Posted 3 years ago

That's clear, Hans, since last indexes with more than two decimal digits will go wrong. One must write:

m = Array[
  Symbol["m" <> StringPadLeft[ToString[#1], 2, "0"] <> 
     StringPadLeft[ToString[#2], 2, "0"]] &, {9, 12}]
m // Flatten // Length
{{m0101, m0102, m0103, m0104, m0105, m0106, m0107, m0108, m0109, 
m0110, m0111, m0112}, {m0201, m0202, m0203, m0204, m0205, m0206, 
m0207, m0208, m0209, m0210, m0211, m0212}, {m0301, m0302, m0303, 
m0304, m0305, m0306, m0307, m0308, m0309, m0310, m0311, 
m0312}, {m0401, m0402, m0403, m0404, m0405, m0406, m0407, m0408, 
m0409, m0410, m0411, m0412}, {m0501, m0502, m0503, m0504, m0505, 
m0506, m0507, m0508, m0509, m0510, m0511, m0512}, {m0601, m0602, 
m0603, m0604, m0605, m0606, m0607, m0608, m0609, m0610, m0611, 
m0612}, {m0701, m0702, m0703, m0704, m0705, m0706, m0707, m0708, 
m0709, m0710, m0711, m0712}, {m0801, m0802, m0803, m0804, m0805, 
m0806, m0807, m0808, m0809, m0810, m0811, m0812}, {m0901, m0902, 
m0903, m0904, m0905, m0906, m0907, m0908, m0909, m0910, m0911, 
m0912}}
108

instead. This will be fine until dimension 99.

I had another problem with the indexed approach and got confused after all our posts here. With:

Clear[b];
bs = Array[Indexed[b, #] &, 3]
b[1] = 17
bs
(* ==> *)
{Indexed[b, {1}], Indexed[b, {2}], Indexed[b, {3}]}
17
{Indexed[b, {1}], Indexed[b, {2}], Indexed[b, {3}]}

I would have expected:

{17, Indexed[b, {2}], Indexed[b, {3}]}

as the last output line. But this is a total misunderstanding. Indexed[b,i] and b[i] are not the same. The statement:

b[1]=17

doesn't change anything within the list bs. Rather, this just creates a new expression b[1] which is replaced by 17 via pattern matching.

POSTED BY: Werner Geiger
Posted 3 years ago
POSTED BY: Werner Geiger
Posted 3 years ago
POSTED BY: Werner Geiger
Posted 3 years ago

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.

POSTED BY: Eric Rimbey
Posted 3 years ago

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.

POSTED BY: Werner Geiger
Posted 3 years ago

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]} *)
POSTED BY: Werner Geiger
Posted 3 years ago

I've roughly browsed the GitHub source code of the project and found that the method used is basically based on database query. There is no too advanced mathematical processing. Therefore, I don't believe that I can learn much from it. However, as you said, maybe I can try to communicate with developers on relevant issues.

POSTED BY: Hongyi Zhao

But, as I suggested, the authors of that package are experts in the area and are most likely to be able to answer the original question that you asked…

POSTED BY: Paul Abbott
Posted 3 years ago

I think, Zhao, this is an important remark. You are right: One does nor really need to assign values to those symbols at arbitrary indexes. The symbols are needed only for the formulation of symbolic problems, e.g. Solve. Whereas for the concrete solutions just vectors (lists) are used.

Actually, I came around the whole topic only when I was trying to measure the performance of assignments.

POSTED BY: Werner Geiger
Posted 3 years ago

Thank you for telling me the MaXrd package and other related information. But my goal is not the same as all of these technologies described and implemented in these packages/literatures.

POSTED BY: Hongyi Zhao
Posted 3 years ago

Just assign the new value directly to the variable.

That's trivial, Hans. Of course, I can write b17=4711.

Obviously, I'm not able to explain my point: How to assign a value to one, say the i-th Symbol bi of the b1, b2, ..., ?

Say you have a vector bs = {b1, b2, ..., b100} of dimension 100. How can you set the value of its i-th component?

(I'm getting tired of that question. Obviously, both of us cannot answer it.)

The only possibility I could find until now, is (see above):

POSTED BY: Werner Geiger
Posted 3 years ago

As an aside, IntegerString can be used for left-padding integers with zeros.

StringPadLeft[ToString[#1], 2, "0"]

can be replaced by

IntegerString[#1, 10, 2]
POSTED BY: Rohit Namjoshi
Posted 3 years ago

Just assign the new value directly to the variable.
Don't go the back route via the receiving list. That would break the link from variable to list.

POSTED BY: Hans Milton
Posted 3 years ago

Yes, of course.

But the question is: How to change the value of an indexed (or better: simulated indexed) variable Symbol["b"<>ToString[i]] for arbitrary i?

POSTED BY: Werner Geiger
Posted 3 years ago

Unfortunately, your last piece of code changes the value and definition of the list bs, but not the value nor the definition of symbol b1.

That is not strange. The assignment, Set, is one-way. Changing the source affects the receiver, but not the other way around.

POSTED BY: Hans Milton
Posted 3 years ago

I don't think so. The second part of each code block is the Echo-Output, not Input. Unfortunately, the community editor cannot handle Output, or at least I don't know how to do it. Often, I insert screenshots, but this is tedious.

POSTED BY: Werner Geiger
Posted 3 years ago

The code you posted has syntax problems

POSTED BY: Hans Milton
Posted 3 years ago
POSTED BY: Updating Name
Posted 3 years ago
POSTED BY: Hongyi Zhao
Posted 3 years ago

Yes, it is acceptable, Zhao. It is even very fast during creation of the Array as you can see from the following comparison code for large vector lengths n:

n = 1000000
Clear[b, bs];
{t, tmp} = AbsoluteTiming[bs = Array[Indexed[b, #] &, n]];
{t // EngineeringForm, bs[[1]]}
Clear[b, bs];
{t, tmp} = AbsoluteTiming[bs = Array[b, n]];
{t // EngineeringForm, bs[[1]]}
Clear[b, bs];
{t, tmp} = AbsoluteTiming[bs = Array[b[#] &, n]];
{t // EngineeringForm, bs[[1]]}
Clear[bs];
{t, tmp} = AbsoluteTiming[bs = Array[Symbol["b" <> ToString[#]] &, n]];
{t // EngineeringForm, bs[[1]]}
(* ==> *)

enter image description here

The unsuccessful call to b[i] seems to be faster than any successful call. The symbolic method (the last one) is comparable slow, which does not surprise since string functions are involved.

POSTED BY: Werner Geiger
Posted 3 years ago

The first parameter of Array is a function. In your case the constant function b. During evaluation of Array b[1], b[2], ... are called which are undefined, hence you get the function calls themselves. Hence you arrive at

(* ==> {b[1], b[2], b[3]} *)

I think the above description cannot be regarded as a problem that causes unpredictable side effects, at most one behavior. Therefore, this method, a.k.a., something as follows, is still acceptable:

Clear[b,t];
Array[b,3]
Array[t,{3,3}]
POSTED BY: Hongyi Zhao
Posted 3 years ago

Because it's the same issue. I did not want to write the same thing twice.

POSTED BY: Werner Geiger
Posted 3 years ago
POSTED BY: Hongyi Zhao
Posted 3 years ago

Therefore, the final solution is the following approach:

In[121]:= m = Array[m, {9, 12}];
m // Flatten // Length
m // Flatten // DeleteDuplicates // Length

Out[122]= 108

Out[123]= 108
POSTED BY: Hongyi Zhao
Posted 3 years ago
POSTED BY: Hans Milton
Posted 3 years ago
POSTED BY: Hongyi Zhao
Posted 3 years ago
Attachments:
POSTED BY: Werner Geiger
Posted 3 years ago

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

Attachments:
POSTED BY: Hans Milton
Posted 3 years ago

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 ...

POSTED BY: Hans Milton
Posted 3 years ago

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.

POSTED BY: Hongyi Zhao
Posted 3 years ago

I see. Never mind.

POSTED BY: Hongyi Zhao
Posted 3 years ago

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

POSTED BY: Werner Geiger
Posted 3 years ago
POSTED BY: Hongyi Zhao

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?

Posted 3 years ago
POSTED BY: Hongyi Zhao
Posted 3 years ago
POSTED BY: Hongyi Zhao
Posted 3 years ago
POSTED BY: Hongyi Zhao
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard