Subsections

- Exercises

# Complex numbers

This section describes the mode used to perform complex arithmetic. This kind of arithmetic is useful to engineers, particularly electrical engineers. Even if you know nothing about complex numbers, you may still find this section useful.

The standard prelude contains the mode declaration

       MODE COMPL = STRUCT(REAL re,im)

You can use values based on this mode to perform complex arithmetic. Here are declarations for values of modes `COMPL` and `REF COMPL` respectively:

       COMPL z1 = (2.4,-4.6);
       COMPL z2:=z1

Most of the operators you need to manipulate complex numbers have been declared in the standard prelude.

You can use the monadic operators `+` and `-` which have also been declared for values of mode `COMPL`.

The dyadic operator `**` has been declared for a left-operand of mode `COMPL` and a right-operand of mode `INT`. The dyadic operators `+ - * /` have been declared for all combinations of complex numbers, real numbers and integers, and so have the boolean operators `=` and `/=`. The assigning operators `TIMESAB`, `DIVAB`, `PLUSAB`, and `MINUSAB` all take a left operand of mode `REF COMPL` and a right-operand of modes `INT`, `REAL` or `COMPL`. In a strong context, a real number will be widened to a complex number. So, for example, in the following identity declaration

       COMPL z3 = -3.4

`z3` will have the same value as if it had been declared by

       COMPL z3 = (-3.4,0)

This is the only case where a real number can be widened into a structure.

The dyadic operator `I` takes left- and right-operands of any combination of `REAL` and `INT` and yields a complex number. It has a priority of 9. For example, in a formula, the context of operands is firm and so widening is not allowed. Nevertheless, the yield of this formula is `COMPL`:

       2 * 3 I 4

Some operators act only on complex numbers. The monadic operator `RE` takes a `COMPL` operand and yields its `re` field with mode `REAL`. Likewise, the monadic operator `IM` takes an operand of mode `COMPL` and yields its `im` field with mode `REAL`. For example, given the declaration above of `z3`, `RE z3` would yield `-3.4`, and `IM z3` would yield `0.0`. Given the complex number `z` declared as

       COMPL z = 2 I 3

then `CONJ z` would yield `RE z I - IM z` or `(2.0,-3.0)`. The operator `ARG` gives the argument of its operand: `ARG z` would yield 0⋅982 793 723 2, lying in the interval (-π,π]. The monadic operator `ABS` with a complex number may be defined as

       OP ABS = (COMPL z)REAL:
          sqrt(RE z**2 + IM z**2)

Remember that in the formula `RE z**2`, the operator `RE` is monadic and so is elaborated first.

As described in the previous section, the mode `COMPL` can be used wherever a mode is required. In particular, procedures taking `COMPL` parameters and yielding `COMPL` values can be declared. Structures containing `COMPL` can be declared as above.

From the section on field selection, it is clear that in the declarations

       COMPL z = (2.0,3.0);
       COMPL w:=z

the selection

       re OF z

has mode `REAL` (and value `2.0`), while the selection

       re OF w

has mode `REF REAL` (and its value is a name). However, the formula

       RE w

still yields a value of mode `REAL` because `RE` is an operator whose single operand has mode `COMPL`. In the above phrase, the `w` will be dereferenced before `RE` is elaborated. Thus it is quite legal to write

       im OF w:=RE w

or

       im OF w:=re OF w

in which case the right-hand side of the assignment will be dereferenced before a copy is assigned.

## Exercises

7.8
  If the complex number `za` has the mode `COMPL` and the value yielded by `(2,-3)`, what is the value of Ans
  (a)
    `CONJ za`

  (b)
    `IM za * RE za * RE za`

  (c)
    `ABS za`

  (d)
    `ARG za`

7.9
  What is the value of the formula `23 - 11 I -10`? Ans
7.10
  Given the declarations

         COMPL a = 2 I 3;
         COMPL b:= CONJ a

  what is the mode and value of each of the following: Ans
  (a)
    `im OF b`

  (b)
    `IM b`

  (c)
    `im OF a`

  (d)
    `IM a`