17.4 Programming with Formulas

Another way to create a new Calculator command uses algebraic formulas. The Z F (calc-user-define-formula) command stores the formula at the top of the stack as the definition for a key. This command prompts for five things: The key, the command name, the function name, the argument list, and the behavior of the command when given non-numeric arguments.

For example, suppose we type ' a+2b RET to push the formula ‘a + 2*b’ onto the stack. We now type Z F m to define this formula on the z m key sequence. The next prompt is for a command name, beginning with ‘calc-’, which should be the long (M-x) form for the new command. If you simply press RET, a default name like calc-User-m will be constructed. In our example, suppose we enter spam RET to define the new command as calc-spam.

If you want to give the formula a long-style name only, you can press SPC or RET when asked which single key to use. For example Z F RET spam RET defines the new command as M-x calc-spam, with no keyboard equivalent.

The third prompt is for an algebraic function name. The default is to use the same name as the command name but without the ‘calc-’ prefix. (If this is of the form ‘User-m’, the hyphen is removed so it won’t be taken for a minus sign in algebraic formulas.) This is the name you will use if you want to enter your new function in an algebraic formula. Suppose we enter yow RET. Then the new function can be invoked by pushing two numbers on the stack and typing z m or x spam, or by entering the algebraic formula ‘yow(x,y)’.

The fourth prompt is for the function’s argument list. This is used to associate values on the stack with the variables that appear in the formula. The default is a list of all variables which appear in the formula, sorted into alphabetical order. In our case, the default would be ‘(a b)’. This means that, when the user types z m, the Calculator will remove two numbers from the stack, substitute these numbers for ‘a’ and ‘b’ (respectively) in the formula, then simplify the formula and push the result on the stack. In other words, 10 RET 100 z m would replace the 10 and 100 on the stack with the number 210, which is ‘a + 2 b’ with ‘a=10’ and ‘b=100’. Likewise, the formula ‘yow(10, 100)’ will be evaluated by substituting ‘a=10’ and ‘b=100’ in the definition.

You can rearrange the order of the names before pressing RET to control which stack positions go to which variables in the formula. If you remove a variable from the argument list, that variable will be left in symbolic form by the command. Thus using an argument list of ‘(b)’ for our function would cause 10 z m to replace the 10 on the stack with the formula ‘a + 20’. If we had used an argument list of ‘(b a)’, the result with inputs 10 and 100 would have been 120.

You can also put a nameless function on the stack instead of just a formula, as in ‘<a, b : a + 2 b>’. See Specifying Operators. In this example, the command will be defined by the formula ‘a + 2 b’ using the argument list ‘(a b)’.

The final prompt is a y-or-n question concerning what to do if symbolic arguments are given to your function. If you answer y, then executing z m (using the original argument list ‘(a b)’) with arguments ‘10’ and ‘x’ will leave the function in symbolic form, i.e., ‘yow(10,x)’. On the other hand, if you answer n, then the formula will always be expanded, even for non-constant arguments: ‘10 + 2 x’. If you never plan to feed algebraic formulas to your new function, it doesn’t matter how you answer this question.

If you answered y to this question you can still cause a function call to be expanded by typing a " (calc-expand-formula). Also, Calc will expand the function if necessary when you take a derivative or integral or solve an equation involving the function.

Once you have defined a formula on a key, you can retrieve this formula with the Z G (calc-user-define-get-defn) command. Press a key, and this command pushes the formula that was used to define that key onto the stack. Actually, it pushes a nameless function that specifies both the argument list and the defining formula. You will get an error message if the key is undefined, or if the key was not defined by a Z F command.

The Z E (calc-user-define-edit) command on a key that has been defined by a formula uses a variant of the calc-edit command to edit the defining formula. Press C-c C-c to finish editing and store the new formula back in the definition, or kill the buffer with C-x k to cancel the edit. (The argument list and other properties of the definition are unchanged; to adjust the argument list, you can use Z G to grab the function onto the stack, edit with `, and then re-execute the Z F command.)

As usual, the Z P command records your definition permanently. In this case it will permanently record all three of the relevant definitions: the key, the command, and the function.

You may find it useful to turn off the default simplifications with m O (calc-no-simplify-mode) when entering a formula to be used as a function definition. For example, the formula ‘deriv(a^2,v)’ which might be used to define a new function ‘dsqr(a,v)’ will be “simplified” to 0 immediately upon entry since deriv considers ‘a’ to be constant with respect to ‘v’. Turning off default simplifications cures this problem: The definition will be stored in symbolic form without ever activating the deriv function. Press m D to turn the default simplifications back on afterwards.