[Mathematica] Saving definitions

  • Context: Mathematica 
  • Thread starter Thread starter GargleBlast42
  • Start date Start date
  • Tags Tags
    Definitions Mathematica
Click For Summary
SUMMARY

This discussion addresses the issue of saving variable definitions in Mathematica when using subscripts, specifically with the command Save. The error encountered, Save::sym, indicates that the argument must be a symbol, which subscripts are not by default. The solution involves using the DownValues and UpValues functions to associate definitions with the main symbol, allowing for successful saving of the definitions into a file.

PREREQUISITES
  • Familiarity with Mathematica syntax and commands
  • Understanding of variable definitions and subscripts in Mathematica
  • Knowledge of DownValues and UpValues functions
  • Basic file handling in Mathematica
NEXT STEPS
  • Explore the DownValues function in detail to understand its applications
  • Learn about UpValues and how they can be utilized in variable definitions
  • Investigate the Save command and its requirements for different data types
  • Review Mathematica's documentation on subscripts and their handling in expressions
USEFUL FOR

Mathematica users, particularly those working with variable definitions and subscripts, as well as educators and students in computational mathematics.

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 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K