Quaternion type: Difference between revisions
Content added Content deleted
Line 1,478: | Line 1,478: | ||
=={{header|Crystal}}== |
=={{header|Crystal}}== |
||
{{trans|Rust:Ruby}} |
|||
<lang ruby>class Quaternion |
<lang ruby>class Quaternion |
||
property a, b, c, d |
property a, b, c, d |
||
def initialize(@a : Int64, @b : Int64, @c : Int64, @d : Int64) |
def initialize(@a : Int64, @b : Int64, @c : Int64, @d : Int64) end |
||
def norm; Math.sqrt( |
def norm; Math.sqrt(a**2 + b**2 + c**2 + d**2) end |
||
def conj; Quaternion.new( |
def conj; Quaternion.new(a, -b, -c, -d) end |
||
⚫ | |||
def +(rhs : Quaternion) |
def +(rhs : Quaternion) |
||
Quaternion.new( |
Quaternion.new(a + rhs.a, b + rhs.b, c + rhs.c, d + rhs.d) |
||
end |
|||
def +(rhs : Int32 | Int64) |
|||
⚫ | |||
end |
end |
||
def -(rhs : Quaternion) |
def -(rhs : Quaternion) |
||
Quaternion.new( |
Quaternion.new(a - rhs.a, b - rhs.b, c - rhs.c, d - rhs.d) |
||
end |
end |
||
def *(rhs : Quaternion) |
def *(rhs : Quaternion) |
||
Quaternion.new( |
Quaternion.new( |
||
a * rhs.a - b * rhs.b - c * rhs.c - d * rhs.d, |
|||
a * rhs.b + b * rhs.a + c * rhs.d - d * rhs.c, |
|||
a * rhs.c - b * rhs.d + c * rhs.a + d * rhs.b, |
|||
a * rhs.d + b * rhs.c - c * rhs.b + d * rhs.a) |
|||
end |
end |
||
def *(n) Quaternion.new(a * n, b * n, c * n, d * n) end |
|||
def *(rhs : Int32 | Int64) |
|||
Quaternion.new( |
def -() Quaternion.new(-a, -b, -c, -d) end |
||
⚫ | |||
end |
|||
⚫ | |||
⚫ | |||
def -() |
|||
Quaternion.new(-self.a, -self.b, -self.c, -self.d) |
|||
end |
|||
⚫ | |||
self.to_s == rhs.to_s |
|||
end |
|||
def to_s |
|||
⚫ | |||
end |
|||
private def sgn(n) |
|||
⚫ | |||
end |
|||
end |
end |
||
Line 1,586: | Line 1,570: | ||
-q0 = (-1 - 2i - 3j - 4k) |
-q0 = (-1 - 2i - 3j - 4k) |
||
conjugate of q0 = (1 - 2i - 3j - 4k) |
conjugate of q0 = (1 - 2i - 3j - 4k) |
||
q0 * (conjugate of q0) = (30 |
q0 * (conjugate of q0) = (30 + 0i + 0j + 0k) |
||
(conjugate of q0) * q0 = (30 |
(conjugate of q0) * q0 = (30 + 0i + 0j + 0k) |
||
r + q0 = (8 + 2i + 3j + 4k) |
r + q0 = (8 + 2i + 3j + 4k) |