[Mathematica] Saving definitions

  • Context: Mathematica 
  • Thread starter Thread starter GargleBlast42
  • Start date Start date
  • Tags Tags
    Definitions Mathematica
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 3K views
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!