quaternion

四元数

Examples

1 assert(Quaternion!int.init == quaternion(1, 0, 0, 0));
2 // 1 = [1; (0, 0, 0)]な四元数の作成
3 auto q = quaternion(1);
4 
5 // 添字によるアクセス
6 assert(q[0] == 1);
7 assert(q[1] == 0);
8 assert(q[2] == 0);
9 assert(q[3] == 0);
10 
11 
12 // 1 + 2i + 3j + 4k = [1; (2, 3, 4)]な四元数の作成
13 q = quaternion(1, 2, 3, 4);
14 assert(q[0] == 1);
15 assert(q[1] == 2);
16 assert(q[2] == 3);
17 assert(q[3] == 4);
18 
19 // a, b, c, dによるアクセス
20 assert(q.a == 1);
21 assert(q.b == 2);
22 assert(q.c == 3);
23 assert(q.d == 4);
24 
25 // スカラー部であるs, ベクトル部であるvによるアクセス
26 assert(q.s == 1);
27 assert(q.v == [2, 3, 4].matrix!(3, 1));
28 
29 // v = (i, j, k)
30 assert(q.i == 2);
31 assert(q.j == 3);
32 assert(q.k == 4);
33 
34 // opIndexやa, b, c, d, i, j, k, s, vへは代入可能
35 q.s = 7;
36 assert(q[0] == 7);
37 
38 // vはベクトルなので、ベクトルを代入可能
39 q.v = [4, 5, 6].matrix!(3, 1);
40 assert(q[1] == 4);
41 assert(q[2] == 5);
42 assert(q[3] == 6);
43 
44 // スカラー部とベクトル部による四元数の作成
45 q = quaternion(8, [9, 10, 11].matrix!(3, 1));
46 assert(q[0] == 8);
47 assert(q[1] == 9);
48 assert(q[2] == 10);
49 assert(q[3] == 11);
50 
51 
52 // 和
53 q = quaternion(1, 2, 3, 4) + quaternion(2, 2, 2, 2);
54 assert(q == quaternion(3, 4, 5, 6));
55 
56 q = q + 3;
57 assert(q == quaternion(6, 4, 5, 6));
58 
59 q = 3 + q;
60 assert(q == quaternion(9, 4, 5, 6));
61 
62 // 複合代入和
63 q += q;
64 assert(q == quaternion(18, 8, 10, 12));
65 
66 q += 3;
67 assert(q == quaternion(21, 8, 10, 12));
68 
69 
70 // 差
71 q = quaternion(1, 2, 3, 4) - quaternion(2, 2, 2, 2);
72 assert(q == quaternion(-1, 0, 1, 2));
73 
74 q = q - 3;
75 assert(q == quaternion(-4, 0, 1, 2));
76 
77 q = 3 - q;
78 assert(q == quaternion(7, 0, -1, -2));
79 
80 // 複合代入和
81 q -= q;
82 assert(q == quaternion(0, 0, 0, 0));
83 
84 q -= 3;
85 assert(q == quaternion(-3, 0, 0, 0));
86 
87 
88 // 積
89 q = quaternion(1, 2, 3, 4) * quaternion(7, 6, 7, 8);
90 assert(q == quaternion(-58, 16, 36, 32));
91 
92 q = quaternion(1, 2, 3, 4) * 4;
93 assert(q == quaternion(4, 8, 12, 16));
94 
95 q = 4 * quaternion(1, 2, 3, 4);
96 assert(q == quaternion(4, 8, 12, 16));
97 
98 q = quaternion(1, 2, 3, 4);
99 q *= quaternion(7, 6, 7, 8);
100 assert(q == quaternion(-58, 16, 36, 32));
101 
102 q = quaternion(1, 2, 3, 4);
103 q *= 4;
104 assert(q == quaternion(4, 8, 12, 16));
105 
106 
107 // 商
108 assert((quaternion(-58.0, 16, 36, 32) / quaternion(7, 6, 7, 8)).approxEqual(quaternion(1, 2, 3, 4)));
109 assert(quaternion(4.0, 8, 12, 16) / 4 == quaternion(1, 2, 3, 4));
110 assert((16.0 / quaternion(1.0, 2, 3, 4)).approxEqual(quaternion(16.0) / quaternion(1.0, 2, 3, 4)));
111 auto p = quaternion(-58.0, 16, 36, 32);
112 p /= quaternion(7, 6, 7, 8);
113 assert(p.approxEqual(quaternion(1, 2, 3, 4)));
114 
115 p = quaternion(4.0, 8, 12, 16);
116 p /= 4;
117 assert(p.approxEqual(quaternion(1, 2, 3, 4)));
118 
119 // 累乗
120 q = quaternion(1, 1, 2, 2);
121 p = q ^^ 3;
122 assert(approxEqual(p, q * q * q));
123 
124 p = q ^^ -3;
125 assert(approxEqual(p, q.inverse * q.inverse * q.inverse));
126 assert(approxEqual(q ^^ 3 * q ^^ -3, quaternion(1, 0, 0, 0)));

Meta