# J Labs

### (1 of 7) ROTATIONS

The function:

` ```length=: %:@:(+/@:*:)"1````

yields the length of a vector to which it is applied, first squaring (*:) the elements, then summing (+/) the squares, and finally taking the square root (%:), thus:

``````

In [ ]:

length=: %:@:(+/@:*:)"1

``````
``````

In [ ]:

length 3 4

``````
``````

In [ ]:

length 3 4 12

``````

### (2 of 7) LENGTH

The function:

` ```X=: +/ . \*````

is the matrix product.

If m is a matrix, and v is a vector, the result of m X v may differ from v in both length and direction.

However, if m is a matrix whose row and column sums are of length 1, then the result of m X v has the same length as v The operation is called a rotation, and m is called a rotation matrix. For example:

``````

In [ ]:

X=: +/ . *

``````
``````

In [ ]:

v=: 3 4

``````
``````

In [ ]:

m=: > 1 2;2 1

``````
``````

In [ ]:

m

``````
``````

In [ ]:

m X v

``````
``````

In [ ]:

length m X v

``````
``````

In [ ]:

length v

``````
``````

In [ ]:

c=: % %: 2        NB. Reciprocal of square root of 2

``````
``````

In [ ]:

m=: > (c, c);((-c),c)

``````
``````

In [ ]:

m

``````
``````

In [ ]:

length m          NB. Lengths of rows of m

``````
``````

In [ ]:

|: m              NB. Transpose of m

``````
``````

In [ ]:

length |: m       NB. Lengths of columns of m

``````
``````

In [ ]:

m X v

``````
``````

In [ ]:

length m X v

``````

### (3 of 7) ROTATION

The function:

` ```r=: (sind , -@:cosd) ,. (cosd , sind)````

produces a rotation matrix. The angle of rotation it produces is determined by the argument of r. For example:

``````

In [ ]:

``````
``````

In [ ]:

r=: (cosd , -@:sind) ,. (sind , cosd)

``````
``````

In [ ]:

m=: r 45

``````
``````

In [ ]:

m

``````
``````

In [ ]:

m X v

``````
``````

In [ ]:

y=: (r 90) X v

``````
``````

In [ ]:

y

``````
``````

In [ ]:

y X v       NB. 90-degree rotation produces a perpendicular vector

``````

### (4 of 7) ROTATION MATRICES

If m =. r a is a rotation matrix in 2-space, then the matrix 1 0 0,0 0,.m produces a corresponding rotation about the x axis in 3-space. For example:

``````

In [ ]:

r 30

``````
``````

In [ ]:

rx30=: 1 0 0,0 0,.r 30

``````
``````

In [ ]:

rx30

``````
``````

In [ ]:

w=: 3 4 5

``````
``````

In [ ]:

rx30 X w

``````
``````

In [ ]:

length w

``````
``````

In [ ]:

length rx30 X w

``````

### (5 of 7) ROTATION IN 3-SPACE

If the rows and columns of a rotation about x are permuted to bring the 1 to the y or z position, the result is a rotation about the corresponding axis. For example:

``````

In [ ]:

ry45=: 1 0 2 { 1 0 2 ({"1) 1 0 0,0 0,.r 45

``````
``````

In [ ]:

rz60=: 1 2 0 { 1 2 0 ({"1) 1 0 0,0 0,.r 60

``````
``````

In [ ]:

ry45

``````
``````

In [ ]:

ry45 X w

``````
``````

In [ ]:

rz60

``````
``````

In [ ]:

rz60 X w

``````

### (6 of 7) ROTATION ABOUT y AND z

The matrix product of two rotations is again a rotation, and a general rotation can therefore be produced as a product of rotations about each of the three axes. For example:

``````

In [ ]:

rall=: rx30 X ry45 X rz60

``````
``````

In [ ]:

rall

``````
``````

In [ ]:

length rall

``````
``````

In [ ]:

rall X w

``````

### (7 of 7) PRODUCT OF ROTATIONS

We will now define a general rotation function GR whose left argument is a quoted list of axes, such as "yxz" or "yz" or "x"or "xyxyx", and whose right argument is a list of angles in degrees. Thus:

``````

In [ ]:

GR=: 4 : 0
res=: (i. =/ i.) 3       NB. Initialize res as identity matrix
p=: >0 1 2;1 0 2;1 2 0   NB. Permutations for axes
while. 0<# x do.
cp=. ('xyz'i.{:x){p                 NB. Current permutation
cr=. cp {"1 cp { 1 0 0,0 0,.r {: y  NB. Current rotation
res =. cr X res
x=. }: x
y=. }: y
end.
res
)

``````
``````

In [ ]:

'xyz' GR 30 45 60

``````
``````

In [ ]:

'yx' GR 45 60

``````
``````

In [ ]:

'y' GR 45

``````
``````

In [ ]:

'xyxyx' GR 1 2 3 4 5

``````

### End of Lab

``````

In [ ]:

``````