Fifty Shades of J/Chapter 13
Table of Contents ... Glossary ... Previous Chapter ... Next Chapter
Principal Topics
- j. (imaginary / complex), r. (angle / polar), o. (pi times / circle function), +. (real+imaginary / GCD), *. (length+angle / LCM), Cartesian and polar coordinates, complex conjugates, groups, reflections, rotations, de Moivreβs theorem, complex powers and logarithms, determinants, inner product, matrix multiplication, quaternions, hypercomplex numbers
Complex Numbers in J
This article is about the facilities available in J for handling complex numbers, something which is greatly helped by a few simple diagrams. When βtalking mathsβ, the representation will be used to denote , which translates into 0j1 in J.
The two complex number constructors
Complex numbers are readily input using numeric constants, e.g. 12.5j_7.9, but in meaningful applications the components are more likely to be expressions from which complex numbers are constructed. J has two tools for constructing complex numbers, namely j. and r.. These correspond to the two possible representations, namely as 2-lists of Cartesian (that is x-y) coordinates, and as 2-lists of polar coordinates (that is {length, angle}). The way in which j. and r. work is illustrated by
(2 j.1),(2 r.1) NB. the two complex no. constructors 2j1 1.0806j1.68294
The second of these results shows that 2 times the coordinates of the end point of a radius of the unit circle at an angle of 1 radian are approximately (1.08,1.68). For primary input in the form of 2-lists, use insert:
(j./2 1),(r./2 1) 2j1 1.0806j1.68294
Informally, j. compresses x-y coordinates into complex numbers, and r. converts polar representation to complex number form. Monadic j. is dyadic j. with a default left argument of 0, while monadic r. is dyadic r. with a default left argument of 1. It is not a coincidence that these defaults are the identity elements of addition and multiplication. The expression r.k where k is real returns the Cartesian coordinates of the point on the unit circle whose polar coordinates are (1,k). For example,
r.1 NB. coords of radius at 1 radian 0.540302j0.841471
r.k is represented by in maths and thus by ^j.k in J, an operation also available through the circle verb as _12 o.k. The fact that r. and ^j. are synonyms links the two complex number constructors. More generally, the circle functions with arguments in the ranges {9, 10, 11, 12} and {β9, β10, β11, β12} are directly relevant to complex number construction; and they, too, have synonyms that will emerge as the discussion continues. The equivalence of r. and ^j. will come to the fore later when discussing complex powers and logarithms.
The complex number deconstructors
The construction process is reversed (that is complex numbers are converted back to 2-lists) by +. for Cartesian coordinates and *. for polar coordinates:
+. 2j1 1.0806j1.682941 NB. +. reverses j./ 2 1 1.0806 1.68294 *. 2j1 1.0806j1.682941 NB. *. reverses r./ 2.23607 0.463648 2 1
The circle verb provides the opportunity to obtain the components of +. and *. one by one:
9 11 o. 2j1 NB. 9 o. is x , 11 o. is y 2 1 10 12 o. 2j1 NB. 10 o. is length, 12 o. is angle 2.23607 0.463648
The following is a βrule of thumbβ table that summarises the meanings of the circle verbs and incorporates the above ideas:
n o. | n o. | ||
---|---|---|---|
_9 | identity | ||
_10 | conjugate | ||
construct | deconstruct | ||
_11 | j. | +. | 9, 11 |
_12 | r. or ^j. | *. | 10, 12 |
Monadic operations with complex numbers
J provides alternative routes for several common complex number operations. In the diagram below, a complex number z is represented by the arrowed line, and other points represent the results of the fundamental monadic arithmetic operations of addition, subtraction, multiplication and division, separately and in combination with j. as well as those of the circle functions that are synonyms.
The symbol Β± is used to denote either of the equivalent verbs +@- or -@+. The points z, Β±z, -z, and +z represent a rectangle formed by reflections in the x and y axes with vertices visited anti-clockwise, while the points j.z, Β±j.z, -j.z, and +j.z represent a rectangle formed by reflections in the diagonal axes with vertices visited clockwise. In addition to the three circle function synonyms shown in the diagram, _12 o. is a synonym for r., as noted earlier.
The symmetries of rectangles can be represented by groups of verbs of order 4 in which I is the identity verb
rotations | {I, -, j., -j.} | j. and -j. denote anti-clockwise/clockwise rotations by |
reflections | {I, -, +, Β±} | + and Β± denote reflections in the main axes |
{I, -, Β±@j., +@j.} | Β±@j., and +@j. denote reflections in diagonal axes |
With these as starting points, the full order-8 group table for the symmetries of the square can easily be obtained. See Essay #26: Working in Groups.
Basic dyadic operations
The basic operations + - * % behave as expected, and rules such as the following are obeyed:
| 5j2 * 3j4 NB. the modulus of a product is .. 26.9258 (|5j2) * (|3j4) NB. .. the product of the moduli 26.9258 12 o. 5j2 * 3j4 NB. the angle of a product .. 1.3078 +/ 12 o. 5j2 3j4 NB. .. is the sum of the angles 1.3078
Also,
+/ 5j2 * 3j4 7j26
is equivalent numerically to and , showing that multiplication of complex numbers is equivalent to the inner product +/ .* for matrices of the form .
When multiplication takes the angle outside the range , both *. and 12 o. automatically make a wraparound to bring the angle back into range. For example,
wrap=.monad :0 t=.(o.2)|y if.t>o.1 do.t=.t-o.2 end. ) +/12 o._5j2 _3j4 NB. sum of angles exceeds pi 4.97538 12 o._5j2*_3j4 _1.3078 wrap 4.97538 NB. 4.975.. + 1.307.. = 2pi _1.30781
Complex numbers raised to real powers are the subject of de Moivreβs theorem. This depends on the fundamental Euler formula , which J expresses as the equivalence of the verbs ^@j. and j./@(2 1&o.). De Moivreβs theorem says that ; so to raise complex to the power , its modulus should be raised to the power , and its angle multiplied by . To see this in action, raise 2j1 to the powers 1, 2, and 8 in first Cartesian and then polar form:
+. 2j1 ^ 1 2 8 2 1 NB. modulus = sqrt(5) 3 4 NB. modulus = 5 _527 _336 NB. modulus = 625 = 5^4 *. 2j1 ^ 1 2 8 2.23607 0.463648 NB. (length,angle) for 2j1 5 0.927295 NB. (length,angle) for 2j1 ^2 625 _2.574 NB. (length,angle) for 2j1 ^8
The angle in the last line above can be confirmed by
wrap 8*0.463648 _2.574
It may be tempting to use the circle function _3 o. (arctan) to obtain angles, but this works only in simple cases because the range of arctan is . The range for complex numbers is double this because arctan makes no distinction between and , whereas the difference between second and fourth quadrants is significant in dealing with complex numbers.
The enhanced arithmetic operations
The second diagram illustrates the actions of the J verbs that are obtained from the basic arithmetic operations by adding a colon to make a di-gram:
Only one of these four forms β namely %: β extends to the dyadic case; for example 4%:z means (4^{th} root of length, angle/4).
In the case of di-grams formed by adding a full stop, 1-.z is the same as with real numbers, and %. and % are exactly equivalent. If either +. or *. are applied to real scalar numbers, the results are 2-lists made by joining zeros. This can be used as a method of stitching 0s, as in
+. 3 4 3 0 4 0
With real numbers, dyadic +. and *. are GCD and LCM respectively, but these should not be used with complex arguments in the expectation of obtaining the separate GCDs and LCMs of their components. For example,
(5j6 +. 10j3), (5j6 *. 10j3) 1 32j75
Complex powers and logarithms
To understand complex powers, start with the synonym relationship between r. and ^j. (or _12 o.), which at first sight should lead to j. and ^.r. also being synonyms. This is indeed true in some cases:
(j. 2j1), (^.r. 2j1) _1j2 _1j2
but not always:
(j. 12j10), (^.r. 12j10) _10j12 _10j_0.566371
This is because, unlike in real arithmetic, the logarithm of a complex number is not a single-valued function. In Cartesian coordinates x and y values stretch out indefinitely in both directions, but in polar coordinates angles wrap around in cycles of in the manner defined by the verb wrap above. In mathematical notation,
As a matter of arbitrary (but natural!) choice, J returns the unique angle which lies in the range . The same wrapping process applies when real numbers are raised to complex powers:
*. 2^ 4j4.5 4j4.6 16 3.11916 NB. unwrapped 16 _3.09471 NB. wrapped
More specifically, if is real and , then is illustrated by
*. 2^ 2j1 3j2 4 0.693147 NB. k to power x, y times ln(k) 8 1.38629 NB. with angle doubled
The cases βcomplex raised to realβ (de Moivreβs theorem) and βreal raised to complexβ have now been covered, leaving only the case βcomplex raised to complexβ to be dealt with. An interesting starting point is the number which at first sight should be about as complex as it gets:
0j1 ^ 0j1 NB. i to the power i 0.20788
Not so!
To explain this result, consider first . Use the formula , and choose (as J does) to make the logarithm single-valued. This gives , which when multipled by gives . Therefore must equal , which has the value 0.20788 to 5 decimal places. This sequence of calculations is confirmed by
^-o.0.5 0.20788
The verb ^. fulfils the familiar βreduce multiplication to additionβ property of logarithms of real numbers: that is, . For example,
^. 1j2 * 3j4 NB. ln a*b 2.41416j2.03444 +/ ^. 1j2 3j4 NB. ln a + ln b 2.41416j2.03444
For powers where both and are fully complex (that is, have non-zero imaginary parts) the sequence of equivalences leads to, for example,
2j1 ^ 1j3 NB. 2j1 to the power 1j3 _0.537177j0.145082 ^ 1j3 * ^. 2j1 NB. e to the power z times ln w _0.537177j0.145082
It is not easy to visualise the relationship of to and diagrammatically; that is, the link of _0.537177j0.145082 with 2j1 and 1j3 is a numerical rather than a graphical one. The same is true for other functions that can accept complex arguments, such as trig ratios and their inverses. Also, the logarithms concerned must be to the base e.
The constant e is one of the five most fundamental numbers in the universe: 0, 1, e, Ο, and i, which are connected by the equation . It is reasonable to suppose that advanced intelligent communicators from outer space (if such there be) would certainly try to convey this set of numbers to us as an immediate βlingua francaβ. This equation can be expressed in J in the following three equivalent ways
(1+^o.j.1), (1+_12 o.o.1), (1+r.o.1) 0j1.22465e_16 0 0j1.22465e_16
The verb clean can be used to remove the small complex values shown above, i.e.
clean =. 3 : 'j./"1 y * 1e_10 <: | y=. +.y' NB. round small complex values to 0 clean (1+^o.j.1), (1+_12 o.o.1), (1+r.o.1) 0 0 0
The equation can be rewritten as , which, since , means that the natural logarithms of negative real numbers are obtained by appending jΟ to the logarithm of the corresponding positive number. For example
^. 5.2 _5.2 NB. (ln r), (ln -r) 1.64866 1.64866j3.14159
Extension to Quaternions
Given a matrix of the form where and are real numbers, e.g.
] M =. 2 2 $ 2 _3 3 2 2 _3 3 2
and an inner product of M with a 2-list, as in
M +/ .* 2 _1 7 4
the same information can be obtained by multiplying two complex scalars:
2j3 * 2j_1 7j4
Similarly, finding the determinant of M is equivalent to a couple of operations on a complex scalar:
det =. -/ .* NB. determinant det M NB. determinant of M 13 *: 10 o. 2j3 NB. sum of squares of 2 and 3 13
Thus complex numbers can be used as a means of reducing the rank level of some operations. An obvious question is then: if complex data can reduce rank 2 operations to rank 1, can it correspondingly reduce rank 3 operations to rank 2? This speculation is not unique to J. In fact, the question was answered in the mid-19th century by Sir William Hamilton, who discovered that this progression is multiplicative rather than additive, that is that the next step up is not from 2 to 3 but from 2 to 4.
First define a verb which transforms a 2-list of real scalars into a matrix of the above form (call this a quaternion form)
r2ltom =. ,._1 1&*@|. NB. real 2-list to matrix r2ltom 2 3 2 _3 3 2
Next observe that it is possible to have a matrix with complex coefficients which nevertheless has a real determinant. For example,
] C =. 2 2 $ 4j3 6j_2 _6j2 4j3 4j3 6j_2 _6j2 4j3 det C 39
The matrix C has the form , which is the form extended to complex elements. Straightforward arithmetic shows that the determinant of a matrix of the above form has real part and imaginary part ; and so if , as is the case for C, the determinant is real; otherwise not. In the βall realβ case, , and .
If the form is now changed to , that is where the overbars denote complex conjugates, then the determinant is arithmetically guaranteed to be real for all values of P, Q, R, and S. If this is now taken as a standard form, then four fundamental matrices β obtainable by setting each of P, Q, R, and S to 1 and the other three to zero β correspond to unit points on the axes of a four dimensional geometrical space as denoted by (1,0), (0,1), (i,0) and (0,i). A verb analogous to r2ltom that constructs the above matrix from a 2-list of complex scalars is
c2ltom =. ,.+@|.@(*&1 _1) NB. complex 2-list to matrix c2ltom 2j3 4j5 2j3 _4j5 4j5 2j_3
Define variables to correspond to (1,0), (0,1), (i,0) and (0,i)
] 'I i j k' =. c2ltom &.> 1 0 ; 0 1 ; 0j1 0 ; 0 0j1 βββββ¬βββββ¬βββββββββ¬ββββββββ β1 0β0 _1β0j1 0β 0 0j1β β0 1β1 0β 0 0j_1β0j1 0β βββββ΄βββββ΄βββββββββ΄ββββββββ
and self-multiply each of these matrices:
times =. +/ .* each NB. matrix multiply times~ i;j;k NB. squares of i, j, and k βββββββ¬ββββββ¬ββββββ β_1 0β_1 0β_1 0β β 0 _1β 0 _1β 0 _1β βββββββ΄ββββββ΄ββββββ times~^:2 i;j;k NB. 4th powers of i, j, and k βββββ¬ββββ¬ββββ β1 0β1 0β1 0β β0 1β0 1β0 1β βββββ΄ββββ΄ββββ
This shows that iΒ² = jΒ² = kΒ² = βI, and iβ΄ = jβ΄ = kβ΄ = I, where I is the identity matrix.
Now multiply i, j, and k by each other:
(i;j;k) times (j;k;i) NB. result is k;i;j βββββββββ¬βββββ¬βββββββββ β 0 0j1β0 _1β0j1 0β β0j1 0β1 0β 0 0j_1β βββββββββ΄βββββ΄βββββββββ (j;k;i) times (i;j;k) NB. result is (-k);(-i);(-j) βββββββββββ¬βββββ¬βββββββββ β 0 0j_1β 0 1β0j_1 0β β0j_1 0β_1 0β 0 0j1β βββββββββββ΄βββββ΄βββββββββ det&> i;j;k NB. determinants equal 1 1 1 1
If i, j, and k are raised to third powers, a further three matrices not previously encountered arise:
] 'ci cj ck' =. (i;j;k) times (i;j;k) times i;j;k ββββββ¬βββββββββ¬ββββββββββ β 0 1β0j_1 0β 0 0j_1β β_1 0β 0 0j1β0j_1 0β ββββββ΄βββββββββ΄ββββββββββ
But now, however much the set of eight matrices I, -I, i, j, k, ci, cj, ck are intermultiplied, the result is always another member of the set. For example,
(i;j;k) times cj;ck;ci NB. result is ck;ci;cj βββββββββββ¬βββββ¬βββββββββ β 0 0j_1β 0 1β0j_1 0β β0j_1 0β_1 0β 0 0j1β βββββββββββ΄βββββ΄βββββββββ
The set of eight matrices possesses the properties of a group, specifically the quaternion group. Although there are eight elements in the group, they are all related to each other. Moreover, the whole set of eight can be generated from any two. For example, if i and j are chosen as generators, then k is ij; ci and cj are defined as cubes of i and j respectively; ck is cj times ci; both iΒ² and jΒ² yield -I; and both iβ΄ and jβ΄ yield the identity I.
If, analogous to j, J were to contain two further independent number constructors k and m such that 2j3k4m5 were a scalar, and the same rules β iΒ² = jΒ² = kΒ² = βI and iβ΄ = jβ΄ = kβ΄ = I β applied, where I=1, i=0j1k0m0, j=0j0k1m0, and k=0j0k0m1, then these scalars would be recognised mathematically as hypercomplex numbers. The four basic hypercomplex numbers for which the real elements are (1 0 0 0), (0 1 0 0), (0 0 1 0), and (0 0 0 1) would follow the same multiplication structure as the set of eight matrices, and they would form a group isomorphic with to the quaternion group.
A scalar such as 0j3k4m5 whose first element is zero corresponds to a pure quaternion, and so pure quaternions exactly match points in three-dimensional space β or quantities such as E, B, and H that define electromagnetic fields as three-dimensional entities. Such equivalences are, of course, useful only if the operations employed on them are guaranteed to produce further pure quaternions.
Code Summary
det=: -/ .* NB. determinant r2ltom=: ,._1 1&*@|. NB. real 2-list to matrix c2ltom=: ,.+@|.@(*&1 _1) NB. complex 2-list to matrix times=: +/ .* each NB. matrix multiply clean=: 3 : 'j./"1 y * 1e_10 <: | y=. +.y' NB. round small complex values to 0 wrap=: monad :0 NB. wrap angle to [-pi, pi] t=. (o.2)|y if. t>o.1 do. t=. t-o.2 end. )