Quaternion type: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 4,238: | Line 4,238: | ||
True |
True |
||
True</pre> |
True</pre> |
||
=={{header|Maple}}== |
|||
<lang Maple> |
|||
with(ArrayTools); |
|||
module Quaternion() |
|||
option object; |
|||
local real := 0; |
|||
local i := 0; |
|||
local j := 0; |
|||
local k := 0; |
|||
export getReal::static := proc(self::Quaternion, $) |
|||
return self:-real; |
|||
end proc; |
|||
export getI::static := proc(self::Quaternion, $) |
|||
return self:-i; |
|||
end proc; |
|||
export getJ::static := proc(self::Quaternion, $) |
|||
return self:-j; |
|||
end proc; |
|||
export getK::static := proc(self::Quaternion, $) |
|||
return self:-k; |
|||
end proc; |
|||
export Norm::static := proc(self::Quaternion, $) |
|||
return sqrt(self:-real^2 + self:-i^2 + self:-j^2 + self:-k^2); |
|||
end proc; |
|||
# NegativeQuaternion returns the additive inverse of the quaternion |
|||
export NegativeQuaternion::static := proc(self::Quaternion, $) |
|||
return Quaternion(- self:-real, - self:-i, - self:-j, - self:-k); |
|||
end proc; |
|||
export Conjugate::static := proc(self::Quaternion, $) |
|||
return Quaternion(self:-real, - self:-i, - self:-j, - self:-k); |
|||
end proc; |
|||
# quaternion addition |
|||
export `+`::static := overload ([ |
|||
proc(self::Quaternion, x::Quaternion) option overload; |
|||
return Quaternion(self:-real + getReal(x), self:-i + getI(x), self:-j + getJ(x), self:-k + getK(x)); |
|||
end proc, |
|||
proc(self::Quaternion, x::algebraic) option overload; |
|||
return Quaternion(self:-real + x, self:-i, self:-j, self:-k); |
|||
end proc, |
|||
proc(x::algebraic, self::Quaternion) option overload; |
|||
return Quaternion(x + self:-real, self:-i, self:-j, self:-k); |
|||
end |
|||
]); |
|||
# convert quaternion to additive inverse |
|||
export `-`::static := overload([ |
|||
proc(self::Quaternion) option overload; |
|||
return Quaternion(-self:-real, -self:-i, -self:-j, -self:-k); |
|||
end |
|||
]); |
|||
# quaternion multiplication is non-abelian so the `.` operator needs to be used |
|||
export `.`::static := overload([ |
|||
proc(self::Quaternion, x::Quaternion) option overload; |
|||
return Quaternion(self:-real * getReal(x) - self:-i * getI(x) - self:-j * getJ(x) - self:-k * getK(x), |
|||
self:-real * getI(x) + self:-i * getReal(x) + self:-j * getK(x) - self:-k * getJ(x), |
|||
self:-real * getJ(x) + self:-j * getReal(x) - self:-i * getK(x) + self:-k * getI(x), |
|||
self:-real * getK(x) + self:-k * getReal(x) + self:-i * getJ(x) - self:-j * getI(x)); |
|||
end proc, |
|||
proc(self::Quaternion, x::algebraic) option overload; |
|||
return Quaternion(self:-real * x, self:-i * x, self:-j * x, self:-k * x); |
|||
end proc, |
|||
proc(x::algebraic, self::Quaternion) option overload; |
|||
return Quaternion(self:-real * x, self:-i * x, self:-j * x, self:-k * x); |
|||
end |
|||
]); |
|||
# redirect division to `.` operator |
|||
export `*`::static := overload([ |
|||
proc(self::Quaternion, x::Quaternion) option overload; |
|||
use `*` = `.` in return self * x; end use |
|||
end proc, |
|||
proc(self::Quaternion, x::algebraic) option overload; |
|||
use `*` = `.` in return x * self; end use |
|||
end proc, |
|||
proc(x::algebraic, self::Quaternion) option overload; |
|||
use `*` = `.` in return x * self; end use |
|||
end |
|||
]); |
|||
# convert quaternion to multiplicative inverse |
|||
export `/`::static := overload([ |
|||
proc(self::Quaternion) option overload; |
|||
return Conjugate(self) . (1/(Norm(self)^2)); |
|||
end proc |
|||
]); |
|||
# QuaternionCommutator computes the commutator of self and x |
|||
export QuaternionCommutator::static := proc(x::Quaternion, y::Quaternion, $) |
|||
return (x . y) - (y . x); |
|||
end proc; |
|||
# display quaternion |
|||
export ModulePrint::static := proc(self::Quaternion, $); |
|||
return cat(self:-real, " + ", self:-i, "i + ", self:-j, "j + ", self:-k, "k"): |
|||
end proc; |
|||
export ModuleApply::static := proc() |
|||
Object(Quaternion, _passed); |
|||
end proc; |
|||
export ModuleCopy::static := proc(new::Quaternion, proto::Quaternion, R::algebraic, imag::algebraic, J::algebraic, K::algebraic, $) |
|||
new:-real := R; |
|||
new:-i := imag; |
|||
new:-j := J; |
|||
new:-k := K; |
|||
end proc; |
|||
end module: |
|||
q := Quaternion(1, 2, 3, 4): |
|||
q1 := Quaternion(2, 3, 4, 5): |
|||
q2 := Quaternion(3, 4, 5, 6): |
|||
r := 7: |
|||
quats := Array([q, q1, q2]): |
|||
print("q, q1, q2"): |
|||
seq(quats[i], i = 1..3); |
|||
print("norms"): |
|||
seq(Norm(quats[i]), i = 1..3); |
|||
print("negative"): |
|||
seq(NegativeQuaternion(quats[i]), i = 1..3); |
|||
print("conjugate"): |
|||
seq(Conjugate(quats[i]), i = 1..3); |
|||
print("addition of real number 7"): |
|||
seq(quats[i] + r, i = 1..3); |
|||
print("multiplication by real number 7"): |
|||
seq(quats[i] . r, i = 1..3); |
|||
print("division by real number 7"): |
|||
seq(quats[i] / 7, i = 1..3); |
|||
print("add quaternions q1 and q2"): |
|||
q1 + q2; |
|||
print("multiply quaternions q1 and q2"); |
|||
q1 . q2; |
|||
print("multiply quaternions q2 and q1"): |
|||
q2 . q1; |
|||
print("quaternion commutator of q1 and q2"): |
|||
QuaternionCommutator(q1,q2); |
|||
print("divide q1 by q2"): |
|||
q1 / q2; |
|||
</lang> |
|||
{{out}}<pre> |
|||
"q, q1, q2" |
|||
1 + 2i + 3j + 4k, 2 + 3i + 4j + 5k, 3 + 4i + 5j + 6k |
|||
"norms" |
|||
1/2 1/2 1/2 |
|||
30 , 3 6 , 86 |
|||
"negative" |
|||
-1 + -2i + -3j + -4k, -2 + -3i + -4j + -5k, -3 + -4i + -5j + -6k |
|||
"conjugate" |
|||
1 + -2i + -3j + -4k, 2 + -3i + -4j + -5k, 3 + -4i + -5j + -6k |
|||
"addition of real number 7" |
|||
8 + 2i + 3j + 4k, 9 + 3i + 4j + 5k, 10 + 4i + 5j + 6k |
|||
"multiplication by real number 7" |
|||
7 + 14i + 21j + 28k, 14 + 21i + 28j + 35k, 21 + 28i + 35j + 42k |
|||
"division by real number 7" |
|||
1/7 + 2/7i + 3/7j + 4/7k, 2/7 + 3/7i + 4/7j + 5/7k, 3/7 + 4/7i + 5/7j + 6/7k |
|||
"add quaternions q1 and q2" |
|||
5 + 7i + 9j + 11k |
|||
"multiply quaternions q1 and q2" |
|||
-56 + 16i + 24j + 26k |
|||
"multiply quaternions q2 and q1" |
|||
-56 + 18i + 20j + 28k |
|||
"quaternion commutator of q1 and q2" |
|||
0 + -2i + 4j + -2k |
|||
"divide q1 by q2" |
|||
34/43 + 1/43i + 0j + 2/43k |
|||
</pre> |
|||
=={{header|Mathematica}}== |
=={{header|Mathematica}}== |
||
<lang Mathematica><<Quaternions` |
<lang Mathematica><<Quaternions` |