Go to the first, previous, next, last section, table of contents.

User-Defined Compositions

The Z C (calc-user-define-composition) command lets you define the display format for any algebraic function. You provide a formula containing a certain number of argument variables on the stack. Any time Calc formats a call to the specified function in the current language mode and with that number of arguments, Calc effectively replaces the function call with that formula with the arguments replaced.

Calc builds the default argument list by sorting all the variable names that appear in the formula into alphabetical order. You can edit this argument list before pressing RET if you wish. Any variables in the formula that do not appear in the argument list will be displayed literally; any arguments that do not appear in the formula will not affect the display at all.

You can define formats for built-in functions, for functions you have defined with Z F (see section Programming with Formulas), or for functions which have no definitions but are being used as purely syntactic objects. You can define different formats for each language mode, and for each number of arguments, using a succession of Z C commands. When Calc formats a function call, it first searches for a format defined for the current language mode (and number of arguments); if there is none, it uses the format defined for the Normal language mode. If neither format exists, Calc uses its built-in standard format for that function (usually just `func(args)').

If you execute Z C with the number 0 on the stack instead of a formula, any defined formats for the function in the current language mode will be removed. The function will revert to its standard format.

For example, the default format for the binomial coefficient function `choose(n, m)' in the Big language mode is

 n
( )
 m

You might prefer the notation,

 C
n m

To define this notation, first make sure you are in Big mode, then put the formula

choriz([cvert([cvspace(1), n]), C, cvert([cvspace(1), m])])

on the stack and type Z C. Answer the first prompt with choose. The second prompt will be the default argument list of `(C m n)'. Edit this list to be `(n m)' and press RET. Now, try it out: For example, turn simplification off with m O and enter `choose(a,b) + choose(7,3)' as an algebraic entry.

 C  +  C 
a b   7 3

As another example, let's define the usual notation for Stirling numbers of the first kind, `stir1(n, m)'. This is just like the regular format for binomial coefficients but with square brackets instead of parentheses.

choriz([string("["), cvert([n, cbase(cvspace(1)), m]), string("]")])

Now type Z C stir1 RET, edit the argument list to `(n m)', and type RET.

The formula provided to Z C usually will involve composition functions, but it doesn't have to. Putting the formula `a + b + c' onto the stack and typing Z C foo RET RET would define the function `foo(x,y,z)' to display like `x + y + z'. This "sum" will act exactly like a real sum for all formatting purposes (it will be parenthesized the same, and so on). However it will be computationally unrelated to a sum. For example, the formula `2 * foo(1, 2, 3)' will display as `2 (1 + 2 + 3)'. Operator precedences have caused the "sum" to be written in parentheses, but the arguments have not actually been summed. (Generally a display format like this would be undesirable, since it can easily be confused with a real sum.)

The special function eval can be used inside a Z C composition formula to cause all or part of the formula to be evaluated at display time. For example, if the formula is `a + eval(b + c)', then `foo(1, 2, 3)' will be displayed as `1 + 5'. Evaluation will use the default simplifications, regardless of the current simplification mode. There are also evalsimp and evalextsimp which simplify as if by a s and a e (respectively). Note that these "functions" operate only in the context of composition formulas (and also in rewrite rules, where they serve a similar purpose; see section Rewrite Rules). On the stack, a call to eval will be left in symbolic form.

It is not a good idea to use eval except as a last resort. It can cause the display of formulas to be extremely slow. For example, while `eval(a + b)' might seem quite fast and simple, there are several situations where it could be slow. For example, `a' and/or `b' could be polar complex numbers, in which case doing the sum requires trigonometry. Or, `a' could be the factorial `fact(100)' which is unevaluated because you have typed m O; eval will evaluate it anyway to produce a large, unwieldy integer.

You can save your display formats permanently using the Z P command (see section Creating User Keys).


Go to the first, previous, next, last section, table of contents.