⍝ Quaternion arithmetic:

    ijk ← ↓ ⍎H'0i1 0j1 0k1'                 ⍝ unit vectors: i j k

    ⍕H¨ ijk ∘.(×H) ijk                      ⍝ i² = j² = k² = -1
┌────┬────┬────┐
│¯1  │0k1 │0j¯1│
├────┼────┼────┤
│0k¯1│¯1  │0i1 │
├────┼────┼────┤
│0j1 │0i¯1│¯1  │
└────┴────┴────┘

    ⍕H ⊃×H/ ijk                             ⍝ ijk = -1
¯1
    dp ← {(10*-⍺)×⌊0.5+⍵×10*⍺}              ⍝ rounded to ⍺ decimal places
     ∆ ← ⍕H∘(2∘dp)                          ⍝ format of rounded result

    S ← ⍳4                                  ⍝ quaternion scalar
    M ← 2 3 4⍴⍳24                           ⍝     ..     matrix

    ∆ M                                     ⍝ formatted matrix
 1i2j3k4     5i6j7k8     9i10j11k12
13i14j15k16 17i18j19k20 21i22j23k24

    ∆ +H M                                  ⍝ conjugate (q*)
 1i¯2j¯3k¯4     5i¯6j¯7k¯8     9i¯10j¯11k¯12
13i¯14j¯15k¯16 17i¯18j¯19k¯20 21i¯22j¯23k¯24

    ∆ ÷H M                                  ⍝ reciprocal
0.03i¯0.07j¯0.1k¯0.13  0.03i¯0.03j¯0.04k¯0.05 0.02i¯0.02j¯0.02k¯0.03
0.02i¯0.02j¯0.02k¯0.02 0.01i¯0.01j¯0.01k¯0.01 0.01i¯0.01j¯0.01k¯0.01

    ∆ |H M                                  ⍝ norm
 5.48 13.19 21.12
29.09 37.07 45.06

    ∆ |H ⍎H '2i4j5k6'                       ⍝ whole-number norm cf: |3j4 → 5
9
    ∆ ×H M                                  ⍝ direction (unit versor)
0.18i0.37j0.55k0.73 0.38i0.45j0.53k0.61 0.43i0.47j0.52k0.57
0.45i0.48j0.52k0.55 0.46i0.49j0.51k0.54 0.47i0.49j0.51k0.53

    ∆ S +H M                                ⍝ sum
 2i4j6k8     6i8j10k12  10i12j14k16
14i16j18k20 18i20j22k24 22i24j26k28

    ∆ S -H M                                ⍝ difference
  0              ¯4i¯4j¯4k¯4     ¯8i¯8j¯8k¯8   
¯12i¯12j¯12k¯12 ¯16i¯16j¯16k¯16 ¯20i¯20j¯20k¯20

    ∆ S ×H M                                ⍝ product
 ¯28i4j6k8     ¯60i12j30k24   ¯92i20j54k40 
¯124i28j78k56 ¯156i36j102k72 ¯188i44j126k88

    ∆ M ×H S                                ⍝ product is non-commutative
 ¯28i4j6k8     ¯60i20j14k32   ¯92i36j22k56 
¯124i52j30k80 ¯156i68j38k104 ¯188i84j46k128

    ∆ M ×H +H M                             ⍝ qq* is real
 30  174  446
846 1374 2030

    ∆ M ÷H S                                ⍝ right quotient M×(÷S)
1           2.33i¯0.27k¯0.53 3.67i¯0.53k¯1.07
5i¯0.8k¯1.6 6.33i¯1.07k¯2.13 7.67i¯1.33k¯2.67

    ∆ (÷H S)×H M                            ⍝ left quotient (÷S)×M
1           2.33j¯0.53k¯0.27 3.67j¯1.07k¯0.53
5j¯1.6k¯0.8 6.33j¯2.13k¯1.07 7.67j¯2.67k¯1.33

    cube ← 0,⍉2 2 2⊤0 to 7                  ⍝ unit cube
    cube
0 0 0 0
0 0 0 1
0 0 1 0
0 0 1 1
0 1 0 0
0 1 0 1
0 1 1 0
0 1 1 1

    spin ← {15 dp(○⍺÷180)⌽H ⍵}              ⍝ ⍵ rotated ⊃⍺° about 1↓⍺ vector

    120 1 1 1 spin cube                     ⍝ 120° rotation about 1 1 1 diagonal
0 0 0 0
0 1 0 0
0 0 0 1
0 1 0 1
0 0 1 0
0 1 1 0
0 0 1 1
0 1 1 1

    ⍕H ↑ cube (120 1 1 1 spin cube)         ⍝ formatted cube and 120° rotation
0 0k1 0j1 0j1k1 0i1 0i1k1 0i1j1 0i1j1k1
0 0i1 0k1 0i1k1 0j1 0i1j1 0j1k1 0i1j1k1

    a ← ○÷3                                 ⍝ 60° rotation angle around vector:
    u ← 0,2 1○○÷3                           ⍝   60° from y axis in y z plane
    p ← 1 ¯1 2                              ⍝ position before rotation
    x ← (10 1 14+4 2 ¯3×3*÷2)÷8             ⍝ expected position after rotation

:If ⎕FR=645
        (0,x) ≡ (a,u)⌽H 0,p                 ⍝ check against expected
    1
:Else                                                                   ⍝ <V>1287
:EndIf

    M ≡ ⍎H ⍕H M                             ⍝ check ⍎ ⍕ round-trip
1
⍝∇ H to

Back to: code

Back to: Workspaces