class ModaiPrct10::MatrizDensa

Clase de Matriz densa

Attributes

columnas[R]
filas[R]
matriz[R]

Public Class Methods

new(matriz) click to toggle source

Inicialización

# File lib/modai_prct10.rb, line 17
def initialize(matriz)

        @matriz = matriz
        @filas = matriz[0].size
        @columnas = matriz[0].size

end

Public Instance Methods

*(o) click to toggle source

Multiplicación de matrices

# File lib/modai_prct10.rb, line 121
def *(o)

        prod = Array.new(matriz.size - 1,0)
        for i in 0...matriz[0].size 
                prod[i] = Array.new(o.matriz.size,0)
                for j in 0...o.matriz.size
                        for pos in 0...matriz.size
                                prod[i][j] = prod[i][j] + (matriz[i][pos] * o.matriz[pos][j])
                        end
                end
        end
        MatrizDensa.new(prod)

end
+(o) click to toggle source

Suma de matrices

# File lib/modai_prct10.rb, line 66
def +(o)

        suma = Array.new(matriz.size - 1)
        for i in 0...matriz.size
              suma[i] = Array.new(matriz[i].size - 1)
                for j in 0...matriz[i].size
                        suma[i][j] = matriz[i][j] + o.matriz[i][j]
                end
        end
        MatrizDensa.new(suma)

end
-(o) click to toggle source

Resta de matrices

# File lib/modai_prct10.rb, line 107
def -(o)

        resta = Array.new(matriz.size - 1)
        for i in 0...matriz.size
                resta[i] = Array.new(matriz[i].size - 1)
                for j in 0...matriz[i].size
                        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_prct10.rb, line 80
def /(o)

        suma = Array.new(matriz.size - 1)
        for i in 0...matriz.size
                suma[i] = Array.new(matriz[i].size - 1)
                for j in 0...matriz[i].size
                
                        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
max() click to toggle source

Máximo de matriz

# File lib/modai_prct10.rb, line 137
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_prct10.rb, line 152
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_f() click to toggle source

Matriz en punto flotante

# File lib/modai_prct10.rb, line 52
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_prct10.rb, line 28
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