Class: Perlin::Vector
- Inherits:
-
Object
- Object
- Perlin::Vector
- Defined in:
- lib/perlin/vector.rb
Class Method Summary collapse
Instance Method Summary collapse
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #/(scalar) ⇒ Object
- #[](i) ⇒ Object
-
#initialize(elements) ⇒ Vector
constructor
A new instance of Vector.
- #inner_product(other) ⇒ Object
- #map(&block) ⇒ Object
- #r ⇒ Object
- #to_a ⇒ Object
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
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 |
#r ⇒ Object
15 16 17 |
# File 'lib/perlin/vector.rb', line 15 def r Math.sqrt(@elements.sum { |e| e**2 }) end |
#to_a ⇒ Object
11 12 13 |
# File 'lib/perlin/vector.rb', line 11 def to_a @elements end |