Class: SparseMatrix

Inherits:
Matriz show all
Defined in:
lib/matriz_dispersa.rb

Overview

Clase que se encarga de crear el hash

Direct Known Subclasses

Matriz_Dispersa

Instance Attribute Summary collapse

Attributes inherited from Matriz

#c, #f, #m

Instance Method Summary collapse

Constructor Details

#initialize(h = {}) ⇒ SparseMatrix

Inicializador propio de la clase



28
29
30
31
32
33
34
35
36
37
# File 'lib/matriz_dispersa.rb', line 28

def initialize(h = {})
    @matrix = Hash.new({})
    for k in h.keys do
        @matrix[k] = if h[k].is_a? SparseVector 
                            h[k]
                    else
                          @matrix[k] = SparseVector.new(h[k])
                        end
    end
end

Instance Attribute Details

#matrixObject (readonly)

Returns the value of attribute matrix.



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

def matrix
  @matrix
end

Instance Method Details

#*(other) ⇒ Object

Operacion de la multiplicacion en la matriz dispersa. Realiza la operacion entre dos matrices dispersa y una dispersa con una densa



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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
217
218
219
220
221
222
# File 'lib/matriz_dispersa.rb', line 158

def *(other) 

				case other
				when Matriz_Dispersa
					 raise ArgumentError, "Las columnas de una matriz no coinciden con las filas de la otra." unless @matrix[1].vector.size == other.matrix.size
        sumatotal = 0
        mul = Array.new                

       #for i in @matrix.keys do
					1.upto(@matrix.keys) do |i|
                mul_f = Array.new
             #for j in @matrix[i].vector.keys do
1.upto(@matrix[i].vector.keys) do |j|
                        #for k in @matrix[i].vector.keys do
			 1.upto(@matrix[i].vector.keys) do |k|
                            suma = @matrix[i].vector[k] * other.matrix[k].vector[j]
                            sumatotal = sumatotal + suma;
                        end
                        mul_f << sumatotal
                        sumatotal = 0
            end
							mul << mul_f
        end
=begin        
        for r in @matrix.keys do
           for j in @matrix[r].vector.keys do
              print "  #{mul[r][j]}  "
           end
           puts
			end
=end
					return mul
			  when Matriz_densa
					raise ArgumentError, "Las columnas de una matriz no coinciden con las filas de la otra." unless @matrix[1].vector.size == other.f
					sumatotal = 0
        mul = Array.new                

        #for i in @matrix.keys do
					  1.upto(@matrix.size) do |i|
                mul_f = Array.new
             #for j in @matrix[i].vector.keys do
1.upto(@matrix[i].vector.size) do |j|
                   #for k in @matrix[i].vector.keys do
	1.upto(@matrix[i].vector.size) do |k|
                      suma = @matrix[i].vector[k] * other.m[k - 1][j - 1]
                      sumatotal = sumatotal + suma;
                   end
                   mul_f << sumatotal
                   sumatotal = 0
            end
							mul << mul_f
        end 
=begin       
        for r in @matrix.keys do
           for j in @matrix[r].vector.keys do
              print "  #{mul[r][j]}  "
           end
           puts
			end
=end
					return mul
			  else
				  raise TypeError.new("No se puede pasar #{other.inspect} a Matriz")
			  end
end

#+(other) ⇒ Object

Operacion de la suma en la matriz dispersa. Realiza la operacion entre dos matrices dispersa y una dispersa con una densa



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
# File 'lib/matriz_dispersa.rb', line 80

def +(other)
			  case other
			  when Matriz_Dispersa
					raise ArgumentError, "Las matrices no son iguales en longitud." unless @matrix.size == other.matrix.size && @matrix[1].vector.size == other.matrix[1].vector.size
					resul = Array.new
					 @matrix.keys.times do |r|
					 r = r + 1
					  #for r in @matrix.keys do
						resul_f = Array.new
						 1.upto(@matrix[r].vector.keys) do |j|  
						 #for j in @matrix[r].vector.keys do       
	#print "  #{@matrix[r].vector[j] + other.matrix[r].vector[j]}  "
							resul_f << @matrix[r].vector[j] + other.matrix[r].vector[j]
end
	#puts
						resul << resul_f
					end
					return resul
			  when Matriz_densa
					raise ArgumentError, "Las matrices no son iguales en longitud." unless @matrix.size == other.f && @matrix[1].vector.size == other.c
					resul = Array.new
					1.upto(other.f) do |r|
					 #for r in 1..other.f do
						resul_f = Array.new
						1.upto(other.c) do |j|
						 #for j in 1..other.c do
       		#print "  #{@matrix[r].vector[j] + other.m[r - 1][j - 1]}"
							resul_f << @matrix[r].vector[j] + other.m[r - 1][j - 1]
       	end
       	#puts
						resul << resul_f
			end
					return resul
			  else
					raise TypeError.new("No se puede pasar #{other.inspect} a Matriz")
			  end
end

#-(other) ⇒ Object

Operacion de la resta en la matriz dispersa. Realiza la operacion entre dos matrices dispersa y una dispersa con una densa



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/matriz_dispersa.rb', line 119

def -(other)
			  case other
			  when Matriz_Dispersa
					raise ArgumentError, "Las matrices no son iguales en longitud." unless @matrix.size == other.matrix.size && @matrix[1].vector.size == other.matrix[1].vector.size
					resul = Array.new
	#for r in @matrix.keys do
					 1.upto(@matrix.keys) do |r|
						resul_f = Array.new
#for j in @matrix[r].vector.keys do  
						 1.upto(@matrix[r].vector.keys) do |j|      
	#print "  #{@matrix[r].vector[j] - other.matrix[r].vector[j]}  "
							resul_f << @matrix[r].vector[j] - other.matrix[r].vector[j]
end
	#puts
						resul << resul_f
					end
					return resul
			  when Matriz_densa
					raise ArgumentError, "Las matrices no son iguales en longitud." unless @matrix.size == other.f && @matrix[1].vector.size == other.c
					resul = Array.new
					#for r in 1..other.f do
					1.upto(other.f) do |r|
						resul_f = Array.new
       	#for j in 1..other.c do
						1.upto(other.c) do |j|
       		#print "  #{@matrix[r].vector[j] - other.m[r - 1][j - 1]}"
							resul_f << @matrix[r].vector[j] - other.m[r - 1][j - 1]
       	end
       	#puts
						resul << resul_f
			end
					return resul
			  else
					raise TypeError.new("No se puede pasar #{other.inspect} a Matriz")
			  end

end

#[](i, j) ⇒ Object

Obtener un valor de la matriz dada una posicion



40
41
42
# File 'lib/matriz_dispersa.rb', line 40

def [](i)
    @matrix[i]
end

#coerce(other) ⇒ Object



269
270
271
# File 'lib/matriz_dispersa.rb', line 269

def coerce(other)
        [other,self]
end

#col(j) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/matriz_dispersa.rb', line 44

def col(j)
    c = {}
    for r in @matrix.keys do
        c[r] = @matrix[r].vector[j] if @matrix[r].vector.keys.include? j
    end
    SparseVector.new c
end

#comprobarObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/matriz_dispersa.rb', line 52

def comprobar
        contar = 0
        contartotal = 0
        result = 0
   for r in @matrix.keys do
                 for j in @matrix[r].vector.keys do
                        contartotal = contartotal + 1
                        if @matrix[r].vector[j] == 0 
                                contar = contar + 1
                        end
                end
      end
        result = (contar * 100) / contartotal
        if result < 60 
                 "La matriz no es dispersa"
        end
end

#maxObject

Obtención del maximo de una matriz dispersa



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/matriz_dispersa.rb', line 226

def max
           
        maximo = @matrix[1].vector[1]
   
        #for i in @matrix.keys do
						@matrix.size.times do |i|
i = i + 1
                #for j in @matrix[i].vector.keys do
#@matrix[i].vector.keys do |j|
1.upto(@matrix[i].vector.size) do |j|
                   if @matrix[i].vector[j] > maximo
                      maximo = @matrix[i].vector[j]
              end
           end
        end
					 return maximo
end

#minObject

Obtención del maximo de una matriz dispersa



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/matriz_dispersa.rb', line 251

def min

   minimo = @matrix[1].vector[1]
        
        #for i in @matrix.keys do
						1.upto(@matrix.size) do |i|
                #for j in @matrix[i].vector.keys do
@matrix[i].vector.size.times do |j|
j = j + 1
                   if @matrix[i].vector[j] < minimo
                      minimo = @matrix[i].vector[j]
              end
           end
        end
					return minimo

end

#mostrarObject



70
71
72
73
74
75
76
77
# File 'lib/matriz_dispersa.rb', line 70

def mostrar
for r in @matrix.keys do
                          for j in @matrix[r].vector.keys do
                                print "#{@matrix[r].vector[j]}  "  
                        end
                        puts
end
end