rslt ← {larg} (fn ##.cx) rarg                       ⍝ Complex arithmetic.

A  complex  array of shape S is represented by a numeric array of shape 2,S with
real  and  imaginary components along the first axis. Operand function fn may be
one of the scalar primitive functions from the following table:
·
        ┌───────────────────────────┬───────────────────────────┐
        │   monadic                 │  dyadic                   │
    ┌───┼───────────────────────────┼───────────────────────────┤
    │ + │   conjugate               │  sum                      │
    ├───┼───────────────────────────┼───────────────────────────┤
    │ - │   negation                │  difference               │
    ├───┼───────────────────────────┼───────────────────────────┤
    │ × │   direction               │  product                  │
    ├───┼───────────────────────────┼───────────────────────────┤
    │ ÷ │   reciprocal              │  quotient                 │
    ├───┼───────────────────────────┼───────────────────────────┤
    │ ∨ │                           │  greatest common divisor  │
    ├───┼───────────────────────────┼───────────────────────────┤
    │ ^ │                           │  least common multiple    │
    ├───┼───────────────────────────┼───────────────────────────┤
    │ * │   exponential             │  power                    │
    ├───┼───────────────────────────┼───────────────────────────┤
    │ ⍟ │   natural logarithm       │  ⍺-logarithm              │
    ├───┼───────────────────────────┼───────────────────────────┤
    │ | │   magnitude               │  complex residue          │
    ├───┼───────────────────────────┼───────────────────────────┤
    │ ⌊ │   complex floor           │                           │
    ├───┼───────────────────────────┼───────────────────────────┤
    │ ⌈ │   complex ceiling         │                           │
    └───┴───────────────────────────┴───────────────────────────┘

Or one of the dyadic circle functions from:

    ┌─────┬─────────────────────┬────┬──────────────────────────┐
    │  0○ │ (1-⍵*2)*÷2          │    │                          │
    ├─────┼─────────────────────┼────┼──────────────────────────┤
    │  1○ │ sin ⍵               │ ¯1○│ arcsin ⍵                 │
    ├─────┼─────────────────────┼────┼──────────────────────────┤
    │  2○ │ cos ⍵               │ ¯2○│ arccos ⍵                 │
    ├─────┼─────────────────────┼────┼──────────────────────────┤
    │  3○ │ tan ⍵               │ ¯3○│ arctan ⍵                 │
    ├─────┼─────────────────────┼────┼──────────────────────────┤
    │  4○ │ (1+⍵*2)*÷2          │ ¯4○│ (⍵+1)×((⍵-1)÷⍵+1)*÷2     │
    ├─────┼─────────────────────┼────┼──────────────────────────┤
    │  5○ │ sinh ⍵              │ ¯5○│ arsinh ⍵                 │
    ├─────┼─────────────────────┼────┼──────────────────────────┤
    │  6○ │ cosh ⍵              │ ¯6○│ arcosh ⍵                 │
    ├─────┼─────────────────────┼────┼──────────────────────────┤
    │  7○ │ tanh ⍵              │ ¯7○│ artanh ⍵                 │
    ├─────┼─────────────────────┼────┼──────────────────────────┤
    │  8○ │ (¯1-⍵*2)*÷2         │ ¯8○│ -(¯1-⍵*2)*÷2             │
    ├─────┼─────────────────────┼────┼──────────────────────────┤
    │  9○ │ real part of ⍵      │ ¯9○│ identity ⍵               │
    ├─────┼─────────────────────┼────┼──────────────────────────┤
    │ 10○ │ magnitude |⍵        │¯10○│ conjugate +⍵             │
    ├─────┼─────────────────────┼────┼──────────────────────────┤
    │ 11○ │ imaginary part of ⍵ │¯11○│ 0j1×⍵                    │
    ├─────┼─────────────────────┼────┼──────────────────────────┤
    │ 12○ │ phase ⍵             │¯12○│ *0j1×⍵                   │
    └─────┴─────────────────────┴────┴──────────────────────────┘

Or matrix inverse/divide:

        ┌───────────────────────────┬───────────────────────────┐
        │   monadic                 │  dyadic                   │
    ┌───┼───────────────────────────┼───────────────────────────┤
    │ ⌹ │   matrix inverse          │  matrix divide            │
    └───┴───────────────────────────┴───────────────────────────┘

Or one of the monadic special formatting functions:

    ┌───┬───────────────────────────────────────────────────────┐
    │ ⍕ │ j-format of complex argument array ⍵                  │
    ├───┼───────────────────────────────────────────────────────┤
    │ ⍎ │  complex array from j-format character vector ⍵       │
    └───┴───────────────────────────────────────────────────────┘

We  choose to arrange the complex coordinates along the _first_, rather than the
_last_ axis because:

- Large complex arrays are more legible:

  pairs along:  first axis      last axis
                ¯¯¯¯¯¯¯¯¯¯      ¯¯¯¯¯¯¯¯¯
                  ¯1  0  1          ¯1  1        ⍝ compass points.
                  ¯1  0  1           0  1
                  ¯1  0  1           1  1

                   1  1  1          ¯1  0
                   0  0  0           0  0
                  ¯1 ¯1 ¯1           1  0

                                    ¯1 ¯1
                                     0 ¯1
                                     1 ¯1

- Scalar-function-with-axis is marginally slicker along the first axis.

  pairs along:  first axis      last axis
                ¯¯¯¯¯¯¯¯¯¯      ¯¯¯¯¯¯¯¯¯
                ⍺+[0]⍵          ⍺+[¯1+⍴⍴⍵]⍵

Branch Cuts
-----------
When converting from cartesian to polar coordinates, for example, we must decide
whether  to map the phase angle into the range ○0 2 or to the range ○¯1 1 (say).
Such a choice defines a line of discontinuity through the complex plane from the
origin  to  infinity;  points close to, but on different sides of the line, will
have phase angles that differ by close to ○2.

Points  _on_  the line can be continuous with those immediately to one side _or_
to  the  other,  but  not both. This line is termed a "branch cut" (of the phase
function  12○⍵).  For  the  phase function in particular, we choose to place the
branch cut along the negative real axis and to be continuous with points above.

Penfield[4] suggests the following diagram to illustrate these choices,  showing
the Domain and Range for the phase function 12○⍵:
    ¯¯¯        ¯ ¯¯
    ┌12○⍵─phase──Dom┐┌12○⍵────────Rng┐
    │       │       ││       │       │ ∨∨∨ branch cut (continuous above).
    │       │       ││       │       │   ⍟ included point.
    │       │       ││       │       │   ○ excluded point.
    │∨∨∨∨∨∨∨⍟───────││───○⍟⍟⍟⍟⍟⍟⍟⍟───│
    │       │       ││  -pi  │   +pi │
    │       │       ││       │       │
    │       │       ││       │       │
    └───────────────┘└───────────────┘

In the diagram, ∨∨∨∨ indicates continuity with points above the branch cut. This
is intended to be _visually_ intuitive: points _on_ the branch cut are notional-
ly _within_ the V-shapes and are thus continuous with points above it and separ-
ated from (discontinuous) points below, as the following close-up shows:

          .       .       .       .      ← region continuous with branch cut.
       \     / \     / \     / \     /
        \ ∘ /   \ ∘ /   \ ∘ /   \ ∘ /    ← ∘∘∘ points on branch cut.
         \_/     \_/     \_/     \_/
      .       .       .       .      .   ← region not continuous with cut.

Another example might be the square-root function *∘(÷2):

    ┌⍵*÷2─sqrt───Dom┐┌⍵*÷2────────Rng┐   ⍟ points included in range /////
    │       │       ││       ⍟///////│   ⍟        ¯¯                /////
    │       │       ││       ⍟///////│   ⍟                          /////
    │       │       ││       ⍟///////│
    │∨∨∨∨∨∨∨⍟───────││───────⍟───────│
    │       │       ││       ○///////│   ○ points excluded from range.
    │       │       ││       ○///////│   ○        ¯¯
    │       │       ││       ○///////│   ○
    └───────────────┘└───────────────┘

Some functions, such as +, are continuous everywhere and so have no branch cuts.

Otherwise:

    ┌⍺*⍵─power───Dom┐┌⍺*⍵─────────Rng┐    ┌─⍟⍵─log─────Dom┐┌⍟⍵──────────Rng┐
    │       │       ││///////│///////│    │       │       ││  pi   │       │
    │       │       ││///////│///////│    │       │       ││⍟⍟⍟⍟⍟⍟⍟⍟⍟⍟⍟⍟⍟⍟⍟│
    │       │       ││///////│///////│    │       │       ││///////│///////│
    │∨∨∨∨∨∨∨○───────││───────┼───────│    │∨∨∨∨∨∨∨○───────││───────┼───────│
    │       │       ││///////│///////│    │       │       ││///////│///////│
    │       │       ││///////│///////│    │       │       ││○○○○○○○○○○○○○○○│
    │       │       ││///////│///////│    │       │       ││ ¯pi   │       │
    └───────────────┘└───────────────┘    └───────────────┘└───────────────┘

    ┌0○⍵─────────Dom┐┌0○⍵─────────Rng┐    ┌4○⍵─────────Dom┐┌4○⍵─────────Rng┐
    │       │       ││       ⍟///////│    │       <       ││       ⍟///////│
    │       │       ││       ⍟///////│    │      1⍟       ││       ⍟///////│
    │  ¯1   │   1   ││       ⍟///////│    │       │       ││       ⍟///////│
    │∨∨∨⍟───┼───⍟^^^││───────⍟───────│    │───────┼───────││───────⍟───────│
    │       │       ││       ○///////│    │       │       ││       ○///////│
    │       │       ││       ○///////│    │     ¯1⍟       ││       ○///////│
    │       │       ││       ○///////│    │       >       ││       ○///////│
    └───────────────┘└───────────────┘    └───────────────┘└───────────────┘

    ┌8○⍵─────────Dom┐┌8○⍵─────────Rng┐    ┌12○⍵─phase──Dom┐┌12○⍵────────Rng┐
    │       │       ││///////│///////│    │       │       ││       │       │
    │      1⍟       ││///////│///////│    │       │       ││       │       │
    │       >       ││///////│///////│    │       │       ││  ¯pi  │   pi  │
    │───────○───────││───────┼───────│    │∨∨∨∨∨∨∨⍟───────││───○⍟⍟⍟⍟⍟⍟⍟⍟───│
    │       <       ││///////│///////│    │       │       ││       │       │
    │     ¯1⍟       ││///////│///////│    │       │       ││       │       │
    │       │       ││///////│///////│    │       │       ││       │       │
    └───────────────┘└───────────────┘    └───────────────┘└───────────────┘

    ┌¯1○⍵─arcsin─Dom┐┌¯1○⍵────────Rng┐    ┌¯2○⍵─arccos─Dom┐┌¯2○⍵────────Rng┐
    │       │       ││   ⍟///│///○   │    │       │       ││       ⍟///○   │
    │       │       ││   ⍟///│///○   │    │       │       ││       ⍟///○   │
    │  ¯1   │   1   ││¯pi÷2//│///○   │    │  ¯1   │   1   ││       ⍟///○   │
    │∨∨∨⍟───┼───⍟^^^││───⍟───┼───⍟───│    │∨∨∨⍟───┼───⍟^^^││───────⍟───⍟───│
    │       │       ││   ○///│///pi÷2│    │       │       ││       ○///⍟ pi│
    │       │       ││   ○///│///⍟   │    │       │       ││       ○///⍟   │
    │       │       ││   ○///│///⍟   │    │       │       ││       ○///⍟   │
    └───────────────┘└───────────────┘    └───────────────┘└───────────────┘

    ┌¯3○⍵─arctan─Dom┐┌¯3○⍵────────Rng┐    ┌¯4○⍵────────Dom┐┌¯4○⍵────────Rng┐
    │       >       ││   ⍟///│///○   │    │       │       ││///////│///////│
    │      1○       ││   ⍟///│///○   │    │       │       ││//////1○///////│
    │       │       ││   ⍟///│///pi÷2│    │  ¯1   │   1   ││///////│///////│
    │───────┼───────││───○───┼───○───│    │───⍟^^^○∨∨∨⍟───││───────┼───────│
    │       │       ││¯pi÷2//│///⍟   │    │       │       ││///////│///////│
    │     ¯1○       ││   ○///│///⍟   │    │       │       ││/////¯1○///////│
    │       <       ││   ○///│///⍟   │    │       │       ││///////│///////│
    └───────────────┘└───────────────┘    └───────────────┘└───────────────┘

    ┌¯5○⍵─arsinh─Dom┐┌¯5○⍵────────Rng┐    ┌¯6○⍵─arcosh─Dom┐┌¯6○⍵────────Rng┐
    │       <       ││       │       │    │       │       ││       │       │
    │      1⍟       ││○○○○○○○⍟⍟⍟⍟⍟⍟⍟⍟│    │       │       ││    pi ⍟⍟⍟⍟⍟⍟⍟⍟│
    │       │       ││/pi÷2//│///////│    │       │   1   ││       ⍟///////│
    │───────┼───────││───────┼───────│    │∨∨∨∨∨∨∨∨∨∨∨⍟───││───────⍟───────│
    │       │       ││///////│/¯pi÷2/│    │       │       ││       ○///////│
    │     ¯1⍟       ││⍟⍟⍟⍟⍟⍟⍟⍟○○○○○○○│    │       │       ││   ¯pi ○○○○○○○○│
    │       >       ││       │       │    │       │       ││               │
    └───────────────┘└───────────────┘    └───────────────┘└───────────────┘

    ┌¯7○⍵─artanh─Dom┐┌¯7○⍵────────Rng┐    ┌¯8○⍵────────Dom┐┌¯8○⍵────────Rng┐
    │       │       ││       │       │    │       │       ││///////│///////│
    │       │       ││○○○○○○○○⍟⍟⍟⍟⍟⍟⍟│    │      1⍟       ││//////1○///////│
    │  ¯1   │   1   ││/pi÷2//│///////│    │       >       ││///////│///////│
    │^^^○───┼───○∨∨∨││───────┼───────│    │───────○───────││───────┼───────│
    │       │       ││///////│/¯pi÷2/│    │       <       ││///////│///////│
    │       │       ││⍟⍟⍟⍟⍟⍟⍟○○○○○○○○│    │     ¯1⍟       ││/////¯1○///////│
    │       │       ││       │       │    │       │       ││///////│///////│
    └───────────────┘└───────────────┘    └───────────────┘└───────────────┘

Technical notes:

All  of  the  complex _scalar_ functions  are coded in terms of primitive scalar
functions,  which means that potentially large arrays are processed in parallel.

Matrix  inverse/division uses →gauss_jordan← modified to accept a nested matrix
of complex coordinates.

The function definitions are taken from Penfield [4],  except where these differ
from the extended APL standard [5], as in the case of 8○⍵ and ¯8○⍵.

In what follows,  underscored functions represent their complex counterparts. In
other words, + is a shorthand for (+cx).
             ¯
Sum Difference Product Quotient
-------------------------------
If (a b) and (c d) represent complex numbers a+bi and c+di, then:

    a b+c d == a b+c d                      ⍝ + complex sum.
       ¯                                      ¯
    a b-c d == a b-c d                      ⍝ - complex difference.
       ¯                                      ¯
    a b×c d == (ac-bd)(bc+ad)               ⍝ × complex product.
       ¯                                      ¯
    a b÷c d == (ac+bd)(bc-ad) ÷ +/c d*2     ⍝ ÷ complex quotient.
       ¯                                      ¯
Complex Floor ⌊
------------- ¯
In  the  following  diagram, '⎕'s represent "Gaussian integers"; those points in
the complex plane whose real and imaginary parts are both whole numbers.

Each  oblique  rectangle shows the catchment area for the ⎕ in the centre of its
south-west border.  Each  rectangle's western borders are closed and its eastern
ones are open.

For example, in the following diagram, floor maps all points in the shaded area,
including ⍟s but excluding ○s, to the point ⌹:

            ⎕               ⎕               ⎕
          /'│'\           /'│'\           /'│'\
        /   │  '\       /'  │  '\       /'  │  '\
      /     │    '\   /'    │    '\   /'    │    '\
    .'      │      '\'      │      '\'      │      '\
     '\     │        '\     │        '\     │        '\
       '\   │          '\   │          '\   │          '\
         '\ │            '\ │            '\ │            '\
    ───────'⎕──────────────'⎕──────────────'⎕──────────────'⎕
          /'│'\           ⍟'│'○           /'│'\           /'
        /'  │  '\       ⍟'\\│\\'○       /'  │  '\       /'
      /'    │    '\   ⍟'\\\\│\\\\'○   /'    │    '\   /'
    .'      │      '⍟'\\\\\\│\\\\\\'○'      │      '\'
     '\     │        '⍟\\\\\│\\\\\\\\'○     │        '\
       '\   │          '⍟\\\│\\\\\\\\\\'○   │          '\
         '\ │            '⍟\│\\\\\\\\\\\\\○ │            '\
    ───────'⎕──────────────'⌹───────────────⎕──────────────'⎕
          /'│'\           /'│'⍟\\\\\\\\\\\○'│'\           /'
        /'  │  '\       /'  │  '⍟ \\\\\\○'  │  '\       /'
      /'    │    '\   /'    │    '⍟\\\○'    │    '\   /'
    .'      │      '\'      │      '⍟'      │      '\'
     '\     │        '\     │        '\     │        '\
       '\   │          '\   │          '\   │          '\
         '\ │            '\ │            '\ │            '\
    ───────'⎕──────────────'⎕──────────────'⎕──────────────'⎕
            │'\           /'│'\           /'│'\           /'
            │  '\       /'  │  '\       /'  │  '\       /'
            │    '\   /'    │    '\   /'    │    '\   /'
            │      '⎕'      │      '⎕'      │      '⎕'

exp
---
              ⍵*1   ⍵*2   ⍵*3
    *⍵ == 1 + --- + --- + --- + ...
    ¯         !1    !2    !3
sin
---
          ⍵*1   ⍵*3   ⍵*5   ⍵*7
   1○⍵ == --- - --- + --- - --- + ...
    ¯     !1    !3    !5    !7
cos
---
          ⍵*0   ⍵*2   ⍵*4   ⍵*6
   2○⍵ == --- - --- + --- - --- + ...
    ¯     !0    !2    !4    !6

Other function are defined in terms of the preceding ones:

Residue: ⍺ | ⍵  →  ⍺=0:⍵ ⋄ ⍵-⍺×⌊⍵÷⍺ 
           ¯        ¯       ¯ ¯¯ ¯
NatLog:    ⍟ ⍵  →  (⍟|⍵) j 12○ ⍵          where 12○ is the polar phase angle. 
           ¯         ¯       ¯                    ¯
Power:   ⍺ * ⍵  →  * ⍵ × ⍟ ⍺ 
           ¯       ¯   ¯ ¯
⍺-log:   ⍺ ⍟ ⍵  →  (⍟ ⍵)÷ ⍟ ⍺ 
           ¯        ¯   ¯ ¯
References
----------
[1] McDonnell E.E. "Complex Floor", Proc APL Congress 73 (1973).

[2] McDonnell E.E. "Fuzzy Residue", APL79 (1979).

[3] Forkes D. "Complex Floor Revisited", APL Quote Quad 12.1 (1981) pp 107-111.
    ISBN:0-89791-035-4

[4] Penfield P. Jr. "Principal Values and Branch Cuts in Complex APL", APL Quote
    Quad V12.1 (1981) pp 248-256. ISBN:0-89791-035-4

[5] "Programming Language Extended APL", ISO/IEC 13751:2001 (2001).

Examples:

    ⎕pp←5                                   ⍝ display only 5 sig figs.

    poly←{2 1∘.○(○2÷⍵)×(⍳⍵)-⍳1}             ⍝ ⍵-polygon.

    ⍕cx poly 3                              ⍝ triangle.
1 ¯0.5j0.86603 ¯0.5j¯0.86603

    ⍕cx {⍵×cx ⍵} poly 3                     ⍝ triangle squared.
1 ¯0.5j¯0.86603 ¯0.5j0.86603

⍝ See →cxsh← for further examples.

See also: cxsh rats nats polar

Back to: contents

Back to: Dyalog APL

Trouble seeing APL font?