class MatrizDensa
Clase de Matriz densa¶ ↑
Attributes
columnas[R]
filas[R]
matriz[R]
Public Class Methods
new(matriz)
click to toggle source
Inicialización¶ ↑
Calls superclass method
MatrizAbstracta::new
# File lib/modai_prct12/matrizDensa.rb, line 5 def initialize(matriz) super end
Public Instance Methods
*(o)
click to toggle source
Multiplicación de matrices¶ ↑
# File lib/modai_prct12/matrizDensa.rb, line 126 def *(o) prod = Array.new(matriz.size - 1,0) 0.upto(matriz[0].size - 1) do |i| prod[i] = Array.new(o.matriz.size,0) (o.matriz.size).times do |j| pos = 0 # 0.upto(matriz.size - 1) do |pos| (0...(matriz.size)).collect { prod[i][j] = prod[i][j] + (matriz[i][pos] * o.matriz[pos][j]) pos += 1 } # end end end MatrizDensa.new(prod) end
+(o)
click to toggle source
Suma de matrices¶ ↑
# File lib/modai_prct12/matrizDensa.rb, line 50 def +(o) if o.instance_of? MatrizDispersa other = to_densa(o) else other = o end suma = Array.new(matriz.size - 1) 0.upto(matriz.size - 1) do |i| suma[i] = Array.new(matriz[i].size - 1) # 0.upto(matriz[i].size - 1) do |j| j = 0 (0...(matriz[i].size)).collect { suma[i][j] = matriz[i][j] + other.matriz[i][j] j += 1 } # end end MatrizDensa.new(suma) end
-(o)
click to toggle source
/(o)
click to toggle source
Suma de matrices densa con dispersa (sobreescribimos el operador / como prueba)¶ ↑
# File lib/modai_prct12/matrizDensa.rb, line 81 def /(o) suma = Array.new(matriz.size - 1) 0.upto(matriz.size - 1) do |i| suma[i] = Array.new(matriz[i].size - 1) 0.upto(matriz[i].size - 1) do |j| suma[i][j] = matriz[i][j] #=== comprobamos el hash if (o.matriz[i] != nil) #=== hay datos en el has para la columna if o.matriz[i].has_key?(j) suma[i][j] = matriz[i][j] + o.matriz[i][j] end end end end MatrizDensa.new(suma) end
coerce(other)
click to toggle source
encontrar() { |matriz[j]| ... }
click to toggle source
max()
click to toggle source
min()
click to toggle source
to_densa(o)
click to toggle source
Pasamos de Dispersa a Densa¶ ↑
# File lib/modai_prct12/matrizDensa.rb, line 180 def to_densa(o) densa = Array.new(o.matriz.size - 1) for i in 0...o.matriz.size densa[i] = Array.new(o.matriz.size - 1) for j in 0...o.matriz.size densa[i][j] = 0.to_f if o.matriz[i] != nil o.matriz[i].each do |key, value| if key = j densa[i][j] = o.matriz[i][key].to_f end end end end end MatrizDensa.new(densa) end
to_f()
click to toggle source
to_s()
click to toggle source
Convertimos a string¶ ↑
# File lib/modai_prct12/matrizDensa.rb, line 12 def to_s fil = 0 print "[" while fil < filas col = 0 while col < columnas print "#{matriz[fil][col].to_s}" if (col + 1) < columnas then print ", " end col += 1 end if (fil + 1) < filas then print ", " end fil += 1 end print "]" end