Class: Vector
- Defined in:
- lib/classifier/extensions/vector.rb,
sig/vendor/matrix.rbs
Overview
Type stubs for matrix gem Using untyped elements since our usage is primarily with Floats/Numerics
Constant Summary collapse
- EPSILON =
1e-10
Class Method Summary collapse
Instance Method Summary collapse
- #* ⇒ Object
- #- ⇒ Vector
- #[] ⇒ Object
- #collect {|arg0| ... } ⇒ Vector
- #each {|arg0| ... } ⇒ void
- #is_a? ⇒ Boolean
- #magnitude ⇒ Float
- #normalize ⇒ Vector
- #size ⇒ Integer
- #to_a ⇒ Array[untyped]
Class Method Details
Instance Method Details
#collect {|arg0| ... } ⇒ Vector
12 |
# File 'sig/vendor/matrix.rbs', line 12
def collect: () { (untyped) -> untyped } -> Vector
|
#each {|arg0| ... } ⇒ void
This method returns an undefined value.
11 |
# File 'sig/vendor/matrix.rbs', line 11
def each: () { (untyped) -> void } -> void
|
#is_a? ⇒ Boolean
16 |
# File 'sig/vendor/matrix.rbs', line 16
def is_a?: (untyped) -> bool
|
#magnitude ⇒ Float
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/classifier/extensions/vector.rb', line 29 def magnitude # Cache magnitude since Vector is immutable after creation @magnitude ||= begin sum_of_squares = 0.to_r size.times do |i| sum_of_squares += self[i]**2.to_r end Math.sqrt(sum_of_squares.to_f) end end |
#normalize ⇒ Vector
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/classifier/extensions/vector.rb', line 40 def normalize magnitude_value = magnitude return Vector[*Array.new(size, 0.0)] if magnitude_value <= 0.0 normalized_values = [] size.times do |i| normalized_values << (self[i] / magnitude_value) end Vector[*normalized_values] end |
#size ⇒ Integer
7 |
# File 'sig/vendor/matrix.rbs', line 7
def size: () -> Integer
|