class SparseMatrix::SparseMatrix
Attributes
MAT[RW]
Public Class Methods
new(*args)
click to toggle source
# File lib/sparse_matrix.rb, line 69 def initialize(*args) @n,@m=args[0],args[1] datos = args[2] @MAT = Array.new() for i in 0...@n for j in 0...@m if datos[i][j] != 0 @MAT[i]=SparseVector.new(i,j,datos[i][j]) end end end @max=max() @min=min() end
Public Instance Methods
*(b)
click to toggle source
# File lib/sparse_matrix.rb, line 155 def *(b) #metodo para multiplicar matrices, dispersa como densas if b.instance_of? SparseMatrix c = DenseMatrix.new(2,2,[[0.0,0.0],[0.0,0.0]]) 0.upto @MAT.size-1 do |i| 0.upto @MAT.size-1 do |j| c.mat[i][j]=0 0.upto @MAT.size-1 do |k| c.mat[i][j] += self.valor(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.size-1 do |i| 0.upto @MAT.size-1 do |j| c.mat[i][j]=0 0.upto @MAT.size-1 do |k| c.mat[i][j] += self.valor(i,k)*b.mat[k][j] end end end c end end
+(other)
click to toggle source
# File lib/sparse_matrix.rb, line 135 def +(other) #metodo para sumar matrices, dispersa como densa if other.instance_of? SparseMatrix o=DenseMatrix.new(@n,@m,Array.new(@n){Array.new(@m)}) 0.upto @MAT.size-1 do |i| 0.upto @MAT.size-1 do |j| o.mat[i][j]=self.valor(i,j)+other.valor(i,j) end end return SparseMatrix.new(@n,@m,o.mat) #SparseMatrix.new(@n,@m,o) else o=DenseMatrix.new(@n,@m,Array.new(@n){Array.new(@m)}) 0.upto @n-1 do |i| 0.upto @m-1 do |j| o.mat[i][j]=self.valor(i,j)+other.mat[i][j] end end return o end end
insert(vector)
click to toggle source
# File lib/sparse_matrix.rb, line 85 def insert(vector) @MAT<<vector end
max()
click to toggle source
# File lib/sparse_matrix.rb, line 115 def max #elemento maximo de la matrix maximo=@MAT[0].value for i in 0...@m do if maximo < @MAT[i].value maximo=@MAT[i].value end end return maximo end
min()
click to toggle source
# File lib/sparse_matrix.rb, line 125 def min #elemento minimo de la matrix minimo=@MAT[0].value for i in (0...@m) do if minimo > @MAT[i].value minimo=@MAT[i].value end end return minimo end
to_s()
click to toggle source
# File lib/sparse_matrix.rb, line 89 def to_s #devuelve una cadena string cadena="" for i in 0...@n cadena+="[" for j in 0...@m cadena+=self.valor(i,j).to_s if j < @m-1 cadena+=", " end end cadena+="]" end return cadena end
valor(k,j)
click to toggle source
# File lib/sparse_matrix.rb, line 105 def valor(k,j) #para buscar un valor especifico dev=0 for i in 0...@MAT.size if(@MAT[i].i==k) && (@MAT[i].j==j) return @MAT[i].value end end dev end