class MatrizDispersa
Attributes
hash[R]
mat[R]
Public Class Methods
new(nfil, ncol, mat)
click to toggle source
Calls superclass method
Matriz::new
# File lib/matriz.rb, line 126 def initialize (nfil, ncol, mat) super(nfil, ncol) @mat = Array.new(mat) nceros = 0 # numero de elementos nulos de la matriz (0) nelementos= (nfil * ncol)*0.6 # elementos de la matriz aplicado el 60 % psincero = 0 # posiciones de los elementos de la matriz cuyo valor no es nulo (0) @hash = Hash.new(0) for i in 0...nfil do for j in 0...ncol do if (mat[i][j]==0) nceros=nceros+1 else psincero="[#{i}][#{j}]" if (mat[i][j].is_a?Fraccion) a = mat[i][j].num b = mat[i][j].denom @hash[psincero] = Rational(a,b) #cad = " " #cad << "#{a}" #cad << "/" #cad << "#{b}" #@hash[psincero] = cad else @hash[psincero] = mat[i][j] end end end end if nceros >= nelementos # compruebo que la matriz sea dispersa #puts "La matriz es dispersa" else raise ArgumentError, 'La Matriz no es dispersa' end end
Public Instance Methods
+(other)
click to toggle source
# File lib/matriz.rb, line 172 def +(other) case other when MatrizDensa other.+(self) when MatrizDispersa raise ArgumentError, "Las matrices no son cuadradas." unless @nfil == other.nfil && @ncol == other.ncol suma = MatrizDispersa.new(nfil,ncol,0) suma = hash.merge(other.hash){|key,oldval,newval| oldval+newval } return suma # devuelve un objeto de tipo Matriz Dispersa else raise TypeError, "La matriz no es dispersa ni densa" unless other.instance_of? MatrizDispersa end end
-(other)
click to toggle source
# File lib/matriz.rb, line 189 def -(other) case other when MatrizDensa other.-(self) when MatrizDispersa raise ArgumentError, "Las matrices no son cuadradas." unless @nfil == other.nfil && @ncol == other.ncol resta = MatrizDispersa.new(nfil,ncol,0) resta = hash.merge(other.hash){|key,oldval,newval| oldval-newval} return resta # devuelve un objeto de tipo Matriz Dispersa else raise TypeError, "La matriz no es dispersa ni densa " unless other.instance_of? MatrizDispersa end end
max()
click to toggle source
# File lib/matriz.rb, line 204 def max max = 0 hash.each do |clave,valor| if (valor > max) max=valor end end return max end
min()
click to toggle source
# File lib/matriz.rb, line 214 def min min = 9999 hash.each do |clave,valor| if (valor < min) min=valor end end return min end
to_s()
click to toggle source
# File lib/matriz.rb, line 161 def to_s if (hash.values != nil) cad = "" cad << "#{hash}" return cad else return 0 end #return hash end