JPhrases/ComplexNumbers

From J Wiki
Jump to: navigation, search

8E. Complex Numbers

m0=: cnj=: + Conjugate
m1=: mag=: | Magnitude
m2=: %:@(cnj*]) "
m3=: rai=: +. Real and imaginary parts
m4=: maa=: *. Magnitude and angle
m5=: irai=: rai^:_1 Inverse rai
m6=: imaa=: maa^:_1 Inverse maa
m7=: rou=: [:^ 0j2p1&% * i. Roots of unity
m8=: rpg=: rai@rou Regular polygon
d9=: zero=: ] * 10&^@-@[ < | Zero any real y less than 10^-x in mag
m10=: z=:({.,{:*1e_6"_<%~/@:|)&.rai Zero imaginary if relatively small
m11=: (1e_10&$:) : (j./"1@((] * (<:|)) +.)) Clean y

The function z may be used to zero any imaginary part that is relatively small compared to the corresponding real part. For example:

   (] ,: z) a=:3+j.10^-2*i. 5
3j1 3j0.01 3j0.0001 3j1e_6 3j1e_8
3j1 3j0.01 3j0.0001      3      3

   z a
3j1 3j0.01 3j0.0001 3 3

Complex numbers can be scaled by multiplication by a real number, and shifted and rotated by addition and multiplication by complex numbers. For example:

   ]a=: rou 3                        NB. Third roots of unity
1 _0.5j0.866025 _0.5j_0.866025

   |a                                NB. Lie on the unit circle
1 1 1

   1ad30                             NB. Complex of mag 1 and angle of 30 degrees
0.866025j0.5

   6&zero@rai&.> (] ; 3j4&+ ; 1ad30&* ; 2ad60&*) a
+---------------------------------------------------+
|   1        0|  4       4| 0.866025 0.5| 1  1.73205|
| 0.5 0.866025|2.5 4.86603|_0.866025 0.5|_2        0|
|_0.5 0.866025|2.5 3.13397|        0  _1| 1 _1.73205|
+---------------------------------------------------+
 Coordinates  Shift by 3,4   Rotate by   Rotate by
 of triangle                 30 degrees  60 degrees