Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

[Mathematica] Saving definitions

  1. Jul 14, 2010 #1
    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 (Text):
    Save::sym: Argument A1 at position 2 is expected to be a symbol.
    How can I get around this?
     
  2. jcsd
  3. Jul 14, 2010 #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}}
     
  4. Jul 15, 2010 #3
    Thank you very much Simon, it solved the problem!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: [Mathematica] Saving definitions
  1. In mathematica (Replies: 4)

Loading...