# File lib/sparse_matrix.rb, line 195 def initialize(r=0,c=0,matrix=[]) super(r,c) @mat = matrix end
# File lib/sparse_matrix.rb, line 253 def *(b) #multiplicacion de matrices, dispersas y densas if b.instance_of? SparseMatrix c = DenseMatrix.new(2,2,[[0.0,0.0],[0.0,0.0]]) 0.upto @mat.length-1 do |i| 0.upto @mat.length-1 do |j| c.mat[i][j]=0 0.upto @mat.length-1 do |k| c.mat[i][j] += @mat[i][k]*b.valor(k,j) end end end c else c = DenseMatrix.new(2,2,[[0.0,0.0],[0.0,0.0]]) 0.upto @mat.length-1 do |i| 0.upto @mat.length-1 do |j| c.mat[i][j]=0 0.upto @mat.length-1 do |k| c.mat[i][j] += @mat[i][k]*b.mat[k][j] end end end c end end
# File lib/sparse_matrix.rb, line 234 def +(b) #suma de matrices, tando densas como dispersas c = DenseMatrix.new(2,2,[[0.0,0.0],[0.0,0.0]]) if b.instance_of? SparseMatrix 0.upto @mat.size-1 do |i| 0.upto @mat.size-1 do |j| c.mat[i][j] = self.mat[i][j]+b.valor(i,j) end end c else 0.upto @mat.size-1 do |i| 0.upto @mat.size-1 do |j| c.mat[i][j] = self.mat[i][j]+b.mat[i][j] end end c end end
# File lib/sparse_matrix.rb, line 218 def print_matrix() #metodo que imprime la matrix en pantalla 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
# File lib/sparse_matrix.rb, line 202 def to_s() #devuelve cadena string 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