require "Matriz.rb" require "Fraccion.rb" class Matriz_dispersa < Matriz attr_accessor :filas, :cols, :matriz #Contructor para las matrices dispersas def initialize(m) @filas = m.size @cols = m[1].size aux_m = Hash.new @filas.times do |i| @cols.times do |j| if m[i][j] != 0 aux_m[i.to_s+"_"+j.to_s] = m[i][j] end end end @matriz = aux_m end #Función que imprime una matriz dispersa def to_s aux_m = Array.new @filas.times do |i| aux_m[i] = Array.new @cols.times do |j| if m[i.to_s+"_"+j.to_s] != nil aux_m[i][j] = @matriz[i.to_s+"_"+j.to_s] else aux_m[i][j] = 0 end end end @matriz.each do |fila| puts fila.join(" ") end end #Sobrecarga del operador + para operar con matrices dispersas y densas def + (other) aux_m = Array.new @filas.times do |i| aux_m[i] = Array.new @cols.times do |j| aux_m[i][j] = 0; end end if other.is_a?(Matriz_dispersa) @filas.times do |i| @cols.times do |j| if @matriz[i.to_s+"_"+j.to_s] != nil if other.matriz[i.to_s+"_"+j.to_s] != nil aux_m[i][j] = @matriz[i.to_s+"_"+j.to_s] + other.matriz[i.to_s+"_"+j.to_s] else aux_m[i][j] = @matriz[i.to_s+"_"+j.to_s] end else if other.matriz[i.to_s+"_"+j.to_s] != nil aux_m[i][j] = other.matriz[i.to_s+"_"+j.to_s] end end end end elsif other.is_a?(Matriz_densa) @filas.times do |i| @cols.times do |j| if @matriz[i.to_s+"_"+j.to_s] != nil aux_m[i][j] = @matriz[i.to_s+"_"+j.to_s] + other.matriz[i][j] else aux_m[i][j] = other.matriz[i][j] end end end end Matriz.new(aux_m).comprobar() end #Sobrecarga del operador - para operar con matrices dispersas y densas def - (other) aux_m = Array.new @filas.times do |i| aux_m[i] = Array.new @cols.times do |j| aux_m[i][j] = 0; end end if other.is_a?(Matriz_dispersa) @filas.times do |i| @cols.times do |j| if @matriz[i.to_s+"_"+j.to_s] != nil if other.matriz[i.to_s+"_"+j.to_s] != nil aux_m[i][j] = @matriz[i.to_s+"_"+j.to_s] - other.matriz[i.to_s+"_"+j.to_s] else aux_m[i][j] = @matriz[i.to_s+"_"+j.to_s] end else if other.matriz[i.to_s+"_"+j.to_s] != nil aux_m[i][j] = -(other.matriz[i.to_s+"_"+j.to_s]) end end end end elsif other.is_a?(Matriz_densa) @filas.times do |i| @cols.times do |j| if @matriz[i.to_s+"_"+j.to_s] != nil aux_m[i][j] = @matriz[i.to_s+"_"+j.to_s] - other.matriz[i][j] else aux_m[i][j] = -(other.matriz[i][j]) end end end end Matriz.new(aux_m).comprobar() end #Sobrecarga del operador * para operar con matrices dispersas y densas def * (other) aux_m = Array.new @filas.times do |i| aux_m[i] = Array.new @cols.times do |j| aux_m[i][j] = 0; end end if other.is_a?(Matriz_dispersa) @filas.times do |i| other.cols.times do |j| other.filas.times do |k| if @matriz[i.to_s+"_"+k.to_s] != nil if other.matriz[k.to_s+"_"+j.to_s] != nil aux_m[i][j] += @matriz[i.to_s+"_"+k.to_s] * other.matriz[k.to_s+"_"+j.to_s] end end end end end elsif other.is_a?(Matriz_densa) @filas.times do |i| other.cols.times do |j| other.filas.times do |k| if @matriz[i.to_s+"_"+k.to_s] != nil aux_m[i][j] += @matriz[i.to_s+"_"+k.to_s] * other.matriz[k][j] end end end end end Matriz.new(aux_m).comprobar() end #Calcula la traspuesta de una matriz dispersa y la devuelve según el tipo que sea def traspuesta () aux_m = Array.new @filas.times do |i| aux_m[i] = Array.new @cols.times do |j| if @matriz[((@filas-1)-i).to_s+"_"+((@cols-1)-j).to_s] != nil aux_m[i][j] = @matriz[((@filas-1)-i).to_s+"_"+((@cols-1)-j).to_s] else aux_m[i][j] = 0 end end end Matriz.new(aux_m).comprobar end #Calcula el máximo de una matriz dispersa def max() aux_max = nil @filas.times do |i| @cols.times do |j| if @matriz[i.to_s+"_"+j.to_s] != nil and aux_max == nil aux_max = 0 elsif @matriz[i.to_s+"_"+j.to_s] != nil and aux_max < @matriz[i.to_s+"_"+j.to_s] aux_max = @matriz[i.to_s+"_"+j.to_s] end end end aux_max end #Calcula el mínimo de una matriz dispersa def min() aux_min = nil @filas.times do |i| @cols.times do |j| if @matriz[i.to_s+"_"+j.to_s] != nil and aux_min == nil aux_min = 0 elsif @matriz[i.to_s+"_"+j.to_s] != nil and aux_min > @matriz[i.to_s+"_"+j.to_s] aux_min = @matriz[i.to_s+"_"+j.to_s] end end end aux_min end end