Class: MatrizDispersa

Inherits:
Matriz
  • Object
show all
Defined in:
lib/matriz.rb

Instance Attribute Summary collapse

Attributes inherited from Matriz

#matriz

Instance Method Summary collapse

Methods inherited from Matriz

#[]

Methods included from Operatoria

#*, #-@, #Producto_escalar, #[]=

Constructor Details

#initialize(matriz) ⇒ MatrizDispersa

Returns a new instance of MatrizDispersa.



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/matriz.rb', line 180

def initialize(matriz)
   @filas = matriz.size
   @columnas = matriz[0].size
   @hash_no_ceros={}
    
  #comprobamos que la matriz es dispersa o no
  @n_elementos= (matriz.size * matriz[0].size)*0.6 
  n_ceros=0
   filas.times do |i|
      columnas.times do |j|
			if (matriz[i][j]==0)  
				 n_ceros=n_ceros+1
			else
				 pos_no_cero="#{i}#{j}"
				 @hash_no_ceros[pos_no_cero]=matriz[i][j] 
			end
		end
   end
   
   if n_ceros < @n_elementos
      raise RuntimeError, 'La Matriz no es dispersa'
   else
   end
   
end

Instance Attribute Details

#columnasObject

modificar el initialize,pues no necesito almacenar los ‘0’ guardar los indices donde se encuentran dichos ceros metodo que dado una fila y columna y un porcentaje de ceros prc,construye una matriz aleatoria



178
179
180
# File 'lib/matriz.rb', line 178

def columnas
  @columnas
end

#filasObject

modificar el initialize,pues no necesito almacenar los ‘0’ guardar los indices donde se encuentran dichos ceros metodo que dado una fila y columna y un porcentaje de ceros prc,construye una matriz aleatoria



178
179
180
# File 'lib/matriz.rb', line 178

def filas
  @filas
end

#hash_no_cerosObject

modificar el initialize,pues no necesito almacenar los ‘0’ guardar los indices donde se encuentran dichos ceros metodo que dado una fila y columna y un porcentaje de ceros prc,construye una matriz aleatoria



178
179
180
# File 'lib/matriz.rb', line 178

def hash_no_ceros
  @hash_no_ceros
end

Instance Method Details

#+(other) ⇒ Object

Raises:

  • (TypeError)


230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/matriz.rb', line 230

def +(other)
  raise TypeError, "La matriz other no es dispersa" unless other.instance_of? MatrizDispersa
  raise ArgumentError, "La longitud de las matrices no coincide." unless @filas == other.filas && @columnas == other.columnas
  suma=MatrizDispersa.new(Array.new(@filas,0){Array.new(@columnas,0)})
  suma.hash_no_ceros = (hash_no_ceros.merge(other.hash_no_ceros){|key,oldval,newval| oldval+newval}).clone
  if comprobar(suma.hash_no_ceros)
	return suma
  else
	m = Array.new(@filas,0){Array.new(@columnas,0)}
	suma.hash_no_ceros.each {|key, value| m[(key[0]).to_i][(key[1]).to_i] = value }
	sum = MatrizDensa.new(m)
		return sum
  end
  
			
end

#-(other) ⇒ Object

Raises:

  • (TypeError)


247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/matriz.rb', line 247

def -(other)
    raise TypeError, "La matriz other no es dispersa" unless other.instance_of? MatrizDispersa
    raise ArgumentError, "La longitud de las matrices no coincide." unless @filas == other.filas && @columnas == other.columnas
    resta=MatrizDispersa.new(Array.new(@filas,0){Array.new(@columnas,0)})
    resta.hash_no_ceros = (self.hash_no_ceros.merge(other.hash_no_ceros){|key,oldval,newval| oldval-0-newval}).clone      
    if comprobar(resta.hash_no_ceros)
			return resta
    else
			puts resta.hash_no_ceros
			m = Array.new(@filas,0){Array.new(@columnas,0)}
			resta.hash_no_ceros.each {|key, value| m[(key[0]).to_i][(key[1]).to_i] = value }
			res = MatrizDensa.new(m)
			return res
	end
end

#==(other) ⇒ Object



263
264
265
# File 'lib/matriz.rb', line 263

def ==(other)
hash_no_ceros == other.hash_no_ceros
end

#comprobar(hash) ⇒ Object



207
208
209
210
211
212
213
# File 'lib/matriz.rb', line 207

def comprobar (hash)
	if hash.length  > ((@filas * @columnas)*0.4)
		false
	else
		true
end
end

#to_sObject



215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/matriz.rb', line 215

def to_s
   filas.times do |i|
     columnas.times do |j|
 if (hash_no_ceros.key?("#{i}#{j}"))
    print hash_no_ceros["#{i}#{j}"]
    print "  "
 else
    print "0  "
 end
	 end
	 puts
   end
end