Fifty Shades of J/Chapter 46
Table of Contents ... Glossary ... Previous Chapter ... Next Chapter
Principal Topics
- = (self classify), ` (tie), `: (evoke gerund), ,. (stitch), " (rank conjunction), j. (imaginary), +. (real / imaginary), tables, identity matrix, inner product, apb notation, upper/lower triangular matrices.
Identity Table
The identity table of any order can be formed in many ways, of which the simplest is as a fork i.=/i. . This also spawns definitions of upper triangular tables with or without the diagonal
<"2 (i.=/i.)`(i.<:/i.)`(i.</i.)/.3 3 3 ┌─────┬─────┬─────┐ │1 0 0│1 1 1│0 1 1│ │0 1 0│0 1 1│0 0 1│ │0 0 1│0 0 1│0 0 0│ └─────┴─────┴─────┘
and lower triangular tables follow in an obvious way.
Another even shorter definition for the identity table is =@i. . Superficially this looks like equals but in fact it is the monadic verb self-classify (=) which is at the heart of the matter. When its argument contains no duplicates it is just the equals table of the argument with itself.
Plane Transformations
Start with id and di
id=: i. =/ i. NB. identity matrix id 3 1 0 0 0 1 0 0 0 1 di=.<"1@(i. ,. i.) NB. coords of diagonal di 3 ┌───┬───┬───┐ │0 0│1 1│2 2│ └───┴───┴───┘
so a scaling matrix for enlarging 2 units in the x direction and 3 in the direction is
2 3(di 2)}id 2 2 0 0 3
Define a pennant by suitable (x,y) coordinates :
]pen=: |:>0 0;5 5;4 5;4 4 0 5 4 4 0 5 5 4
... and here are the coordinates of this scaling
mp=:+/ .* (2 3(di 2)}id 2) mp pen 0 10 8 8 0 15 15 12
To translate pen two units to the right and three units down
2 _3 + pen 2 7 6 6 _3 2 2 1
The isomorphic plane transformations whose combinations cover all possible such transformations are scaling, translating and rotating.
Rotating in 2D
cs=:+.@^@j. NB. obtains cos y and sin y pi=:1p1 cs pi%3 0.5 0.866025
Use cs to obtain the rotation matrix
rot2=:(cs&-)`(|.&cs)`:0 NB. evoke gerund rot2 pi%3 0.5 _0.866025 0.866025 0.5 (rot2 pi%3)+/ .*1 1 _0.366025 1.36603
Another way of performing this rotation is to use complex numbers based on the identity eiy = cos y+i sin y :
r2=:* ^@j. 1j1 r2 pi%3 _0.366025j1.36603
Here is its rotation of the pennant through an anti-clockwise angle of π/2
(rot2 pi%2)+/ .*pen 0 _5 _5 _4 0 5 4 4
... or using the alternative method
q=:0 5j5 4j5 4 4 (**@|)&.+. q (r2 every) pi%2 NB. (**@|)&.+. removes near-zero components from complex numbers 0 _5j5 _5j4 0j4 0j4
Scaling and translation in 3D extends naturally into three dimensions:
2 4 3(di 3)}id 3 2 0 0 0 4 0 0 0 3 (2 4 3(di 3)}id 3)mp 2 1 3 4 4 9
To translate four points in 3D by 2 x-units -3 y-units and 4 z-units :
2 _3 4 +"(0 1)pen,1 2 7 6 6 _3 2 2 1 5 5 5 5
In 3D there are infinitely many possible axes of rotation of which the three main ones are about axes at right angles to the Oxy, Ozx and Oyz planes, for which purpose the result of rot2 y has to be placed by amendment into the relevant 2 by 2 block of id 3 . For example the relevant coordinate pairs for the Oxy rotation are
<"(1)2 2#: i.4 ┌───┬───┬───┬───┐ │0 0│0 1│1 0│1 1│ └───┴───┴───┴───┘
and those for Ozx and Oyz are obtained by doubling and incrementing respectively. Rather than using a 3D solid object, the illustrations show the effect of rotating a ray joining the origin to the point with coordinates (2,1,3).
rxy=: monad :'(,rot2 y)(<"(1)2 2#: i.4)}id 3' rxy pi%3 0.5 _0.866025 0 0.866025 0.5 0 0 0 1 (rxy pi%2) mp 2 1 3 _1 2 3 rzx=: monad :'(,rot2 y)(<"(1)2*2 2#: i.4)}id 3' (rzx pi%2) mp 2 1 3 _3 1 2 ryz=: monad :'(,rot2 y)(<"(1)>:2 2#: i.4)}id 3' (ryz pi%2) mp 2 1 3 2 _3 1
Code Summary
id=: i. =/ i. NB. identity matrix di=: <"1@(i. ,. i.) NB. coeffs of diagonal mp=: +/ .* NB. matrix product cs=: +.@^@j. NB. obtains cos y and sin y pi=: 1p1 rot2=: (cs&-)`(|.&cs)`:0 NB. 2D rotation matrix r2=: * ^@j. NB. ditto as complex list
3D rotations about major axes:
rxy=: monad :'(,rot2 y)(<"(1)2 2#: i.4)}id 3' rzx=: monad :'(,rot2 y)(<"(1)2*2 2#: i.4)}id 3' ryz=: monad :'(,rot2 y)(<"(1)>:2 2#: i.4)}id 3'