Class: MatrizDispersa

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

Overview

Clase que hereda de la clase Matriz. Almacena matrices dispersas (matrices que la mayoria de sus posiciones son nulas 0)

Instance Attribute Summary collapse

Attributes inherited from Matriz

#col, #fil

Instance Method Summary collapse

Methods inherited from Matriz

#+, #-

Constructor Details

#initialize(f, c, posx, posy, valor) ⇒ MatrizDispersa

se recibe el valor de filas, columnas, los dos arrays con las posiciones en que hay valores y los valores correspondientes



323
324
325
326
327
328
329
# File 'lib/matrices_p9.rb', line 323

def initialize(f,c,posx, posy, valor)
	super(f,c)
	@posx = posx
	@posy = posy
	@valor = valor

end

Instance Attribute Details

#posxObject

Almacena las filas en que se encuentra algun valor



312
313
314
# File 'lib/matrices_p9.rb', line 312

def posx
  @posx
end

#posyObject

Almacena las columnas en que se encuentra algun valor



315
316
317
# File 'lib/matrices_p9.rb', line 315

def posy
  @posy
end

#valorObject

Almacena los valores correspondientes



318
319
320
# File 'lib/matrices_p9.rb', line 318

def valor
  @valor
end

Instance Method Details

#maxObject

devuelve el mayor valor almacenado en la matriz



342
343
344
345
346
347
348
349
350
# File 'lib/matrices_p9.rb', line 342

def max
	m = self.valor[0]
	for i in (0...self.valor.size.to_i)
			if (self.valor[i]> m)
				m = self.valor[i]
			end
	end
	return m
end

#minObject

devuelve el menor valor almacenado en la matriz



353
354
355
356
357
358
359
360
361
# File 'lib/matrices_p9.rb', line 353

def min
	m = self.valor[0]
	for i in (0...self.valor.size.to_i)
			if (self.valor[i]< m)
				m = self.valor[i]
			end
	end
	return m
end

#pos(a, b) ⇒ Object

Retorna el valor correspondiente a las posiciones que se le pasen como parámetros



363
364
365
366
367
368
369
370
# File 'lib/matrices_p9.rb', line 363

def pos(a,b)
	for i in (0...self.posx.size)
		if(posx[i]==a and posy[i]==b)
			return valor[i]
		end
	end
	return nil
end

#to_sObject

Mdevuelve la matriz en forma de string



332
333
334
335
336
337
338
339
# File 'lib/matrices_p9.rb', line 332

def to_s
	s=String.new
	s << "["
	for i in (0...@fil.to_i)
		s << "[#{posx[i]},#{posy[i]},#{valor[i]}]"
	end
	s << "]"
end