2 * Introduction to operators::
3 * Arithmetic operators::
4 * Relational operators::
6 * Operators for Equations::
7 * Assignment operators::
8 * User defined operators::
11 @c -----------------------------------------------------------------------------
12 @node Introduction to operators, Arithmetic operators, Operators, Operators
13 @section Introduction to operators
14 @c -----------------------------------------------------------------------------
16 It is possible to define new operators with specified precedence, to undefine
17 existing operators, or to redefine the precedence of existing operators. An
18 operator may be unary prefix or unary postfix, binary infix, n-ary infix,
19 matchfix, or nofix. "Matchfix" means a pair of symbols which enclose their
20 argument or arguments, and "nofix" means an operator which takes no arguments.
21 As examples of the different types of operators, there are the following.
29 exponentiation @code{a^b}
33 list construction @code{[a, b]}
36 (There are no built-in nofix operators; for an example of such an operator,
39 The mechanism to define a new operator is straightforward. It is only necessary
40 to declare a function as an operator; the operator function might or might not
43 An example of user-defined operators is the following. Note that the explicit
44 function call @code{"dd" (a)} is equivalent to @code{dd a}, likewise
45 @code{"<-" (a, b)} is equivalent to @code{a <- b}. Note also that the functions
46 @code{"dd"} and @code{"<-"} are undefined in this example.
54 @c "<-" (a, "dd" (b));
67 (%i6) "<-" (a, "dd" (b));
71 The Maxima functions which define new operators are summarized in this table,
72 stating the default left and right binding powers (lbp and rbp, respectively).
73 @c REWORK FOLLOWING COMMENT.
74 @c IT'S NOT CLEAR ENOUGH, GIVEN THAT IT'S FAIRLY IMPORTANT
75 (Binding power determines operator precedence. However, since left and right
76 binding powers can differ, binding power is somewhat more complicated than
77 precedence.) Some of the operation definition functions take additional
78 arguments; see the function descriptions for details.
80 @c MAKE ANCHORS FOR ALL 6 FUNCTIONS AND CHANGE @code TO @ref ACCORDINGLY
91 (binding power not applicable)
93 (binding power not applicable)
96 For comparison, here are some built-in operators and their left and right
128 @mref{remove} and @mref{kill} remove operator properties from an atom.
129 @code{remove ("@var{a}", op)} removes only the operator properties of @var{a}.
130 @code{kill ("@var{a}")} removes all properties of @var{a}, including the
131 operator properties. Note that the name of the operator must be enclosed in
134 @c MAYBE COPY THIS EXAMPLE TO remove AND/OR kill
137 @c "##" (a, b) := a^b;
139 @c remove ("##", op);
151 (%i2) "##" (a, b) := a^b;
156 (%i4) remove ("##", op);
159 Incorrect syntax: # is not a prefix operator
171 Incorrect syntax: # is not a prefix operator
178 @opencatbox{Categories:}
183 @c -----------------------------------------------------------------------------
184 @node Arithmetic operators, Relational operators, Introduction to operators, Operators
185 @section Arithmetic operators
186 @c -----------------------------------------------------------------------------
188 @c -----------------------------------------------------------------------------
196 @fnindex Multiplication
198 @fnindex Exponentiation
206 The symbols @code{+} @code{*} @code{/} and @code{^} represent addition,
207 multiplication, division, and exponentiation, respectively. The names of these
208 operators are @code{"+"} @code{"*"} @code{"/"} and @code{"^"}, which may appear
209 where the name of a function or operator is required.
211 The symbols @code{+} and @code{-} represent unary addition and negation,
212 respectively, and the names of these operators are @code{"+"} and @code{"-"},
215 Subtraction @code{a - b} is represented within Maxima as addition,
216 @code{a + (- b)}. Expressions such as @code{a + (- b)} are displayed as
217 subtraction. Maxima recognizes @code{"-"} only as the name of the unary
218 negation operator, and not as the name of the binary subtraction operator.
220 Division @code{a / b} is represented within Maxima as multiplication,
221 @code{a * b^(- 1)}. Expressions such as @code{a * b^(- 1)} are displayed as
222 division. Maxima recognizes @code{"/"} as the name of the division operator.
224 Addition and multiplication are n-ary, commutative operators.
225 Division and exponentiation are binary, noncommutative operators.
227 Maxima sorts the operands of commutative operators to construct a canonical
228 representation. For internal storage, the ordering is determined by
229 @mrefdot{orderlessp} For display, the ordering for addition is determined by
230 @mrefcomma{ordergreatp} and for multiplication, it is the same as the internal
233 Arithmetic computations are carried out on literal numbers (integers, rationals,
234 ordinary floats, and bigfloats). Except for exponentiation, all arithmetic
235 operations on numbers are simplified to numbers. Exponentiation is simplified
236 to a number if either operand is an ordinary float or bigfloat or if the result
237 is an exact integer or rational; otherwise an exponentiation may be simplified
238 to @mref{sqrt} or another exponentiation or left unchanged.
240 Floating-point contagion applies to arithmetic computations: if any operand is
241 a bigfloat, the result is a bigfloat; otherwise, if any operand is an ordinary
242 float, the result is an ordinary float; otherwise, the operands are rationals
243 or integers and the result is a rational or integer.
245 Arithmetic computations are a simplification, not an evaluation.
246 Thus arithmetic is carried out in quoted (but simplified) expressions.
248 Arithmetic operations are applied element-by-element to lists when the global
249 flag @mref{listarith} is @code{true}, and always applied element-by-element to
250 matrices. When one operand is a list or matrix and another is an operand of
251 some other type, the other operand is combined with each of the elements of the
256 Addition and multiplication are n-ary, commutative operators.
257 Maxima sorts the operands to construct a canonical representation.
258 The names of these operators are @code{"+"} and @code{"*"}.
261 @c c + g + d + a + b + e + f;
262 @c [op (%), args (%)];
263 @c c * g * d * a * b * e * f;
264 @c [op (%), args (%)];
265 @c apply ("+", [a, 8, x, 2, 9, x, x, a]);
266 @c apply ("*", [a, 8, x, 2, 9, x, x, a]);
269 (%i1) c + g + d + a + b + e + f;
270 (%o1) g + f + e + d + c + b + a
271 (%i2) [op (%), args (%)];
272 (%o2) [+, [g, f, e, d, c, b, a]]
273 (%i3) c * g * d * a * b * e * f;
275 (%i4) [op (%), args (%)];
276 (%o4) [*, [a, b, c, d, e, f, g]]
277 (%i5) apply ("+", [a, 8, x, 2, 9, x, x, a]);
279 (%i6) apply ("*", [a, 8, x, 2, 9, x, x, a]);
284 Division and exponentiation are binary, noncommutative operators.
285 The names of these operators are @code{"/"} and @code{"^"}.
289 @c [map (op, %), map (args, %)];
290 @c [apply ("/", [a, b]), apply ("^", [a, b])];
293 (%i1) [a / b, a ^ b];
297 (%i2) [map (op, %), map (args, %)];
298 (%o2) [[/, ^], [[a, b], [a, b]]]
299 (%i3) [apply ("/", [a, b]), apply ("^", [a, b])];
305 Subtraction and division are represented internally
306 in terms of addition and multiplication, respectively.
309 @c [inpart (a - b, 0), inpart (a - b, 1), inpart (a - b, 2)];
310 @c [inpart (a / b, 0), inpart (a / b, 1), inpart (a / b, 2)];
313 (%i1) [inpart (a - b, 0), inpart (a - b, 1), inpart (a - b, 2)];
315 (%i2) [inpart (a / b, 0), inpart (a / b, 1), inpart (a / b, 2)];
321 Computations are carried out on literal numbers.
322 Floating-point contagion applies.
325 @c 17 + b - (1/2)*29 + 11^(2/4);
326 @c [17 + 29, 17 + 29.0, 17 + 29b0];
329 (%i1) 17 + b - (1/2)*29 + 11^(2/4);
331 (%o1) b + sqrt(11) + -
333 (%i2) [17 + 29, 17 + 29.0, 17 + 29b0];
334 (%o2) [46, 46.0, 4.6b1]
337 Arithmetic computations are a simplification, not an evaluation.
341 @c '(17 + 29*11/7 - 5^3);
343 @c '(17 + 29*11/7 - 5^3);
348 (%i2) '(17 + 29*11/7 - 5^3);
354 (%i4) '(17 + 29*11/7 - 5^3);
360 Arithmetic is carried out element-by-element for lists (depending on
361 @code{listarith}) and matrices.
364 @c matrix ([a, x], [h, u]) - matrix ([1, 2], [3, 4]);
365 @c 5 * matrix ([a, x], [h, u]);
366 @c listarith : false;
367 @c [a, c, m, t] / [1, 7, 2, 9];
370 @c [a, c, m, t] / [1, 7, 2, 9];
374 (%i1) matrix ([a, x], [h, u]) - matrix ([1, 2], [3, 4]);
380 (%i2) 5 * matrix ([a, x], [h, u]);
384 (%i3) listarith : false;
386 (%i4) [a, c, m, t] / [1, 7, 2, 9];
390 (%i5) [a, c, m, t] ^ x;
393 (%i6) listarith : true;
395 (%i7) [a, c, m, t] / [1, 7, 2, 9];
399 (%i8) [a, c, m, t] ^ x;
401 (%o8) [a , c , m , t ]
404 @opencatbox{Categories:}
409 @c -----------------------------------------------------------------------------
413 Exponentiation operator.
414 Maxima recognizes @code{**} as the same operator as @mref{^} in input,
415 and it is displayed as @code{^} in 1-dimensional output,
416 or by placing the exponent as a superscript in 2-dimensional output.
418 The @mref{fortran} function displays the exponentiation operator as @code{**},
419 whether it was input as @code{**} or @code{^}.
426 @c string (x**y + x^z);
427 @c fortran (x**y + x^z);
430 (%i1) is (a**b = a^b);
435 (%i3) string (x**y + x^z);
437 (%i4) fortran (x**y + x^z);
442 @opencatbox{Categories:}
447 @c -----------------------------------------------------------------------------
449 @fnindex Noncommutative exponentiation
452 Noncommutative exponentiation operator.
453 @code{^^} is the exponentiation operator corresponding to noncommutative
454 multiplication @code{.}, just as the ordinary exponentiation operator @code{^}
455 corresponds to commutative multiplication @code{*}.
457 Noncommutative exponentiation is displayed by @code{^^} in 1-dimensional output,
458 and by placing the exponent as a superscript within angle brackets @code{< >}
459 in 2-dimensional output.
464 @c a . a . b . b . b + a * a * a * b * b;
465 @c string (a . a . b . b . b + a * a * a * b * b);
468 (%i1) a . a . b . b . b + a * a * a * b * b;
471 (%i2) string (a . a . b . b . b + a * a * a * b * b);
472 (%o2) a^3*b^2+a^^2 . b^^3
475 @opencatbox{Categories:}
480 @c -----------------------------------------------------------------------------
484 The dot operator, for matrix (non-commutative) multiplication.
485 When @code{"."} is used in this way, spaces should be left on both sides of
486 it, e.g. @code{A . B} This distinguishes it plainly from a decimal point in
487 a floating point number.
491 @mrefcomma{dot0nscsimp}
495 @mrefcomma{dotconstrules}
496 @mrefcomma{dotdistrib}
497 @mrefcomma{dotexptsimp}
502 @opencatbox{Categories:}
507 @c -----------------------------------------------------------------------------
508 @node Relational operators, Logical operators, Arithmetic operators, Operators
509 @section Relational operators
510 @c -----------------------------------------------------------------------------
512 @c -----------------------------------------------------------------------------
518 @fnindex Less than or equal
519 @fnindex Greater than or equal
520 @fnindex Greater than
523 @deffnx {Operator} <=
524 @deffnx {Operator} >=
527 The symbols @code{<} @code{<=} @code{>=} and @code{>} represent less than, less
528 than or equal, greater than or equal, and greater than, respectively. The names
529 of these operators are @code{"<"} @code{"<="} @code{">="} and @code{">"}, which
530 may appear where the name of a function or operator is required.
532 These relational operators are all binary operators; constructs such as
533 @code{a < b < c} are not recognized by Maxima.
535 Relational expressions are evaluated to Boolean values by the functions
536 @mref{is} and @mrefcomma{maybe} and the programming constructs
537 @mrefcomma{if} @mrefcomma{while} and @mrefdot{unless} Relational expressions
538 are not otherwise evaluated or simplified to Boolean values, although the
539 arguments of relational expressions are evaluated (when evaluation is not
540 otherwise prevented by quotation).
542 When a relational expression cannot be evaluated to @code{true} or @code{false},
543 the behavior of @code{is} and @code{if} are governed by the global flag
544 @mrefdot{prederror} When @code{prederror} is @code{true}, @code{is} and
545 @code{if} trigger an error. When @code{prederror} is @code{false}, @code{is}
546 returns @code{unknown}, and @code{if} returns a partially-evaluated conditional
549 @code{maybe} always behaves as if @code{prederror} were @code{false}, and
550 @code{while} and @code{unless} always behave as if @code{prederror} were
553 Relational operators do not distribute over lists or other aggregates.
555 See also @mrefcomma{=} @mrefcomma{#} @mrefcomma{equal} and @mrefdot{notequal}
559 Relational expressions are evaluated to Boolean values by some functions and
560 programming constructs.
563 @c [x, y, z] : [123, 456, 789];
566 @c if x >= z then 1 else 0;
567 @c block ([S], S : 0, for i:1 while i <= 100 do S : S + i,
571 (%i1) [x, y, z] : [123, 456, 789];
572 (%o1) [123, 456, 789]
577 (%i4) if x >= z then 1 else 0;
579 (%i5) block ([S], S : 0, for i:1 while i <= 100 do S : S + i,
584 Relational expressions are not otherwise evaluated or simplified to Boolean
585 values, although the arguments of relational expressions are evaluated.
588 @c [x, y, z] : [123, 456, 789];
589 @c [x < y, y <= z, z >= y, y > z];
593 (%o1) [123, 456, 789]
594 (%i2) [x < y, y <= z, z >= y, y > z];
595 (%o2) [123 < 456, 456 <= 789, 789 >= 456, 456 > 789]
597 (%o3) [true, true, true, false]
600 @opencatbox{Categories:}
605 @c -----------------------------------------------------------------------------
606 @node Logical operators, Operators for Equations, Relational operators, Operators
607 @section Logical operators
608 @c -----------------------------------------------------------------------------
612 @c -----------------------------------------------------------------------------
614 @fnindex Logical conjunction
615 @deffn {Operator} and
617 The logical conjunction operator. @code{and} is an n-ary infix operator;
618 its operands are Boolean expressions, and its result is a Boolean value.
620 @code{and} forces evaluation (like @mref{is}) of one or more operands,
621 and may force evaluation of all operands.
623 Operands are evaluated in the order in which they appear. @code{and} evaluates
624 only as many of its operands as necessary to determine the result. If any
625 operand is @code{false}, the result is @code{false} and no further operands are
628 The global flag @mref{prederror} governs the behavior of @code{and} when an
629 evaluated operand cannot be determined to be @code{true} or @code{false}.
630 @code{and} prints an error message when @code{prederror} is @code{true}.
631 Otherwise, operands which do not evaluate to @code{true} or @code{false} are
632 accepted, and the result is a Boolean expression.
634 @code{and} is not commutative: @code{a and b} might not be equal to
635 @code{b and a} due to the treatment of indeterminate operands.
637 @opencatbox{Categories:}
644 @c -----------------------------------------------------------------------------
646 @fnindex Logical negation
647 @deffn {Operator} not
649 The logical negation operator. @code{not} is a prefix operator;
650 its operand is a Boolean expression, and its result is a Boolean value.
652 @code{not} forces evaluation (like @code{is}) of its operand.
654 The global flag @mref{prederror} governs the behavior of @code{not} when its
655 operand cannot be determined to be @code{true} or @code{false}. @code{not}
656 prints an error message when @code{prederror} is @code{true}. Otherwise,
657 operands which do not evaluate to @code{true} or @code{false} are accepted,
658 and the result is a Boolean expression.
660 @opencatbox{Categories:}
667 @c -----------------------------------------------------------------------------
669 @fnindex Logical disjunction
672 The logical disjunction operator. @code{or} is an n-ary infix operator;
673 its operands are Boolean expressions, and its result is a Boolean value.
675 @code{or} forces evaluation (like @mref{is}) of one or more operands,
676 and may force evaluation of all operands.
678 Operands are evaluated in the order in which they appear. @code{or} evaluates
679 only as many of its operands as necessary to determine the result. If any
680 operand is @code{true}, the result is @code{true} and no further operands are
683 The global flag @mref{prederror} governs the behavior of @code{or} when an
684 evaluated operand cannot be determined to be @code{true} or @code{false}.
685 @code{or} prints an error message when @code{prederror} is @code{true}.
686 Otherwise, operands which do not evaluate to @code{true} or @code{false} are
687 accepted, and the result is a Boolean expression.
689 @code{or} is not commutative: @code{a or b} might not be equal to @code{b or a}
690 due to the treatment of indeterminate operands.
692 @opencatbox{Categories:}
697 @c -----------------------------------------------------------------------------
698 @node Operators for Equations, Assignment operators, Logical operators, Operators
699 @section Operators for Equations
700 @c -----------------------------------------------------------------------------
702 @c -----------------------------------------------------------------------------
704 @fnindex Not equal (syntactic inequality)
707 Represents the negation of syntactic equality @mrefdot{=}
709 Note that because of the rules for evaluation of predicate expressions
710 (in particular because @code{not @var{expr}} causes evaluation of @var{expr}),
711 @code{not @var{a} = @var{b}} is equivalent to @code{is(@var{a} # @var{b})},
712 instead of @code{@var{a} # @var{b}}.
735 (%i6) is (not a = b);
739 @opencatbox{Categories:}
744 @c -----------------------------------------------------------------------------
746 @fnindex Equation operator
747 @fnindex Equal (syntactic equality)
750 The equation operator.
752 An expression @code{@var{a} = @var{b}}, by itself, represents an unevaluated
753 equation, which might or might not hold. Unevaluated equations may appear as
754 arguments to @mref{solve} and @mref{algsys} or some other functions.
756 The function @mref{is} evaluates @code{=} to a Boolean value.
757 @code{is(@var{a} = @var{b})} evaluates @code{@var{a} = @var{b}} to @code{true}
758 when @var{a} and @var{b} are identical. That is, @var{a} and @var{b} are atoms
759 which are identical, or they are not atoms and their operators are identical and
760 their arguments are identical. Otherwise, @code{is(@var{a} = @var{b})}
761 evaluates to @code{false}; it never evaluates to @code{unknown}. When
762 @code{is(@var{a} = @var{b})} is @code{true}, @var{a} and @var{b} are said to be
763 syntactically equal, in contrast to equivalent expressions, for which
764 @code{is(equal(@var{a}, @var{b}))} is @code{true}. Expressions can be
765 equivalent and not syntactically equal.
767 The negation of @code{=} is represented by @mrefdot{#}
768 As with @code{=}, an expression @code{@var{a} # @var{b}}, by itself, is not
769 evaluated. @code{is(@var{a} # @var{b})} evaluates @code{@var{a} # @var{b}} to
770 @code{true} or @code{false}.
772 In addition to @code{is}, some other operators evaluate @code{=} and @code{#}
773 to @code{true} or @code{false}, namely @mrefcomma{if} @mrefcomma{and}@w{}
774 @mrefcomma{or} and @mrefdot{not}
776 Note that because of the rules for evaluation of predicate expressions
777 (in particular because @code{not @var{expr}} causes evaluation of @var{expr}),
778 @code{not @var{a} = @var{b}} is equivalent to @code{is(@var{a} # @var{b})},
779 instead of @code{@var{a} # @var{b}}.
781 @mref{rhs} and @mref{lhs} return the right-hand and left-hand sides,
782 respectively, of an equation or inequation.
784 See also @mref{equal} and @mrefdot{notequal}
788 An expression @code{@var{a} = @var{b}}, by itself, represents
789 an unevaluated equation, which might or might not hold.
792 @c eq_1 : a * x - 5 * y = 17;
793 @c eq_2 : b * x + 3 * y = 29;
794 @c solve ([eq_1, eq_2], [x, y]);
795 @c subst (%, [eq_1, eq_2]);
799 (%i1) eq_1 : a * x - 5 * y = 17;
801 (%i2) eq_2 : b * x + 3 * y = 29;
803 (%i3) solve ([eq_1, eq_2], [x, y]);
805 (%o3) [[x = ---------, y = -----------]]
807 (%i4) subst (%, [eq_1, eq_2]);
809 196 a 5 (29 a - 17 b)
810 (%o4) [--------- - --------------- = 17,
812 196 b 3 (29 a - 17 b)
813 --------- + --------------- = 29]
817 (%o5) [17 = 17, 29 = 29]
820 @code{is(@var{a} = @var{b})} evaluates @code{@var{a} = @var{b}} to @code{true}
821 when @var{a} and @var{b} are syntactically equal (that is, identical).
822 Expressions can be equivalent and not syntactically equal.
825 @c a : (x + 1) * (x - 1);
827 @c [is (a = b), is (a # b)];
828 @c [is (equal (a, b)), is (notequal (a, b))];
831 (%i1) a : (x + 1) * (x - 1);
832 (%o1) (x - 1) (x + 1)
836 (%i3) [is (a = b), is (a # b)];
838 (%i4) [is (equal (a, b)), is (notequal (a, b))];
842 Some operators evaluate @code{=} and @code{#} to @code{true} or @code{false}.
845 @c if expand ((x + y)^2) = x^2 + 2 * x * y + y^2 then FOO else
847 @c eq_3 : 2 * x = 3 * x;
848 @c eq_4 : exp (2) = %e^2;
849 @c [eq_3 and eq_4, eq_3 or eq_4, not eq_3];
852 (%i1) if expand ((x + y)^2) = x^2 + 2 * x * y + y^2 then FOO else
855 (%i2) eq_3 : 2 * x = 3 * x;
857 (%i3) eq_4 : exp (2) = %e^2;
860 (%i4) [eq_3 and eq_4, eq_3 or eq_4, not eq_3];
861 (%o4) [false, true, true]
864 Because @code{not @var{expr}} causes evaluation of @var{expr},
865 @code{not @var{a} = @var{b}} is equivalent to @code{is(@var{a} # @var{b})}.
868 @c [2 * x # 3 * x, not (2 * x = 3 * x)];
869 @c is (2 * x # 3 * x);
872 (%i1) [2 * x # 3 * x, not (2 * x = 3 * x)];
873 (%o1) [2 x # 3 x, true]
874 (%i2) is (2 * x # 3 * x);
878 @opencatbox{Categories:}
883 @c -----------------------------------------------------------------------------
884 @node Assignment operators, User defined operators, Operators for Equations, Operators
885 @section Assignment operators
886 @c -----------------------------------------------------------------------------
888 @c -----------------------------------------------------------------------------
890 @fnindex Assignment operator
895 When the left-hand side is a simple variable (not subscripted), @code{:}
896 evaluates its right-hand side and associates that value with the left-hand side.
898 When the left-hand side is a subscripted element of a list, matrix, declared
899 Maxima array, or Lisp array, the right-hand side is assigned to that element.
900 The subscript must name an existing element; such objects cannot be extended by
901 naming nonexistent elements.
903 When the left-hand side is a subscripted element of a @mrefcomma{hashed array}
904 the right-hand side is assigned to that element, if it already exists,
905 or a new element is allocated, if it does not already exist.
907 When the left-hand side is a list of simple and/or subscripted variables, the
908 right-hand side must evaluate to a list, and the elements of the right-hand
909 side are assigned to the elements of the left-hand side, in parallel.
911 See also @mref{kill} and @mrefcomma{remvalue} which undo the association between
912 the left-hand side and its value.
916 Assignment to a simple variable.
932 Assignment to an element of a list.
948 Assignment to a variable that neither is the name of a list nor of an array
949 creates a @mrefdot{hashed array}
966 (%o4) [hashed, 1, [99]]
974 @c [a, b, c] : [45, 67, 89];
980 (%i1) [a, b, c] : [45, 67, 89];
990 Multiple assignment is carried out in parallel.
991 The values of @code{a} and @code{b} are exchanged in this example.
994 @c [a, b] : [33, 55];
1000 (%i1) [a, b] : [33, 55];
1002 (%i2) [a, b] : [b, a];
1010 @opencatbox{Categories:}
1011 @category{Evaluation}
1012 @category{Operators}
1016 @c -----------------------------------------------------------------------------
1019 @fnindex Assignment operator (evaluates left-hand side)
1020 @deffn {Operator} ::
1022 Assignment operator.
1024 @code{::} is the same as @mref{:} (which see) except that @code{::} evaluates
1025 its left-hand side as well as its right-hand side.
1034 @c x :: [11, 22, 33];
1046 (%i4) x : '[a, b, c];
1048 (%i5) x :: [11, 22, 33];
1058 @opencatbox{Categories:}
1059 @category{Evaluation}
1060 @category{Operators}
1064 @c -----------------------------------------------------------------------------
1066 @fnindex Macro function definition operator
1067 @deffn {Operator} ::=
1069 Macro function definition operator.
1070 @code{::=} defines a function (called a "macro" for historical reasons) which
1071 quotes its arguments, and the expression which it returns (called the "macro
1072 expansion") is evaluated in the context from which the macro was called.
1073 A macro function is otherwise the same as an ordinary function.
1075 @mref{macroexpand} returns a macro expansion (without evaluating it).
1076 @code{macroexpand (foo (x))} followed by @code{''%} is equivalent to
1077 @code{foo (x)} when @code{foo} is a macro function.
1079 @code{::=} puts the name of the new macro function onto the global list
1080 @mrefdot{macros} @mrefcomma{kill} @mrefcomma{remove} and @mref{remfunction}@w{}
1081 unbind macro function definitions and remove names from @code{macros}.
1083 @mref{fundef} or @mref{dispfun} return a macro function definition or assign it
1084 to a label, respectively.
1086 Macro functions commonly contain @mref{buildq} and @mref{splice} expressions to
1087 construct an expression, which is then evaluated.
1091 A macro function quotes its arguments, so message (1) shows @code{y - z}, not
1092 the value of @code{y - z}. The macro expansion (the quoted expression
1093 @code{'(print ("(2) x is equal to", x))}) is evaluated in the context from which
1094 the macro was called, printing message (2).
1100 @c printq1 (x) ::= block (print ("(1) x is equal to", x),
1101 @c '(print ("(2) x is equal to", x)))$
1108 (%i4) printq1 (x) ::= block (print ("(1) x is equal to", x),
1109 '(print ("(2) x is equal to", x)))$
1110 (%i5) printq1 (y - z);
1111 (1) x is equal to y - z
1112 (2) x is equal to %pi
1116 An ordinary function evaluates its arguments, so message (1) shows the value of
1117 @code{y - z}. The return value is not evaluated, so message (2) is not printed
1118 until the explicit evaluation @code{''%}.
1124 @c printe1 (x) := block (print ("(1) x is equal to", x),
1125 @c '(print ("(2) x is equal to", x)))$
1133 (%i4) printe1 (x) := block (print ("(1) x is equal to", x),
1134 '(print ("(2) x is equal to", x)))$
1135 (%i5) printe1 (y - z);
1136 (1) x is equal to 1234 - 1729 w
1137 (%o5) print((2) x is equal to, x)
1139 (2) x is equal to %pi
1143 @code{macroexpand} returns a macro expansion.
1144 @code{macroexpand (foo (x))} followed by @code{''%} is equivalent to
1145 @code{foo (x)} when @code{foo} is a macro function.
1151 @c g (x) ::= buildq ([x], print ("x is equal to", x))$
1152 @c macroexpand (g (y - z));
1160 (%i4) g (x) ::= buildq ([x], print ("x is equal to", x))$
1161 (%i5) macroexpand (g (y - z));
1162 (%o5) print(x is equal to, y - z)
1164 x is equal to 1234 - 1729 w
1167 x is equal to 1234 - 1729 w
1171 @opencatbox{Categories:}
1172 @category{Function definition}
1173 @category{Operators}
1177 @c -----------------------------------------------------------------------------
1179 @fnindex Function definition operator
1180 @deffn {Operator} :=
1182 The function definition operator.
1184 @code{@var{f}(@var{x_1}, ..., @var{x_n}) := @var{expr}} defines a function named
1185 @var{f} with arguments @var{x_1}, @dots{}, @var{x_n} and function body
1186 @var{expr}. @code{:=} never evaluates the function body (unless explicitly
1187 evaluated by quote-quote @code{'@w{}'}).
1188 The function body is evaluated every time the function is called.
1190 @code{@var{f}[@var{x_1}, ..., @var{x_n}] := @var{expr}} defines a so-called
1191 @mrefdot{memoizing function}
1192 Its function body is evaluated just once for each distinct value of its arguments,
1193 and that value is returned, without evaluating the function body,
1194 whenever the arguments have those values again.
1195 (A function of this kind is also known as a ``array function''.)
1197 @code{@var{f}[@var{x_1}, ..., @var{x_n}](@var{y_1}, ..., @var{y_m}) := @var{expr}}
1198 is a special case of a @mrefdot{memoizing function}
1199 @code{@var{f}[@var{x_1}, ..., @var{x_n}]} is a @mref{memoizing function} which returns a lambda expression
1200 with arguments @code{@var{y_1}, ..., @var{y_m}}.
1201 The function body is evaluated once for each distinct value of @code{@var{x_1}, ..., @var{x_n}},
1202 and the body of the lambda expression is that value.
1204 When the last or only function argument @var{x_n} is a list of one element, the
1205 function defined by @code{:=} accepts a variable number of arguments. Actual
1206 arguments are assigned one-to-one to formal arguments @var{x_1}, @dots{},
1207 @var{x_(n - 1)}, and any further actual arguments, if present, are assigned to
1208 @var{x_n} as a list.
1210 All function definitions appear in the same namespace; defining a function
1211 @code{f} within another function @code{g} does not automatically limit the scope
1212 of @code{f} to @code{g}. However, @code{local(f)} makes the definition of
1213 function @code{f} effective only within the block or other compound expression
1214 in which @mref{local} appears.
1216 If some formal argument @var{x_k} is a quoted symbol, the function defined by
1217 @code{:=} does not evaluate the corresponding actual argument. Otherwise all
1218 actual arguments are evaluated.
1220 See also @mref{define} and @mrefdot{::=}
1224 @code{:=} never evaluates the function body (unless explicitly evaluated by
1228 @c expr : cos(y) - sin(x);
1229 @c F1 (x, y) := expr;
1231 @c F2 (x, y) := ''expr;
1235 (%i1) expr : cos(y) - sin(x);
1236 (%o1) cos(y) - sin(x)
1237 (%i2) F1 (x, y) := expr;
1238 (%o2) F1(x, y) := expr
1240 (%o3) cos(y) - sin(x)
1241 (%i4) F2 (x, y) := ''expr;
1242 (%o4) F2(x, y) := cos(y) - sin(x)
1244 (%o5) cos(b) - sin(a)
1247 @code{f(@var{x_1}, ..., @var{x_n}) := ...} defines an ordinary function.
1250 @c G1(x, y) := (print ("Evaluating G1 for x=", x, "and y=", y),
1252 @c G1([1, a], [2, b]);
1253 @c G1([1, a], [2, b]);
1256 (%i1) G1(x, y) := (print ("Evaluating G1 for x=", x, "and y=", y),
1258 (%o1) G1(x, y) := (print("Evaluating G1 for x=", x, "and y=",
1260 (%i2) G1([1, a], [2, b]);
1261 Evaluating G1 for x= [1, a] and y= [2, b]
1263 (%i3) G1([1, a], [2, b]);
1264 Evaluating G1 for x= [1, a] and y= [2, b]
1268 @code{f[@var{x_1}, ..., @var{x_n}] := ...} defines a @mrefdot{memoizing function}
1271 @c G2[a] := (print ("Evaluating G2 for a=", a), a^2);
1279 (%i1) G2[a] := (print ("Evaluating G2 for a=", a), a^2);
1281 (%o1) G2 := (print("Evaluating G2 for a=", a), a )
1284 Evaluating G2 for a= 1234
1289 Evaluating G2 for a= 2345
1291 (%i5) arrayinfo (G2);
1292 (%o5) [hashed, 1, [1234], [2345]]
1293 (%i6) listarray (G2);
1294 (%o6) [1522756, 5499025]
1297 @code{@var{f}[@var{x_1}, ..., @var{x_n}](@var{y_1}, ..., @var{y_m}) := @var{expr}}
1298 is a special case of a @mrefdot{memoizing function}
1301 @c G3[n](x) := (print ("Evaluating G3 for n=", n), diff (sin(x)^2,
1310 (%i1) G3[n](x) := (print ("Evaluating G3 for n=", n), diff (sin(x)^2,
1312 (%o1) G3 (x) := (print("Evaluating G3 for n=", n),
1315 diff(sin (x), x, n))
1317 Evaluating G3 for n= 2
1319 (%o2) lambda([x], 2 cos (x) - 2 sin (x))
1322 (%o3) lambda([x], 2 cos (x) - 2 sin (x))
1325 (%o4) 2 cos (1) - 2 sin (1)
1326 (%i5) arrayinfo (G3);
1327 (%o5) [hashed, 1, [2]]
1328 (%i6) listarray (G3);
1330 (%o6) [lambda([x], 2 cos (x) - 2 sin (x))]
1333 When the last or only function argument @var{x_n} is a list of one element,
1334 the function defined by @code{:=} accepts a variable number of arguments.
1337 @c H ([L]) := apply ("+", L);
1341 (%i1) H ([L]) := apply ("+", L);
1342 (%o1) H([L]) := apply("+", L)
1347 @code{local} makes a local function definition.
1350 @c foo (x) := 1 - x;
1352 @c block (local (foo), foo (x) := 2 * x, foo (100));
1356 (%i1) foo (x) := 1 - x;
1357 (%o1) foo(x) := 1 - x
1360 (%i3) block (local (foo), foo (x) := 2 * x, foo (100));
1366 @opencatbox{Categories:}
1367 @category{Function definition}
1368 @category{Operators}
1372 @c -----------------------------------------------------------------------------
1373 @node User defined operators, , Assignment operators, Operators
1374 @section User defined operators
1375 @c -----------------------------------------------------------------------------
1377 @c -----------------------------------------------------------------------------
1379 @deffn {Function} infix @
1380 @fname{infix} (@var{op}) @
1381 @fname{infix} (@var{op}, @var{lbp}, @var{rbp}) @
1382 @fname{infix} (@var{op}, @var{lbp}, @var{rbp}, @var{lpos}, @var{rpos}, @var{pos})
1384 Declares @var{op} to be an infix operator. An infix operator is a function of
1385 two arguments, with the name of the function written between the arguments.
1386 For example, the subtraction operator @code{-} is an infix operator.
1388 @code{infix (@var{op})} declares @var{op} to be an infix operator with default
1389 binding powers (left and right both equal to 180) and parts of speech (left and
1390 right both equal to @code{any}).
1391 @c HOW IS pos DIFFERENT FROM lpos AND rpos ??
1393 @code{infix (@var{op}, @var{lbp}, @var{rbp})} declares @var{op} to be an infix
1394 operator with stated left and right binding powers and default parts of speech
1395 (left and right both equal to @code{any}).
1397 @code{infix (@var{op}, @var{lbp}, @var{rbp}, @var{lpos}, @var{rpos}, @var{pos})}
1398 declares @var{op} to be an infix operator with stated left and right binding
1399 powers and parts of speech @var{lpos}, @var{rpos}, and @var{pos} for the left
1400 operand, the right operand, and the operator result, respectively.
1402 "Part of speech", in reference to operator declarations, means expression type.
1403 Three types are recognized: @code{expr}, @code{clause}, and @code{any},
1404 indicating an algebraic expression, a Boolean expression, or any kind of
1405 expression, respectively. Maxima can detect some syntax errors by comparing the
1406 declared part of speech to an actual expression.
1408 The precedence of @var{op} with respect to other operators derives from the left
1409 and right binding powers of the operators in question. If the left and right
1410 binding powers of @var{op} are both greater the left and right binding powers of
1411 some other operator, then @var{op} takes precedence over the other operator.
1412 If the binding powers are not both greater or less, some more complicated
1415 The associativity of @var{op} depends on its binding powers. Greater left
1416 binding power (@var{lbp}) implies an instance of @var{op} is evaluated before
1417 other operators to its left in an expression, while greater right binding power
1418 (@var{rbp}) implies an instance of @var{op} is evaluated before other operators
1419 to its right in an expression. Thus greater @var{lbp} makes @var{op}
1420 right-associative, while greater @var{rbp} makes @var{op} left-associative.
1421 If @var{lbp} is equal to @var{rbp}, @var{op} is left-associative.
1423 See also @ref{Introduction to operators}.
1427 If the left and right binding powers of @var{op} are both greater
1428 the left and right binding powers of some other operator,
1429 then @var{op} takes precedence over the other operator.
1432 @c :lisp (get '$+ 'lbp)
1433 @c :lisp (get '$+ 'rbp)
1434 @c infix ("##", 101, 101);
1435 @c "##"(a, b) := sconcat("(", a, ",", b, ")");
1437 @c infix ("##", 99, 99);
1441 (%i1) :lisp (get '$+ 'lbp)
1443 (%i1) :lisp (get '$+ 'rbp)
1445 (%i1) infix ("##", 101, 101);
1447 (%i2) "##"(a, b) := sconcat("(", a, ",", b, ")");
1448 (%o2) (a ## b) := sconcat("(", a, ",", b, ")")
1449 (%i3) 1 + a ## b + 2;
1451 (%i4) infix ("##", 99, 99);
1453 (%i5) 1 + a ## b + 2;
1457 Greater @var{lbp} makes @var{op} right-associative,
1458 while greater @var{rbp} makes @var{op} left-associative.
1461 @c infix ("##", 100, 99);
1462 @c "##"(a, b) := sconcat("(", a, ",", b, ")")$
1463 @c foo ## bar ## baz;
1464 @c infix ("##", 100, 101);
1465 @c foo ## bar ## baz;
1468 (%i1) infix ("##", 100, 99);
1470 (%i2) "##"(a, b) := sconcat("(", a, ",", b, ")")$
1471 (%i3) foo ## bar ## baz;
1472 (%o3) (foo,(bar,baz))
1473 (%i4) infix ("##", 100, 101);
1475 (%i5) foo ## bar ## baz;
1476 (%o5) ((foo,bar),baz)
1479 Maxima can detect some syntax errors by comparing the
1480 declared part of speech to an actual expression.
1483 @c infix ("##", 100, 99, expr, expr, expr);
1484 @c if x ## y then 1 else 0;
1485 @c infix ("##", 100, 99, expr, expr, clause);
1486 @c if x ## y then 1 else 0;
1489 (%i1) infix ("##", 100, 99, expr, expr, expr);
1491 (%i2) if x ## y then 1 else 0;
1492 Incorrect syntax: Found algebraic expression where logical
1496 (%i2) infix ("##", 100, 99, expr, expr, clause);
1498 (%i3) if x ## y then 1 else 0;
1499 (%o3) if x ## y then 1 else 0
1502 @opencatbox{Categories:}
1503 @category{Operators}
1504 @category{Declarations and inferences}
1509 @c -----------------------------------------------------------------------------
1511 @deffn {Function} matchfix @
1512 @fname{matchfix} (@var{ldelimiter}, @var{rdelimiter}) @
1513 @fname{matchfix} (@var{ldelimiter}, @var{rdelimiter}, @var{arg_pos}, @var{pos})
1515 Declares a matchfix operator with left and right delimiters @var{ldelimiter}
1516 and @var{rdelimiter}. The delimiters are specified as strings.
1518 A "matchfix" operator is a function of any number of arguments,
1519 such that the arguments occur between matching left and right delimiters.
1520 The delimiters may be any strings, so long as the parser can
1521 distinguish the delimiters from the operands
1522 and other expressions and operators.
1523 In practice this rules out unparseable delimiters such as
1524 @code{%}, @code{,}, @code{$} and @code{;},
1525 and may require isolating the delimiters with white space.
1526 The right delimiter can be the same or different from the left delimiter.
1528 A left delimiter can be associated with only one right delimiter;
1529 two different matchfix operators cannot have the same left delimiter.
1531 An existing operator may be redeclared as a matchfix operator
1532 without changing its other properties.
1533 In particular, built-in operators such as addition @code{+} can
1534 be declared matchfix,
1535 but operator functions cannot be defined for built-in operators.
1537 The command @code{matchfix (@var{ldelimiter}, @var{rdelimiter}, @var{arg_pos},
1538 @var{pos})} declares the argument part-of-speech @var{arg_pos} and result
1539 part-of-speech @var{pos}, and the delimiters @var{ldelimiter} and
1542 "Part of speech", in reference to operator declarations, means expression type.
1543 Three types are recognized: @code{expr}, @code{clause}, and @code{any},
1544 indicating an algebraic expression, a Boolean expression, or any kind of
1545 expression, respectively.
1546 Maxima can detect some syntax errors by comparing the
1547 declared part of speech to an actual expression.
1549 @c DUNNO IF WE REALLY NEED TO MENTION BINDING POWER HERE -- AS NOTED IT'S IRRELEVANT
1550 @c An operator declared by @code{matchfix} is assigned a low binding power.
1551 @c Since a matchfix operator must be evaluated before any expression
1552 @c which contains it,
1553 @c binding power is effectively irrelevant
1554 @c to the declaration of a matchfix operator.
1556 The function to carry out a matchfix operation is an ordinary
1557 user-defined function.
1558 The operator function is defined
1560 with the function definition operator @code{:=} or @code{define}.
1561 The arguments may be written between the delimiters,
1562 or with the left delimiter as a quoted string and the arguments
1563 following in parentheses.
1564 @code{dispfun (@var{ldelimiter})} displays the function definition.
1566 The only built-in matchfix operator is the list constructor @code{[ ]}.
1567 Parentheses @code{( )} and double-quotes @code{" "}
1568 act like matchfix operators,
1569 but are not treated as such by the Maxima parser.
1571 @code{matchfix} evaluates its arguments.
1572 @code{matchfix} returns its first argument, @var{ldelimiter}.
1573 @c HOW TO TAKE AWAY THE MATCHFIX PROPERTY ??
1577 Delimiters may be almost any strings.
1580 @c matchfix ("@@", "~");
1582 @c matchfix (">>", "<<");
1584 @c matchfix ("foo", "oof");
1586 @c >> w + foo x, y oof + z << / @@ p, q ~;
1589 (%i1) matchfix ("@@@@", "~");
1591 (%i2) @@@@ a, b, c ~;
1593 (%i3) matchfix (">>", "<<");
1595 (%i4) >> a, b, c <<;
1597 (%i5) matchfix ("foo", "oof");
1599 (%i6) foo a, b, c oof;
1601 (%i7) >> w + foo x, y oof + z << / @@@@ p, q ~;
1602 >>z + foox, yoof + w<<
1603 (%o7) ----------------------
1607 Matchfix operators are ordinary user-defined functions.
1610 (%i1) matchfix ("!-", "-!");
1612 (%i2) !- x, y -! := x/y - y/x;
1614 (%o2) !-x, y-! := - - -
1616 (%i3) define (!-x, y-!, x/y - y/x);
1618 (%o3) !-x, y-! := - - -
1620 (%i4) define ("!-" (x, y), x/y - y/x);
1622 (%o4) !-x, y-! := - - -
1624 (%i5) dispfun ("!-");
1626 (%t5) !-x, y-! := - - -
1640 @opencatbox{Categories:}
1642 @category{Operators}
1646 @c -----------------------------------------------------------------------------
1647 @anchor{function_nary}
1648 @deffn {Function} nary @
1649 @fname{nary} (@var{op}) @
1650 @fname{nary} (@var{op}, @var{bp}, @var{arg_pos}, @var{pos})
1652 An @code{nary} operator is used to denote a function of any number of arguments,
1653 each of which is separated by an occurrence of the operator, e.g. A+B or A+B+C.
1654 The @code{nary("x")} function is a syntax extension function to declare @code{x}
1655 to be an @code{nary} operator. Functions may be declared to be @code{nary}. If
1656 @code{declare(j,nary);} is done, this tells the simplifier to simplify, e.g.
1657 @code{j(j(a,b),j(c,d))} to @code{j(a, b, c, d)}.
1659 See also @ref{Introduction to operators}.
1661 @opencatbox{Categories:}
1662 @category{Operators}
1667 @c -----------------------------------------------------------------------------
1669 @deffn {Function} nofix @
1670 @fname{nofix} (@var{op}) @
1671 @fname{nofix} (@var{op}, @var{pos})
1673 @code{nofix} operators are used to denote functions of no arguments.
1674 The mere presence of such an operator in a command will cause the
1675 corresponding function to be evaluated. For example, when one types
1676 "exit;" to exit from a Maxima break, "exit" is behaving similar to a
1677 @code{nofix} operator. The function @code{nofix("x")} is a syntax extension
1678 function which declares @code{x} to be a @code{nofix} operator.
1680 See also @ref{Introduction to operators}.
1682 @opencatbox{Categories:}
1683 @category{Operators}
1688 @c -----------------------------------------------------------------------------
1690 @deffn {Function} postfix @
1691 @fname{postfix} (@var{op}) @
1692 @fname{postfix} (@var{op}, @var{lbp}, @var{lpos}, @var{pos})
1694 @code{postfix} operators like the @code{prefix} variety denote functions of a
1695 single argument, but in this case the argument immediately precedes an
1696 occurrence of the operator in the input string, e.g. 3!. The
1697 @code{postfix("x")} function is a syntax extension function to declare @code{x}
1698 to be a @code{postfix} operator.
1700 See also @ref{Introduction to operators}.
1702 @opencatbox{Categories:}
1703 @category{Operators}
1708 @c -----------------------------------------------------------------------------
1710 @deffn {Function} prefix @
1711 @fname{prefix} (@var{op}) @
1712 @fname{prefix} (@var{op}, @var{rbp}, @var{rpos}, @var{pos})
1714 A @code{prefix} operator is one which signifies a function of one argument,
1715 which argument immediately follows an occurrence of the operator.
1716 @code{prefix("x")} is a syntax extension function to declare @code{x} to be a
1717 @code{prefix} operator.
1719 See also @ref{Introduction to operators}.
1721 @opencatbox{Categories:}
1722 @category{Operators}