Message Boards Message Boards

0
|
1759 Views
|
4 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Clear all variables defined in a given cell

Posted 1 year ago

Based on the suggested method here, I try to clear all variables defined in a given cell as follows:

In[281]:= SelectionMove[EvaluationNotebook[],All,EvaluationCell];Options[NotebookSelection[EvaluationNotebook[]],CellLabel]
{cell} = Cells@@%
ClearAll[#] & /@ 
  Cases[NotebookRead[cell], HoldPattern[RowBox[{sym_, "=", _}]] :> sym, 
   Infinity];

gen2=SGGenSet229me[[2,1;;3,1;;3]];

ubasSG229me//FactorMatrix//Last//Inverse//HermiteDecomposition//Last
(*
tmInv is one of the bases of SG229me
*)
tmInv=TMSG229meToITA[[1;;3,1;;3]]//Inverse;
%//HermiteDecomposition//Last
G=Transpose[tmInv] . tmInv
%==Transpose[gen2] . G . gen2
(*
tm is the transformation matrix which convert the representation of this group to an orthogonal basis
*)
tm=tmInv//Inverse
conjgen2=noTranSGGenSet229meToITA[[2,1;;3,1;;3]]
Inverse[tm] . gen2 . tm==conjgen2
conj//OrthogonalMatrixQ
(*
The metric change under change-of-basis
*)
Transpose[tm] . G . tm == IdentityMatrix[3]

{valgen2,vecgen2}=Eigensystem[gen2]
{valconjgen2,vecconjgen2}=Eigensystem[conjgen2]

Out[281]= {CellLabel -> "In[281]:="}

Out[282]= {
CellObject[
 "09eef945-c108-482c-a081-f7198678f646", 
  "7e18a8bb-14fb-460e-8275-756c0388fd0c"]}

During evaluation of In[281]:= ClearAll::ssym: RowBox[{{,RowBox[{valgen2,,,vecgen2}],}}] is not a symbol or a string.

During evaluation of In[281]:= ClearAll::ssym: RowBox[{{,RowBox[{valconjgen2,,,vecconjgen2}],}}] is not a symbol or a string.

Out[285]= {{1/2, 1/2, 1/2}, {0, 2, 0}, {0, 0, 2}}

Out[287]= {{1/2, 1/2, 1/2}, {0, 2, 0}, {0, 0, 2}}

Out[288]= {{11/4, -(5/4), -(5/4)}, {-(5/4), 11/
  4, -(5/4)}, {-(5/4), -(5/4), 11/4}}

Out[289]= True

Out[290]= {{1, 1/2, 1/2}, {1/2, 1, 1/2}, {1/2, 1/2, 1}}

Out[291]= {{0, 1, 0}, {-1, 0, 0}, {0, 0, -1}}

Out[292]= True

Out[293]= True

Out[294]= True

Out[295]= {{-1, 
  I, -I}, {{1/2, 1/2, 1}, {3/2 - I/2, 3/2 + I/2, 1}, {3/2 + I/2, 
   3/2 - I/2, 1}}}

Out[296]= {{-1, I, -I}, {{0, 0, 1}, {-I, 1, 0}, {I, 1, 0}}}

As you can see, this method will trigger the following warning messages:

During evaluation of In[281]:= ClearAll::ssym: RowBox[{{,RowBox[{valgen2,,,vecgen2}],}}] is not a symbol or a string.

During evaluation of In[281]:= ClearAll::ssym: RowBox[{{,RowBox[{valconjgen2,,,vecconjgen2}],}}] is not a symbol or a string.

Any tips for fixing this problem?

Regards,
Zhao

POSTED BY: Hongyi Zhao
4 Replies
Posted 1 year ago

Sorry, Zhao, but your questions and code are a nightmare. I feel you want to do some kind of restructuring code, actually, just rename variables. This is a trivial task and done with Find/Replace (Edit-menu > Find... within the Wolfram desktop). Just like in each and every IDE or editor. With tools like stone aged "make" or any other batch language, you can do it with just one call on an arbitrary number of files. Search/Replace rules can be formed with string patterns or regular expressions (RegularExpression) if they should be complicated. This is rarely the case in those kinds of tasks.

POSTED BY: Werner Geiger
Posted 1 year ago

Look, while it's possible to get into an infinite regress situation asking "why?", I think a resonable person can see that "avoid invalid variable references" is closer to the kind of objective I was asking for than "clear all variables defined in a given cell".

If I understand your situation, you've renamed variables and you want to make sure you've done the renaming completely throughout your notebook leaving no dangling instances of the old names. There are two ways I can think of that are much simpler and more reliable than trying to parse cell data. First, just use the find/replace dialog. This won't clear the old symbols, but it will make sure you don't have old instances of those symbols hanging around, so there should be no problem. Second, change the symbols where they're defined, then restart the kernel (or just clear all of the symbols in the relevant context, probably Global). Now trying to evaluate any expressions using the old symbols will fail. Also, the old symbols will display in the undefined format, so you can see them by inspection.

POSTED BY: Eric Rimbey
Posted 1 year ago

As the title of this question indicates, my actual goal is:

Clear all variables defined in a given cell.

The context/background of this question is as follows: I used a number of custom variables in one of my cells, and then I made some changes to the naming convention of some of them, which could result in inconsistent and invalid variable references, if I clear out all possible user-defined variables in the cell, I can clearly see which ones are not being used correctly when referencing.

POSTED BY: Hongyi Zhao
Posted 1 year ago

Is there any way you can simplify your example? And maybe you can explain your actual objective, because parsing cell data to find symbols to clear just seems awkward and unmaintainable.

POSTED BY: Eric Rimbey
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