www.delorie.com/gnu/docs/calc/calc_296.html   search  
Buy the book!

GNU Emacs Calc 2.02 Manual

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

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 var-q0 through var-q9. Or you can enter any variable name. The prefix `var-' is supplied for you; when a name appears in a formula (as in `a+q2') the prefix `var-' is also supplied there, so normally you can simply forget about `var-' everywhere. Its only purpose is to enable you to use Calc variables without fear of accidentally clobbering some variable in another Emacs package. If you really want to store in an arbitrary Lisp variable, just backspace over the `var-'.

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 section 7.5 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. See section 10.8 Reducing and Mapping Vectors, 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, 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 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 var-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 var-e, var-i, and var-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 s c (calc-copy-variable) command copies the stored value of one variable to another. It differs from a simple s r followed by an s t in two important ways. First, 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. Second, the "magic" contents of a variable like var-e can be copied into another variable with this command, perhaps because you need to unstore var-e right now but you wish to put it back when you're done. The s c command is the only way to manipulate these magic values intact.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003