Class: Ephem::Core::Vector

Inherits:
Object
  • Object
show all
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.

Examples:

Create a vector representing a position in space

position = Ephem::Vector.new(1.0, 2.0, 3.0)

Calculate the cross product of two vectors

pos = Ephem::Vector.new(1.0, 0.0, 0.0)
vel = Ephem::Vector.new(0.0, 1.0, 0.0)
angular_momentum = pos.cross(vel)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y, z) ⇒ Vector

Creates a new Vector instance.

Examples:

Create a vector at the origin

vector = Ephem::Vector.new(0, 0, 0)

Parameters:

  • x (Numeric)

    The x component of the vector

  • y (Numeric)

    The y component of the vector

  • z (Numeric)

    The z component of the vector

Raises:



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

#xNumeric (readonly)

Returns x component of the vector.

Returns:

  • (Numeric)

    x component of the vector



18
19
20
# File 'lib/ephem/core/vector.rb', line 18

def x
  @x
end

#yNumeric (readonly)

Returns y component of the vector.

Returns:

  • (Numeric)

    y component of the vector



21
22
23
# File 'lib/ephem/core/vector.rb', line 21

def y
  @y
end

#zNumeric (readonly)

Returns z component of the vector.

Returns:

  • (Numeric)

    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.

Examples:

Create a vector using square bracket notation

vector = Ephem::Vector[1, 2, 3]

Parameters:

  • x (Numeric)

    The x component of the vector

  • y (Numeric)

    The y component of the vector

  • z (Numeric)

    The z component of the vector

Returns:

  • (Ephem::Vector)

    A new vector instance



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.

Examples:

Add two position vectors

sum = vector1 + vector2

Parameters:

  • other (Ephem::Vector)

    The vector to add

Returns:

  • (Ephem::Vector)

    A new vector representing the sum



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.

Examples:

Calculate relative position

relative_position = position1 - position2

Parameters:

  • other (Ephem::Vector)

    The vector to subtract

Returns:

  • (Ephem::Vector)

    A new vector representing the difference



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.

Examples:

Check if two vectors are equal

vector1 == vector2

Parameters:

  • other (Ephem::Vector)

    The vector to compare with

Returns:

  • (Boolean)

    true if vectors are equal, false otherwise

Raises:



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.

Examples:

Access z component

z_value = vector[2]

Parameters:

  • index (Integer)

    Index of the component (0 for x, 1 for y, 2 for z)

Returns:

  • (Numeric)

    The value of the component at the specified index

Raises:



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.

Examples:

Calculate angular momentum (position × velocity)

angular_momentum = position.cross(velocity)

Parameters:

  • other (Ephem::Vector)

    The vector to calculate cross product with

Returns:

  • (Ephem::Vector)

    A new vector representing the cross product



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.

Examples:

Calculate work done (force · displacement)

work = force.dot(displacement)

Parameters:

  • other (Ephem::Vector)

    The vector to calculate dot product with

Returns:

  • (Numeric)

    The dot product of the two vectors



93
94
95
# File 'lib/ephem/core/vector.rb', line 93

def dot(other)
  x * other.x + y * other.y + z * other.z
end

#hashInteger

Generates a hash value for the vector.

Returns:

  • (Integer)

    Hash value based on vector components and class



166
167
168
# File 'lib/ephem/core/vector.rb', line 166

def hash
  [x, y, z, self.class].hash
end

#inspectString Also known as: to_s

Returns a string representation of the vector.

Returns:

  • (String)

    String representation in format “Vector[x, y, z]”



138
139
140
# File 'lib/ephem/core/vector.rb', line 138

def inspect
  "Vector[#{x}, #{y}, #{z}]"
end

#magnitudeNumeric Also known as: length, norm

Calculates the magnitude (length) of the vector.

Examples:

Calculate distance from origin

distance = vector.magnitude

Returns:

  • (Numeric)

    The magnitude 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_aArray<Numeric>

Converts the vector to an array.

Examples:

Get vector components as array

coordinates = vector.to_a

Returns:

  • (Array<Numeric>)

    Array containing [x, y, z] components



131
132
133
# File 'lib/ephem/core/vector.rb', line 131

def to_a
  [x, y, z]
end