Class: Perlin::Vector

Inherits:
Object
  • Object
show all
Defined in:
lib/perlin/vector.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(elements) ⇒ Vector

Returns a new instance of Vector.



7
8
9
# File 'lib/perlin/vector.rb', line 7

def initialize(elements)
  @elements = elements
end

Class Method Details

.[](*elements) ⇒ Object



3
4
5
# File 'lib/perlin/vector.rb', line 3

def self.[](*elements)
  Vector.new(elements)
end

Instance Method Details

#+(other) ⇒ Object



27
28
29
# File 'lib/perlin/vector.rb', line 27

def +(other)
  Vector.new(@elements.zip(other.to_a).map { |a, b| a + b })
end

#-(other) ⇒ Object



31
32
33
# File 'lib/perlin/vector.rb', line 31

def -(other)
  Vector.new(@elements.zip(other.to_a).map { |a, b| a - b })
end

#/(scalar) ⇒ Object



35
36
37
# File 'lib/perlin/vector.rb', line 35

def /(scalar)
  Vector.new(@elements.map { |e| e / scalar })
end

#[](i) ⇒ Object



19
20
21
# File 'lib/perlin/vector.rb', line 19

def [](i)
  @elements[i]
end

#inner_product(other) ⇒ Object



39
40
41
# File 'lib/perlin/vector.rb', line 39

def inner_product(other)
  @elements.zip(other.to_a).sum { |a, b| a * b }
end

#map(&block) ⇒ Object



23
24
25
# File 'lib/perlin/vector.rb', line 23

def map(&block)
  Vector.new(@elements.map(&block))
end

#rObject



15
16
17
# File 'lib/perlin/vector.rb', line 15

def r
  Math.sqrt(@elements.sum { |e| e**2 })
end

#to_aObject



11
12
13
# File 'lib/perlin/vector.rb', line 11

def to_a
  @elements
end