Class: SparseMatrix::DenseMatrix
- Inherits:
-
AbstractMatrix
- Object
- AbstractMatrix
- SparseMatrix::DenseMatrix
- Defined in:
- lib/sparse_matrix.rb
Instance Attribute Summary collapse
-
#mat ⇒ Object
Returns the value of attribute mat.
Instance Method Summary collapse
- #*(b) ⇒ Object
- #+(b) ⇒ Object
-
#initialize(fn = "./input/null") ⇒ DenseMatrix
constructor
A new instance of DenseMatrix.
- #mapmap(a) ⇒ Object
- #print_matrix ⇒ Object
- #read_matrix ⇒ Object
- #to_m(a) ⇒ Object
- #to_s ⇒ Object
Methods inherited from AbstractMatrix
Constructor Details
#initialize(fn = "./input/null") ⇒ DenseMatrix
Returns a new instance of DenseMatrix.
21 22 23 24 |
# File 'lib/sparse_matrix.rb', line 21 def initialize(fn="./input/null") @filename=fn @mat = 0 end |
Instance Attribute Details
#mat ⇒ Object
Returns the value of attribute mat.
26 27 28 |
# File 'lib/sparse_matrix.rb', line 26 def mat @mat end |
Instance Method Details
#*(b) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/sparse_matrix.rb', line 91 def *(b) c=DenseMatrix.new() c.read_matrix for i in(0...@mat.length) for j in(0...@mat.length) c.mat[i][j]=0 for k in (0...@mat.length) c.mat[i][j] += @mat[i][k]*b.mat[k][j] end end end c end |
#+(b) ⇒ Object
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/sparse_matrix.rb', line 80 def +(b) c = DenseMatrix.new() c.read_matrix for i in (0...@mat.length) for j in(0...@mat.length) c.mat[i][j] = self.mat[i][j]+b.mat[i][j] end end c end |
#mapmap(a) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/sparse_matrix.rb', line 35 def mapmap(a) a.map { |r| r.map { |e| yield e } } end |
#print_matrix ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/sparse_matrix.rb', line 64 def print_matrix() printf "| " for i in (0... @mat.length) for j in (0... @mat.length) if j==0 printf "{ " end printf "#{@mat[i][j]}\t" if j == @mat.length-1 printf " } ," end end end printf "|" end |
#read_matrix ⇒ Object
28 29 30 31 32 33 |
# File 'lib/sparse_matrix.rb', line 28 def read_matrix text = File.open(@filename).read a = text.split(/\n\n+/) a = text.split(/\n/) @mat = to_m(a) end |
#to_m(a) ⇒ Object
43 44 45 46 |
# File 'lib/sparse_matrix.rb', line 43 def to_m(a) a = a.map { |r| r.split(/\s+/) } a = mapmap(a) { |x| x.to_f } end |
#to_s ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/sparse_matrix.rb', line 48 def to_s() s="| " for i in (0... @mat.length) for j in (0... @mat.length) if j==0 s += "{ " end s += "#{@mat[i][j]}\t" if j == @mat.length-1 s += " } , " end end end s += "|" end |