Mathematica [Mathematica] Saving definitions

AI Thread Summary
The discussion revolves around saving variable definitions in Mathematica, specifically for variables formatted as Subscript[A,1], Subscript[A,2], etc. Users encounter an error when trying to use the Save command, which expects a symbol rather than a subscripted variable. A workaround involves associating definitions with the symbol x instead of the subscript directly. The solution successfully allows users to save definitions by leveraging the DownValues and UpValues associated with the symbol. The problem is resolved by correctly structuring the definitions, enabling effective file saving of variable states.
GargleBlast42
Messages
28
Reaction score
0
Hi,
I'm trying to save (into a file) definitions of some variables which are of the form Subscript[A,1], Subscript[A,2],... where the subscript is used as an index for the variable. When using the Save command I obtain the error

Code:
Save::sym: Argument A1 at position 2 is expected to be a symbol.

How can I get around this?
 
Physics news on Phys.org
By default, a definition is stored under the symbol name of the Head of the LHS.
The following help make it clear:

In[1]:= Subscript[x, 1]=2;
In[2]:= Subscript[x, 2]:=Subscript[x, 1]+2

In[3]:= Save[NotebookDirectory[]<>"subdefns.dat",Subscript[x, 1]]
During evaluation of In[16]:= Save::sym: Argument Subscript[x, 1] at position 2 is expected to be a symbol. >>
Out[3]= Save[/home/simon/Desktop/subdefns.dat,Subscript[x, 1]]

In[4]:= Save[NotebookDirectory[]<>"subdefns.dat",Subscript]

In[5]:= {DownValues[x],DownValues[Subscript]}
Out[5]= {{},{HoldPattern[Subscript[x, 1]]:>2,HoldPattern[Subscript[x, 2]]:>Subscript[x, 1]+2}}

In[6]:= FilePrint[NotebookDirectory[]<>"subdefns.dat"]
Attributes[Subscript] = {NHoldRest}

Subscript[x, 1] = 2

Subscript[x, 2] := Subscript[x, 1] + 2

If you want to have the definition associated with the symbol x, you could try

In[7]:= x/:Subscript[x, 3]=5
Out[7]= 5

In[8]:= {DownValues[x],UpValues[x]}
Out[8]= {{},{HoldPattern[Subscript[x, 3]]:>5}}
 
Thank you very much Simon, it solved the problem!
 

Similar threads

Replies
4
Views
2K
Replies
1
Views
2K
Replies
5
Views
3K
Replies
13
Views
2K
Replies
4
Views
2K
Back
Top