Class: Matriz_dispersa
Constant Summary
Constants included from Matriz
Instance Attribute Summary collapse
-
#col ⇒ Object
readonly
Returns the value of attribute col.
-
#dato ⇒ Object
readonly
Returns the value of attribute dato.
-
#fil ⇒ Object
readonly
Returns the value of attribute fil.
-
#pos ⇒ Object
readonly
Returns the value of attribute pos.
Instance Method Summary collapse
-
#*(other) ⇒ Object
Producto: segun clase de other: si es un numero realiza el producto ecalar, si en cambio es otra matriz se realiza el producto de matrices.
-
#+(other) ⇒ Object
Devuelve la suma de dos matrices.
-
#-(other) ⇒ Object
Devuelve la resta de dos matrices.
-
#==(other) ⇒ Object
Compara si dos matrices son iguales o no.
-
#buscar(i, j) ⇒ Object
Devuelve una posicion del array que coincide con los indices de los param.
-
#coerce(other) ⇒ Object
Metodo para hacer la conversion de datos para poder operar con la clase matriz dispersa.
-
#generar(o) ⇒ Object
Segun la opcion elegida (1 o 2) crear la matriz dispersa con valores aleatorios (1)enteros o (2)racionales.
-
#initialize(m, n) ⇒ Matriz_dispersa
constructor
Estructura de datos de la matriz dispersa.
-
#introducir_datos(o) ⇒ Object
El usuario puede elegir los tipos de datos (1.enteros y 2.racionales) y datos que contendra la matriz.
-
#llenar(posciones, datos) ⇒ Object
Crea la matriz dispersa a partir del array de posiciones y de datos: posiciones y datos tienen que tener la misma dimension y datos no puede contener ceros.
-
#max ⇒ Object
Devuelve el elemento mayor de la matriz.
-
#mi_random ⇒ Object
Hago esto, pq rand(-10..10) puede generar el valor cero y esto no nos interesa almacenarlo.
-
#min ⇒ Object
Devuelve el elemento menor de la matriz.
-
#t ⇒ Object
Calcula la traspuesta de la matriz.
-
#to_s ⇒ Object
Metodo para mostrar una matriz dispersa.
Constructor Details
#initialize(m, n) ⇒ Matriz_dispersa
Estructura de datos de la matriz dispersa
8 9 10 11 12 13 14 |
# File 'lib/matriz_dispersa.rb', line 8 def initialize (m,n) #Estructura de datos de la matriz dispersa raise TypeError, "Error. Tipo de dimensiones incorrectas" if ((m < 0) or (n < 0)) super # @fil, @col = m, n @pos = Array.new(0){} @dato = Array.new(0){} end |
Instance Attribute Details
#col ⇒ Object (readonly)
Returns the value of attribute col.
6 7 8 |
# File 'lib/matriz_dispersa.rb', line 6 def col @col end |
#dato ⇒ Object (readonly)
Returns the value of attribute dato.
6 7 8 |
# File 'lib/matriz_dispersa.rb', line 6 def dato @dato end |
#fil ⇒ Object (readonly)
Returns the value of attribute fil.
6 7 8 |
# File 'lib/matriz_dispersa.rb', line 6 def fil @fil end |
#pos ⇒ Object (readonly)
Returns the value of attribute pos.
6 7 8 |
# File 'lib/matriz_dispersa.rb', line 6 def pos @pos end |
Instance Method Details
#*(other) ⇒ Object
Producto: segun clase de other: si es un numero realiza el producto ecalar, si en cambio es otra matriz se realiza el producto de matrices.
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/matriz_dispersa.rb', line 260 def *(other) #Producto: segun clase de other: si es un numero realiza el producto ecalar, si en cambio es otra matriz se realiza el producto de matrices. if other.is_a?(Numeric) nueva=self @dato.size.times do |i| # for i in ([email protected]) nueva.dato[i] = other*nueva.dato[i] end nueva elsif other.is_a?(Matriz) if (self.col == other.fil) if (other.is_a?(Matriz_densa)) other=self.coerce(other) other=other[0] end nueva= Matriz_dispersa.new(@fil,other.col) @fil.times do |i| # for i in (0...@fil) @col.times do |j| # for j in (0...@col) resul = 0 @col.times do |k| # for k in (0...@col) n = self.buscar(i,k) if n != -1 m = other.buscar(k,j) if m != -1 resul += self.dato[n] * other.dato[m] end end end if resul != 0 nueva.dato << resul nueva.pos << [i,j] end end # for j end #for i nueva else puts "Error. No se pueden multiplicar estas matrices. La col de la M1 debe coincidir con la fil de M2" end end end |
#+(other) ⇒ Object
Devuelve la suma de dos matrices
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/matriz_dispersa.rb', line 192 def +(other) #Devuelve la suma de dos matrices raise unless other.is_a?(Matriz) #deben ser matrices, da = si se comparan densas con dispersas if (other.is_a?(Matriz_densa)) other=self.coerce(other) other=other[0] end if (@fil == other.fil) and (@col == other.col) nueva= Matriz_dispersa.new(@fil,@col) @pos.size.times do |i| # 0.upto(@pos.size-1) do |i| # for i in ([email protected]) k = other.buscar(@pos[i][0],@pos[i][1]) if (k!= -1) #existe esa pos if (@dato[i]+other.dato[k]) != 0 nueva.pos << @pos[i] nueva.dato << (@dato[i]+other.dato[k]) end else nueva.pos << @pos[i] nueva.dato << @dato[i] end end #times #almacenamos los que no se hayan sumado de la otra matriz other.pos.size.times do |i| # 0.upto(other.pos.size-1) do |i| #for i in (0...other.pos.size) k = nueva.buscar(other.pos[i][0],other.pos[i][1]) if (k==-1) nueva.pos << other.pos[i] nueva.dato << other.dato[i] end end else puts "No se pueden sumar, no tienen las mismas dimensiones" end #if nueva end |
#-(other) ⇒ Object
Devuelve la resta de dos matrices
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/matriz_dispersa.rb', line 226 def -(other) #Devuelve la resta de dos matrices raise unless other.is_a?(Matriz) #deben ser matrices, da = si se comparan densas con dispersas if (other.is_a?(Matriz_densa)) other=self.coerce(other) other=other[0] end if (@fil == other.fil) and (@col == other.col) nueva= Matriz_dispersa.new(@fil,@col) @pos.size.times do |i| # 0.upto(@pos.size-1) do |i| # for i in ([email protected]) k = other.buscar(@pos[i][0],@pos[i][1]) if (k!= -1) #existe esa pos if (@dato[i]-other.dato[k]) != 0 nueva.pos << @pos[i] nueva.dato << (@dato[i]-other.dato[k]) end else nueva.pos << @pos[i] nueva.dato << @dato[i] end end #for #almacenamos los que no se hayan sumado de la otra matriz other.pos.size.times do |i| # 0.upto(other.pos.size-1) do |i| #for i in (0...other.pos.size) k = nueva.buscar(other.pos[i][0],other.pos[i][1]) if (k==-1) nueva.pos << other.pos[i] nueva.dato << -other.dato[i] end end else puts "No se pueden restar, no tienen las mismas dimensiones" end #if nueva end |
#==(other) ⇒ Object
Compara si dos matrices son iguales o no
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/matriz_dispersa.rb', line 143 def ==(other) #Compara si dos matrices son iguales o no raise unless other.is_a?(Matriz) #deben ser matrices, da = si se comparan densas con dispersas if (other.is_a?(Matriz_densa)) other=self.coerce(other) other=other[0] end if (@fil == other.fil) and (@col == other.col) if (@pos.size == 0) and (other.pos.size == 0)#si ambos estan estan vacios... return true elsif (@pos.size == other.pos.size) for i in (0...@pos.size) k = other.buscar(@pos[i][0],@pos[i][1]) #buscamos esa pos en el otro vector if (k == -1) return false elsif (@dato[i] != other.dato[k]) return false end end return true else return false end else return false end end |
#buscar(i, j) ⇒ Object
Devuelve una posicion del array que coincide con los indices de los param
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/matriz_dispersa.rb', line 64 def buscar (i,j) #Devuelve una posicion del array que coincide con los indices de los param aux=[i,j] posicion= -1 k=0 while (k < @pos.size) and (posicion==-1) if (@pos[k]==aux) posicion=k end k=k+1 end posicion end |
#coerce(other) ⇒ Object
Metodo para hacer la conversion de datos para poder operar con la clase matriz dispersa
170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/matriz_dispersa.rb', line 170 def coerce(other) #Metodo para hacer la conversion de datos para poder operar con la clase matriz dispersa a=Matriz_dispersa.new(other.fil,other.col) for i in (0...other.fil) for j in (0...other.col) if (other.mat[i][j] != 0) a.pos << [i,j] a.dato << other.mat[i][j] end end end return[a,self] end |
#generar(o) ⇒ Object
Segun la opcion elegida (1 o 2) crear la matriz dispersa con valores aleatorios (1)enteros o (2)racionales
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/matriz_dispersa.rb', line 47 def generar (o) #Segun la opcion elegida (1 o 2) crear la matriz dispersa con valores aleatorios (1)enteros o (2)racionales if (@fil*@col) == 1 elementos = 1 elsif elementos =(rand(60..100)*(@fil*@col))/100 #minimo 60% de ceros end dim= (@fil*@col)-elementos #num de elementos posibles para introducir valores respetando la dispersion @pos = Array.new(dim){[rand(0..(@fil-1)),rand(0..(@col-1))]} for i in (0...dim) if (o==1) #generamos enteros @dato = Array.new(dim){mi_random*rand(1..10)} elsif (o==2) #generamos racionales @dato = Array.new(dim){NumerosRacionales.new(mi_random*rand(1..10),rand(1..10))} end end end |
#introducir_datos(o) ⇒ Object
El usuario puede elegir los tipos de datos (1.enteros y 2.racionales) y datos que contendra la matriz
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/matriz_dispersa.rb', line 78 def introducir_datos (o) #El usuario puede elegir los tipos de datos (1.enteros y 2.racionales) y datos que contendra la matriz if (@fil*@col)==1 max=0 elsif max= (@fil*@col*60)/100 max = (@fil*@col)-max end total= -1 while (total<0) or (total>max) puts "Cuantos datos va a introducir? [0-#{max}]" STDOUT.flush total=gets.chomp total=total.to_i end if (o==1) #de numeros enteros for k in (0...total) i,j= -1,-1 while (i<0) or (i>(@fil-1)) puts "introduce la coordenada i: " STDOUT.flush i=(gets.chomp).to_i end while (j<0) or (j>(@col-1)) puts "introduce la coordenada j: " STDOUT.flush j=(gets.chomp).to_i end @pos << [i.to_i,j.to_i] dato=0 while (dato == 0) puts "introduce el dato (=/=0) de la casilla (#{i},#{j}): " STDOUT.flush dato=(gets.chomp).to_i end @dato << dato end elsif #de numeros racionales for k in (0...total) i,j= -1,-1 while (i<0) or (i>(@fil-1)) puts "introduce la coordenada i: " STDOUT.flush i=(gets.chomp).to_i end while (j<0) or (j>(@col-1)) puts "introduce la coordenada j: " STDOUT.flush j=(gets.chomp).to_i end @pos << [i.to_i,j.to_i] num=0 while (num == 0) puts "introduce el dato (=/=0) de la casilla (#{i},#{j}): " puts "numerador: " STDOUT.flush num=(gets.chomp).to_i end puts "denominador: " STDOUT.flush den=gets.chomp @dato << NumerosRacionales.new(num,den.to_i) end end end |
#llenar(posciones, datos) ⇒ Object
Crea la matriz dispersa a partir del array de posiciones y de datos: posiciones y datos tienen que tener la misma dimension y datos no puede contener ceros
16 17 18 19 20 21 |
# File 'lib/matriz_dispersa.rb', line 16 def llenar (posciones,datos) #Crea la matriz dispersa a partir del array de posiciones y de datos: posiciones y datos tienen que tener la misma dimension y datos no puede contener ceros for i in (0...posciones.size) @pos << posciones[i] @dato << datos[i] end end |
#max ⇒ Object
Devuelve el elemento mayor de la matriz
299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/matriz_dispersa.rb', line 299 def max #Devuelve el elemento mayor de la matriz r = -999999 aux=@dato aux<< 0 @dato.each do |i| if (i.to_f>r.to_f) r=i end end r end |
#mi_random ⇒ Object
Hago esto, pq rand(-10..10) puede generar el valor cero y esto no nos interesa almacenarlo
39 40 41 42 43 44 45 |
# File 'lib/matriz_dispersa.rb', line 39 def mi_random #Hago esto, pq rand(-10..10) puede generar el valor cero y esto no nos interesa almacenarlo if (rand(10) > 4) 1 else -1 end end |
#min ⇒ Object
Devuelve el elemento menor de la matriz
311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/matriz_dispersa.rb', line 311 def min #Devuelve el elemento menor de la matriz r = 999999 aux=@dato aux<< 0 @dato.each do |i| if (i.to_f<r.to_f) r=i end end r end |
#t ⇒ Object
Calcula la traspuesta de la matriz
183 184 185 186 187 188 189 190 |
# File 'lib/matriz_dispersa.rb', line 183 def t ##Calcula la traspuesta de la matriz nueva = Matriz_dispersa.new(@col,@fil) for i in (0...@pos.size) nueva.pos << [@pos[i][1],@pos[i][0]] nueva.dato << @dato[i] end nueva end |
#to_s ⇒ Object
Metodo para mostrar una matriz dispersa
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/matriz_dispersa.rb', line 23 def to_s #Metodo para mostrar una matriz dispersa for i in (0...self.fil) print "(" for j in (0...self.col) b = buscar(i,j) if (b != -1) print " #{self.dato[b]}\t" else print " 0\t" end end puts ")\n" end puts "\n" end |