sparse_matrix 1.5.0 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +0 -2
  3. data/README.md +5 -0
  4. data/documentacion/Gemfile.html +114 -0
  5. data/documentacion/Gemfile_lock.html +179 -0
  6. data/documentacion/Guardfile.html +144 -0
  7. data/documentacion/LICENSE_txt.html +131 -0
  8. data/documentacion/Object.html +224 -0
  9. data/documentacion/README_md.html +169 -0
  10. data/documentacion/Rakefile.html +119 -0
  11. data/documentacion/SparseMatrix/AbstractMatrix.html +418 -0
  12. data/documentacion/SparseMatrix/DenseMatrix.html +462 -0
  13. data/documentacion/SparseMatrix/Fraction.html +749 -0
  14. data/documentacion/SparseMatrix/SparseMatrix.html +553 -0
  15. data/documentacion/SparseMatrix/SparseVector.html +292 -0
  16. data/documentacion/SparseMatrix.html +226 -0
  17. data/documentacion/created.rid +13 -0
  18. data/documentacion/documentacion/created_rid.html +109 -0
  19. data/documentacion/images/add.png +0 -0
  20. data/documentacion/images/arrow_up.png +0 -0
  21. data/documentacion/images/brick.png +0 -0
  22. data/documentacion/images/brick_link.png +0 -0
  23. data/documentacion/images/bug.png +0 -0
  24. data/documentacion/images/bullet_black.png +0 -0
  25. data/documentacion/images/bullet_toggle_minus.png +0 -0
  26. data/documentacion/images/bullet_toggle_plus.png +0 -0
  27. data/documentacion/images/date.png +0 -0
  28. data/documentacion/images/delete.png +0 -0
  29. data/documentacion/images/find.png +0 -0
  30. data/documentacion/images/loadingAnimation.gif +0 -0
  31. data/documentacion/images/macFFBgHack.png +0 -0
  32. data/documentacion/images/package.png +0 -0
  33. data/documentacion/images/page_green.png +0 -0
  34. data/documentacion/images/page_white_text.png +0 -0
  35. data/documentacion/images/page_white_width.png +0 -0
  36. data/documentacion/images/plugin.png +0 -0
  37. data/documentacion/images/ruby.png +0 -0
  38. data/documentacion/images/tag_blue.png +0 -0
  39. data/documentacion/images/tag_green.png +0 -0
  40. data/documentacion/images/transparent.png +0 -0
  41. data/documentacion/images/wrench.png +0 -0
  42. data/documentacion/images/wrench_orange.png +0 -0
  43. data/documentacion/images/zoom.png +0 -0
  44. data/documentacion/index.html +106 -0
  45. data/documentacion/js/darkfish.js +155 -0
  46. data/documentacion/js/jquery.js +18 -0
  47. data/documentacion/js/navigation.js +142 -0
  48. data/documentacion/js/search.js +94 -0
  49. data/documentacion/js/search_index.js +1 -0
  50. data/documentacion/js/searcher.js +228 -0
  51. data/documentacion/rdoc.css +595 -0
  52. data/documentacion/sparse_matrix_gemspec.html +138 -0
  53. data/documentacion/table_of_contents.html +178 -0
  54. data/lib/sparse_matrix/version.rb +1 -1
  55. data/lib/sparse_matrix.rb +194 -162
  56. metadata +51 -1
data/lib/sparse_matrix.rb CHANGED
@@ -1,7 +1,27 @@
1
+ # = Fichero que contiene la gema para el trabajo con matrices.
2
+ #
3
+ # Operaciones con matrices *dispersas* y *densas*.
4
+ #
5
+ # Authors: KEVIN ISAAC ROBAYNA HERNANDEZ, JOSE ANTONIO RODRIGUEZ LEANDRO
6
+ #
7
+ # Email: kevinirobaynahdez@gmail.com, alu0100696691@ull.edu.es
8
+ #
9
+
1
10
  require "sparse_matrix/version"
2
11
 
12
+
13
+ # == Modulo que contiene la gema para el trabajo con matrices.
14
+ #
15
+ # Para la realizacion de esta practica se ha creado una jeraquia de clases. Todo esta integrado en un module llamado SparseMatrix,
16
+ # el cual contiene la clase madre abstracta AbstractMatrix con la que creamos las clases SparseMatrix y DenseMatrix.
17
+ # Para almacenar los datos de la matriz dispersa usamos un vector de duplas usando la clase SparseVector.
18
+ # Los metodos implementados usando metodologia funcional son:
19
+ #
20
+ # * suma
21
+ # * multiplicacion
22
+ #
3
23
  module SparseMatrix
4
- class AbstractMatrix
24
+ class AbstractMatrix #clase abstracta matrix
5
25
 
6
26
  def initialize(r=0,c=0)
7
27
  @row = r
@@ -23,15 +43,14 @@ module SparseMatrix
23
43
  end
24
44
 
25
45
  def +(b)
26
- raise "Error. metodo no definido."
46
+ raise "Error. metodo no definido."
47
+ end
48
+ def *(b)
49
+ raise "Error. metodo no definido."
50
+ end
27
51
  end
28
-
29
- def *(b)
30
- raise "Error. metodo no definido."
31
- end
32
- end
33
52
 
34
- class SparseVector
53
+ class SparseVector #clase para guardar vector de tuplas
35
54
  def initialize(i=0,j=0,v=0)
36
55
  @i = i
37
56
  @j = j
@@ -45,7 +64,7 @@ class SparseVector
45
64
 
46
65
  end
47
66
 
48
- class SparseMatrix < AbstractMatrix
67
+ class SparseMatrix < AbstractMatrix #matrix dispersa
49
68
 
50
69
  def initialize(*args)
51
70
  @n,@m=args[0],args[1]
@@ -67,7 +86,7 @@ class SparseMatrix < AbstractMatrix
67
86
  @MAT<<vector
68
87
  end
69
88
 
70
- def to_s
89
+ def to_s #devuelve una cadena string
71
90
  cadena=""
72
91
  for i in 0...@n
73
92
  cadena+="["
@@ -83,7 +102,7 @@ class SparseMatrix < AbstractMatrix
83
102
 
84
103
  end
85
104
 
86
- def valor(k,j)
105
+ def valor(k,j) #para buscar un valor especifico
87
106
  dev=0
88
107
  for i in 0...@MAT.size
89
108
  if(@MAT[i].i==k) && (@MAT[i].j==j)
@@ -93,8 +112,7 @@ class SparseMatrix < AbstractMatrix
93
112
  dev
94
113
  end
95
114
 
96
-
97
- def max
115
+ def max #elemento maximo de la matrix
98
116
  maximo=@MAT[0].value
99
117
  for i in 0...@m do
100
118
  if maximo < @MAT[i].value
@@ -102,56 +120,56 @@ class SparseMatrix < AbstractMatrix
102
120
  end
103
121
  end
104
122
  return maximo
105
- end
123
+ end
106
124
 
107
- def min
108
- minimo=@MAT[0].value
109
- for i in (0...@m) do
110
- if minimo > @MAT[i].value
111
- minimo=@MAT[i].value
112
- end
113
- end
114
- return minimo
125
+ def min #elemento minimo de la matrix
126
+ minimo=@MAT[0].value
127
+ for i in (0...@m) do
128
+ if minimo > @MAT[i].value
129
+ minimo=@MAT[i].value
115
130
  end
131
+ end
132
+ return minimo
133
+ end
116
134
 
117
- def +(other)
118
- if other.instance_of? SparseMatrix
119
- o=DenseMatrix.new(@n,@m,Array.new(@n){Array.new(@m)})
120
- for i in 0...@MAT.size
121
- for j in 0...@MAT.size
122
- o.mat[i][j]=self.valor(i,j)+other.valor(i,j)
123
- end
135
+ def +(other) #metodo para sumar matrices, dispersa como densa
136
+ if other.instance_of? SparseMatrix
137
+ o=DenseMatrix.new(@n,@m,Array.new(@n){Array.new(@m)})
138
+ 0.upto @MAT.size-1 do |i|
139
+ 0.upto @MAT.size-1 do |j|
140
+ o.mat[i][j]=self.valor(i,j)+other.valor(i,j)
124
141
  end
125
- return SparseMatrix.new(@n,@m,o.mat) #SparseMatrix.new(@n,@m,o)
126
- else
127
- o=DenseMatrix.new(@n,@m,Array.new(@n){Array.new(@m)})
128
- for i in 0...@n
129
- for j in 0...@m
130
- o.mat[i][j]=self.valor(i,j)+other.mat[i][j]
131
- end
142
+ end
143
+ return SparseMatrix.new(@n,@m,o.mat) #SparseMatrix.new(@n,@m,o)
144
+ else
145
+ o=DenseMatrix.new(@n,@m,Array.new(@n){Array.new(@m)})
146
+ 0.upto @n-1 do |i|
147
+ 0.upto @m-1 do |j|
148
+ o.mat[i][j]=self.valor(i,j)+other.mat[i][j]
132
149
  end
133
- return o
134
150
  end
151
+ return o
135
152
  end
153
+ end
136
154
 
137
- def *(b)
155
+ def *(b) #metodo para multiplicar matrices, dispersa como densas
138
156
  if b.instance_of? SparseMatrix
139
157
  c = DenseMatrix.new(2,2,[[0.0,0.0],[0.0,0.0]])
140
- for i in(0...@MAT.size)
141
- for j in(0...@MAT.size)
158
+ 0.upto @MAT.size-1 do |i|
159
+ 0.upto @MAT.size-1 do |j|
142
160
  c.mat[i][j]=0
143
- for k in (0...@MAT.size)
161
+ 0.upto @MAT.size-1 do |k|
144
162
  c.mat[i][j] += self.valor(i,k)*b.valor(k,j)
145
163
  end
146
164
  end
147
165
  end
148
166
  c
149
- else
167
+ else
150
168
  c = DenseMatrix.new(2,2,[[0.0,0.0],[0.0,0.0]])
151
- for i in(0...@MAT.size)
152
- for j in(0...@MAT.size)
169
+ 0.upto @MAT.size-1 do |i|
170
+ 0.upto @MAT.size-1 do |j|
153
171
  c.mat[i][j]=0
154
- for k in (0...@MAT.size)
172
+ 0.upto @MAT.size-1 do |k|
155
173
  c.mat[i][j] += self.valor(i,k)*b.mat[k][j]
156
174
  end
157
175
  end
@@ -159,11 +177,10 @@ class SparseMatrix < AbstractMatrix
159
177
  c
160
178
  end
161
179
  end
162
-
163
180
  end
164
181
 
165
182
 
166
- class DenseMatrix < AbstractMatrix
183
+ class DenseMatrix < AbstractMatrix #clase para matrices densas
167
184
  @mat
168
185
  def initialize(r=0,c=0,matrix=[])
169
186
  super(r,c)
@@ -172,7 +189,7 @@ class DenseMatrix < AbstractMatrix
172
189
 
173
190
  attr_accessor :mat,:r,:c
174
191
 
175
- def to_s()
192
+ def to_s() #devuelve cadena string
176
193
  s="| "
177
194
  for i in (0... @mat.length)
178
195
  for j in (0... @mat.length)
@@ -188,7 +205,7 @@ class DenseMatrix < AbstractMatrix
188
205
  s += "|"
189
206
  end
190
207
 
191
- def print_matrix()
208
+ def print_matrix() #metodo que imprime la matrix en pantalla
192
209
  printf "| "
193
210
  for i in (0... @mat.length)
194
211
  for j in (0... @mat.length)
@@ -204,53 +221,53 @@ class DenseMatrix < AbstractMatrix
204
221
  printf "|"
205
222
  end
206
223
 
207
- def +(b)
208
- c = DenseMatrix.new(2,2,[[0.0,0.0],[0.0,0.0]])
209
- if b.instance_of? SparseMatrix
210
- for i in (0...@mat.length)
211
- for j in (0...@mat.size)
212
- c.mat[i][j] = self.mat[i][j]+b.valor(i,j)
213
- end
214
- end
215
- c
216
- else
217
- for i in (0...mat.length)
218
- for j in (0...@mat.size)
219
- c.mat[i][j] = self.mat[i][j]+b.mat[i][j]
224
+ def +(b) #suma de matrices, tando densas como dispersas
225
+ c = DenseMatrix.new(2,2,[[0.0,0.0],[0.0,0.0]])
226
+ if b.instance_of? SparseMatrix
227
+ 0.upto @mat.size-1 do |i|
228
+ 0.upto @mat.size-1 do |j|
229
+ c.mat[i][j] = self.mat[i][j]+b.valor(i,j)
230
+ end
231
+ end
232
+ c
233
+ else
234
+ 0.upto @mat.size-1 do |i|
235
+ 0.upto @mat.size-1 do |j|
236
+ c.mat[i][j] = self.mat[i][j]+b.mat[i][j]
237
+ end
238
+ end
239
+ c
240
+ end
220
241
  end
221
- end
222
- c
223
- end
224
- end
225
242
 
226
- def *(b)
227
- if b.instance_of? SparseMatrix
228
- c = DenseMatrix.new(2,2,[[0.0,0.0],[0.0,0.0]])
229
- for i in(0...@mat.length)
230
- for j in(0...@mat.length)
231
- c.mat[i][j]=0
232
- for k in (0...@mat.length)
233
- c.mat[i][j] += @mat[i][k]*b.valor(k,j)
243
+ def *(b) #multiplicacion de matrices, dispersas y densas
244
+ if b.instance_of? SparseMatrix
245
+ c = DenseMatrix.new(2,2,[[0.0,0.0],[0.0,0.0]])
246
+ 0.upto @mat.length-1 do |i|
247
+ 0.upto @mat.length-1 do |j|
248
+ c.mat[i][j]=0
249
+ 0.upto @mat.length-1 do |k|
250
+ c.mat[i][j] += @mat[i][k]*b.valor(k,j)
251
+ end
234
252
  end
235
253
  end
236
- end
237
- c
254
+ c
238
255
  else
239
- c = DenseMatrix.new(2,2,[[0.0,0.0],[0.0,0.0]])
240
- for i in(0...@mat.length)
241
- for j in(0...@mat.length)
242
- c.mat[i][j]=0
243
- for k in (0...@mat.length)
244
- c.mat[i][j] += @mat[i][k]*b.mat[k][j]
256
+ c = DenseMatrix.new(2,2,[[0.0,0.0],[0.0,0.0]])
257
+ 0.upto @mat.length-1 do |i|
258
+ 0.upto @mat.length-1 do |j|
259
+ c.mat[i][j]=0
260
+ 0.upto @mat.length-1 do |k|
261
+ c.mat[i][j] += @mat[i][k]*b.mat[k][j]
262
+ end
245
263
  end
246
264
  end
265
+ c
266
+ end
247
267
  end
248
- c
249
- end
250
- end
251
268
  end
252
269
 
253
- class Fraction
270
+ class Fraction #clase para operaciones de numeros fraccionales
254
271
  include Comparable
255
272
  def initialize (*args)
256
273
  if args.size == 2
@@ -264,97 +281,112 @@ class Fraction
264
281
  end
265
282
  attr_accessor :num_,:den_
266
283
 
267
- def to_s
284
+ def to_s #devuelve string
268
285
  "#{@num_}/#{@den_}"
269
286
  end
270
- def to_f
287
+ def to_f #devuelve float
271
288
  @num_.to_f/@den_.to_f
272
289
  end
273
290
 
274
- def ==(b)
275
- return @num_.eql?(b.num_) && @den_.eql?(b.den_)
276
- end
277
-
278
- def abs
279
- c = @num_.to_f/@den_.to_f
280
- return c.abs
281
- end
282
-
283
- def reciprocal
284
- f=Fraction.new
285
- f.num_=@den_
286
- f.den_ = @num_
287
- f
288
- end
291
+ def ==(b) #comparar
292
+ return @num_.eql?(b.num_) && @den_.eql?(b.den_)
293
+ end
289
294
 
290
- def -@
291
- Fraction.new(-@num_,@den_)
292
- end
295
+ def abs #valor absoluto
296
+ c = @num_.to_f/@den_.to_f
297
+ return c.abs
298
+ end
293
299
 
294
- def +(b)
295
- r=Fraction.new
296
- if (@den_==b.den_)
297
- r.num_ = @num_ + b.num_
298
- r.den_ = @den_
299
- else
300
- r.num_ = @num_ * b.den_ + b.num_ * @den_
301
- r.den_ = @den_ * b.den_
302
- end
303
- r.num_,r.den_ = minimiza(r.num_,r.den_)
304
- return r
305
- end
300
+ def reciprocal #devuelve fraccion dada la vuelta
301
+ f=Fraction.new
302
+ f.num_=@den_
303
+ f.den_ = @num_
304
+ f
305
+ end
306
306
 
307
- def -(b)
308
- r =Fraction.new
309
- if (@den_ == b.den_)
310
- r.num_=@num_- b.num_
311
- r.den_=@den_
312
- else
313
- r.num_=@num_ * b.den_ - b.num_ * @den_
314
- r.den_ = @den_ * b.den_
315
- end
316
- r.num_,r.den_ = minimiza(r.num_,r.den_)
317
- r
318
- end
307
+ def -@ #opuesto
308
+ Fraction.new(-@num_,@den_)
309
+ end
310
+
311
+ def +(b) #suma de numeros fraccionarios
312
+ r=Fraction.new
313
+ if b.instance_of? Fraction
314
+ if (@den_==b.den_)
315
+ r.num_ = @num_ + b.num_
316
+ r.den_ = @den_
317
+ else
318
+ r.num_ = @num_ * b.den_ + b.num_ * @den_
319
+ r.den_ = @den_ * b.den_
320
+ end
321
+ else
322
+ r=self+Fraction.new(b,1)
323
+ end
324
+ r.num_,r.den_ = minimiza(r.num_,r.den_)
325
+ r
326
+ end
319
327
 
320
- def *(b)
321
- r =Fraction.new
322
- r.num_=@num_ * b.num_
323
- r.den_=@den_ * b.den_
324
- r.num_,r.den_ = minimiza(r.num_,r.den_)
325
- return r
326
- end
328
+ def -(b) #resta de numeros fraccionarios
329
+ r =Fraction.new
330
+ if b.instance_of? Fraction
331
+ if (@den_==b.den_)
332
+ r.num_ = @num_ - b.num_
333
+ r.den_ = @den_
334
+ else
335
+ r.num_ = @num_ * b.den_ - b.num_ * @den_
336
+ r.den_ = @den_ * b.den_
337
+ end
338
+ else
339
+ r=self-Fraction.new(b,1)
340
+ end
341
+ r.num_,r.den_ = minimiza(r.num_,r.den_)
342
+ r
343
+ end
327
344
 
328
- def /(b)
329
- r =Fraction.new
330
- r.num_=@num_ / b.num_
331
- r.den_=@den_ * b.den_
332
- r.num_,r.den_ = minimiza(r.num_,r.den_)
333
- r
334
- end
345
+ def *(b) #multiplicacion de numeros fraccionarios
346
+ r =Fraction.new
347
+ if b.instance_of? Fraction
348
+ r.num_=@num_ * b.num_
349
+ r.den_=@den_ * b.den_
350
+ r.num_,r.den_ = minimiza(r.num_,r.den_)
351
+ r
352
+ else
353
+ r=self*Fraction.new(b,1)
354
+ end
355
+ end
335
356
 
336
- def <=>(b)
337
- self.to_f <=> b.to_f
338
- end
357
+ def /(b) #division de numeros fraccionarios
358
+ r =Fraction.new
359
+ if b.instance_of? Fraction
360
+ r.num_=@num_ / b.num_
361
+ r.den_=@den_ * b.den_
362
+ r.num_,r.den_ = minimiza(r.num_,r.den_)
363
+ r
364
+ else
365
+ r=self/Fraction.new(b,1)
366
+ end
367
+ end
339
368
 
340
- def minimiza(x,y)
341
- d = gcd(x,y)
342
- x = x/d
343
- y = y/d
344
- return x,y
345
- end
369
+ def <=>(b) #comparar
370
+ self.to_f <=> b.to_f
371
+ end
346
372
 
347
- def coerce(other)
348
- [self,other]
349
- end
350
- end
373
+ def minimiza(x,y) #minimizar fraccion
374
+ d = gcd(x,y)
375
+ x = x/d
376
+ y = y/d
377
+ return x,y
378
+ end
351
379
 
352
- def gcd(u, v)
353
- u, v = u.abs, v.abs
354
- while v != 0
355
- u, v = v, u % v
380
+ def coerce(b)
381
+ [self,Fraction.new(b,1)]
356
382
  end
357
- u
358
383
  end
359
384
 
385
+ def gcd(u, v) #maximo comun divisor
386
+ u, v = u.abs, v.abs
387
+ while v != 0
388
+ u, v = v, u % v
389
+ end
390
+ u
391
+ end
360
392
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sparse_matrix
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - KevinRobayna
@@ -108,6 +108,56 @@ files:
108
108
  - LICENSE.txt
109
109
  - README.md
110
110
  - Rakefile
111
+ - documentacion/Gemfile.html
112
+ - documentacion/Gemfile_lock.html
113
+ - documentacion/Guardfile.html
114
+ - documentacion/LICENSE_txt.html
115
+ - documentacion/Object.html
116
+ - documentacion/README_md.html
117
+ - documentacion/Rakefile.html
118
+ - documentacion/SparseMatrix.html
119
+ - documentacion/SparseMatrix/AbstractMatrix.html
120
+ - documentacion/SparseMatrix/DenseMatrix.html
121
+ - documentacion/SparseMatrix/Fraction.html
122
+ - documentacion/SparseMatrix/SparseMatrix.html
123
+ - documentacion/SparseMatrix/SparseVector.html
124
+ - documentacion/created.rid
125
+ - documentacion/documentacion/created_rid.html
126
+ - documentacion/images/add.png
127
+ - documentacion/images/arrow_up.png
128
+ - documentacion/images/brick.png
129
+ - documentacion/images/brick_link.png
130
+ - documentacion/images/bug.png
131
+ - documentacion/images/bullet_black.png
132
+ - documentacion/images/bullet_toggle_minus.png
133
+ - documentacion/images/bullet_toggle_plus.png
134
+ - documentacion/images/date.png
135
+ - documentacion/images/delete.png
136
+ - documentacion/images/find.png
137
+ - documentacion/images/loadingAnimation.gif
138
+ - documentacion/images/macFFBgHack.png
139
+ - documentacion/images/package.png
140
+ - documentacion/images/page_green.png
141
+ - documentacion/images/page_white_text.png
142
+ - documentacion/images/page_white_width.png
143
+ - documentacion/images/plugin.png
144
+ - documentacion/images/ruby.png
145
+ - documentacion/images/tag_blue.png
146
+ - documentacion/images/tag_green.png
147
+ - documentacion/images/transparent.png
148
+ - documentacion/images/wrench.png
149
+ - documentacion/images/wrench_orange.png
150
+ - documentacion/images/zoom.png
151
+ - documentacion/index.html
152
+ - documentacion/js/darkfish.js
153
+ - documentacion/js/jquery.js
154
+ - documentacion/js/navigation.js
155
+ - documentacion/js/search.js
156
+ - documentacion/js/search_index.js
157
+ - documentacion/js/searcher.js
158
+ - documentacion/rdoc.css
159
+ - documentacion/sparse_matrix_gemspec.html
160
+ - documentacion/table_of_contents.html
111
161
  - lib/sparse_matrix.rb
112
162
  - lib/sparse_matrix/version.rb
113
163
  - sparse_matrix.gemspec