## `_negate` -- the negative of an expression

#### Introduction

`_negate``(x)` computes the negative of `x`.

#### Call(s)

-x `_negate(x)`

#### Parameters

`x` | - | an arithmetical expression, a polynomial of type `DOM_POLY`, or a set

#### Returns

an arithmetical expression, a polynomial, or a set.

### Overloadable:

`x`

#### Related Functions

`_invert`, `_subtract`, `^`, `/`, `*`, `+`, `-`, `poly`

#### Details

- `-x` is equivalent to the function call `_negate(x)`. It represents the inverse of the element `x` of an additive group. For standard expressions, `-x` is the inverse with respect to the `+` operation.
- The negative of a number of type `Type::Numeric` is returned as a number.
- If `x` is not an element of a libary domain with a `"_negate"` method, `-x` is internally represented as `x*(-1)` = `_mult(x, -1)`.
- If `x` is an element of a domain with a slot `"_negate"`, then this method is used to compute `-x`. Many library domains overload the unary `-` operator by an appropriate `"_negate"` slot.
- If neither `x` nor `y` overload the binary operator `-` by a `"_subtract"` method, the difference `x - y` is equivalent to `x + y*(-1)` = `_plus(x, _mult(y, -1))`.
- The negative of a polynomial of type `DOM_POLY` yields a polynomial with the negative of the original coefficients.
- For finite sets, `-X` is the set {-x; x in X}.
- `_negate` is a function of the system kernel.

#### Example 1

The negative of an expression is the inverse with respect to `+`:

    >> x - x = x + _negate(x)

                                       0 = 0

    >> -1 + x - 2*x + 23

                                      22 - x

Internally, a symbolic `-x` is represented as `x*(-1)` = `_mult(x, -1)`:

    >> type(-x), op(-x, 0), op(-x, 1), op(-x, 2)

                               "_mult", _mult, x, -1

#### Example 2

The negative of a polynomial yields a polynomial:

    >> -poly(x^2 + x - 1, [x])

                                      2
                              poly(- x  - x + 1, [x])

    >> -poly(x, [x], Dom::Integer)

                          poly((-1) x, [x], Dom::Integer)

#### Example 3

For finite sets, `-X` is the set {-x; x in X}:

    >> -{a, b, c}

                                   {-a, -b, -c}

#### Example 4

Various library domains such as matrix domains or residue class domains overload `_negate`:

    >> x := Dom::Matrix(Dom::IntegerMod(7))([2, 10]): x, -x, x + (-x)

                    +-         -+  +-         -+  +-         -+
                    |  2 mod 7  |  |  5 mod 7  |  |  0 mod 7  |
                    |           |, |           |, |           |
                    |  3 mod 7  |  |  4 mod 7  |  |  0 mod 7  |
                    +-         -+  +-         -+  +-         -+

    >> delete x:

#### Example 5

This example demonstrates how to implement a slot `"_negate"` for a domain. The following domain `myString` is to represent character strings. The negative `-x` of such a string `x` is to consist of the characters in reverse order.

The `"new"` method uses `expr2text` to convert any MuPAD object to a string. This string is the internal representation of elements of `myString`. The `"print"` method turns this string into the screen output:

    >> myString := newDomain("myString"):
       myString::new := proc(x)
       begin
         if args(0) = 0 then x := "" end_if;
         case domtype(x)
           of myString do return(x);
           of DOM_STRING do return(new(dom, x));
           otherwise return(new(dom, expr2text(x)));
         end_case
       end_proc:
       myString::print := x -> extop(x, 1):

Without a `"_negate"` method, the system handles elements of this domain like any symbolic object:

    >> x := myString(x): -x, type(-x), op(-x, 0), op(-x, 1), op(-x, 2)

                             -x, "_mult", _mult, x, -1

Now, we implement the `"_negate"` method. There is no need to check the argument, because `_negate(x)` calls this slot if and only if `x` is of type `myString`. The slot uses `revert` to generate the reverted string:

    >> myString::_negate := x -> myString::new(revert(extop(x, 1))):

Now, `myString` objects can be reverted by the `-` operator:

    >> -myString("This is a string")

                                 gnirts a si sihT

In the following call, `myString::_negate` is not called because there is no `"_subtract"` method for `myString` objects:

    >> myString("This is a string") - myString("a string")

                            This is a string - a string

We provide the slots `"_plus"` and `"_subtract"`:

    >> myString::_plus := proc()
       begin
         myString::new(_concat(map(args(), extop, 1))):
       end_proc:
       myString::_subtract := (x, y) -> x + myString::_negate(y):

Now, the `"_negate"` slot is called:

    >> myString("This is a string") - myString("This is a string")

                         This is a stringgnirts a si sihT

    >> delete myString, x:

#### Changes

- no changes

Do you have questions or comments?
Copyright © SciFace Software GmbH & Co. KG 2000