### 7.1 Introduction to operators

It is possible to define new operators with specified precedence, to undefine existing operators, or to redefine the precedence of existing operators. An operator may be unary prefix or unary postfix, binary infix, n-ary infix, matchfix, or nofix. "Matchfix" means a pair of symbols which enclose their argument or arguments, and "nofix" means an operator which takes no arguments. As examples of the different types of operators, there are the following.

unary prefix

  negation `- a`

unary postfix

  factorial `a!`

binary infix

  exponentiation `a^b`

n-ary infix

  addition `a + b`

matchfix

  list construction `[a, b]`

(There are no built-in nofix operators; for an example of such an operator, see `nofix`.)

The mechanism to define a new operator is straightforward. It is only necessary to declare a function as an operator; the operator function might or might not be defined.

An example of user-defined operators is the following. Note that the explicit function call `"dd" (a)` is equivalent to `dd a`, likewise `"<-" (a, b)` is equivalent to `a <- b`. Note also that the functions `"dd"` and `"<-"` are undefined in this example.

    (%i1) prefix ("dd");
    (%o1)                          dd
    (%i2) dd a;
    (%o2)                         dd a
    (%i3) "dd" (a);
    (%o3)                         dd a
    (%i4) infix ("<-");
    (%o4)                          <-
    (%i5) a <- dd b;
    (%o5)                      a <- dd b
    (%i6) "<-" (a, "dd" (b));
    (%o6)                      a <- dd b

The Maxima functions which define new operators are summarized in this table, stating the default left and right binding powers (lbp and rbp, respectively). (Binding power determines operator precedence. However, since left and right binding powers can differ, binding power is somewhat more complicated than precedence.) Some of the operation definition functions take additional arguments; see the function descriptions for details.

`prefix`

  rbp=180

`postfix`

  lbp=180

`infix`

  lbp=180, rbp=180

`nary`

  lbp=180, rbp=180

`matchfix`

  (binding power not applicable)

`nofix`

  (binding power not applicable)

For comparison, here are some built-in operators and their left and right binding powers.

    Operator   lbp     rbp

      :        180     20
      ::       180     20
      :=       180     20
      ::=      180     20
      !        160
      !!       160
      ^        140     139
      .        130     129
      *        120
      /        120     120
      +        100     100
      -        100     134
      =        80      80
      #        80      80
      >        80      80
      >=       80      80
      <        80      80
      <=       80      80
      not              70
      and      65
      or       60
      ,        10
      $        -1
      ;        -1

`remove` and `kill` remove operator properties from an atom. `remove ("a", op)` removes only the operator properties of a. `kill ("a")` removes all properties of a, including the operator properties. Note that the name of the operator must be enclosed in quotation marks.

    (%i1) infix ("##");
    (%o1)                          ##
    (%i2) "##" (a, b) := a^b;
                                         b
    (%o2)                     a ## b := a
    (%i3) 5 ## 3;
    (%o3)                          125
    (%i4) remove ("##", op);
    (%o4)                         done
    (%i5) 5 ## 3;
    Incorrect syntax: # is not a prefix operator
    5 ##
      ^
    (%i5) "##" (5, 3);
    (%o5)                          125
    (%i6) infix ("##");
    (%o6)                          ##
    (%i7) 5 ## 3;
    (%o7)                          125
    (%i8) kill ("##");
    (%o8)                         done
    (%i9) 5 ## 3;
    Incorrect syntax: # is not a prefix operator
    5 ##
      ^
    (%i9) "##" (5, 3);
    (%o9)                       ##(5, 3)

·

Categories: Operators ·Syntax