matrix_disp 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,519 @@
1
+ #require "matrix_disp/version"
2
+
3
+ module Math
4
+
5
+ class Matriz
6
+ def initialize (f,c,v)
7
+ @f, @c = f, c
8
+ @v = v
9
+ end
10
+
11
+ def def_tipo
12
+ if (((@v.count{|e| e == 0}*100)/(@f*@c)) >= 60)
13
+ resultado = SparseMatrix.new
14
+ for i in (0...@f*@c)
15
+ tmp = SparseVector.new
16
+ if @v[i] != 0
17
+ if resultado.matrix[i%@c].is_a? SparseVector
18
+ resultado.matrix[i%@c][i/@f] = @v[i]
19
+ else
20
+ tmp[i/@f] = @v[i]
21
+ resultado.matrix[i%@c] = tmp
22
+ end
23
+ end
24
+ end
25
+ return resultado
26
+ else
27
+ return DenseMatrix.new(@f,@c,@v)
28
+ end
29
+ end
30
+ end
31
+
32
+ class Fraccion
33
+ include Comparable
34
+ #Accessors para poder acceder a los métodos :num y :denom
35
+ attr_accessor :num
36
+ attr_accessor :denom
37
+
38
+ def coerce(other)
39
+ [self, other]
40
+ end
41
+ #método que llama al constructor del objeto de la clase
42
+ def initialize(n, d)
43
+ @num = n
44
+ @denom = d
45
+ gcd #reduce la fracción
46
+ end
47
+ #método que reduce la fracción calculando el mcd y dividiendolo entre el numerador y el denominador
48
+ def gcd
49
+ mcd, v = @num.abs, @denom.abs
50
+ while v > 0
51
+ mcd, v = v, mcd % v
52
+ end
53
+ @num = @num/mcd
54
+ @denom = @denom/mcd
55
+ end
56
+ #método que muestra la fracción en la forma a/b
57
+ def to_s
58
+ "#{@num}/#{@denom}"
59
+ #puts "#{@num}/#{@denom}"
60
+ #cadena = "#{@num}/#{@denom}" #se usa para que la función to_s devuelva lo que acaba de imprimir por pantalla
61
+ end
62
+ def to_f
63
+ @num.to_f/@denom.to_f
64
+ end
65
+ #método que muestra la fracción en la forma a/b en flotante
66
+ def to_sf
67
+ "#{@num.to_f}/#{@denom.to_f}"
68
+ #puts "#{@num.to_f}/#{@denom.to_f}"
69
+ #cadena = "#{@num.to_f}/#{@denom.to_f}" #se usa para que la función to_s devuelva lo que acaba de imprimir por pantalla
70
+ end
71
+ #método que devuelve true si la fracción con la que se compara es igual a la actual
72
+ def ==(o)
73
+ return @num == o.num && @denom == o.denom if o.instance_of? Fraccion
74
+ false
75
+ end
76
+ #método que calcula el valor absoluto de la fracción
77
+ def abs
78
+ @denom = @denom.abs
79
+ @num = @num.abs
80
+ end
81
+ #método que calcula el recíproco de la fracción.
82
+ def reciprocal
83
+ tmpNumerador = @num
84
+ @num = @denom
85
+ @denom = tmpNumerador
86
+ end
87
+ #método que calcula el inverso de la fracción
88
+ def -@
89
+ @num = -@num
90
+ end
91
+ #método que devuelve la suma de la fracción actual con la que se pasa como parámetro
92
+ def +(other)
93
+ Fraccion.new((@num*other.denom)+(@denom*other.num), @denom*other.denom)
94
+ end
95
+ #método que devuelve la resta de la fracción actual con la que se pasa como parámetro
96
+ def -(other)
97
+ Fraccion.new((@num*other.denom)-(@denom*other.num), @denom*other.denom)
98
+ end
99
+ #método que devuelve la multiplicación de la fracción actual con la que se pasa como parámetro
100
+ def *(other)
101
+ Fraccion.new(@num*other.num, @denom*other.denom)
102
+ end
103
+ #método que devuelve la división de la fracción actual con la que se pasa como parámetro
104
+ def /(other)
105
+ other.reciprocal
106
+ Fraccion.new(@num*other.num, @denom*other.denom)
107
+ end
108
+ #método que devuelve el módulo de la fracción actual con la que se pasa como parámetro
109
+ def %(other)
110
+ other.reciprocal
111
+ a = Fraccion.new(@num*other.num, @denom*other.denom)
112
+ a.num % a.denom
113
+ end
114
+ def divReciprocal(other)
115
+ other.reciprocal
116
+ a = Fraccion.new(@num*other.num, @denom*other.denom)
117
+ a.reciprocal
118
+ a
119
+ end
120
+ def <=>(other)
121
+ (@num.to_f/@denom.to_f) <=> (other.num.to_f/other.denom.to_f)
122
+ end
123
+ end
124
+
125
+ class SparseVector
126
+ attr_reader :vector
127
+
128
+ def initialize(h = {})
129
+ @vector = Hash.new(0)
130
+ @vector = @vector.merge!(h)
131
+ end
132
+
133
+ def [](i)
134
+ @vector[i]
135
+ end
136
+
137
+ def []=(i,v)
138
+ @vector[i] = v
139
+ end
140
+
141
+ def to_s
142
+ @vector.to_s
143
+ end
144
+
145
+ def keys
146
+ @vector.keys
147
+ end
148
+
149
+ def +(other)
150
+ resultado = SparseVector.new
151
+ for i in other.vector.keys do
152
+ resultado[i] = other[i]
153
+ end
154
+ for i in @vector.keys do
155
+ resultado[i] = @vector[i] + other[i]
156
+ end
157
+ resultado
158
+ end
159
+
160
+ def -(other)
161
+ resultado = SparseVector.new
162
+ for i in other.vector.keys do
163
+ resultado[i] = other[i]
164
+ end
165
+ for i in @vector.keys do
166
+ resultado[i] = @vector[i] - other[i]
167
+ end
168
+ resultado
169
+ end
170
+ end
171
+
172
+ class SparseMatrix < Matriz
173
+
174
+ attr_reader :matrix
175
+
176
+ def initialize(h = {})
177
+ @matrix = Hash.new({})
178
+ for k in h.keys do
179
+ @matrix[k] = if h[k].is_a? SparseVector
180
+ h[k]
181
+ else
182
+ @matrix[k] = SparseVector.new(h[k])
183
+ end
184
+ end
185
+ end
186
+
187
+ def [](i)
188
+ @matrix[i]
189
+ end
190
+
191
+ def []=(i,other)
192
+ for y in other.keys do
193
+ puts "---- other[#{y}] #{other[y]}"
194
+ @matrix[i][y] = other[y]
195
+ puts "---- matrix[#{i}][#{y}] #{@matrix[i][y]}"
196
+ end
197
+ end
198
+
199
+ def imp
200
+ for i in @matrix.keys do
201
+ puts "#{i} ---- #{@matrix[i].to_s}"
202
+ end
203
+ end
204
+
205
+ def col(j)
206
+ c = {}
207
+ for r in @matrix.keys do
208
+ c[r] = @matrix[r].vector[j] if @matrix[r].vector.keys.include? j
209
+ end
210
+ SparseVector.new c
211
+ end
212
+
213
+ def +(other)
214
+ resultado = SparseMatrix.new
215
+ for i in other.matrix.keys do
216
+ resultado.matrix[i] = other[i]
217
+ end
218
+ for i in @matrix.keys do
219
+ for j in @matrix[i].keys do
220
+ if other.matrix[i][j] == nil
221
+ resultado.matrix[i] = @matrix[i]
222
+ else
223
+ resultado.matrix[i][j] = @matrix[i][j]+other.matrix[i][j]
224
+ end
225
+ end
226
+ end
227
+ resultado
228
+ end
229
+
230
+ def -(other)
231
+ resultado = SparseMatrix.new
232
+ for i in other.matrix.keys do
233
+ resultado.matrix[i] = other[i]
234
+ end
235
+ for i in @matrix.keys do
236
+ for j in @matrix[i].keys do
237
+ if other.matrix[i][j] == nil
238
+ resultado.matrix[i] = @matrix[i]
239
+ else
240
+ resultado.matrix[i][j] = @matrix[i][j]-other.matrix[i][j]
241
+ end
242
+ end
243
+ end
244
+ resultado
245
+ end
246
+
247
+
248
+
249
+ def traspuesta
250
+ resultado = SparseMatrix.new
251
+ for i in @matrix.keys do
252
+ for j in @matrix[i].keys do
253
+ tmp = SparseVector.new
254
+ for x in @matrix.keys do
255
+ for y in @matrix[x].keys do
256
+ #puts "#{j} == #{y}"
257
+ if j == y
258
+ #puts "#{j} == #{y}, por tanto tmp[#{i}] = #{@matrix[x][y]}"
259
+ tmp[i] = @matrix[x][y]
260
+ #puts "TMP[#{i}] = #{tmp[i]}"
261
+ end
262
+ end
263
+ end
264
+ resultado.matrix[j] = tmp
265
+ #puts "resultado.matrix[#{j}] = #{tmp}, Y QUEDA: #{resultado.matrix[j]}"
266
+ end
267
+ end
268
+ resultado
269
+ end
270
+
271
+ def *(other)
272
+ trasp = self.traspuesta
273
+ resultado = SparseMatrix.new
274
+ for i in trasp.matrix.keys do
275
+ for j in trasp.matrix[i].keys do
276
+ if other.matrix[i][j] != nil
277
+
278
+ end
279
+ end
280
+ end
281
+ end
282
+
283
+ def max
284
+ max = 0
285
+ for i in @matrix.keys do
286
+ for j in @matrix[i].keys do
287
+ if @matrix[i][j].is_a? Fraccion
288
+ tmp = Fraccion.new(max,1)
289
+ if @matrix[i][j] > tmp
290
+ max = @matrix[i][j].to_f
291
+ end
292
+ else
293
+ if @matrix[i][j] > max
294
+ max = @matrix[i][j]
295
+ end
296
+ end
297
+ end
298
+ end
299
+ max
300
+ end
301
+
302
+ def min
303
+ min = 999999999
304
+ for i in @matrix.keys do
305
+ for j in @matrix[i].keys do
306
+ if @matrix[i][j].is_a? Fraccion
307
+ tmp = Fraccion.new(min,1)
308
+ if @matrix[i][j] < tmp
309
+ min = @matrix[i][j].to_f
310
+ end
311
+ else
312
+ if @matrix[i][j] < min
313
+ min = @matrix[i][j]
314
+ end
315
+ end
316
+ end
317
+ end
318
+ min
319
+ end
320
+ end
321
+
322
+ class DenseMatrix < Matriz
323
+ attr_accessor :m
324
+ attr_accessor :f
325
+ attr_accessor :c
326
+
327
+ def initialize(filas, columnas, v)
328
+ @f = filas
329
+ @c = columnas
330
+ @m = []
331
+ i = 0
332
+ while i < @f*@c
333
+ @m[i] = v[i]
334
+ i=i+1
335
+ end
336
+ end
337
+
338
+ def [](a,b)
339
+ @m[a+(b*@c)]
340
+ end
341
+
342
+ def []=(a,b,r)
343
+ @m[a+(b*@c)] = r
344
+ end
345
+
346
+ def *(other)
347
+ a = Array.new(@f*@c, 0)
348
+ resultado = DenseMatrix.new(@f, @c, a)
349
+ if @f*@c == other.c*other.f
350
+ for i in (0...@c) do
351
+ for j in (0...@f) do
352
+ s = 0
353
+ for k in (0...@c) do
354
+ s = s + (@m[i+(k*@c)] * other.m[k+(j*other.c)])
355
+ end
356
+ resultado[i,j] = s
357
+ end
358
+ end
359
+ else
360
+ puts "Las matriz A debe tener el mismo numero de filas que las columnas de B"
361
+ end
362
+ resultado
363
+ end
364
+
365
+ def +(other)
366
+ i = 0
367
+ tmp = []
368
+ while i < @f*@c
369
+ tmp[i] = @m[i]+other.m[i]
370
+ i=i+1
371
+ end
372
+ tmp
373
+ end
374
+
375
+ def -(other)
376
+ i = 0
377
+ tmp = []
378
+ while i < @f*@c
379
+ tmp[i] = @m[i]-other.m[i]
380
+ i=i+1
381
+ end
382
+ tmp
383
+ end
384
+
385
+ def det(*other)
386
+ resultado = 0
387
+ if other.size == 0
388
+ for i in (0...@c) do
389
+ j = 0;
390
+ adj = []
391
+ z = 0
392
+ for x in (0...@c) do
393
+ for y in (0...@f) do
394
+ if (x != i) && (y != j)
395
+ adj[z] = @m[x+(y*@c)]
396
+ z=z+1
397
+ end
398
+ end
399
+ end
400
+ if ((i+1)+(j+1))%2 == 0
401
+ resultado = resultado + @m[i+(j*@c)]*det(adj)
402
+ else
403
+ resultado = resultado - @m[i+(j*@c)]*det(adj)
404
+ end
405
+ end
406
+ else
407
+ if other[0].size == 1
408
+ tmp = other[0]
409
+ return tmp[0]
410
+ else
411
+ if other[0].size >= 4
412
+ for i in (0...Math.sqrt(other[0].size).to_i) do
413
+ j=0
414
+ adj = []
415
+ z = 0
416
+ for x in (0...Math.sqrt(other[0].size).to_i) do
417
+ for y in (0...Math.sqrt(other[0].size).to_i) do
418
+ if (x != i) && (y != j)
419
+ adj[z] = other[0][x+(y*Math.sqrt(other[0].size).to_i)]
420
+ z=z+1
421
+ end
422
+ end
423
+ end
424
+ if ((i+1)+(j+1))%2 == 0
425
+ tmp = other[0]
426
+ resultado = resultado + tmp[i+(j*Math.sqrt(other[0].size).to_i)]*det(adj)
427
+ else
428
+ tmp = other[0]
429
+ resultado = resultado - tmp[i+(j*Math.sqrt(other[0].size).to_i)]*det(adj)
430
+ end
431
+ end
432
+ end
433
+ end
434
+ end
435
+ return resultado
436
+ end
437
+
438
+ def adj()
439
+ a = Array.new(@f*@c, 0)
440
+ resultado = DenseMatrix.new(@f, @c, a)
441
+ for i in (0...@c) do
442
+ for j in (0...@f) do
443
+ adj = []
444
+ z = 0
445
+ for x in (0...@c) do
446
+ for y in (0...@f) do
447
+ if (x != i) && (y != j)
448
+ adj[z] = @m[x+(y*@c)]
449
+ z=z+1
450
+ end
451
+ end
452
+ end
453
+ if ((i+1)+(j+1))%2 == 0
454
+ resultado[i,j] = det(adj)
455
+ else
456
+ resultado[i,j] = -det(adj)
457
+ end
458
+ end
459
+ end
460
+ return resultado
461
+ end
462
+
463
+ def tras()
464
+ a = Array.new(@f*@c, 0)
465
+ resultado = DenseMatrix.new(@f, @c, a)
466
+ for i in (0...@c) do
467
+ for j in (0...@f) do
468
+ resultado[i,j] = @m[j+(i*@c)]
469
+ end
470
+ end
471
+ return resultado
472
+ end
473
+
474
+ def inv
475
+ determinante = self.det
476
+ if determinante != 0
477
+ a = Array.new(@f*@c, 0)
478
+ resultado = DenseMatrix.new(@f, @c, a)
479
+ resultado = self.adj
480
+ resultado = resultado.tras
481
+ for i in (0...resultado.c) do
482
+ for j in (0...resultado.f) do
483
+ resultado[i,j] = (1/determinante.to_f)*resultado[i,j]
484
+ end
485
+ end
486
+ return resultado
487
+ end
488
+ end
489
+
490
+ def imprimir
491
+ for i in (0...@c*@f) do
492
+ print "#{@m[i].round(2)}\t"
493
+ if i % @c == 2
494
+ print "\n"
495
+ end
496
+ end
497
+ end
498
+
499
+ def /(other1)
500
+ a = Array.new(@f*@c, 0)
501
+ resultado = DenseMatrix.new(@f, @c, a)
502
+ other = other1.inv
503
+ if @f*@c == other.c*other.f
504
+ for i in (0...@c) do
505
+ for j in (0...@f) do
506
+ s = 0
507
+ for k in (0...@c) do
508
+ s = s + (@m[i+(k*@c)] / other.m[k+(j*other.c)])
509
+ end
510
+ resultado[i,j] = s
511
+ end
512
+ end
513
+ else
514
+ puts "Las matriz A debe tener el mismo numero de filas que las columnas de B"
515
+ end
516
+ resultado
517
+ end
518
+
519
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'matrix_disp/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "matrix_disp"
8
+ spec.version = MatrixDisp::VERSION
9
+ spec.authors = ["Jonás", "Sergio"]
10
+ spec.email = ["alu0100608359@ull.edu.es"]
11
+ spec.description = %q{Gema para realizar operaciones básicas con matrices (Tanto densas como dispersas)}
12
+ spec.summary = %q{Gema para realizar operaciones básicas con matrices (Tanto densas como dispersas)}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
24
+
25
+
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'matrix_disp/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "matrix_disp"
8
+ spec.version = matrix_disp::VERSION
9
+ spec.authors = ["Jonás", "Sergio"]
10
+ spec.email = ["alu0100608359@ull.edu.es"]
11
+ spec.description = %q{Gema para realizar operaciones básicas con matrices (Tanto densas como dispersas)}
12
+ spec.summary = %q{Gema para realizar operaciones básicas con matrices (Tanto densas como dispersas)}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
24
+
25
+