Class: ModaiPrct10::MatrizDispersa
- Inherits:
-
MatrizAbstracta
- Object
- MatrizAbstracta
- ModaiPrct10::MatrizDispersa
- Defined in:
- lib/modai_prct10.rb
Overview
Clase de Matriz dispersa
Instance Attribute Summary collapse
-
#columnas ⇒ Object
readonly
Returns the value of attribute columnas.
-
#filas ⇒ Object
readonly
Returns the value of attribute filas.
-
#matriz ⇒ Object
readonly
Returns the value of attribute matriz.
Instance Method Summary collapse
-
#+(o) ⇒ Object
Suma de matrices.
-
#-(o) ⇒ Object
Resta de matrices.
-
#initialize(matriz) ⇒ MatrizDispersa
constructor
Inicialización.
-
#max ⇒ Object
Máximo de matriz.
-
#min ⇒ Object
Minimo de matriz.
-
#to_f ⇒ Object
Matriz en punto flotante.
-
#to_s ⇒ Object
Convertimos a string.
Constructor Details
#initialize(matriz) ⇒ MatrizDispersa
Inicialización
172 173 174 175 176 177 178 |
# File 'lib/modai_prct10.rb', line 172 def initialize(matriz) @matriz = matriz @filas = matriz.size @columnas = matriz.size end |
Instance Attribute Details
#columnas ⇒ Object (readonly)
Returns the value of attribute columnas.
180 181 182 |
# File 'lib/modai_prct10.rb', line 180 def columnas @columnas end |
#filas ⇒ Object (readonly)
Returns the value of attribute filas.
180 181 182 |
# File 'lib/modai_prct10.rb', line 180 def filas @filas end |
#matriz ⇒ Object (readonly)
Returns the value of attribute matriz.
180 181 182 |
# File 'lib/modai_prct10.rb', line 180 def matriz @matriz end |
Instance Method Details
#+(o) ⇒ Object
Suma de matrices
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/modai_prct10.rb', line 239 def +(o) suma = Array.new(matriz.size - 1) for i in 0...matriz.size # creamos el hash if (matriz[i] != nil or o.matriz[i] != nil) suma[i] = Hash.new() case true # Los dos tienen hash when (matriz[i] != nil and o.matriz[i] != nil) # cogemos matriz como base para la suma suma[i] = matriz[i] o.matriz[i].each do |key, value| if suma[i].has_key?(key) suma[i][key] = suma[i][key] + o.matriz[i][key] else suma[i][key] = o.matriz[i][key] end end # matriz tiene hash when matriz[i] != nil suma[i] = matriz[i] # o hash when o.matriz[i] != nil suma[i] = o.matriz[i] end end end MatrizDispersa.new(suma) end |
#-(o) ⇒ Object
Resta de matrices
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/modai_prct10.rb', line 286 def -(o) resta = Array.new(matriz.size - 1) for i in 0...matriz.size # creamos el hash if (matriz[i] != nil or o.matriz[i] != nil) resta[i] = Hash.new() case true # Los dos tienen hash when (matriz[i] != nil and o.matriz[i] != nil) # cogemos matriz como base para la resta resta[i] = matriz[i] o.matriz[i].each do |key, value| if resta[i].has_key?(key) resta[i][key] = resta[i][key] - o.matriz[i][key] else resta[i][key] = o.matriz[i][key] * -1 end end # matriz tiene hash when matriz[i] != nil resta[i] = matriz[i] # o hash when o.matriz[i] != nil resta[i] = o.matriz[i] resta[i].each do |key, value| resta[i][key] = resta[i][key] * -1 end end end end MatrizDispersa.new(resta) end |
#max ⇒ Object
Máximo de matriz
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 |
# File 'lib/modai_prct10.rb', line 380 def max maximo = 0.to_f for i in 0...matriz.size # Hay datos en la fila if matriz[i] != nil matriz[i].each do |key, value| if matriz[i][key].to_f > maximo maximo = matriz[i][key].to_f end end end end maximo end |
#min ⇒ Object
Minimo de matriz
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 |
# File 'lib/modai_prct10.rb', line 398 def min minimo = 0.to_f for i in 0...matriz.size # Hay datos en la fila if matriz[i] != nil matriz[i].each do |key, value| if matriz[i][key].to_f < minimo minimo = matriz[i][key].to_f end end end end minimo end |
#to_f ⇒ Object
Matriz en punto flotante
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/modai_prct10.rb', line 219 def to_f flotante = Array.new(matriz.size - 1) for i in 0...matriz.size # Hay datos en la fila if matriz[i] != nil flotante[i] = Hash.new() matriz[i].each do |key, value| flotante[i][key] = matriz[i][key].to_f end end end MatrizDispersa.new(flotante) end |
#to_s ⇒ Object
Convertimos a string
183 184 185 186 187 188 189 190 191 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 |
# File 'lib/modai_prct10.rb', line 183 def to_s fil = 0 print "[" while fil < filas col = 0 while col < columnas # Hay datos en la fila if matriz[fil] != nil if matriz[fil].has_key?(col) print "#{matriz[fil][col].to_s}" else print "0" end else print "0" end if (col + 1) < columnas then print ", " end col += 1 end if (fil + 1) < filas then print ", " end fil += 1 end print "]" end |