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

14. Storing and Recalling

Calculator variables are really just Lisp variables that contain numbers or formulas in a form that Calc can understand. The commands in this section allow you to manipulate variables conveniently. Commands related to variables use the s prefix key.

14.1 Storing Variables  
14.2 Recalling Variables  
14.3 Other Operations on Variables  
14.4 The Let Command  
14.5 The Evaluates-To Operator  

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

14.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 8.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 11.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] [ ? ]

14.2 Recalling Variables

The most straightforward way to extract the stored value from a variable is to use the s r (calc-recall) command. This command prompts for a variable name (similarly to calc-store), looks up the value of the specified variable, and pushes that value onto the stack. It is an error to try to recall a void variable.

It is also possible to recall the value from a variable by evaluating a formula containing that variable. For example, ' a RET = is the same as s r a RET except that if the variable is void, the former will simply leave the formula `a' on the stack whereas the latter will produce an error message.

The r prefix may be followed by a digit, so that r 9 is equivalent to s r 9. (The r prefix is otherwise unused in the current version of Calc.)

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

14.3 Other Operations on Variables

The s e (calc-edit-variable) command edits the stored value of a variable without ever putting that value on the stack or simplifying or evaluating the value. It prompts for the name of the variable to edit. If the variable has no stored value, the editing buffer will start out empty. If the editing buffer is empty when you press M-# M-# to finish, the variable will be made void. See section 7.2 Editing Stack Entries, for a general description of editing.

The s e command is especially useful for creating and editing rewrite rules which are stored in variables. Sometimes these rules contain formulas which must not be evaluated until the rules are actually used. (For example, they may refer to `deriv(x,y)', where x will someday become some expression involving y; if you let Calc evaluate the rule while you are defining it, Calc will replace `deriv(x,y)' with 0 because the formula x does not itself refer to y.) By contrast, recalling the variable, editing with `, and storing will evaluate the variable's value as a side effect of putting the value on the stack.

There are several special-purpose variable-editing commands that use the s prefix followed by a shifted letter:

s A
Edit AlgSimpRules. See section 12.3.2 Algebraic Simplifications.
s D
Edit Decls. See section 8.6 Declarations.
s E
Edit EvalRules. See section 12.3.1 Default Simplifications.
s F
Edit FitRules. See section 12.8 Curve Fitting.
s G
Edit GenCount. See section 12.6 Solving Equations.
s H
Edit Holidays. See section 9.5.3 Business Days.
s I
Edit IntegLimit. See section 12.5 Calculus.
s L
Edit LineStyles. See section 15. Graphics.
s P
Edit PointStyles. See section 15. Graphics.
s R
Edit PlotRejects. See section 15. Graphics.
s T
Edit TimeZone. See section 9.5.4 Time Zones.
s U
Edit Units. See section 13.4 User-Defined Units.
s X
Edit ExtSimpRules. See section 12.3.3 "Unsafe" Simplifications.

These commands are just versions of s e that use fixed variable names rather than prompting for the variable name.

The s p (calc-permanent-variable) command saves a variable's value permanently in your `.emacs' file, so that its value will still be available in future Emacs sessions. You can re-execute s p later on to update the saved value, but the only way to remove a saved variable is to edit your `.emacs' file by hand. (See section 8.1 General Mode Commands, for a way to tell Calc to use a different file instead of `.emacs'.)

If you do not specify the name of a variable to save (i.e., s p RET), all `var-' variables with defined values are saved except for the special constants pi, e, i, phi, and gamma; the variables TimeZone and PlotRejects; FitRules, DistribRules, and other built-in rewrite rules; and PlotDatan variables generated by the graphics commands. (You can still save these variables by explicitly naming them in an s p command.)

The s i (calc-insert-variables) command writes the values of all `var-' variables into a specified buffer. The variables are written in the form of Lisp setq commands which store the values in string form. You can place these commands in your `.emacs' buffer if you wish, though in this case it would be easier to use s p RET. (Note that s i omits the same set of variables as s p RET; the difference is that s i will store the variables in any buffer, and it also stores in a more human-readable format.)

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

14.4 The Let Command

If you have an expression like `a+b^2' on the stack and you wish to compute its value where b=3, you can simply store 3 in b and then press = to reevaluate the formula. This has the side-effect of leaving the stored value of 3 in b for future operations.

The s l (calc-let) command evaluates a formula under a temporary assignment of a variable. It stores the value on the top of the stack into the specified variable, then evaluates the second-to-top stack entry, then restores the original value (or lack of one) in the variable. Thus after ' a+b^2 RET 3 s l b RET, the stack will contain the formula `a + 9'. The subsequent command 5 s l a RET will replace this formula with the number 14. The variables `a' and `b' are not permanently affected in any way by these commands.

The value on the top of the stack may be an equation or assignment, or a vector of equations or assignments, in which case the default will be analogous to the case of s t RET. See section 14.1 Storing Variables.

Also, you can answer the variable-name prompt with an equation or assignment: s l b=3 RET is the same as storing 3 on the stack and typing s l b RET.

The a b (calc-substitute) command is another way to substitute a variable with a value in a formula. It does an actual substitution rather than temporarily assigning the variable and evaluating. For example, letting n=2 in `f(n pi)' with a b will produce `f(2 pi)', whereas s l would give `f(6.28)' since the evaluation step will also evaluate pi.

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

14.5 The Evaluates-To Operator

The special algebraic symbol `=>' is known as the evaluates-to operator. (It will show up as an evalto function call in other language modes like Pascal and TeX.) This is a binary operator, that is, it has a lefthand and a righthand argument, although it can be entered with the righthand argument omitted.

A formula like `a => b' is evaluated by Calc as follows: First, a is not simplified or modified in any way. The previous value of argument b is thrown away; the formula a is then copied and evaluated as if by the = command according to all current modes and stored variable values, and the result is installed as the new value of b.

For example, suppose you enter the algebraic formula `2 + 3 => 17'. The number 17 is ignored, and the lefthand argument is left in its unevaluated form; the result is the formula `2 + 3 => 5'.

You can enter an `=>' formula either directly using algebraic entry (in which case the righthand side may be omitted since it is going to be replaced right away anyhow), or by using the s = (calc-evalto) command, which takes a from the stack and replaces it with `a => b'.

Calc keeps track of all `=>' operators on the stack, and recomputes them whenever anything changes that might affect their values, i.e., a mode setting or variable value. This occurs only if the `=>' operator is at the top level of the formula, or if it is part of a top-level vector. In other words, pushing `2 + (a => 17)' will change the 17 to the actual value of `a' when you enter the formula, but the result will not be dynamically updated when `a' is changed later because the `=>' operator is buried inside a sum. However, a vector of `=>' operators will be recomputed, since it is convenient to push a vector like `[a =>, b =>, c =>]' on the stack to make a concise display of all the variables in your problem. (Another way to do this would be to use `[a, b, c] =>', which provides a slightly different format of display. You can use whichever you find easiest to read.)

The m C (calc-auto-recompute) command allows you to turn this automatic recomputation on or off. If you turn recomputation off, you must explicitly recompute an `=>' operator on the stack in one of the usual ways, such as by pressing =. Turning recomputation off temporarily can save a lot of time if you will be changing several modes or variables before you look at the `=>' entries again.

Most commands are not especially useful with `=>' operators as arguments. For example, given `x + 2 => 17', it won't work to type 1 + to get `x + 3 => 18'. If you want to operate on the lefthand side of the `=>' operator on the top of the stack, type j 1 (that's the digit "one") to select the lefthand side, execute your commands, then type j u to unselect.

All current modes apply when an `=>' operator is computed, including the current simplification mode. Recall that the formula `x + y + x' is not handled by Calc's default simplifications, but the a s command will reduce it to the simpler form `y + 2 x'. You can also type m A to enable an algebraic-simplification mode in which the equivalent of a s is used on all of Calc's results. If you enter `x + y + x =>' normally, the result will be `x + y + x => x + y + x'. If you change to algebraic-simplification mode, the result will be `x + y + x => y + 2 x'. However, just pressing a s once will have no effect on `x + y + x => x + y + x', because the righthand side depends only on the lefthand side and the current mode settings, and the lefthand side is not affected by commands like a s.

The "let" command (s l) has an interesting interaction with the `=>' operator. The s l command evaluates the second-to-top stack entry with the top stack entry supplying a temporary value for a given variable. As you might expect, if that stack entry is an `=>' operator its righthand side will temporarily show this value for the variable. In fact, all `=>'s on the stack will be updated if they refer to that variable. But this change is temporary in the sense that the next command that causes Calc to look at those stack entries will make them revert to the old variable value.

2:  a => a             2:  a => 17         2:  a => a
1:  a + 1 => a + 1     1:  a + 1 => 18     1:  a + 1 => a + 1
    .                      .                   .

                           17 s l a RET        p 8 RET

Here the p 8 command changes the current precision, thus causing the `=>' forms to be recomputed after the influence of the "let" is gone. The d SPC command (calc-refresh) is a handy way to force the `=>' operators on the stack to be recomputed without any other side effects.

Embedded Mode also uses `=>' operators. In embedded mode, the lefthand side of an `=>' operator can refer to variables assigned elsewhere in the file by `:=' operators. The assignment operator `a := 17' does not actually do anything by itself. But Embedded Mode recognizes it and marks it as a sort of file-local definition of the variable. You can enter `:=' operators in algebraic mode, or by using the s : (calc-assign) [assign] command which takes a variable and value from the stack and replaces them with an assignment.

See section 8.8.3 TeX Language Mode, for the way `=>' appears in TeX language output. The eqn mode gives similar treatment to `=>'.

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

This document was generated by XEmacs Webmaster on October, 2 2007 using texi2html