Previous: Variables, Up: Data Types

### 4.15 Formulas

When you press the apostrophe key you may enter any expression or formula in algebraic form. (Calc uses the terms “expression” and “formula” interchangeably.) An expression is built up of numbers, variable names, and function calls, combined with various arithmetic operators. Parentheses may be used to indicate grouping. Spaces are ignored within formulas, except that spaces are not permitted within variable names or numbers. Arithmetic operators, in order from highest to lowest precedence, and with their equivalent function names, are:

_’ [subscr] (subscripts);

postfix ‘%’ [percent] (as in ‘25% = 0.25’);

prefix ‘!’ [lnot] (logical “not,” as in ‘!x’);

+/-’ [sdev] (the standard deviation symbol) and ‘mod’ [makemod] (the symbol for modulo forms);

postfix ‘!’ [fact] (factorial, as in ‘n!’) and postfix ‘!!’ [dfact] (double factorial);

^’ [pow] (raised-to-the-power-of);

prefix ‘+’ and ‘-’ [neg] (as in ‘-x’);

*’ [mul];

/’ [div], ‘%’ [mod] (modulo), and ‘\’ [idiv] (integer division);

infix ‘+’ [add] and ‘-’ [sub] (as in ‘x-y’);

|’ [vconcat] (vector concatenation);

relations ‘=’ [eq], ‘!=’ [neq], ‘<’ [lt], ‘>’ [gt], ‘<=’ [leq], and ‘>=’ [geq];

&&’ [land] (logical “and”);

||’ [lor] (logical “or”);

the C-style “if” operator ‘a?b:c’ [if];

!!!’ [pnot] (rewrite pattern “not”);

&&&’ [pand] (rewrite pattern “and”);

|||’ [por] (rewrite pattern “or”);

:=’ [assign] (for assignments and rewrite rules);

::’ [condition] (rewrite pattern condition);

=>’ [evalto].

Note that, unlike in usual computer notation, multiplication binds more strongly than division: ‘a*b/c*d’ is equivalent to (a*b)/(c*d)’.

The multiplication sign ‘*’ may be omitted in many cases. In particular, if the righthand side is a number, variable name, or parenthesized expression, the ‘*’ may be omitted. Implicit multiplication has the same precedence as the explicit ‘*’ operator. The one exception to the rule is that a variable name followed by a parenthesized expression, as in ‘f(x)’, is interpreted as a function call, not an implicit ‘*’. In many cases you must use a space if you omit the ‘*’: ‘2a’ is the same as ‘2*a’, and ‘a b’ is the same as ‘a*b’, but ‘ab’ is a variable called ab, not the product of ‘a’ and ‘b’! Also note that ‘f (x)’ is still a function call.

The rules are slightly different for vectors written with square brackets. In vectors, the space character is interpreted (like the comma) as a separator of elements of the vector. Thus ‘[ 2a b+c d ] is equivalent to ‘[2*a, b+c, d]’, whereas ‘2a b+c d’ is equivalent to ‘2*a*b + c*d’. Note that spaces around the brackets, and around explicit commas, are ignored. To force spaces to be interpreted as multiplication you can enclose a formula in parentheses as in ‘[(a b) 2(c d)]’, which is interpreted as ‘[a*b, 2*c*d]’. An implicit comma is also inserted between ‘][’, as in the matrix ‘[[1 2][3 4]]’.

Vectors that contain commas (not embedded within nested parentheses or brackets) do not treat spaces specially: ‘[a b, 2 c d]’ is a vector of two elements. Also, if it would be an error to treat spaces as separators, but not otherwise, then Calc will ignore spaces: ‘[a - b] is a vector of one element, but ‘[a -b] is a vector of two elements. Finally, vectors entered with curly braces instead of square brackets do not give spaces any special treatment. When Calc displays a vector that does not contain any commas, it will insert parentheses if necessary to make the meaning clear: ‘[(a b)].

The expression ‘5%-2’ is ambiguous; is this five-percent minus two, or five modulo minus-two? Calc always interprets the leftmost symbol as an infix operator preferentially (modulo, in this case), so you would need to write ‘(5%)-2’ to get the former interpretation.

A function call is, e.g., ‘sin(1+x)’. (The Calc algebraic function foo corresponds to the Emacs Lisp function calcFunc-foo, but unless you access the function from within Emacs Lisp, you don't need to worry about it.) Most mathematical Calculator commands like calc-sin have function equivalents like sin. If no Lisp function is defined for a function called by a formula, the call is left as it is during algebraic manipulation: ‘f(x+y)’ is left alone. Beware that many innocent-looking short names like in and re have predefined meanings which could surprise you; however, single letters or single letters followed by digits are always safe to use for your own function names. See Function Index.

In the documentation for particular commands, the notation H S (calc-sinh) [sinh] means that the key sequence H S, the command M-x calc-sinh, and the algebraic function sinh(x) all represent the same operation.

Commands that interpret (“parse”) text as algebraic formulas include algebraic entry ('), editing commands like  which parse the contents of the editing buffer when you finish, the C-x * g and C-x * r commands, the C-y command, the X window system “paste” mouse operation, and Embedded mode. All of these operations use the same rules for parsing formulas; in particular, language modes (see Language Modes) affect them all in the same way.

When you read a large amount of text into the Calculator (say a vector which represents a big set of rewrite rules; see Rewrite Rules), you may wish to include comments in the text. Calc's formula parser ignores the symbol ‘%%’ and anything following it on a line:

     [ a + b,   %% the sum of "a" and "b"
c + d,
%% last line is coming up:
e + f ]
`

This is parsed exactly the same as ‘[ a + b, c + d, e + f ]’.

See Syntax Tables, for a way to create your own operators and other input notations. See Compositions, for a way to create new display formats.

See Algebra, for commands for manipulating formulas symbolically.