t50_matriz_dispersa_densa 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. data/Documentacion/Fraccion.html +816 -0
  2. data/Documentacion/Gemfile.html +109 -0
  3. data/Documentacion/LICENSE_txt.html +126 -0
  4. data/Documentacion/Matriz.html +405 -0
  5. data/Documentacion/Matriz_Dispersa.html +159 -0
  6. data/Documentacion/Matriz_densa.html +479 -0
  7. data/Documentacion/Object.html +212 -0
  8. data/Documentacion/Rakefile.html +114 -0
  9. data/Documentacion/SparseMatrix.html +775 -0
  10. data/Documentacion/SparseVector.html +307 -0
  11. data/Documentacion/T50MatrizDispersaDensa.html +167 -0
  12. data/Documentacion/TestMatrix.html +467 -0
  13. data/Documentacion/bin/t50_matriz_dispersa_densa.html +54 -0
  14. data/Documentacion/created.rid +13 -0
  15. data/Documentacion/images/brick.png +0 -0
  16. data/Documentacion/images/brick_link.png +0 -0
  17. data/Documentacion/images/bug.png +0 -0
  18. data/Documentacion/images/bullet_black.png +0 -0
  19. data/Documentacion/images/bullet_toggle_minus.png +0 -0
  20. data/Documentacion/images/bullet_toggle_plus.png +0 -0
  21. data/Documentacion/images/date.png +0 -0
  22. data/Documentacion/images/find.png +0 -0
  23. data/Documentacion/images/loadingAnimation.gif +0 -0
  24. data/Documentacion/images/macFFBgHack.png +0 -0
  25. data/Documentacion/images/package.png +0 -0
  26. data/Documentacion/images/page_green.png +0 -0
  27. data/Documentacion/images/page_white_text.png +0 -0
  28. data/Documentacion/images/page_white_width.png +0 -0
  29. data/Documentacion/images/plugin.png +0 -0
  30. data/Documentacion/images/ruby.png +0 -0
  31. data/Documentacion/images/tag_green.png +0 -0
  32. data/Documentacion/images/wrench.png +0 -0
  33. data/Documentacion/images/wrench_orange.png +0 -0
  34. data/Documentacion/images/zoom.png +0 -0
  35. data/Documentacion/index.html +172 -0
  36. data/Documentacion/js/darkfish.js +118 -0
  37. data/Documentacion/js/jquery.js +32 -0
  38. data/Documentacion/js/quicksearch.js +114 -0
  39. data/Documentacion/js/thickbox-compressed.js +10 -0
  40. data/Documentacion/lib/Fraccion_rb.html +54 -0
  41. data/Documentacion/lib/gcd_rb.html +52 -0
  42. data/Documentacion/lib/matriz_densa_rb.html +54 -0
  43. data/Documentacion/lib/matriz_dispersa_rb.html +58 -0
  44. data/Documentacion/lib/matriz_rb.html +58 -0
  45. data/Documentacion/lib/t50_matriz_dispersa_densa/version_rb.html +52 -0
  46. data/Documentacion/lib/t50_matriz_dispersa_densa_rb.html +72 -0
  47. data/Documentacion/rdoc.css +763 -0
  48. data/Documentacion/test/tc_matrix_rb.html +62 -0
  49. data/Gemfile +4 -0
  50. data/LICENSE.txt +22 -0
  51. data/README.md +67 -0
  52. data/Rakefile +10 -0
  53. data/bin/t50_matriz_dispersa_densa +3 -0
  54. data/lib/Fraccion.rb +128 -0
  55. data/lib/gcd.rb +7 -0
  56. data/lib/matriz.rb +71 -0
  57. data/lib/matriz.rb~ +136 -0
  58. data/lib/matriz_densa.rb +128 -0
  59. data/lib/matriz_densa.rb~ +148 -0
  60. data/lib/matriz_dispersa.rb +276 -0
  61. data/lib/matriz_dispersa.rb~ +275 -0
  62. data/lib/t50_matriz_dispersa_densa.rb +27 -0
  63. data/lib/t50_matriz_dispersa_densa.rb~ +7 -0
  64. data/lib/t50_matriz_dispersa_densa/version.rb +3 -0
  65. data/t50_matriz_dispersa_densa.gemspec +19 -0
  66. data/test/tc_matrix.rb +57 -0
  67. data/test/tc_matrix.rb~ +57 -0
  68. metadata +115 -0
@@ -0,0 +1,128 @@
1
+ require "matriz.rb"
2
+
3
+ class Matriz_densa < Matriz
4
+
5
+ # Operacion de la suma en la matriz densa. Realiza la operacion entre dos matrices densa y una densa con una dispersa.
6
+ def +(other)
7
+
8
+
9
+ case other
10
+ when Matriz_densa
11
+ raise ArgumentError, "Las matrices no son iguales en longitud." unless @f == other.f && @c == other.c
12
+ r =@m
13
+
14
+ #for i in 0...@f do
15
+ 0.upto(@f) do |i|
16
+ #for j in 0...@c do
17
+ 0.upto(@c) do |j|
18
+ r[i][j]=@m[i][j]+other.m[i][j]
19
+ end
20
+ end
21
+ return r
22
+ when Matriz_Dispersa
23
+ raise ArgumentError, "Las matrices no son iguales en longitud." unless @f == other.matrix.size && @c == other.matrix[1].vector.size
24
+ other + self
25
+
26
+ else
27
+ raise TypeError.new("No se puede pasar #{other.inspect} a Matriz")
28
+ end
29
+ end
30
+
31
+ # Operacion de la resta en la matriz densa. Realiza la operacion entre dos matrices densa y una densa con una dispersa.
32
+ def -(other)
33
+
34
+ case other
35
+ when Matriz_densa
36
+ raise ArgumentError, "Las matrices no son iguales en longitud." unless @f == other.f && @c == other.c
37
+ rs =@m
38
+
39
+ #for i in 0...@f do
40
+ @f.times do |i|
41
+ #for j in 0...@c do
42
+ @c.times do |j|
43
+ rs[i][j]=@m[i][j]+other.m[i][j]
44
+ end
45
+ end
46
+ return rs
47
+ when Matriz_Dispersa
48
+ raise ArgumentError, "Las matrices no son iguales en longitud." unless @f == other.matrix.size && @c == other.matrix[1].vector.size
49
+ other - self
50
+
51
+ else
52
+ raise TypeError.new("No se puede pasar #{other.inspect} a Matriz")
53
+ end
54
+ end
55
+
56
+ # Operación de la multiplicación en la matriz densa. Realiza la operacion entre dos matrices densa y una densa con una dispersa.
57
+ def *(other)
58
+ case other
59
+ when Matriz_densa
60
+ raise ArgumentError, "Las columnas de una matriz no coinciden con las filas de la otra." unless @c == other.f
61
+ z = Array.new
62
+ for i in 0...@f do
63
+ z[i] = Array.new
64
+ for j in 0...@c do
65
+ z[i][j] = 0
66
+ end
67
+ end
68
+ #for i in 0...@f do
69
+ 0.upto(@f) do |i|
70
+ #for j in 0...@c do
71
+ 0.upto(@c) do |j|
72
+ #for k in 0...@f do
73
+ 0.upto(@f) do |k|
74
+ z[i][j] += @m[i][k] * other.m[k][j]
75
+ end
76
+ end
77
+ end
78
+ return z
79
+ when Matriz_Dispersa
80
+ raise ArgumentError, "Las columnas de una matriz no coinciden con las filas de la otra." unless @c == other.matrix.size
81
+ other * self
82
+ else
83
+ raise TypeError.new("No se puede pasar #{other.inspect} a Matriz")
84
+ end
85
+ end
86
+
87
+
88
+ def to_s
89
+ for i in 0...@f do
90
+ for j in 0...@c do
91
+ print @m[i][j]
92
+ end
93
+ puts
94
+ end
95
+
96
+ end
97
+
98
+ # Devuelve el maximo de una matriz densa
99
+ def max
100
+
101
+ maximo = @m[0][0].to_f
102
+ for i in 0...@f do
103
+ for j in 0...@c do
104
+ if @m[i][j].to_f > maximo.to_f
105
+ maximo = @m[i][j]
106
+ end
107
+ end
108
+ end
109
+ maximo
110
+
111
+ end
112
+
113
+ # Devuelve el minimo de una matriz densa
114
+ def min
115
+
116
+ minimo = @m[0][0].to_f
117
+ for i in 0...@f do
118
+ for j in 0...@c do
119
+ if @m[i][j].to_f < minimo.to_f
120
+ minimo = @m[i][j]
121
+ end
122
+ end
123
+ end
124
+ minimo
125
+
126
+ end
127
+
128
+ end
@@ -0,0 +1,148 @@
1
+ require "matriz.rb"
2
+
3
+ class Matriz_densa < Matriz
4
+
5
+ # Operacion de la suma en la matriz densa. Realiza la operacion entre dos matrices densa y una densa con una dispersa.
6
+ def +(other)
7
+
8
+
9
+ case other
10
+ when Matriz_densa
11
+ raise ArgumentError, "Las matrices no son iguales en longitud." unless @f == other.f && @c == other.c
12
+ r =@m
13
+
14
+ #for i in 0...@f do
15
+ 0.upto(@f) do |i|
16
+ #for j in 0...@c do
17
+ 0.upto(@c) do |j|
18
+ r[i][j]=@m[i][j]+other.m[i][j]
19
+ end
20
+ end
21
+ return r
22
+ when Matriz_Dispersa
23
+ raise ArgumentError, "Las matrices no son iguales en longitud." unless @f == other.matrix.size && @c == other.matrix[1].vector.size
24
+ other + self
25
+
26
+ else
27
+ raise TypeError.new("No se puede pasar #{other.inspect} a Matriz")
28
+ end
29
+ end
30
+
31
+ # Operacion de la resta en la matriz densa. Realiza la operacion entre dos matrices densa y una densa con una dispersa.
32
+ def -(other)
33
+
34
+ case other
35
+ when Matriz_densa
36
+ raise ArgumentError, "Las matrices no son iguales en longitud." unless @f == other.f && @c == other.c
37
+ rs =@m
38
+
39
+ #for i in 0...@f do
40
+ @f.times do |i|
41
+ #for j in 0...@c do
42
+ @c.times do |j|
43
+ rs[i][j]=@m[i][j]+other.m[i][j]
44
+ end
45
+ end
46
+ return rs
47
+ when Matriz_Dispersa
48
+ raise ArgumentError, "Las matrices no son iguales en longitud." unless @f == other.matrix.size && @c == other.matrix[1].vector.size
49
+ other - self
50
+
51
+ else
52
+ raise TypeError.new("No se puede pasar #{other.inspect} a Matriz")
53
+ end
54
+ end
55
+
56
+ # Operación de la multiplicación en la matriz densa. Realiza la operacion entre dos matrices densa y una densa con una dispersa.
57
+ def *(other)
58
+ case other
59
+ when Matriz_densa
60
+ raise ArgumentError, "Las columnas de una matriz no coinciden con las filas de la otra." unless @c == other.f
61
+ z = Array.new
62
+ for i in 0...@f do
63
+ z[i] = Array.new
64
+ for j in 0...@c do
65
+ z[i][j] = 0
66
+ end
67
+ end
68
+ #for i in 0...@f do
69
+ 0.upto(@f) do |i|
70
+ #for j in 0...@c do
71
+ 0.upto(@c) do |j|
72
+ #for k in 0...@f do
73
+ 0.upto(@f) do |k|
74
+ z[i][j] += @m[i][k] * other.m[k][j]
75
+ end
76
+ end
77
+ end
78
+ return z
79
+ when Matriz_Dispersa
80
+ raise ArgumentError, "Las columnas de una matriz no coinciden con las filas de la otra." unless @c == other.matrix.size
81
+ other * self
82
+ else
83
+ raise TypeError.new("No se puede pasar #{other.inspect} a Matriz")
84
+ end
85
+ end
86
+
87
+
88
+ def to_s
89
+ for i in 0...@f do
90
+ for j in 0...@c do
91
+ print @m[i][j]
92
+ end
93
+ puts
94
+ end
95
+
96
+ end
97
+
98
+ def /(other)
99
+
100
+ w = Array.new
101
+ for i in 0...@f do
102
+ w[i] = Array.new
103
+ for j in 0...@c do
104
+ w[i][j] = 0
105
+ end
106
+ end
107
+
108
+ for i in 0...@f do
109
+ for j in 0...@c do
110
+ for k in 0...@f - 1 do
111
+ w[i][j] += @m[i][k] * other.m[i][j]
112
+ end
113
+ end
114
+ end
115
+ return w
116
+ end
117
+
118
+ # Devuelve el máximo de una matriz densa
119
+ def max
120
+
121
+ maximo = @m[0][0].to_f
122
+ for i in 0...@f do
123
+ for j in 0...@c do
124
+ if @m[i][j].to_f > maximo.to_f
125
+ maximo = @m[i][j]
126
+ end
127
+ end
128
+ end
129
+ maximo
130
+
131
+ end
132
+
133
+ # Devuelve el minimo de una matriz densa
134
+ def min
135
+
136
+ minimo = @m[0][0].to_f
137
+ for i in 0...@f do
138
+ for j in 0...@c do
139
+ if @m[i][j].to_f < minimo.to_f
140
+ minimo = @m[i][j]
141
+ end
142
+ end
143
+ end
144
+ minimo
145
+
146
+ end
147
+
148
+ end
@@ -0,0 +1,276 @@
1
+ require 'Fraccion.rb'
2
+ require 'matriz.rb'
3
+ require "matrix"
4
+
5
+ class SparseVector
6
+ attr_reader :vector
7
+
8
+ def initialize(h = {})
9
+ @vector = Hash.new(0)
10
+ @vector = @vector.merge!(h)
11
+ end
12
+
13
+ def [](i)
14
+ @vector[i]
15
+ end
16
+
17
+ def to_s
18
+ @vector.to_s
19
+ end
20
+ end
21
+
22
+ #Clase que se encarga de crear el hash
23
+ class SparseMatrix < Matriz
24
+
25
+ attr_reader :matrix
26
+
27
+ # Inicializador propio de la clase
28
+ def initialize(h = {})
29
+ @matrix = Hash.new({})
30
+ for k in h.keys do
31
+ @matrix[k] = if h[k].is_a? SparseVector
32
+ h[k]
33
+ else
34
+ @matrix[k] = SparseVector.new(h[k])
35
+ end
36
+ end
37
+ end
38
+
39
+ # Obtener un valor de la matriz dada una posicion
40
+ def [](i)
41
+ @matrix[i]
42
+ end
43
+
44
+ def col(j)
45
+ c = {}
46
+ for r in @matrix.keys do
47
+ c[r] = @matrix[r].vector[j] if @matrix[r].vector.keys.include? j
48
+ end
49
+ SparseVector.new c
50
+ end
51
+
52
+ def comprobar
53
+ contar = 0
54
+ contartotal = 0
55
+ result = 0
56
+ for r in @matrix.keys do
57
+ for j in @matrix[r].vector.keys do
58
+ contartotal = contartotal + 1
59
+ if @matrix[r].vector[j] == 0
60
+ contar = contar + 1
61
+ end
62
+ end
63
+ end
64
+ result = (contar * 100) / contartotal
65
+ if result < 60
66
+ "La matriz no es dispersa"
67
+ end
68
+ end
69
+
70
+ def mostrar
71
+ for r in @matrix.keys do
72
+ for j in @matrix[r].vector.keys do
73
+ print "#{@matrix[r].vector[j]} "
74
+ end
75
+ puts
76
+ end
77
+ end
78
+
79
+ # Operacion de la suma en la matriz dispersa. Realiza la operacion entre dos matrices dispersa y una dispersa con una densa
80
+ def +(other)
81
+ case other
82
+ when Matriz_Dispersa
83
+ raise ArgumentError, "Las matrices no son iguales en longitud." unless @matrix.size == other.matrix.size && @matrix[1].vector.size == other.matrix[1].vector.size
84
+ resul = Array.new
85
+ @matrix.keys.times do |r|
86
+ r = r + 1
87
+ #for r in @matrix.keys do
88
+ resul_f = Array.new
89
+ 1.upto(@matrix[r].vector.keys) do |j|
90
+ #for j in @matrix[r].vector.keys do
91
+ #print " #{@matrix[r].vector[j] + other.matrix[r].vector[j]} "
92
+ resul_f << @matrix[r].vector[j] + other.matrix[r].vector[j]
93
+ end
94
+ #puts
95
+ resul << resul_f
96
+ end
97
+ return resul
98
+ when Matriz_densa
99
+ raise ArgumentError, "Las matrices no son iguales en longitud." unless @matrix.size == other.f && @matrix[1].vector.size == other.c
100
+ resul = Array.new
101
+ 1.upto(other.f) do |r|
102
+ #for r in 1..other.f do
103
+ resul_f = Array.new
104
+ 1.upto(other.c) do |j|
105
+ #for j in 1..other.c do
106
+ #print " #{@matrix[r].vector[j] + other.m[r - 1][j - 1]}"
107
+ resul_f << @matrix[r].vector[j] + other.m[r - 1][j - 1]
108
+ end
109
+ #puts
110
+ resul << resul_f
111
+ end
112
+ return resul
113
+ else
114
+ raise TypeError.new("No se puede pasar #{other.inspect} a Matriz")
115
+ end
116
+ end
117
+
118
+ # Operacion de la resta en la matriz dispersa. Realiza la operacion entre dos matrices dispersa y una dispersa con una densa
119
+ def -(other)
120
+ case other
121
+ when Matriz_Dispersa
122
+ raise ArgumentError, "Las matrices no son iguales en longitud." unless @matrix.size == other.matrix.size && @matrix[1].vector.size == other.matrix[1].vector.size
123
+ resul = Array.new
124
+ #for r in @matrix.keys do
125
+ 1.upto(@matrix.keys) do |r|
126
+ resul_f = Array.new
127
+ #for j in @matrix[r].vector.keys do
128
+ 1.upto(@matrix[r].vector.keys) do |j|
129
+ #print " #{@matrix[r].vector[j] - other.matrix[r].vector[j]} "
130
+ resul_f << @matrix[r].vector[j] - other.matrix[r].vector[j]
131
+ end
132
+ #puts
133
+ resul << resul_f
134
+ end
135
+ return resul
136
+ when Matriz_densa
137
+ raise ArgumentError, "Las matrices no son iguales en longitud." unless @matrix.size == other.f && @matrix[1].vector.size == other.c
138
+ resul = Array.new
139
+ #for r in 1..other.f do
140
+ 1.upto(other.f) do |r|
141
+ resul_f = Array.new
142
+ #for j in 1..other.c do
143
+ 1.upto(other.c) do |j|
144
+ #print " #{@matrix[r].vector[j] - other.m[r - 1][j - 1]}"
145
+ resul_f << @matrix[r].vector[j] - other.m[r - 1][j - 1]
146
+ end
147
+ #puts
148
+ resul << resul_f
149
+ end
150
+ return resul
151
+ else
152
+ raise TypeError.new("No se puede pasar #{other.inspect} a Matriz")
153
+ end
154
+
155
+ end
156
+
157
+ # Operacion de la multiplicacion en la matriz dispersa. Realiza la operacion entre dos matrices dispersa y una dispersa con una densa
158
+ def *(other)
159
+
160
+ case other
161
+ when Matriz_Dispersa
162
+ raise ArgumentError, "Las columnas de una matriz no coinciden con las filas de la otra." unless @matrix[1].vector.size == other.matrix.size
163
+ sumatotal = 0
164
+ mul = Array.new
165
+
166
+ #for i in @matrix.keys do
167
+ 1.upto(@matrix.keys) do |i|
168
+ mul_f = Array.new
169
+ #for j in @matrix[i].vector.keys do
170
+ 1.upto(@matrix[i].vector.keys) do |j|
171
+ #for k in @matrix[i].vector.keys do
172
+ 1.upto(@matrix[i].vector.keys) do |k|
173
+ suma = @matrix[i].vector[k] * other.matrix[k].vector[j]
174
+ sumatotal = sumatotal + suma;
175
+ end
176
+ mul_f << sumatotal
177
+ sumatotal = 0
178
+ end
179
+ mul << mul_f
180
+ end
181
+ =begin
182
+ for r in @matrix.keys do
183
+ for j in @matrix[r].vector.keys do
184
+ print " #{mul[r][j]} "
185
+ end
186
+ puts
187
+ end
188
+ =end
189
+ return mul
190
+ when Matriz_densa
191
+ raise ArgumentError, "Las columnas de una matriz no coinciden con las filas de la otra." unless @matrix[1].vector.size == other.f
192
+ sumatotal = 0
193
+ mul = Array.new
194
+
195
+ #for i in @matrix.keys do
196
+ 1.upto(@matrix.size) do |i|
197
+ mul_f = Array.new
198
+ #for j in @matrix[i].vector.keys do
199
+ 1.upto(@matrix[i].vector.size) do |j|
200
+ #for k in @matrix[i].vector.keys do
201
+ 1.upto(@matrix[i].vector.size) do |k|
202
+ suma = @matrix[i].vector[k] * other.m[k - 1][j - 1]
203
+ sumatotal = sumatotal + suma;
204
+ end
205
+ mul_f << sumatotal
206
+ sumatotal = 0
207
+ end
208
+ mul << mul_f
209
+ end
210
+ =begin
211
+ for r in @matrix.keys do
212
+ for j in @matrix[r].vector.keys do
213
+ print " #{mul[r][j]} "
214
+ end
215
+ puts
216
+ end
217
+ =end
218
+ return mul
219
+ else
220
+ raise TypeError.new("No se puede pasar #{other.inspect} a Matriz")
221
+ end
222
+ end
223
+
224
+ # Obtención del maximo de una matriz dispersa
225
+
226
+ def max
227
+
228
+ maximo = @matrix[1].vector[1]
229
+
230
+ #for i in @matrix.keys do
231
+ @matrix.size.times do |i|
232
+ i = i + 1
233
+ #for j in @matrix[i].vector.keys do
234
+ #@matrix[i].vector.keys do |j|
235
+ 1.upto(@matrix[i].vector.size) do |j|
236
+ if @matrix[i].vector[j] > maximo
237
+ maximo = @matrix[i].vector[j]
238
+ end
239
+ end
240
+ end
241
+ return maximo
242
+ end
243
+
244
+ def [](i,j)
245
+
246
+ return @matrix[i].vector[j]
247
+
248
+ end
249
+
250
+ # Obtención del maximo de una matriz dispersa
251
+ def min
252
+
253
+ minimo = @matrix[1].vector[1]
254
+
255
+ #for i in @matrix.keys do
256
+ 1.upto(@matrix.size) do |i|
257
+ #for j in @matrix[i].vector.keys do
258
+ @matrix[i].vector.size.times do |j|
259
+ j = j + 1
260
+ if @matrix[i].vector[j] < minimo
261
+ minimo = @matrix[i].vector[j]
262
+ end
263
+ end
264
+ end
265
+ return minimo
266
+
267
+ end
268
+
269
+ def coerce(other)
270
+ [other,self]
271
+ end
272
+ end
273
+
274
+ class Matriz_Dispersa < SparseMatrix
275
+
276
+ end