Class: Abst::Matrix

Inherits:
Object
  • Object
show all
Includes:
Group
Defined in:
lib/include/matrix.rb

Direct Known Subclasses

SquareMatrix

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Group

included

Constructor Details

#initialize(m) ⇒ Matrix

Returns a new instance of Matrix.



22
23
24
25
26
27
28
29
# File 'lib/include/matrix.rb', line 22

def initialize(m)
	raise MatrixSizeError unless self.class.height == m.size
	@coef = m.map{|row| self.class.coef_vector.new(row)}
	@height = self.class.height
	@width = self.class.width
rescue VectorSizeError
	raise MatrixSizeError
end

Class Attribute Details

.coef_classObject (readonly)

Returns the value of attribute coef_class.



8
9
10
# File 'lib/include/matrix.rb', line 8

def coef_class
  @coef_class
end

.coef_vectorObject (readonly)

Returns the value of attribute coef_vector.



8
9
10
# File 'lib/include/matrix.rb', line 8

def coef_vector
  @coef_vector
end

.heightObject (readonly)

Returns the value of attribute height.



8
9
10
# File 'lib/include/matrix.rb', line 8

def height
  @height
end

.widthObject (readonly)

Returns the value of attribute width.



8
9
10
# File 'lib/include/matrix.rb', line 8

def width
  @width
end

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



19
20
21
# File 'lib/include/matrix.rb', line 19

def height
  @height
end

#widthObject (readonly)

Returns the value of attribute width.



19
20
21
# File 'lib/include/matrix.rb', line 19

def width
  @width
end

Class Method Details

.inspectObject



14
15
16
# File 'lib/include/matrix.rb', line 14

def inspect
	return to_s
end

.to_sObject



10
11
12
# File 'lib/include/matrix.rb', line 10

def to_s
	return "#{height} * #{width} #{self.coef_class} Matrix"
end

Instance Method Details

#*(other) ⇒ Object

Raises:

  • (NotImplementedError)


45
46
47
# File 'lib/include/matrix.rb', line 45

def *(other)
	raise NotImplementedError
end

#add_sub(op, other) ⇒ Object



41
42
43
# File 'lib/include/matrix.rb', line 41

def add_sub(op, other)
	return self.class.new(@coef.zip(other.coef).map{|a, b| a.__send__(op, b)})
end

#eachObject



31
32
33
34
35
36
37
38
39
# File 'lib/include/matrix.rb', line 31

def each
	return Enumerator.new(self) unless block_given?

	@coef.each do |row|
		row.each do |i|
			yield i
		end
	end
end

#inspectObject



108
109
110
# File 'lib/include/matrix.rb', line 108

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

#solveObject

Param

self must be n * (n + 1) matrix s.t. (MB) M is invertible n * n matrix B is a column vector

Return

a column vector X s.t. MX == B

Raises:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/include/matrix.rb', line 53

def solve
	raise MatrixSizeError unless @height + 1 == @width

	inverse = []
	m = self.to_a
	n = @height

	n.times do |j|
		# Find non-zero entry
		row = nil
		j.upto(n - 1) do |i|
			unless m[i][j].zero?
				row = i
				break
			end
		end
		return nil unless row

		# Swap?
		if j < row
			m[row], m[j] = m[j], m[row]
		end

		# Eliminate
		inverse[j] = m[j][j].inverse
		(j + 1).upto(n - 1) do |i|
			c = m[i][j] * inverse[j]

			(j + 1).upto(n) do |j2|
				m[i][j2] -= m[j][j2] * c
			end
		end
	end

	# Solve triangular system
	x = []
	(n - 1).downto(0) do |i|
		temp = m[i][n]
		(i + 1).upto(n - 1) do |j|
			temp -= m[i][j] * x[j]
		end
		x[i] = temp * inverse[i]
	end

	return Abst::Vector(self.class.coef_class, x)
end

#to_aObject



100
101
102
# File 'lib/include/matrix.rb', line 100

def to_a
	return @coef.map{|row| row.to_a}
end

#to_sObject



104
105
106
# File 'lib/include/matrix.rb', line 104

def to_s
	return "[#{@coef.map(&:to_s).join(', ')}]"
end