Quaternion type: Difference between revisions

Added XPL0 example.
m (→‎{{header|Picat}}: code tag for predicate)
(Added XPL0 example.)
Line 8,239:
q2q1 = (-56, 18, 20, 28)
q1q2 ≠ q2q1 = true
</pre>
 
=={{header|XPL0}}==
<lang XPL0>proc QPrint(Q); \Display quaternion
real Q;
[RlOut(0, Q(0)); Text(0, " + "); RlOut(0, Q(1)); Text(0, "i + ");
RlOut(0, Q(2)); Text(0, "j + "); RlOut(0, Q(3)); Text(0, "k");
CrLf(0);
];
func real QNorm(Q); \Return norm of a quaternion
real Q;
return sqrt( Q(0)*Q(0) + Q(1)*Q(1) + Q(2)*Q(2) + Q(3)*Q(3) );
 
func real QNeg(Q, R); \Return negative of a quaternion: Q:= -R
real Q, R;
[Q(0):= -R(0); Q(1):= -R(1); Q(2):= -R(2); Q(3):= -R(3);
return Q;
];
func real QConj(Q, R); \Return conjugate of a quaternion: Q:= conj R
real Q, R;
[Q(0):= R(0); Q(1):= -R(1); Q(2):= -R(2); Q(3):= -R(3);
return Q;
];
func real QRAdd(Q, R, Real); \Return quaternion plus real: Q:= R + Real
real Q, R, Real;
[Q(0):= R(0) + Real; Q(1):= R(1); Q(2):= R(2); Q(3):= R(3);
return Q;
];
func real QAdd(Q, R, S); \Return quaternion sum: Q:= R + S
real Q, R, S;
[Q(0):= R(0) + S(0); Q(1):= R(1) + S(1); Q(2):= R(2) + S(2); Q(3):= R(3) + S(3);
return Q;
];
func real QRMul(Q, R, Real); \Return quaternion times real: Q:= R + Real
real Q, R, Real;
[Q(0):= R(0) * Real; Q(1):= R(1) * Real; Q(2):= R(2) * Real; Q(3):= R(3) * Real;
return Q;
];
func real QMul(Q, R, S); \Return quaternion product: Q:= R * S
real Q, R, S;
[Q(0):= R(0)*S(0) - R(1)*S(1) - R(2)*S(2) - R(3)*S(3);
Q(1):= R(0)*S(1) + R(1)*S(0) + R(2)*S(3) - R(3)*S(2);
Q(2):= R(0)*S(2) - R(1)*S(3) + R(2)*S(0) + R(3)*S(1);
Q(3):= R(0)*S(3) + R(1)*S(2) - R(2)*S(1) + R(3)*S(0);
return Q;
];
 
real Q, Q1, Q2, R, Q0(4),;
[Q:= [1.0, 2.0, 3.0, 4.0];
Q1:= [2.0, 3.0, 4.0, 5.0];
Q2:= [3.0, 4.0, 5.0, 6.0];
R:= 7.0;
Format(3, 1);
Text(0, "q = "); QPrint(Q);
Text(0, "q1 = "); QPrint(Q1);
Text(0, "q2 = "); QPrint(Q2);
Text(0, "norm(q) = "); RlOut(0, QNorm(Q)); CrLf(0);
Text(0, "-q = "); QPrint(QNeg(Q0, Q));
Text(0, "conj(q) = "); QPrint(QConj(Q0, Q));
Text(0, "r + q = "); QPrint(QRAdd(Q0, Q, R));
Text(0, "q1 + q2 = "); QPrint(QAdd (Q0, Q1, Q2));
Text(0, "r * q = "); QPrint(QRMul(Q0, Q, R));
Text(0, "q1 * q2 = "); QPrint(QMul (Q0, Q1, Q2));
Text(0, "q2 * q1 = "); QPrint(QMul (Q0, Q2, Q1));
]</lang>
 
{{out}}
<pre>
q = 1.0 + 2.0i + 3.0j + 4.0k
q1 = 2.0 + 3.0i + 4.0j + 5.0k
q2 = 3.0 + 4.0i + 5.0j + 6.0k
norm(q) = 5.5
-q = -1.0 + -2.0i + -3.0j + -4.0k
conj(q) = 1.0 + -2.0i + -3.0j + -4.0k
r + q = 8.0 + 2.0i + 3.0j + 4.0k
q1 + q2 = 5.0 + 7.0i + 9.0j + 11.0k
r * q = 7.0 + 14.0i + 21.0j + 28.0k
q1 * q2 = -56.0 + 16.0i + 24.0j + 26.0k
q2 * q1 = -56.0 + 18.0i + 20.0j + 28.0k
</pre>
 
772

edits