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

Resta de matrices

# File lib/modai_prct12/matrizDensa.rb, line 110
def -(o)

        resta = Array.new(matriz.size - 1)

        0.upto(matriz.size - 1) do |i|
                resta[i] = Array.new(matriz[i].size - 1)

                0.upto(matriz[i].size - 1) do |j|
                        resta[i][j] = matriz[i][j] - o.matriz[i][j]
                end
        end
        MatrizDensa.new(resta)

end
/(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

El metodo coerce para onvierte el valor especificado en el tipo especificado.

# File lib/modai_prct12/matrizDensa.rb, line 202
def coerce(other)
        return self, other
end
encontrar() { |matriz[j]| ... } click to toggle source

Devolvemos la posición del primer elemento cuyo cuadrado es mayor a 6

# File lib/modai_prct12/matrizDensa.rb, line 208
def encontrar  

        valor = "["

        0.upto(matriz.size - 1) do |i|

                (matriz[i].size).times do |j| 
                        
                       if (yield(matriz[i][j])) 
                       
                                valor += "[#{i},#{j}]"

                      end

                end
        end

        valor += "]"

        valor

end
max() click to toggle source

Máximo de matriz

# File lib/modai_prct12/matrizDensa.rb, line 150
def max

        maximo = 0.to_f
        for i in 0...matriz.size
                for j in 0...matriz[i].size
                        if matriz[i][j].to_f > maximo
                                maximo = matriz[i][j].to_f
                        end
                end
        end
        maximo

end
min() click to toggle source

Minimo de matriz

# File lib/modai_prct12/matrizDensa.rb, line 165
def min

        minimo = $tope
        for i in 0...matriz.size
                for j in 0...matriz[i].size
                        if matriz[i][j].to_f < minimo
                                minimo = matriz[i][j].to_f
                        end
                end
        end
        minimo

end
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

Matriz en punto flotante

# File lib/modai_prct12/matrizDensa.rb, line 36
def to_f

        flotante = Array.new(matriz.size - 1)
        for i in 0...matriz.size
                flotante[i] = Array.new(matriz[i].size - 1)
                for j in 0...matriz[i].size
                        flotante[i][j] = (matriz[i][j]).to_f
                end
        end
        MatrizDensa.new(flotante)

end
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