Class: Ephem::Core::Vector
- Inherits:
-
Object
- Object
- Ephem::Core::Vector
- Defined in:
- lib/ephem/core/vector.rb
Overview
A three-dimensional vector class designed for astronomical calculations. This class provides basic vector operations and is optimized for celestial mechanics and astronomical coordinate transformations.
Instance Attribute Summary collapse
-
#x ⇒ Numeric
readonly
X component of the vector.
-
#y ⇒ Numeric
readonly
Y component of the vector.
-
#z ⇒ Numeric
readonly
Z component of the vector.
Class Method Summary collapse
-
.[](x, y, z) ⇒ Ephem::Vector
Alternative constructor for creating vectors.
Instance Method Summary collapse
-
#+(other) ⇒ Ephem::Vector
Adds two vectors.
-
#-(other) ⇒ Ephem::Vector
Subtracts two vectors.
-
#==(other) ⇒ Boolean
(also: #eql?)
Checks equality with another vector.
-
#[](index) ⇒ Numeric
Provides array-like access to vector components.
-
#cross(other) ⇒ Ephem::Vector
Calculates the cross product with another vector.
-
#dot(other) ⇒ Numeric
Calculates the dot product with another vector.
-
#hash ⇒ Integer
Generates a hash value for the vector.
-
#initialize(x, y, z) ⇒ Vector
constructor
Creates a new Vector instance.
-
#inspect ⇒ String
(also: #to_s)
Returns a string representation of the vector.
-
#magnitude ⇒ Numeric
(also: #length, #norm)
Calculates the magnitude (length) of the vector.
-
#to_a ⇒ Array<Numeric>
Converts the vector to an array.
Constructor Details
#initialize(x, y, z) ⇒ Vector
Creates a new Vector instance.
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/ephem/core/vector.rb', line 36 def initialize(x, y, z) unless x.is_a?(Numeric) && y.is_a?(Numeric) && z.is_a?(Numeric) raise InvalidInputError, "Vector components must be numeric" end @x = x @y = y @z = z freeze end |
Instance Attribute Details
#x ⇒ Numeric (readonly)
Returns x component of the vector.
18 19 20 |
# File 'lib/ephem/core/vector.rb', line 18 def x @x end |
#y ⇒ Numeric (readonly)
Returns y component of the vector.
21 22 23 |
# File 'lib/ephem/core/vector.rb', line 21 def y @y end |
#z ⇒ Numeric (readonly)
Returns z component of the vector.
24 25 26 |
# File 'lib/ephem/core/vector.rb', line 24 def z @z end |
Class Method Details
.[](x, y, z) ⇒ Ephem::Vector
Alternative constructor for creating vectors.
57 58 59 |
# File 'lib/ephem/core/vector.rb', line 57 def self.[](x, y, z) new(x, y, z) end |
Instance Method Details
#+(other) ⇒ Ephem::Vector
Adds two vectors.
69 70 71 |
# File 'lib/ephem/core/vector.rb', line 69 def +(other) self.class.new(x + other.x, y + other.y, z + other.z) end |
#-(other) ⇒ Ephem::Vector
Subtracts two vectors.
81 82 83 |
# File 'lib/ephem/core/vector.rb', line 81 def -(other) self.class.new(x - other.x, y - other.y, z - other.z) end |
#==(other) ⇒ Boolean Also known as: eql?
Checks equality with another vector.
180 181 182 183 184 185 186 |
# File 'lib/ephem/core/vector.rb', line 180 def ==(other) unless other.is_a?(self.class) raise InvalidInputError, "Can only compare with another Vector" end [x, y, z] == [other.x, other.y, other.z] end |
#[](index) ⇒ Numeric
Provides array-like access to vector components.
154 155 156 157 158 159 160 161 |
# File 'lib/ephem/core/vector.rb', line 154 def [](index) case index when 0 then x when 1 then y when 2 then z else raise IndexError, "Invalid index: #{index}" end end |
#cross(other) ⇒ Ephem::Vector
Calculates the cross product with another vector.
105 106 107 108 109 110 111 |
# File 'lib/ephem/core/vector.rb', line 105 def cross(other) self.class.new( y * other.z - z * other.y, z * other.x - x * other.z, x * other.y - y * other.x ) end |
#dot(other) ⇒ Numeric
Calculates the dot product with another vector.
93 94 95 |
# File 'lib/ephem/core/vector.rb', line 93 def dot(other) x * other.x + y * other.y + z * other.z end |
#hash ⇒ Integer
Generates a hash value for the vector.
166 167 168 |
# File 'lib/ephem/core/vector.rb', line 166 def hash [x, y, z, self.class].hash end |
#inspect ⇒ String Also known as: to_s
Returns a string representation of the vector.
138 139 140 |
# File 'lib/ephem/core/vector.rb', line 138 def inspect "Vector[#{x}, #{y}, #{z}]" end |
#magnitude ⇒ Numeric Also known as: length, norm
Calculates the magnitude (length) of the vector.
119 120 121 |
# File 'lib/ephem/core/vector.rb', line 119 def magnitude Math.sqrt(x * x + y * y + z * z) end |
#to_a ⇒ Array<Numeric>
Converts the vector to an array.
131 132 133 |
# File 'lib/ephem/core/vector.rb', line 131 def to_a [x, y, z] end |