[Mathematica] Saving definitions

In summary, a user is having trouble saving definitions of variables with subscripts as indices. They are receiving an error message stating that the argument at position 2 is expected to be a symbol. The user is looking for a solution to this problem and is given a suggestion to try using the symbol x instead of Subscript. This solves the issue and the user is grateful for the help.
  • #1
GargleBlast42
28
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
  • #2
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}}
 
  • #3
Thank you very much Simon, it solved the problem!
 

1. How do I save my Mathematica definitions for future use?

To save your Mathematica definitions, you can use the "Save" or "Save As" options under the "File" menu. This will create a .m file which contains all of your defined variables and functions.

2. Can I save my definitions in a specific directory?

Yes, you can specify the directory where you want to save your definitions by using the "SaveDirectory" option with the "Save" or "Save As" command. For example: Save["directory/filename.m", expr, SaveDirectory->"desired_directory"].

3. Can I load saved definitions into a new Mathematica session?

Yes, you can load saved definitions by using the "Get" or "Needs" commands. These commands will load the definitions from the specified .m file into your current session.

4. How do I update my saved definitions?

If you have made changes to your definitions and want to update the saved version, you can use the "Save" or "Save As" command again. This will overwrite the previous .m file with the updated definitions.

5. Are there any other ways to save my definitions besides using .m files?

Yes, you can also save your definitions in a notebook by using the "Save As" option under the "File" menu. This will create a .nb file which will contain both your definitions and any associated code or comments.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
412
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
549
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
934
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
134
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
261
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • Programming and Computer Science
Replies
1
Views
536
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • Programming and Computer Science
Replies
5
Views
370
Back
Top