Class: Quaternion
- Inherits:
-
Object
- Object
- Quaternion
- Defined in:
- lib/quaternion.rb
Direct Known Subclasses
Instance Method Summary collapse
-
#*(q) ⇒ Object
Returns the result of multiplying the quaternion by a scalar or another quaternion.
-
#+(q) ⇒ Object
Returns the sum of two quaternions.
-
#-(q) ⇒ Object
Returns the difference of two quaternions.
-
#-@ ⇒ Object
Returns the additive inverse of the quaternion.
-
#/(s) ⇒ Object
Returns the result of dividing the quaternion by a scalar.
-
#==(q) ⇒ Object
Returns true if two quaternions are equal (meaning that their corresponding entries are equal to each other) and false otherwise.
- #coerce(other) ⇒ Object
-
#conjugate ⇒ Object
Returns the conjugate of the quaternion.
-
#get ⇒ Object
Returns the quaternion’s values as a scalar and a vector.
-
#initialize(*args) ⇒ Quaternion
constructor
Create new quaternion from 4 values.
-
#inverse ⇒ Object
Returns the multiplicative inverse of the quaterion.
-
#norm ⇒ Object
Returns the magnitude of the quaternion.
-
#normalized ⇒ Object
Returns a normalized quaternion.
-
#set(w, x, y, z) ⇒ Object
Set the quaternion’s values.
-
#to_s ⇒ Object
Returns the string representation of the quaternion.
Constructor Details
#initialize(*args) ⇒ Quaternion
Create new quaternion from 4 values. If no arguments are provided, creates the zero quaternion.
15 16 17 18 19 20 21 22 23 |
# 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 |
Instance Method Details
#*(q) ⇒ Object
Returns the result of multiplying the quaternion by a scalar or another quaternion.
87 88 89 90 91 92 93 94 95 96 97 98 |
# 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) 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) result = self.class.new(beta0, *beta_s) return result end end |
#+(q) ⇒ Object
Returns the sum of two quaternions.
64 65 66 67 |
# File 'lib/quaternion.rb', line 64 def +(q) beta0, beta_s = q.get() return Quaternion.new(@beta0 + beta0, *(@beta_s + beta_s)) end |
#-(q) ⇒ Object
Returns the difference of two quaternions.
70 71 72 73 |
# File 'lib/quaternion.rb', line 70 def -(q) beta0, beta_s = q.get() return Quaternion.new(@beta0 - beta0, *(@beta_s - beta_s)) end |
#-@ ⇒ Object
Returns the additive inverse of the quaternion.
76 77 78 |
# File 'lib/quaternion.rb', line 76 def -@ Quaternion.new(-@beta0, -@beta_s[0], -@beta_s[1], -@beta_s[2]) end |
#/(s) ⇒ Object
Returns the result of dividing the quaternion by a scalar.
81 82 83 |
# File 'lib/quaternion.rb', line 81 def /(s) return Quaternion.new(@beta0 / s, *(@beta_s / s)) end |
#==(q) ⇒ Object
Returns true if two quaternions are equal (meaning that their corresponding entries are equal to each other) and false otherwise.
102 103 104 105 106 107 108 |
# File 'lib/quaternion.rb', line 102 def ==(q) if get() == q.get() return true else return false end end |
#coerce(other) ⇒ Object
115 116 117 |
# File 'lib/quaternion.rb', line 115 def coerce(other) return self, other end |
#conjugate ⇒ Object
Returns the conjugate of the quaternion.
48 49 50 |
# File 'lib/quaternion.rb', line 48 def conjugate return Quaternion.new(@beta0, *(-1*@beta_s)) end |
#get ⇒ Object
Returns the quaternion’s values as a scalar and a vector.
38 39 40 |
# File 'lib/quaternion.rb', line 38 def get return @beta0, @beta_s end |
#inverse ⇒ Object
Returns the multiplicative inverse of the quaterion.
53 54 55 |
# File 'lib/quaternion.rb', line 53 def inverse return self.conjugate() / self.norm() ** 2 end |
#norm ⇒ Object
Returns the magnitude of the quaternion.
43 44 45 |
# File 'lib/quaternion.rb', line 43 def norm return Math.sqrt(@beta0**2 + @beta_s.norm()**2) end |
#normalized ⇒ Object
Returns a normalized quaternion. q.normalized() is equivalent to q/q.norm().
59 60 61 |
# File 'lib/quaternion.rb', line 59 def normalized return self / norm() end |
#set(w, x, y, z) ⇒ Object
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
32 33 34 35 |
# File 'lib/quaternion.rb', line 32 def set(w, x, y, z) @beta0 = w @beta_s = Vector[x,y,z] end |
#to_s ⇒ Object
Returns the string representation of the quaternion.
111 112 113 |
# File 'lib/quaternion.rb', line 111 def to_s return "(" + @beta0.to_s + ", " + @beta_s.to_s + ")" end |