Next: Relational operators, Previous: Introduction to operators, Up: Operators [Contents][Index]

### 7.2 Arithmetic operators

Operator: +
Operator: -
Operator: *
Operator: /
Operator: ^

  The symbols `+` `*` `/` and `^` represent addition, multiplication, division, and exponentiation, respectively. The names of these operators are `"+"` `"*"` `"/"` and `"^"`, which may appear where the name of a function or operator is required.

  The symbols `+` and `-` represent unary addition and negation, respectively, and the names of these operators are `"+"` and `"-"`, respectively.

  Subtraction `a - b` is represented within Maxima as addition, `a + (- b)`. Expressions such as `a + (- b)` are displayed as subtraction. Maxima recognizes `"-"` only as the name of the unary negation operator, and not as the name of the binary subtraction operator.

  Division `a / b` is represented within Maxima as multiplication, `a * b^(- 1)`. Expressions such as `a * b^(- 1)` are displayed as division. Maxima recognizes `"/"` as the name of the division operator.

  Addition and multiplication are n-ary, commutative operators. Division and exponentiation are binary, noncommutative operators.

  Maxima sorts the operands of commutative operators to construct a canonical representation. For internal storage, the ordering is determined by `orderlessp`. For display, the ordering for addition is determined by `ordergreatp`, and for multiplication, it is the same as the internal ordering.

  Arithmetic computations are carried out on literal numbers (integers, rationals, ordinary floats, and bigfloats). Except for exponentiation, all arithmetic operations on numbers are simplified to numbers. Exponentiation is simplified to a number if either operand is an ordinary float or bigfloat or if the result is an exact integer or rational; otherwise an exponentiation may be simplified to `sqrt` or another exponentiation or left unchanged.

  Floating-point contagion applies to arithmetic computations: if any operand is a bigfloat, the result is a bigfloat; otherwise, if any operand is an ordinary float, the result is an ordinary float; otherwise, the operands are rationals or integers and the result is a rational or integer.

  Arithmetic computations are a simplification, not an evaluation. Thus arithmetic is carried out in quoted (but simplified) expressions.

  Arithmetic operations are applied element-by-element to lists when the global flag `listarith` is `true`, and always applied element-by-element to matrices. When one operand is a list or matrix and another is an operand of some other type, the other operand is combined with each of the elements of the list or matrix.

  Examples:

  Addition and multiplication are n-ary, commutative operators. Maxima sorts the operands to construct a canonical representation. The names of these operators are `"+"` and `"*"`.

      (%i1) c + g + d + a + b + e + f;
      (%o1)               g + f + e + d + c + b + a
      (%i2) [op (%), args (%)];
      (%o2)              [+, [g, f, e, d, c, b, a]]
      (%i3) c * g * d * a * b * e * f;
      (%o3)                     a b c d e f g
      (%i4) [op (%), args (%)];
      (%o4)              [*, [a, b, c, d, e, f, g]]
      (%i5) apply ("+", [a, 8, x, 2, 9, x, x, a]);
      (%o5)                    3 x + 2 a + 19
      (%i6) apply ("*", [a, 8, x, 2, 9, x, x, a]);
                                       2  3
      (%o6)                       144 a  x

  Division and exponentiation are binary, noncommutative operators. The names of these operators are `"/"` and `"^"`.

      (%i1) [a / b, a ^ b];
                                    a   b
      (%o1)                        [-, a ]
                                    b
      (%i2) [map (op, %), map (args, %)];
      (%o2)              [[/, ^], [[a, b], [a, b]]]
      (%i3) [apply ("/", [a, b]), apply ("^", [a, b])];
                                    a   b
      (%o3)                        [-, a ]
                                    b

  Subtraction and division are represented internally in terms of addition and multiplication, respectively.

      (%i1) [inpart (a - b, 0), inpart (a - b, 1), inpart (a - b, 2)];
      (%o1)                      [+, a, - b]
      (%i2) [inpart (a / b, 0), inpart (a / b, 1), inpart (a / b, 2)];
                                         1
      (%o2)                       [*, a, -]
                                         b

  Computations are carried out on literal numbers. Floating-point contagion applies.

      (%i1) 17 + b - (1/2)*29 + 11^(2/4);
                                             5
      (%o1)                   b + sqrt(11) + -
                                             2
      (%i2) [17 + 29, 17 + 29.0, 17 + 29b0];
      (%o2)                   [46, 46.0, 4.6b1]

  Arithmetic computations are a simplification, not an evaluation.

      (%i1) simp : false;
      (%o1)                         false
      (%i2) '(17 + 29*11/7 - 5^3);
                                    29 11    3
      (%o2)                    17 + ----- - 5
                                      7
      (%i3) simp : true;
      (%o3)                         true
      (%i4) '(17 + 29*11/7 - 5^3);
                                      437
      (%o4)                         - ---
                                       7

  Arithmetic is carried out element-by-element for lists (depending on `listarith`) and matrices.

      (%i1) matrix ([a, x], [h, u]) - matrix ([1, 2], [3, 4]);

                              [ a - 1  x - 2 ]
      (%o1)                   [              ]
                              [ h - 3  u - 4 ]

      (%i2) 5 * matrix ([a, x], [h, u]);
                                [ 5 a  5 x ]
      (%o2)                     [          ]
                                [ 5 h  5 u ]
      (%i3) listarith : false;
      (%o3)                         false
      (%i4) [a, c, m, t] / [1, 7, 2, 9];
                                [a, c, m, t]
      (%o4)                     ------------
                                [1, 7, 2, 9]
      (%i5) [a, c, m, t] ^ x;
                                            x
      (%o5)                     [a, c, m, t]
      (%i6) listarith : true;
      (%o6)                         true
      (%i7) [a, c, m, t] / [1, 7, 2, 9];
                                    c  m  t
      (%o7)                     [a, -, -, -]
                                    7  2  9
      (%i8) [a, c, m, t] ^ x;
                                x   x   x   x
      (%o8)                   [a , c , m , t ]

  ·

  Categories: Operators

Operator: **

  Exponentiation operator. Maxima recognizes `**` as the same operator as `^` in input, and it is displayed as `^` in 1-dimensional output, or by placing the exponent as a superscript in 2-dimensional output.

  The `fortran` function displays the exponentiation operator as `**`, whether it was input as `**` or `^`.

  Examples:

      (%i1) is (a**b = a^b);
      (%o1)                         true
      (%i2) x**y + x^z;
                                    z    y
      (%o2)                        x  + x
      (%i3) string (x**y + x^z);
      (%o3)                        x^z+x^y
      (%i4) fortran (x**y + x^z);
            x**z+x**y
      (%o4)                         done

  ·

  Categories: Operators

Operator: ^^

  Noncommutative exponentiation operator. `^^` is the exponentiation operator corresponding to noncommutative multiplication `.`, just as the ordinary exponentiation operator `^` corresponds to commutative multiplication `*`.

  Noncommutative exponentiation is displayed by `^^` in 1-dimensional output, and by placing the exponent as a superscript within angle brackets `< >` in 2-dimensional output.

  Examples:

      (%i1) a . a . b . b . b + a * a * a * b * b;
                              3  2    <2>    <3>
      (%o1)                  a  b  + a    . b
      (%i2) string (a . a . b . b . b + a * a * a * b * b);
      (%o2)                  a^3*b^2+a^^2 . b^^3

  ·

  Categories: Operators

Operator: .

  The dot operator, for matrix (non-commutative) multiplication. When `"."` is used in this way, spaces should be left on both sides of it, e.g. `A . B` This distinguishes it plainly from a decimal point in a floating point number.

  See also `Dot`, `dot0nscsimp`, `dot0simp`, `dot1simp`, `dotassoc`, `dotconstrules`, `dotdistrib`, `dotexptsimp`, `dotident`, and `dotscrules`.

  ·

  Categories: Operators

Next: Relational operators, Previous: Introduction to operators, Up: Operators [Contents][Index]