Class: Abst::Matrix
Direct Known Subclasses
Class Attribute Summary collapse
-
.coef_class ⇒ Object
readonly
Returns the value of attribute coef_class.
-
.coef_vector ⇒ Object
readonly
Returns the value of attribute coef_vector.
-
.height ⇒ Object
readonly
Returns the value of attribute height.
-
.width ⇒ Object
readonly
Returns the value of attribute width.
Instance Attribute Summary collapse
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Class Method Summary collapse
Instance Method Summary collapse
- #*(other) ⇒ Object
- #add_sub(op, other) ⇒ Object
- #each ⇒ Object
-
#initialize(m) ⇒ Matrix
constructor
A new instance of Matrix.
- #inspect ⇒ Object
-
#solve ⇒ Object
- Param
-
self must be n * (n + 1) matrix s.t.
- #to_a ⇒ Object
- #to_s ⇒ Object
Methods included from Group
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_class ⇒ Object (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_vector ⇒ Object (readonly)
Returns the value of attribute coef_vector.
8 9 10 |
# File 'lib/include/matrix.rb', line 8 def coef_vector @coef_vector end |
.height ⇒ Object (readonly)
Returns the value of attribute height.
8 9 10 |
# File 'lib/include/matrix.rb', line 8 def height @height end |
.width ⇒ Object (readonly)
Returns the value of attribute width.
8 9 10 |
# File 'lib/include/matrix.rb', line 8 def width @width end |
Instance Attribute Details
#height ⇒ Object (readonly)
Returns the value of attribute height.
19 20 21 |
# File 'lib/include/matrix.rb', line 19 def height @height end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
19 20 21 |
# File 'lib/include/matrix.rb', line 19 def width @width end |
Class Method Details
.inspect ⇒ Object
14 15 16 |
# File 'lib/include/matrix.rb', line 14 def inspect return to_s end |
.to_s ⇒ Object
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
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 |
#each ⇒ Object
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 |
#inspect ⇒ Object
108 109 110 |
# File 'lib/include/matrix.rb', line 108 def inspect return "#{self.class}\n#{self}" end |
#solve ⇒ Object
- 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
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_a ⇒ Object
100 101 102 |
# File 'lib/include/matrix.rb', line 100 def to_a return @coef.map{|row| row.to_a} end |
#to_s ⇒ Object
104 105 106 |
# File 'lib/include/matrix.rb', line 104 def to_s return "[#{@coef.map(&:to_s).join(', ')}]" end |