Class: Abst::Vector

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

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(coef) ⇒ Vector

Returns a new instance of Vector.

Raises:



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_classObject (readonly)

Returns the value of attribute coef_class.



6
7
8
# File 'lib/include/vector.rb', line 6

def coef_class
  @coef_class
end

.sizeObject (readonly)

Returns the value of attribute size.



6
7
8
# File 'lib/include/vector.rb', line 6

def size
  @size
end

Class Method Details

.inspectObject



12
13
14
# File 'lib/include/vector.rb', line 12

def inspect
	return to_s
end

.to_sObject



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

Raises:



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

Raises:



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

#eachObject



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

#inspectObject



67
68
69
# File 'lib/include/vector.rb', line 67

def inspect
	return "#{self.class}\n#{self}"
end

#sizeObject



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

def size
	return self.class.size
end

#squared_lengthObject



51
52
53
# File 'lib/include/vector.rb', line 51

def squared_length
	return self.coef.map{|i| i ** 2}.inject(&:+)
end

#to_aObject



59
60
61
# File 'lib/include/vector.rb', line 59

def to_a
	return @coef.dup
end

#to_sObject



63
64
65
# File 'lib/include/vector.rb', line 63

def to_s
	return @coef.to_s
end