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 -----------------------------------------------------------------------------
451 @fnindex Noncommutative exponentiation
454 Noncommutative exponentiation operator.
455 @code{^^} is the exponentiation operator corresponding to noncommutative
456 multiplication @code{.}, just as the ordinary exponentiation operator @code{^}
457 corresponds to commutative multiplication @code{*}.
459 Noncommutative exponentiation is displayed by @code{^^} in 1-dimensional output,
460 and by placing the exponent as a superscript within angle brackets @code{< >}
461 in 2-dimensional output.
466 @c a . a . b . b . b + a * a * a * b * b;
467 @c string (a . a . b . b . b + a * a * a * b * b);
470 (%i1) a . a . b . b . b + a * a * a * b * b;
473 (%i2) string (a . a . b . b . b + a * a * a * b * b);
474 (%o2) a^3*b^2+a^^2 . b^^3
477 @opencatbox{Categories:}
482 @c -----------------------------------------------------------------------------
486 @fnindex Noncommutative multiplication
489 The dot operator, for matrix (non-commutative) multiplication.
490 When @code{"."} is used in this way, spaces should be left on both sides of
491 it, e.g. @code{A . B} This distinguishes it plainly from a decimal point in
492 a floating point number.
496 @mrefcomma{dot0nscsimp}
500 @mrefcomma{dotconstrules}
501 @mrefcomma{dotdistrib}
502 @mrefcomma{dotexptsimp}
507 @opencatbox{Categories:}
512 @c -----------------------------------------------------------------------------
513 @node Relational operators, Logical operators, Arithmetic operators, Operators
514 @section Relational operators
515 @c -----------------------------------------------------------------------------
517 @c -----------------------------------------------------------------------------
523 @fnindex Less than or equal
524 @fnindex Greater than or equal
525 @fnindex Greater than
528 @deffnx {Operator} <=
529 @deffnx {Operator} >=
532 The symbols @code{<} @code{<=} @code{>=} and @code{>} represent less than, less
533 than or equal, greater than or equal, and greater than, respectively. The names
534 of these operators are @code{"<"} @code{"<="} @code{">="} and @code{">"}, which
535 may appear where the name of a function or operator is required.
537 These relational operators are all binary operators; constructs such as
538 @code{a < b < c} are not recognized by Maxima.
540 Relational expressions are evaluated to Boolean values by the functions
541 @mref{is} and @mrefcomma{maybe} and the programming constructs
542 @mrefcomma{if} @mrefcomma{while} and @mrefdot{unless} Relational expressions
543 are not otherwise evaluated or simplified to Boolean values, although the
544 arguments of relational expressions are evaluated (when evaluation is not
545 otherwise prevented by quotation).
547 When a relational expression cannot be evaluated to @code{true} or @code{false},
548 the behavior of @code{is} and @code{if} are governed by the global flag
549 @mrefdot{prederror} When @code{prederror} is @code{true}, @code{is} and
550 @code{if} trigger an error. When @code{prederror} is @code{false}, @code{is}
551 returns @code{unknown}, and @code{if} returns a partially-evaluated conditional
554 @code{maybe} always behaves as if @code{prederror} were @code{false}, and
555 @code{while} and @code{unless} always behave as if @code{prederror} were
558 Relational operators do not distribute over lists or other aggregates.
560 See also @mrefcomma{=} @mrefcomma{#} @mrefcomma{equal} and @mrefdot{notequal}
564 Relational expressions are evaluated to Boolean values by some functions and
565 programming constructs.
568 @c [x, y, z] : [123, 456, 789];
571 @c if x >= z then 1 else 0;
572 @c block ([S], S : 0, for i:1 while i <= 100 do S : S + i,
576 (%i1) [x, y, z] : [123, 456, 789];
577 (%o1) [123, 456, 789]
582 (%i4) if x >= z then 1 else 0;
584 (%i5) block ([S], S : 0, for i:1 while i <= 100 do S : S + i,
589 Relational expressions are not otherwise evaluated or simplified to Boolean
590 values, although the arguments of relational expressions are evaluated.
593 @c [x, y, z] : [123, 456, 789];
594 @c [x < y, y <= z, z >= y, y > z];
598 (%o1) [123, 456, 789]
599 (%i2) [x < y, y <= z, z >= y, y > z];
600 (%o2) [123 < 456, 456 <= 789, 789 >= 456, 456 > 789]
602 (%o3) [true, true, true, false]
605 @opencatbox{Categories:}
610 @c -----------------------------------------------------------------------------
611 @node Logical operators, Operators for Equations, Relational operators, Operators
612 @section Logical operators
613 @c -----------------------------------------------------------------------------
617 @c -----------------------------------------------------------------------------
619 @deffn {Operator} and
621 @fnindex Logical conjunction
624 The logical conjunction operator. @code{and} is an n-ary infix operator;
625 its operands are Boolean expressions, and its result is a Boolean value.
627 @code{and} forces evaluation (like @mref{is}) of one or more operands,
628 and may force evaluation of all operands.
630 Operands are evaluated in the order in which they appear. @code{and} evaluates
631 only as many of its operands as necessary to determine the result. If any
632 operand is @code{false}, the result is @code{false} and no further operands are
635 The global flag @mref{prederror} governs the behavior of @code{and} when an
636 evaluated operand cannot be determined to be @code{true} or @code{false}.
637 @code{and} prints an error message when @code{prederror} is @code{true}.
638 Otherwise, operands which do not evaluate to @code{true} or @code{false} are
639 accepted, and the result is a Boolean expression.
641 @code{and} is not commutative: @code{a and b} might not be equal to
642 @code{b and a} due to the treatment of indeterminate operands.
644 @opencatbox{Categories:}
651 @c -----------------------------------------------------------------------------
653 @deffn {Operator} not
655 @fnindex Logical negation
658 The logical negation operator. @code{not} is a prefix operator;
659 its operand is a Boolean expression, and its result is a Boolean value.
661 @code{not} forces evaluation (like @code{is}) of its operand.
663 The global flag @mref{prederror} governs the behavior of @code{not} when its
664 operand cannot be determined to be @code{true} or @code{false}. @code{not}
665 prints an error message when @code{prederror} is @code{true}. Otherwise,
666 operands which do not evaluate to @code{true} or @code{false} are accepted,
667 and the result is a Boolean expression.
669 @opencatbox{Categories:}
676 @c -----------------------------------------------------------------------------
680 @fnindex Logical disjunction
683 The logical disjunction operator. @code{or} is an n-ary infix operator;
684 its operands are Boolean expressions, and its result is a Boolean value.
686 @code{or} forces evaluation (like @mref{is}) of one or more operands,
687 and may force evaluation of all operands.
689 Operands are evaluated in the order in which they appear. @code{or} evaluates
690 only as many of its operands as necessary to determine the result. If any
691 operand is @code{true}, the result is @code{true} and no further operands are
694 The global flag @mref{prederror} governs the behavior of @code{or} when an
695 evaluated operand cannot be determined to be @code{true} or @code{false}.
696 @code{or} prints an error message when @code{prederror} is @code{true}.
697 Otherwise, operands which do not evaluate to @code{true} or @code{false} are
698 accepted, and the result is a Boolean expression.
700 @code{or} is not commutative: @code{a or b} might not be equal to @code{b or a}
701 due to the treatment of indeterminate operands.
703 @opencatbox{Categories:}
708 @c -----------------------------------------------------------------------------
709 @node Operators for Equations, Assignment operators, Logical operators, Operators
710 @section Operators for Equations
711 @c -----------------------------------------------------------------------------
713 @c -----------------------------------------------------------------------------
717 @fnindex Not equal (syntactic inequality)
720 Represents the negation of syntactic equality @mrefdot{=}
722 Note that because of the rules for evaluation of predicate expressions
723 (in particular because @code{not @var{expr}} causes evaluation of @var{expr}),
724 @code{not @var{a} = @var{b}} is equivalent to @code{is(@var{a} # @var{b})},
725 instead of @code{@var{a} # @var{b}}.
748 (%i6) is (not a = b);
752 @opencatbox{Categories:}
757 @c -----------------------------------------------------------------------------
761 @fnindex Equation operator
762 @fnindex Equal (syntactic equality)
765 The equation operator.
767 An expression @code{@var{a} = @var{b}}, by itself, represents an unevaluated
768 equation, which might or might not hold. Unevaluated equations may appear as
769 arguments to @mref{solve} and @mref{algsys} or some other functions.
771 The function @mref{is} evaluates @code{=} to a Boolean value.
772 @code{is(@var{a} = @var{b})} evaluates @code{@var{a} = @var{b}} to @code{true}
773 when @var{a} and @var{b} are identical. That is, @var{a} and @var{b} are atoms
774 which are identical, or they are not atoms and their operators are identical and
775 their arguments are identical. Otherwise, @code{is(@var{a} = @var{b})}
776 evaluates to @code{false}; it never evaluates to @code{unknown}. When
777 @code{is(@var{a} = @var{b})} is @code{true}, @var{a} and @var{b} are said to be
778 syntactically equal, in contrast to equivalent expressions, for which
779 @code{is(equal(@var{a}, @var{b}))} is @code{true}. Expressions can be
780 equivalent and not syntactically equal.
782 The negation of @code{=} is represented by @mrefdot{#}
783 As with @code{=}, an expression @code{@var{a} # @var{b}}, by itself, is not
784 evaluated. @code{is(@var{a} # @var{b})} evaluates @code{@var{a} # @var{b}} to
785 @code{true} or @code{false}.
787 In addition to @code{is}, some other operators evaluate @code{=} and @code{#}
788 to @code{true} or @code{false}, namely @mrefcomma{if} @mrefcomma{and}@w{}
789 @mrefcomma{or} and @mrefdot{not}
791 Note that because of the rules for evaluation of predicate expressions
792 (in particular because @code{not @var{expr}} causes evaluation of @var{expr}),
793 @code{not @var{a} = @var{b}} is equivalent to @code{is(@var{a} # @var{b})},
794 instead of @code{@var{a} # @var{b}}.
796 @mref{rhs} and @mref{lhs} return the right-hand and left-hand sides,
797 respectively, of an equation or inequation.
799 See also @mref{equal} and @mrefdot{notequal}
803 An expression @code{@var{a} = @var{b}}, by itself, represents
804 an unevaluated equation, which might or might not hold.
807 @c eq_1 : a * x - 5 * y = 17;
808 @c eq_2 : b * x + 3 * y = 29;
809 @c solve ([eq_1, eq_2], [x, y]);
810 @c subst (%, [eq_1, eq_2]);
814 (%i1) eq_1 : a * x - 5 * y = 17;
816 (%i2) eq_2 : b * x + 3 * y = 29;
818 (%i3) solve ([eq_1, eq_2], [x, y]);
820 (%o3) [[x = ---------, y = -----------]]
822 (%i4) subst (%, [eq_1, eq_2]);
824 196 a 5 (29 a - 17 b)
825 (%o4) [--------- - --------------- = 17,
827 196 b 3 (29 a - 17 b)
828 --------- + --------------- = 29]
832 (%o5) [17 = 17, 29 = 29]
835 @code{is(@var{a} = @var{b})} evaluates @code{@var{a} = @var{b}} to @code{true}
836 when @var{a} and @var{b} are syntactically equal (that is, identical).
837 Expressions can be equivalent and not syntactically equal.
840 @c a : (x + 1) * (x - 1);
842 @c [is (a = b), is (a # b)];
843 @c [is (equal (a, b)), is (notequal (a, b))];
846 (%i1) a : (x + 1) * (x - 1);
847 (%o1) (x - 1) (x + 1)
851 (%i3) [is (a = b), is (a # b)];
853 (%i4) [is (equal (a, b)), is (notequal (a, b))];
857 Some operators evaluate @code{=} and @code{#} to @code{true} or @code{false}.
860 @c if expand ((x + y)^2) = x^2 + 2 * x * y + y^2 then FOO else
862 @c eq_3 : 2 * x = 3 * x;
863 @c eq_4 : exp (2) = %e^2;
864 @c [eq_3 and eq_4, eq_3 or eq_4, not eq_3];
867 (%i1) if expand ((x + y)^2) = x^2 + 2 * x * y + y^2 then FOO else
870 (%i2) eq_3 : 2 * x = 3 * x;
872 (%i3) eq_4 : exp (2) = %e^2;
875 (%i4) [eq_3 and eq_4, eq_3 or eq_4, not eq_3];
876 (%o4) [false, true, true]
879 Because @code{not @var{expr}} causes evaluation of @var{expr},
880 @code{not @var{a} = @var{b}} is equivalent to @code{is(@var{a} # @var{b})}.
883 @c [2 * x # 3 * x, not (2 * x = 3 * x)];
884 @c is (2 * x # 3 * x);
887 (%i1) [2 * x # 3 * x, not (2 * x = 3 * x)];
888 (%o1) [2 x # 3 x, true]
889 (%i2) is (2 * x # 3 * x);
893 @opencatbox{Categories:}
898 @c -----------------------------------------------------------------------------
899 @node Assignment operators, User defined operators, Operators for Equations, Operators
900 @section Assignment operators
901 @c -----------------------------------------------------------------------------
903 @c -----------------------------------------------------------------------------
907 @fnindex Assignment operator
912 When the left-hand side is a simple variable (not subscripted), @code{:}
913 evaluates its right-hand side and associates that value with the left-hand side.
915 When the left-hand side is a subscripted element of a list, matrix, declared
916 Maxima array, or Lisp array, the right-hand side is assigned to that element.
917 The subscript must name an existing element; such objects cannot be extended by
918 naming nonexistent elements.
920 When the left-hand side is a subscripted element of a @mrefcomma{hashed array}
921 the right-hand side is assigned to that element, if it already exists,
922 or a new element is allocated, if it does not already exist.
924 When the left-hand side is a list of simple and/or subscripted variables, the
925 right-hand side must evaluate to a list, and the elements of the right-hand
926 side are assigned to the elements of the left-hand side, in parallel.
928 See also @mref{kill} and @mrefcomma{remvalue} which undo the association between
929 the left-hand side and its value.
933 Assignment to a simple variable.
949 Assignment to an element of a list.
965 Assignment to a variable that neither is the name of a list nor of an array
966 creates a @mrefdot{hashed array}
983 (%o4) [hashed, 1, [99]]
991 @c [a, b, c] : [45, 67, 89];
997 (%i1) [a, b, c] : [45, 67, 89];
1007 Multiple assignment is carried out in parallel.
1008 The values of @code{a} and @code{b} are exchanged in this example.
1011 @c [a, b] : [33, 55];
1017 (%i1) [a, b] : [33, 55];
1019 (%i2) [a, b] : [b, a];
1027 @opencatbox{Categories:}
1028 @category{Evaluation}
1029 @category{Operators}
1033 @c -----------------------------------------------------------------------------
1036 @deffn {Operator} ::
1038 @fnindex Assignment operator (evaluates left-hand side)
1041 Assignment operator.
1043 @code{::} is the same as @mref{:} (which see) except that @code{::} evaluates
1044 its left-hand side as well as its right-hand side.
1053 @c x :: [11, 22, 33];
1065 (%i4) x : '[a, b, c];
1067 (%i5) x :: [11, 22, 33];
1077 @opencatbox{Categories:}
1078 @category{Evaluation}
1079 @category{Operators}
1083 @c -----------------------------------------------------------------------------
1085 @deffn {Operator} ::=
1087 @fnindex Macro function definition operator
1090 Macro function definition operator.
1091 @code{::=} defines a function (called a "macro" for historical reasons) which
1092 quotes its arguments, and the expression which it returns (called the "macro
1093 expansion") is evaluated in the context from which the macro was called.
1094 A macro function is otherwise the same as an ordinary function.
1096 @mref{macroexpand} returns a macro expansion (without evaluating it).
1097 @code{macroexpand (foo (x))} followed by @code{''%} is equivalent to
1098 @code{foo (x)} when @code{foo} is a macro function.
1100 @code{::=} puts the name of the new macro function onto the global list
1101 @mrefdot{macros} @mrefcomma{kill} @mrefcomma{remove} and @mref{remfunction}@w{}
1102 unbind macro function definitions and remove names from @code{macros}.
1104 @mref{fundef} or @mref{dispfun} return a macro function definition or assign it
1105 to a label, respectively.
1107 Macro functions commonly contain @mref{buildq} and @mref{splice} expressions to
1108 construct an expression, which is then evaluated.
1112 A macro function quotes its arguments, so message (1) shows @code{y - z}, not
1113 the value of @code{y - z}. The macro expansion (the quoted expression
1114 @code{'(print ("(2) x is equal to", x))}) is evaluated in the context from which
1115 the macro was called, printing message (2).
1121 @c printq1 (x) ::= block (print ("(1) x is equal to", x),
1122 @c '(print ("(2) x is equal to", x)))$
1129 (%i4) printq1 (x) ::= block (print ("(1) x is equal to", x),
1130 '(print ("(2) x is equal to", x)))$
1131 (%i5) printq1 (y - z);
1132 (1) x is equal to y - z
1133 (2) x is equal to %pi
1137 An ordinary function evaluates its arguments, so message (1) shows the value of
1138 @code{y - z}. The return value is not evaluated, so message (2) is not printed
1139 until the explicit evaluation @code{''%}.
1145 @c printe1 (x) := block (print ("(1) x is equal to", x),
1146 @c '(print ("(2) x is equal to", x)))$
1154 (%i4) printe1 (x) := block (print ("(1) x is equal to", x),
1155 '(print ("(2) x is equal to", x)))$
1156 (%i5) printe1 (y - z);
1157 (1) x is equal to 1234 - 1729 w
1158 (%o5) print((2) x is equal to, x)
1160 (2) x is equal to %pi
1164 @code{macroexpand} returns a macro expansion.
1165 @code{macroexpand (foo (x))} followed by @code{''%} is equivalent to
1166 @code{foo (x)} when @code{foo} is a macro function.
1172 @c g (x) ::= buildq ([x], print ("x is equal to", x))$
1173 @c macroexpand (g (y - z));
1181 (%i4) g (x) ::= buildq ([x], print ("x is equal to", x))$
1182 (%i5) macroexpand (g (y - z));
1183 (%o5) print(x is equal to, y - z)
1185 x is equal to 1234 - 1729 w
1188 x is equal to 1234 - 1729 w
1192 @opencatbox{Categories:}
1193 @category{Function definition}
1194 @category{Operators}
1198 @c -----------------------------------------------------------------------------
1200 @deffn {Operator} :=
1202 @fnindex Function definition operator
1205 The function definition operator.
1207 @code{@var{f}(@var{x_1}, ..., @var{x_n}) := @var{expr}} defines a function named
1208 @var{f} with arguments @var{x_1}, @dots{}, @var{x_n} and function body
1209 @var{expr}. @code{:=} never evaluates the function body (unless explicitly
1210 evaluated by quote-quote @code{'@w{}'}).
1211 The function body is evaluated every time the function is called.
1213 @code{@var{f}[@var{x_1}, ..., @var{x_n}] := @var{expr}} defines a so-called
1214 @mrefdot{memoizing function}
1215 Its function body is evaluated just once for each distinct value of its arguments,
1216 and that value is returned, without evaluating the function body,
1217 whenever the arguments have those values again.
1218 (A function of this kind is also known as a ``array function''.)
1220 @code{@var{f}[@var{x_1}, ..., @var{x_n}](@var{y_1}, ..., @var{y_m}) := @var{expr}}
1221 is a special case of a @mrefdot{memoizing function}
1222 @code{@var{f}[@var{x_1}, ..., @var{x_n}]} is a @mref{memoizing function} which returns a lambda expression
1223 with arguments @code{@var{y_1}, ..., @var{y_m}}.
1224 The function body is evaluated once for each distinct value of @code{@var{x_1}, ..., @var{x_n}},
1225 and the body of the lambda expression is that value.
1227 When the last or only function argument @var{x_n} is a list of one element, the
1228 function defined by @code{:=} accepts a variable number of arguments. Actual
1229 arguments are assigned one-to-one to formal arguments @var{x_1}, @dots{},
1230 @var{x_(n - 1)}, and any further actual arguments, if present, are assigned to
1231 @var{x_n} as a list.
1233 All function definitions appear in the same namespace; defining a function
1234 @code{f} within another function @code{g} does not automatically limit the scope
1235 of @code{f} to @code{g}. However, @code{local(f)} makes the definition of
1236 function @code{f} effective only within the block or other compound expression
1237 in which @mref{local} appears.
1239 If some formal argument @var{x_k} is a quoted symbol, the function defined by
1240 @code{:=} does not evaluate the corresponding actual argument. Otherwise all
1241 actual arguments are evaluated.
1243 See also @mref{define} and @mrefdot{::=}
1247 @code{:=} never evaluates the function body (unless explicitly evaluated by
1251 @c expr : cos(y) - sin(x);
1252 @c F1 (x, y) := expr;
1254 @c F2 (x, y) := ''expr;
1258 (%i1) expr : cos(y) - sin(x);
1259 (%o1) cos(y) - sin(x)
1260 (%i2) F1 (x, y) := expr;
1261 (%o2) F1(x, y) := expr
1263 (%o3) cos(y) - sin(x)
1264 (%i4) F2 (x, y) := ''expr;
1265 (%o4) F2(x, y) := cos(y) - sin(x)
1267 (%o5) cos(b) - sin(a)
1270 @code{f(@var{x_1}, ..., @var{x_n}) := ...} defines an ordinary function.
1273 @c G1(x, y) := (print ("Evaluating G1 for x=", x, "and y=", y),
1275 @c G1([1, a], [2, b]);
1276 @c G1([1, a], [2, b]);
1279 (%i1) G1(x, y) := (print ("Evaluating G1 for x=", x, "and y=", y),
1281 (%o1) G1(x, y) := (print("Evaluating G1 for x=", x, "and y=",
1283 (%i2) G1([1, a], [2, b]);
1284 Evaluating G1 for x= [1, a] and y= [2, b]
1286 (%i3) G1([1, a], [2, b]);
1287 Evaluating G1 for x= [1, a] and y= [2, b]
1291 @code{f[@var{x_1}, ..., @var{x_n}] := ...} defines a @mrefdot{memoizing function}
1294 @c G2[a] := (print ("Evaluating G2 for a=", a), a^2);
1302 (%i1) G2[a] := (print ("Evaluating G2 for a=", a), a^2);
1304 (%o1) G2 := (print("Evaluating G2 for a=", a), a )
1307 Evaluating G2 for a= 1234
1312 Evaluating G2 for a= 2345
1314 (%i5) arrayinfo (G2);
1315 (%o5) [hashed, 1, [1234], [2345]]
1316 (%i6) listarray (G2);
1317 (%o6) [1522756, 5499025]
1320 @code{@var{f}[@var{x_1}, ..., @var{x_n}](@var{y_1}, ..., @var{y_m}) := @var{expr}}
1321 is a special case of a @mrefdot{memoizing function}
1324 @c G3[n](x) := (print ("Evaluating G3 for n=", n), diff (sin(x)^2,
1333 (%i1) G3[n](x) := (print ("Evaluating G3 for n=", n), diff (sin(x)^2,
1335 (%o1) G3 (x) := (print("Evaluating G3 for n=", n),
1338 diff(sin (x), x, n))
1340 Evaluating G3 for n= 2
1342 (%o2) lambda([x], 2 cos (x) - 2 sin (x))
1345 (%o3) lambda([x], 2 cos (x) - 2 sin (x))
1348 (%o4) 2 cos (1) - 2 sin (1)
1349 (%i5) arrayinfo (G3);
1350 (%o5) [hashed, 1, [2]]
1351 (%i6) listarray (G3);
1353 (%o6) [lambda([x], 2 cos (x) - 2 sin (x))]
1356 When the last or only function argument @var{x_n} is a list of one element,
1357 the function defined by @code{:=} accepts a variable number of arguments.
1360 @c H ([L]) := apply ("+", L);
1364 (%i1) H ([L]) := apply ("+", L);
1365 (%o1) H([L]) := apply("+", L)
1370 @code{local} makes a local function definition.
1373 @c foo (x) := 1 - x;
1375 @c block (local (foo), foo (x) := 2 * x, foo (100));
1379 (%i1) foo (x) := 1 - x;
1380 (%o1) foo(x) := 1 - x
1383 (%i3) block (local (foo), foo (x) := 2 * x, foo (100));
1389 @opencatbox{Categories:}
1390 @category{Function definition}
1391 @category{Operators}
1395 @c -----------------------------------------------------------------------------
1396 @node User defined operators, , Assignment operators, Operators
1397 @section User defined operators
1398 @c -----------------------------------------------------------------------------
1400 @c -----------------------------------------------------------------------------
1402 @deffn {Function} infix @
1403 @fname{infix} (@var{op}) @
1404 @fname{infix} (@var{op}, @var{lbp}, @var{rbp}) @
1405 @fname{infix} (@var{op}, @var{lbp}, @var{rbp}, @var{lpos}, @var{rpos}, @var{pos})
1407 Declares @var{op} to be an infix operator. An infix operator is a function of
1408 two arguments, with the name of the function written between the arguments.
1409 For example, the subtraction operator @code{-} is an infix operator.
1411 @code{infix (@var{op})} declares @var{op} to be an infix operator with default
1412 binding powers (left and right both equal to 180) and parts of speech (left and
1413 right both equal to @code{any}).
1414 @c HOW IS pos DIFFERENT FROM lpos AND rpos ??
1416 @code{infix (@var{op}, @var{lbp}, @var{rbp})} declares @var{op} to be an infix
1417 operator with stated left and right binding powers and default parts of speech
1418 (left and right both equal to @code{any}).
1420 @code{infix (@var{op}, @var{lbp}, @var{rbp}, @var{lpos}, @var{rpos}, @var{pos})}
1421 declares @var{op} to be an infix operator with stated left and right binding
1422 powers and parts of speech @var{lpos}, @var{rpos}, and @var{pos} for the left
1423 operand, the right operand, and the operator result, respectively.
1425 "Part of speech", in reference to operator declarations, means expression type.
1426 Three types are recognized: @code{expr}, @code{clause}, and @code{any},
1427 indicating an algebraic expression, a Boolean expression, or any kind of
1428 expression, respectively. Maxima can detect some syntax errors by comparing the
1429 declared part of speech to an actual expression.
1431 The precedence of @var{op} with respect to other operators derives from the left
1432 and right binding powers of the operators in question. If the left and right
1433 binding powers of @var{op} are both greater the left and right binding powers of
1434 some other operator, then @var{op} takes precedence over the other operator.
1435 If the binding powers are not both greater or less, some more complicated
1438 The associativity of @var{op} depends on its binding powers. Greater left
1439 binding power (@var{lbp}) implies an instance of @var{op} is evaluated before
1440 other operators to its left in an expression, while greater right binding power
1441 (@var{rbp}) implies an instance of @var{op} is evaluated before other operators
1442 to its right in an expression. Thus greater @var{lbp} makes @var{op}
1443 right-associative, while greater @var{rbp} makes @var{op} left-associative.
1444 If @var{lbp} is equal to @var{rbp}, @var{op} is left-associative.
1446 See also @ref{Introduction to operators}.
1450 If the left and right binding powers of @var{op} are both greater
1451 the left and right binding powers of some other operator,
1452 then @var{op} takes precedence over the other operator.
1455 @c :lisp (get '$+ 'lbp)
1456 @c :lisp (get '$+ 'rbp)
1457 @c infix ("##", 101, 101);
1458 @c "##"(a, b) := sconcat("(", a, ",", b, ")");
1460 @c infix ("##", 99, 99);
1464 (%i1) :lisp (get '$+ 'lbp)
1466 (%i1) :lisp (get '$+ 'rbp)
1468 (%i1) infix ("##", 101, 101);
1470 (%i2) "##"(a, b) := sconcat("(", a, ",", b, ")");
1471 (%o2) (a ## b) := sconcat("(", a, ",", b, ")")
1472 (%i3) 1 + a ## b + 2;
1474 (%i4) infix ("##", 99, 99);
1476 (%i5) 1 + a ## b + 2;
1480 Greater @var{lbp} makes @var{op} right-associative,
1481 while greater @var{rbp} makes @var{op} left-associative.
1484 @c infix ("##", 100, 99);
1485 @c "##"(a, b) := sconcat("(", a, ",", b, ")")$
1486 @c foo ## bar ## baz;
1487 @c infix ("##", 100, 101);
1488 @c foo ## bar ## baz;
1491 (%i1) infix ("##", 100, 99);
1493 (%i2) "##"(a, b) := sconcat("(", a, ",", b, ")")$
1494 (%i3) foo ## bar ## baz;
1495 (%o3) (foo,(bar,baz))
1496 (%i4) infix ("##", 100, 101);
1498 (%i5) foo ## bar ## baz;
1499 (%o5) ((foo,bar),baz)
1502 Maxima can detect some syntax errors by comparing the
1503 declared part of speech to an actual expression.
1506 @c infix ("##", 100, 99, expr, expr, expr);
1507 @c if x ## y then 1 else 0;
1508 @c infix ("##", 100, 99, expr, expr, clause);
1509 @c if x ## y then 1 else 0;
1512 (%i1) infix ("##", 100, 99, expr, expr, expr);
1514 (%i2) if x ## y then 1 else 0;
1515 Incorrect syntax: Found algebraic expression where logical
1519 (%i2) infix ("##", 100, 99, expr, expr, clause);
1521 (%i3) if x ## y then 1 else 0;
1522 (%o3) if x ## y then 1 else 0
1525 @opencatbox{Categories:}
1526 @category{Operators}
1527 @category{Declarations and inferences}
1532 @c -----------------------------------------------------------------------------
1534 @deffn {Function} matchfix @
1535 @fname{matchfix} (@var{ldelimiter}, @var{rdelimiter}) @
1536 @fname{matchfix} (@var{ldelimiter}, @var{rdelimiter}, @var{arg_pos}, @var{pos})
1538 Declares a matchfix operator with left and right delimiters @var{ldelimiter}
1539 and @var{rdelimiter}. The delimiters are specified as strings.
1541 A "matchfix" operator is a function of any number of arguments,
1542 such that the arguments occur between matching left and right delimiters.
1543 The delimiters may be any strings, so long as the parser can
1544 distinguish the delimiters from the operands
1545 and other expressions and operators.
1546 In practice this rules out unparseable delimiters such as
1547 @code{%}, @code{,}, @code{$} and @code{;},
1548 and may require isolating the delimiters with white space.
1549 The right delimiter can be the same or different from the left delimiter.
1551 A left delimiter can be associated with only one right delimiter;
1552 two different matchfix operators cannot have the same left delimiter.
1554 An existing operator may be redeclared as a matchfix operator
1555 without changing its other properties.
1556 In particular, built-in operators such as addition @code{+} can
1557 be declared matchfix,
1558 but operator functions cannot be defined for built-in operators.
1560 The command @code{matchfix (@var{ldelimiter}, @var{rdelimiter}, @var{arg_pos},
1561 @var{pos})} declares the argument part-of-speech @var{arg_pos} and result
1562 part-of-speech @var{pos}, and the delimiters @var{ldelimiter} and
1565 "Part of speech", in reference to operator declarations, means expression type.
1566 Three types are recognized: @code{expr}, @code{clause}, and @code{any},
1567 indicating an algebraic expression, a Boolean expression, or any kind of
1568 expression, respectively.
1569 Maxima can detect some syntax errors by comparing the
1570 declared part of speech to an actual expression.
1572 @c DUNNO IF WE REALLY NEED TO MENTION BINDING POWER HERE -- AS NOTED IT'S IRRELEVANT
1573 @c An operator declared by @code{matchfix} is assigned a low binding power.
1574 @c Since a matchfix operator must be evaluated before any expression
1575 @c which contains it,
1576 @c binding power is effectively irrelevant
1577 @c to the declaration of a matchfix operator.
1579 The function to carry out a matchfix operation is an ordinary
1580 user-defined function.
1581 The operator function is defined
1583 with the function definition operator @code{:=} or @code{define}.
1584 The arguments may be written between the delimiters,
1585 or with the left delimiter as a quoted string and the arguments
1586 following in parentheses.
1587 @code{dispfun (@var{ldelimiter})} displays the function definition.
1589 The only built-in matchfix operator is the list constructor @code{[ ]}.
1590 Parentheses @code{( )} and double-quotes @code{" "}
1591 act like matchfix operators,
1592 but are not treated as such by the Maxima parser.
1594 @code{matchfix} evaluates its arguments.
1595 @code{matchfix} returns its first argument, @var{ldelimiter}.
1596 @c HOW TO TAKE AWAY THE MATCHFIX PROPERTY ??
1600 Delimiters may be almost any strings.
1603 @c matchfix ("@@", "~");
1605 @c matchfix (">>", "<<");
1607 @c matchfix ("foo", "oof");
1609 @c >> w + foo x, y oof + z << / @@ p, q ~;
1612 (%i1) matchfix ("@@@@", "~");
1614 (%i2) @@@@ a, b, c ~;
1616 (%i3) matchfix (">>", "<<");
1618 (%i4) >> a, b, c <<;
1620 (%i5) matchfix ("foo", "oof");
1622 (%i6) foo a, b, c oof;
1624 (%i7) >> w + foo x, y oof + z << / @@@@ p, q ~;
1625 >>z + foox, yoof + w<<
1626 (%o7) ----------------------
1630 Matchfix operators are ordinary user-defined functions.
1633 (%i1) matchfix ("!-", "-!");
1635 (%i2) !- x, y -! := x/y - y/x;
1637 (%o2) !-x, y-! := - - -
1639 (%i3) define (!-x, y-!, x/y - y/x);
1641 (%o3) !-x, y-! := - - -
1643 (%i4) define ("!-" (x, y), x/y - y/x);
1645 (%o4) !-x, y-! := - - -
1647 (%i5) dispfun ("!-");
1649 (%t5) !-x, y-! := - - -
1663 @opencatbox{Categories:}
1665 @category{Operators}
1669 @c -----------------------------------------------------------------------------
1670 @anchor{function_nary}
1671 @deffn {Function} nary @
1672 @fname{nary} (@var{op}) @
1673 @fname{nary} (@var{op}, @var{bp}, @var{arg_pos}, @var{pos})
1675 An @code{nary} operator is used to denote a function of any number of arguments,
1676 each of which is separated by an occurrence of the operator, e.g. A+B or A+B+C.
1677 The @code{nary("x")} function is a syntax extension function to declare @code{x}
1678 to be an @code{nary} operator. Functions may be declared to be @code{nary}. If
1679 @code{declare(j,nary);} is done, this tells the simplifier to simplify, e.g.
1680 @code{j(j(a,b),j(c,d))} to @code{j(a, b, c, d)}.
1682 See also @ref{Introduction to operators}.
1684 @opencatbox{Categories:}
1685 @category{Operators}
1690 @c -----------------------------------------------------------------------------
1692 @deffn {Function} nofix @
1693 @fname{nofix} (@var{op}) @
1694 @fname{nofix} (@var{op}, @var{pos})
1696 @code{nofix} operators are used to denote functions of no arguments.
1697 The mere presence of such an operator in a command will cause the
1698 corresponding function to be evaluated. For example, when one types
1699 "exit;" to exit from a Maxima break, "exit" is behaving similar to a
1700 @code{nofix} operator. The function @code{nofix("x")} is a syntax extension
1701 function which declares @code{x} to be a @code{nofix} operator.
1703 See also @ref{Introduction to operators}.
1705 @opencatbox{Categories:}
1706 @category{Operators}
1711 @c -----------------------------------------------------------------------------
1713 @deffn {Function} postfix @
1714 @fname{postfix} (@var{op}) @
1715 @fname{postfix} (@var{op}, @var{lbp}, @var{lpos}, @var{pos})
1717 @code{postfix} operators like the @code{prefix} variety denote functions of a
1718 single argument, but in this case the argument immediately precedes an
1719 occurrence of the operator in the input string, e.g. 3!. The
1720 @code{postfix("x")} function is a syntax extension function to declare @code{x}
1721 to be a @code{postfix} operator.
1723 See also @ref{Introduction to operators}.
1725 @opencatbox{Categories:}
1726 @category{Operators}
1731 @c -----------------------------------------------------------------------------
1733 @deffn {Function} prefix @
1734 @fname{prefix} (@var{op}) @
1735 @fname{prefix} (@var{op}, @var{rbp}, @var{rpos}, @var{pos})
1737 A @code{prefix} operator is one which signifies a function of one argument,
1738 which argument immediately follows an occurrence of the operator.
1739 @code{prefix("x")} is a syntax extension function to declare @code{x} to be a
1740 @code{prefix} operator.
1742 See also @ref{Introduction to operators}.
1744 @opencatbox{Categories:}
1745 @category{Operators}