class Quaternion
Public Class Methods
Create new quaternion from 4 values. If no arguments are provided, creates the zero quaternion.
# File lib/quaternion.rb, line 15 def initialize(*args) if args.length() == 4 set(*args) elsif args.length() == 0 set(0, 0, 0, 0) else raise(ArgumentError, "wrong number of arguments (must be 0 or 4)") end end
Public Instance Methods
Returns the result of multiplying the quaternion by a scalar or another quaternion.
# File lib/quaternion.rb, line 87 def *(q) if q.is_a?(Numeric) return Quaternion.new(@beta0 * q, *(@beta_s * q)) elsif q.is_a?(Quaternion) beta0, beta_s = quatMult(q) result = Quaternion.new(beta0, *beta_s) return result end end
Returns the sum of two quaternions.
# File lib/quaternion.rb, line 64 def +(q) beta0, beta_s = q.get() return Quaternion.new(@beta0 + beta0, *(@beta_s + beta_s)) end
Returns the difference of two quaternions.
# File lib/quaternion.rb, line 70 def -(q) beta0, beta_s = q.get() return Quaternion.new(@beta0 - beta0, *(@beta_s - beta_s)) end
Returns the additive inverse of the quaternion.
# File lib/quaternion.rb, line 76 def -@ Quaternion.new(-@beta0, -@beta_s[0], -@beta_s[1], -@beta_s[2]) end
Returns the result of dividing the quaternion by a scalar.
# File lib/quaternion.rb, line 81 def /(s) return Quaternion.new(@beta0 / s, *(@beta_s / s)) end
Returns true if two quaternions are equal (meaning that their corresponding entries are equal to each other) and false otherwise.
# File lib/quaternion.rb, line 99 def ==(q) if get() == q.get() return true else return false end end
# File lib/quaternion.rb, line 112 def coerce(other) return self, other end
Returns the conjugate of the quaternion.
# File lib/quaternion.rb, line 48 def conjugate return Quaternion.new(@beta0, *(-1*@beta_s)) end
Returns the quaternion's values as a scalar and a vector.
# File lib/quaternion.rb, line 38 def get return @beta0, @beta_s end
Returns the multiplicative inverse of the quaterion.
# File lib/quaternion.rb, line 53 def inverse return self.conjugate() / self.norm() ** 2 end
Returns the magnitude of the quaternion.
# File lib/quaternion.rb, line 43 def norm return Math.sqrt(@beta0**2 + @beta_s.norm()**2) end
Returns a normalized quaternion. q.normalized() is equivalent to q/q.norm().
# File lib/quaternion.rb, line 59 def normalized return self / norm() end
Set the quaternion's values.
Params:
w
-
the real part of the quaterion
x
-
the i-component
y
-
the j-component
z
-
the k-component
# File lib/quaternion.rb, line 32 def set(w, x, y, z) @beta0 = w @beta_s = Vector[x,y,z] end
Returns the string representation of the quaternion.
# File lib/quaternion.rb, line 108 def to_s return "(" + @beta0.to_s + ", " + @beta_s.to_s + ")" end
Private Instance Methods
# File lib/quaternion.rb, line 117 def cross_product(v1, v2) # returns the cross product of vectors v1 and v2. return Vector[ v1[1]*v2[2] - v1[2]*v2[1], v1[2]*v2[0] - v1[0]*v2[2], v1[0]*v2[1] - v1[1]*v2[0] ] end
Multiplies with another quaternion, and returns the resulting beta0 and beta_s values.
# File lib/quaternion.rb, line 126 def quatMult(q) q_beta0, q_beta_s = q.get() beta0 = @beta0 * q_beta0 - @beta_s.inner_product(q_beta_s) beta_s = @beta0 * q_beta_s + q_beta0 * @beta_s + cross_product(@beta_s, q_beta_s) return beta0, beta_s end