Class: Abst::Vector
- Inherits:
-
Object
- Object
- Abst::Vector
- Defined in:
- lib/include/vector.rb
Class Attribute Summary collapse
-
.coef_class ⇒ Object
readonly
Returns the value of attribute coef_class.
-
.size ⇒ Object
readonly
Returns the value of attribute size.
Class Method Summary collapse
Instance Method Summary collapse
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #==(other) ⇒ Object
- #[](index) ⇒ Object
- #each ⇒ Object
-
#initialize(coef) ⇒ Vector
constructor
A new instance of Vector.
- #inspect ⇒ Object
- #size ⇒ Object
- #squared_length ⇒ Object
- #to_a ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(coef) ⇒ Vector
Returns a new instance of Vector.
20 21 22 23 |
# File 'lib/include/vector.rb', line 20 def initialize(coef) raise VectorSizeError unless coef.size == self.class.size @coef = coef.to_a end |
Class Attribute Details
.coef_class ⇒ Object (readonly)
Returns the value of attribute coef_class.
6 7 8 |
# File 'lib/include/vector.rb', line 6 def coef_class @coef_class end |
.size ⇒ Object (readonly)
Returns the value of attribute size.
6 7 8 |
# File 'lib/include/vector.rb', line 6 def size @size end |
Class Method Details
.inspect ⇒ Object
12 13 14 |
# File 'lib/include/vector.rb', line 12 def inspect return to_s end |
.to_s ⇒ Object
8 9 10 |
# File 'lib/include/vector.rb', line 8 def to_s return "#{size} length #{self.coef_class} Vector" end |
Instance Method Details
#+(other) ⇒ Object
25 26 27 28 |
# File 'lib/include/vector.rb', line 25 def +(other) raise VectorSizeError unless self.size == other.size return self.class.new(self.coef.zip(other.coef).map{|a, b| a + b}) end |
#-(other) ⇒ Object
30 31 32 33 |
# File 'lib/include/vector.rb', line 30 def -(other) raise VectorSizeError unless self.size == other.size return self.class.new(self.coef.zip(other.coef).map{|a, b| a - b}) end |
#==(other) ⇒ Object
35 36 37 |
# File 'lib/include/vector.rb', line 35 def ==(other) return @coef == other.to_a end |
#[](index) ⇒ Object
55 56 57 |
# File 'lib/include/vector.rb', line 55 def [](index) return @coef[index] end |
#each ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/include/vector.rb', line 43 def each return Enumerator.new(self) unless block_given? @coef.each do |i| yield i end end |
#inspect ⇒ Object
67 68 69 |
# File 'lib/include/vector.rb', line 67 def inspect return "#{self.class}\n#{self}" end |
#size ⇒ Object
39 40 41 |
# File 'lib/include/vector.rb', line 39 def size return self.class.size end |
#squared_length ⇒ Object
51 52 53 |
# File 'lib/include/vector.rb', line 51 def squared_length return self.coef.map{|i| i ** 2}.inject(&:+) end |
#to_a ⇒ Object
59 60 61 |
# File 'lib/include/vector.rb', line 59 def to_a return @coef.dup end |
#to_s ⇒ Object
63 64 65 |
# File 'lib/include/vector.rb', line 63 def to_s return @coef.to_s end |