Class: ModaiPrct10::MatrizDensa

Inherits:
MatrizAbstracta show all
Defined in:
lib/modai_prct10.rb

Overview

Clase de Matriz densa

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(matriz) ⇒ MatrizDensa

Inicialización



17
18
19
20
21
22
23
# File 'lib/modai_prct10.rb', line 17

def initialize(matriz)

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

end

Instance Attribute Details

#columnasObject (readonly)

Returns the value of attribute columnas.



25
26
27
# File 'lib/modai_prct10.rb', line 25

def columnas
  @columnas
end

#filasObject (readonly)

Returns the value of attribute filas.



25
26
27
# File 'lib/modai_prct10.rb', line 25

def filas
  @filas
end

#matrizObject (readonly)

Returns the value of attribute matriz.



25
26
27
# File 'lib/modai_prct10.rb', line 25

def matriz
  @matriz
end

Instance Method Details

#*(o) ⇒ Object

Multiplicación de matrices



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# 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) ⇒ Object

Suma de matrices



66
67
68
69
70
71
72
73
74
75
76
77
# 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) ⇒ Object

Resta de matrices



107
108
109
110
111
112
113
114
115
116
117
118
# 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) ⇒ Object

Suma de matrices densa con dispersa (sobreescribimos el operador / como prueba)



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# 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

#maxObject

Máximo de matriz



137
138
139
140
141
142
143
144
145
146
147
148
149
# 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

#minObject

Minimo de matriz



152
153
154
155
156
157
158
159
160
161
162
163
164
# 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_fObject

Matriz en punto flotante



52
53
54
55
56
57
58
59
60
61
62
63
# 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_sObject

Convertimos a string



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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