calc: Storing Variables

 
 13.1 Storing Variables
 ======================
 
 The ‘s s’ (‘calc-store’) command stores the value at the top of the
 stack into a specified variable.  It prompts you to enter the name of
 the variable.  If you press a single digit, the value is stored
 immediately in one of the “quick” variables ‘q0’ through ‘q9’.  Or you
 can enter any variable name.
 
    The ‘s s’ command leaves the stored value on the stack.  There is
 also an ‘s t’ (‘calc-store-into’) command, which removes a value from
 the stack and stores it in a variable.
 
    If the top of stack value is an equation ‘a = 7’ or assignment ‘a :=
 7’ with a variable on the lefthand side, then Calc will assign that
 variable with that value by default, i.e., if you type ‘s s <RET>’ or ‘s
 t <RET>’.  In this example, the value 7 would be stored in the variable
 ‘a’.  (If you do type a variable name at the prompt, the top-of-stack
 value is stored in its entirety, even if it is an equation: ‘s s b
 <RET>’ with ‘a := 7’ on the stack stores ‘a := 7’ in ‘b’.)
 
    In fact, the top of stack value can be a vector of equations or
 assignments with different variables on their lefthand sides; the
 default will be to store all the variables with their corresponding
 righthand sides simultaneously.
 
    It is also possible to type an equation or assignment directly at the
 prompt for the ‘s s’ or ‘s t’ command: ‘s s foo = 7’.  In this case the
 expression to the right of the ‘=’ or ‘:=’ symbol is evaluated as if by
 the ‘=’ command, and that value is stored in the variable.  No value is
 taken from the stack; ‘s s’ and ‘s t’ are equivalent when used in this
 way.
 
    The prefix keys ‘s’ and ‘t’ may be followed immediately by a digit;
 ‘s 9’ is equivalent to ‘s s 9’, and ‘t 9’ is equivalent to ‘s t 9’.
 (The ‘t’ prefix is otherwise used for trail and time/date commands.)
 
    There are also several “arithmetic store” commands.  For example, ‘s
 +’ removes a value from the stack and adds it to the specified variable.
 The other arithmetic stores are ‘s -’, ‘s *’, ‘s /’, ‘s ^’, and ‘s |’
 (vector concatenation), plus ‘s n’ and ‘s &’ which negate or invert the
 value in a variable, and ‘s [’ and ‘s ]’ which decrease or increase a
 variable by one.
 
    All the arithmetic stores accept the Inverse prefix to reverse the
 order of the operands.  If ‘v’ represents the contents of the variable,
 and ‘a’ is the value drawn from the stack, then regular ‘s -’ assigns ‘v
 := v - a’, but ‘I s -’ assigns ‘v := a - v’.  While ‘I s *’ might seem
 pointless, it is useful if matrix multiplication is involved.  Actually,
 all the arithmetic stores use formulas designed to behave usefully both
 forwards and backwards:
 
      s +        v := v + a          v := a + v
      s -        v := v - a          v := a - v
      s *        v := v * a          v := a * v
      s /        v := v / a          v := a / v
      s ^        v := v ^ a          v := a ^ v
      s |        v := v | a          v := a | v
      s n        v := v / (-1)       v := (-1) / v
      s &        v := v ^ (-1)       v := (-1) ^ v
      s [        v := v - 1          v := 1 - v
      s ]        v := v - (-1)       v := (-1) - v
 
    In the last four cases, a numeric prefix argument will be used in
 place of the number one.  (For example, ‘M-2 s ]’ increases a variable
 by 2, and ‘M-2 I s ]’ replaces a variable by minus-two minus the
 variable.
 
    The first six arithmetic stores can also be typed ‘s t +’, ‘s t -’,
 etc.  The commands ‘s s +’, ‘s s -’, and so on are analogous arithmetic
 stores that don’t remove the value ‘a’ from the stack.
 
    All arithmetic stores report the new value of the variable in the
 Trail for your information.  They signal an error if the variable
 previously had no stored value.  If default simplifications have been
 turned off, the arithmetic stores temporarily turn them on for numeric
 arguments only (i.e., they temporarily do an ‘m N’ command).  See
 Simplification Modes.  Large vectors put in the trail by these
 commands always use abbreviated (‘t .’) mode.
 
    The ‘s m’ command is a general way to adjust a variable’s value using
 any Calc function.  It is a “mapping” command analogous to ‘V M’, ‘V R’,
 etc.  SeeReducing and Mapping, to see how to specify a function for
 a mapping command.  Basically, all you do is type the Calc command key
 that would invoke that function normally.  For example, ‘s m n’ applies
 the ‘n’ key to negate the contents of the variable, so ‘s m n’ is
 equivalent to ‘s n’.  Also, ‘s m Q’ takes the square root of the value
 stored in a variable, ‘s m v v’ uses ‘v v’ to reverse the vector stored
 in the variable, and ‘s m H I S’ takes the hyperbolic arcsine of the
 variable contents.
 
    If the mapping function takes two or more arguments, the additional
 arguments are taken from the stack; the old value of the variable is
 provided as the first argument.  Thus ‘s m -’ with ‘a’ on the stack
 computes ‘v - a’, just like ‘s -’.  With the Inverse prefix, the
 variable’s original value becomes the _last_ argument instead of the
 first.  Thus ‘I s m -’ is also equivalent to ‘I s -’.
 
    The ‘s x’ (‘calc-store-exchange’) command exchanges the value of a
 variable with the value on the top of the stack.  Naturally, the
 variable must already have a stored value for this to work.
 
    You can type an equation or assignment at the ‘s x’ prompt.  The
 command ‘s x a=6’ takes no values from the stack; instead, it pushes the
 old value of ‘a’ on the stack and stores ‘a = 6’.
 
    Until you store something in them, most variables are “void,” that
 is, they contain no value at all.  If they appear in an algebraic
 formula they will be left alone even if you press ‘=’ (‘calc-evaluate’).
 The ‘s u’ (‘calc-unstore’) command returns a variable to the void state.
 
    The ‘s c’ (‘calc-copy-variable’) command copies the stored value of
 one variable to another.  One way it differs from a simple ‘s r’
 followed by an ‘s t’ (aside from saving keystrokes) is that the value
 never goes on the stack and thus is never rounded, evaluated, or
 simplified in any way; it is not even rounded down to the current
 precision.
 
    The only variables with predefined values are the “special constants”
 ‘pi’, ‘e’, ‘i’, ‘phi’, and ‘gamma’.  You are free to unstore these
 variables or to store new values into them if you like, although some of
 the algebraic-manipulation functions may assume these variables
 represent their standard values.  Calc displays a warning if you change
 the value of one of these variables, or of one of the other special
 variables ‘inf’, ‘uinf’, and ‘nan’ (which are normally void).
 
    Note that ‘pi’ doesn’t actually have 3.14159265359 stored in it, but
 rather a special magic value that evaluates to ‘pi’ at the current
 precision.  Likewise ‘e’, ‘i’, and ‘phi’ evaluate according to the
 current precision or polar mode.  If you recall a value from ‘pi’ and
 store it back, this magic property will be lost.  The magic property is
 preserved, however, when a variable is copied with ‘s c’.
 
    If one of the “special constants” is redefined (or undefined) so that
 it no longer has its magic property, the property can be restored with
 ‘s k’ (‘calc-copy-special-constant’).  This command will prompt for a
 special constant and a variable to store it in, and so a special
 constant can be stored in any variable.  Here, the special constant that
 you enter doesn’t depend on the value of the corresponding variable;
 ‘pi’ will represent 3.14159... regardless of what is currently stored in
 the Calc variable ‘pi’.  If one of the other special variables, ‘inf’,
 ‘uinf’ or ‘nan’, is given a value, its original behavior can be restored
 by voiding it with ‘s u’.