SparseMatrixProject 0.0.2 → 0.0.3
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.
- data/.travis.yml +14 -0
- data/Gemfile +13 -0
- data/Gemfile~ +12 -0
- data/Guardfile +8 -0
- data/LICENSE.txt +1 -1
- data/LICENSE.txt~ +22 -0
- data/README.md +16 -1
- data/README.md~ +47 -0
- data/Rakefile +4 -0
- data/Rakefile~ +17 -0
- data/SparseMatrixProject.gemspec~ +23 -0
- data/bin/SparseMatrixProject +3 -0
- data/bin/SparseMatrixProject~ +0 -0
- data/lib/SparseMatrixProject/version.rb +1 -1
- data/lib/SparseMatrixProject/version.rb~ +3 -0
- data/lib/SparseMatrixProject.rb~ +5 -0
- data/lib/gcd.rb +15 -0
- data/lib/gcd.rb~ +8 -0
- data/lib/matrix_main.rb~ +71 -0
- data/lib/matriz.rb~ +507 -0
- data/lib/racional.rb +146 -0
- data/lib/racional.rb~ +146 -0
- data/spec/racional_spec.rb +119 -0
- data/spec/racional_spec.rb~ +119 -0
- data/test/tc_Matrices.rb +42 -0
- data/test/tc_Matrices.rb~ +42 -0
- metadata +31 -4
data/lib/matriz.rb~
ADDED
@@ -0,0 +1,507 @@
|
|
1
|
+
#=matriz.rb
|
2
|
+
#
|
3
|
+
# Autores:: Aarón José Vera Cerdeña,Jacobo Saavedra Valdes
|
4
|
+
#
|
5
|
+
# == Este fichero contiene:
|
6
|
+
#Las clases que vamos a utilizar para que contienen los métodos
|
7
|
+
#que van a realizar operaciones con matrices densas y dispersas.
|
8
|
+
#
|
9
|
+
require 'rubygems'
|
10
|
+
require 'bundler/setup'
|
11
|
+
require 'nokogiri'
|
12
|
+
require "../lib/racional.rb"
|
13
|
+
# === Clase Matrices
|
14
|
+
#
|
15
|
+
# Definición de la clase _Matrices_ compuesta por
|
16
|
+
# * metodo initialize
|
17
|
+
# * metodo +(other)
|
18
|
+
# * metodo -(other)
|
19
|
+
# * metodo *(other)
|
20
|
+
#
|
21
|
+
class Matrices
|
22
|
+
include Comparable
|
23
|
+
include Enumerable
|
24
|
+
|
25
|
+
attr_accessor :filas, :columnas, :matriz
|
26
|
+
|
27
|
+
def initialize(f, c)#Crea la matriz con las dimensiones especificadas
|
28
|
+
#atributo
|
29
|
+
@filas=f.to_i; #Numero de filas
|
30
|
+
@columnas=c.to_i; #Numero de columnas
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
def +(other)#Método suma para matrices densas y dispersas
|
35
|
+
if(self.filas == other.filas and self.columnas == other.columnas)
|
36
|
+
# SELF Matrices densas
|
37
|
+
if self.instance_of?Densa
|
38
|
+
temp = Densa.new(self.filas, self.columnas, nil)
|
39
|
+
if other.instance_of?Densa
|
40
|
+
|
41
|
+
0.upto(@filas-1) do |i| #for i in (0...@filas.to_i)
|
42
|
+
##0.upto(@columnas-1) do |j| #for j in (0...@columnas.to_i)
|
43
|
+
j=0
|
44
|
+
(0..(@columnas-1)).collect {
|
45
|
+
temp.matriz[i][j] = (self.matriz[i][j]) + (other.matriz[i][j])
|
46
|
+
j+=1;
|
47
|
+
##end
|
48
|
+
}
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
if other.instance_of?Dispersa
|
53
|
+
|
54
|
+
0.upto(@filas-1) do |i| ## for i in (0...@filas.to_i)
|
55
|
+
|
56
|
+
j=0
|
57
|
+
(0..(@columnas-1)).collect { ##for j in (0...@columnas.to_i)
|
58
|
+
encontrado = 0
|
59
|
+
for k in (0...other.posx.size)
|
60
|
+
if (i==other.posx[k] and j==other.posy[k] and encontrado==0)
|
61
|
+
temp.matriz[i][j] = (self.matriz[i][j]) + (other.valor[k])
|
62
|
+
encontrado = 1
|
63
|
+
end
|
64
|
+
end
|
65
|
+
if (encontrado == 0)
|
66
|
+
temp.matriz[i][j] = self.matriz[i][j]
|
67
|
+
end
|
68
|
+
j+= 1
|
69
|
+
}
|
70
|
+
##end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
# SELF Matriz Dispersa
|
76
|
+
if self.instance_of?Dispersa
|
77
|
+
if other.instance_of?Densa
|
78
|
+
temp = Densa.new(self.filas, self.columnas, nil)
|
79
|
+
0.upto(@filas-1) do |i| ##for i in (0...@filas.to_i)
|
80
|
+
0.upto(@columnas-1) do |j|##for j in (0...@columnas.to_i)
|
81
|
+
encontrado = 0
|
82
|
+
for k in (0...self.posx.size.to_i)
|
83
|
+
if (i==self.posx[k] and j==self.posy[k] and encontrado==0)
|
84
|
+
temp.matriz[i][j] = (other.matriz[i][j]) + (self.valor[k])
|
85
|
+
encontrado = 1
|
86
|
+
end
|
87
|
+
end
|
88
|
+
if (encontrado == 0)
|
89
|
+
temp.matriz[i][j] = other.matriz[i][j]
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
|
96
|
+
if other.instance_of?Dispersa
|
97
|
+
temp = Dispersa.new(self.filas,self.columnas,[],[],[])
|
98
|
+
temp.valor = self.valor
|
99
|
+
temp.posx = self.posx
|
100
|
+
temp.posy = self.posy
|
101
|
+
|
102
|
+
for j in (0...other.posx.size.to_i)
|
103
|
+
encontrado = false
|
104
|
+
for k in (0...self.posx.size.to_i)
|
105
|
+
if(other.posx[j] == temp.posx[k] and other.posy[j] == temp.posy[k])
|
106
|
+
temp.valor[k] = temp.valor[k] + other.valor[j]
|
107
|
+
encontrado = true
|
108
|
+
end
|
109
|
+
|
110
|
+
end
|
111
|
+
if (encontrado == false)
|
112
|
+
temp.posx << other.posx[j]
|
113
|
+
temp.posy << other.posy[j]
|
114
|
+
temp.valor << other.valor[j]
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
return temp
|
121
|
+
else
|
122
|
+
return nil
|
123
|
+
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def *(other) #Método producto para matrices densas y dispersas
|
128
|
+
temp=Densa.new(self.filas,other.columnas,nil)
|
129
|
+
|
130
|
+
|
131
|
+
# SELF Matrices densas
|
132
|
+
if self.instance_of?Densa
|
133
|
+
if other.instance_of?Densa
|
134
|
+
###########################DENSA*DENSA#####################################
|
135
|
+
self.filas.times do |i|
|
136
|
+
other.columnas.times do |j|
|
137
|
+
temp.matriz[i][j]=0
|
138
|
+
other.columnas.times do |k|
|
139
|
+
temp.matriz[i][j] = temp.matriz[i][j] + (self.matriz[i][k] * other.matriz[k][j])
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
if other.instance_of?Dispersa
|
145
|
+
disptodens = Densa.new(self.filas,other.columnas,[0,0,0,0,0,0,0,0,0])
|
146
|
+
|
147
|
+
@filas.times do |i|
|
148
|
+
@columnas.times do |j|
|
149
|
+
encontrado = 0
|
150
|
+
0.upto(other.posx.size) do |k|
|
151
|
+
if (i==other.posx[k] and j==other.posy[k] and encontrado==0)
|
152
|
+
disptodens.matriz[i][j] = other.valor[k]
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
#puts disptodens.to_s
|
158
|
+
|
159
|
+
self.filas.times do |i|
|
160
|
+
disptodens.columnas.times do |j|
|
161
|
+
temp.matriz[i][j]=0
|
162
|
+
disptodens.columnas.times do |k|
|
163
|
+
temp.matriz[i][j] = temp.matriz[i][j] + (self.matriz[i][k] * disptodens.matriz[k][j])
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
|
171
|
+
if self.instance_of?Dispersa
|
172
|
+
if other.instance_of?Dispersa
|
173
|
+
###########################DISPERSA*DISPERSA#####################################
|
174
|
+
##1 dispersa a densa
|
175
|
+
disptodens = Densa.new(self.filas,other.columnas,[0,0,0,0,0,0,0,0,0])
|
176
|
+
@filas.times do |i|
|
177
|
+
@columnas.times do |j|
|
178
|
+
encontrado = 0
|
179
|
+
0.upto(other.posx.size) do |k|
|
180
|
+
if (i==other.posx[k] and j==other.posy[k] and encontrado==0)
|
181
|
+
disptodens.matriz[i][j] = other.valor[k]
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
##2 dispersa a densa
|
188
|
+
disptodens2 = Densa.new(other.filas,self.columnas,[0,0,0,0,0,0,0,0,0])
|
189
|
+
@filas.times do |i|
|
190
|
+
@columnas.times do |j|
|
191
|
+
encontrado = 0
|
192
|
+
0.upto(self.posx.size) do |k|
|
193
|
+
if (i==self.posx[k] and j==self.posy[k] and encontrado==0)
|
194
|
+
disptodens2.matriz[i][j] = self.valor[k]
|
195
|
+
end
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
|
201
|
+
temp=Densa.new(self.filas, self.columnas,[0,0,0,0,0,0,0,0,0])
|
202
|
+
puts disptodens.to_s####################################################################
|
203
|
+
puts disptodens2.to_s###################################################################
|
204
|
+
disptodens.filas.times do |i|
|
205
|
+
disptodens2.columnas.times do |j|
|
206
|
+
temp.matriz[i][j]=0
|
207
|
+
disptodens2.columnas.times do |k|
|
208
|
+
temp.matriz[i][j] = temp.matriz[i][j] + (disptodens2.matriz[i][k] * disptodens.matriz[k][j])
|
209
|
+
end
|
210
|
+
end
|
211
|
+
end
|
212
|
+
end
|
213
|
+
|
214
|
+
if other.instance_of?Densa
|
215
|
+
###########################DISPERSA*DENSA#####################################
|
216
|
+
disptodens = Densa.new(other.filas,self.columnas,[0,0,0,0,0,0,0,0,0])
|
217
|
+
@filas.times do |i|
|
218
|
+
@columnas.times do |j|
|
219
|
+
encontrado = 0
|
220
|
+
0.upto(self.posx.size) do |k|
|
221
|
+
if (i==self.posx[k] and j==self.posy[k] and encontrado==0)
|
222
|
+
disptodens.matriz[i][j] = self.valor[k]
|
223
|
+
end
|
224
|
+
end
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
228
|
+
#puts disptodens.to_s
|
229
|
+
|
230
|
+
#puts other.to_s
|
231
|
+
|
232
|
+
temp=Densa.new(self.filas, self.columnas,[0,0,0,0,0,0,0,0,0])
|
233
|
+
other.filas.times do |i|
|
234
|
+
other.columnas.times do |j|
|
235
|
+
disptodens.columnas.times do |k|
|
236
|
+
temp.matriz[i][j] = temp.matriz[i][j] + (disptodens.matriz[i][k] * other.matriz[k][j])
|
237
|
+
end
|
238
|
+
end
|
239
|
+
end
|
240
|
+
end
|
241
|
+
end
|
242
|
+
temp
|
243
|
+
end
|
244
|
+
|
245
|
+
##############################RESTA###########################################
|
246
|
+
|
247
|
+
def -(other) #Resta de matrices Densas y Dispersas##
|
248
|
+
if(self.filas == other.filas and self.columnas == other.columnas)
|
249
|
+
# SELF Matrices densas
|
250
|
+
if self.instance_of?Densa
|
251
|
+
temp = Densa.new(self.filas, self.columnas, nil)
|
252
|
+
if other.instance_of?Densa
|
253
|
+
for i in (0...@filas.to_i)
|
254
|
+
for j in (0...@columnas.to_i)
|
255
|
+
temp.matriz[i][j] = (self.matriz[i][j]) - (other.matriz[i][j])
|
256
|
+
end
|
257
|
+
end
|
258
|
+
end
|
259
|
+
|
260
|
+
if other.instance_of?Dispersa
|
261
|
+
for i in (0...@filas.to_i)
|
262
|
+
for j in (0...@columnas.to_i)
|
263
|
+
encontrado = 0
|
264
|
+
for k in (0...other.posx.size)
|
265
|
+
if (i==other.posx[k] and j==other.posy[k] and encontrado==0)
|
266
|
+
temp.matriz[i][j] = (self.matriz[i][j]) - (other.valor[k])
|
267
|
+
encontrado = 1
|
268
|
+
end
|
269
|
+
end
|
270
|
+
if (encontrado == 0)
|
271
|
+
temp.matriz[i][j] = self.matriz[i][j]
|
272
|
+
end
|
273
|
+
end
|
274
|
+
end
|
275
|
+
end
|
276
|
+
end
|
277
|
+
|
278
|
+
# SELF Matriz Dispersa
|
279
|
+
if self.instance_of?Dispersa
|
280
|
+
if other.instance_of?Densa
|
281
|
+
temp = Densa.new(self.filas, self.columnas, nil)
|
282
|
+
for i in (0...@filas.to_i)
|
283
|
+
for j in (0...@columnas.to_i)
|
284
|
+
encontrado = 0
|
285
|
+
for k in (0...self.posx.size.to_i)
|
286
|
+
if (i==self.posx[k] and j==self.posy[k] and encontrado==0)
|
287
|
+
temp.matriz[i][j] = (other.matriz[i][j]) - (self.valor[k])
|
288
|
+
encontrado = 1
|
289
|
+
end
|
290
|
+
end
|
291
|
+
if (encontrado == 0)
|
292
|
+
temp.matriz[i][j] = other.matriz[i][j]
|
293
|
+
end
|
294
|
+
end
|
295
|
+
end
|
296
|
+
end
|
297
|
+
|
298
|
+
|
299
|
+
if other.instance_of?Dispersa
|
300
|
+
temp = Dispersa.new(self.filas,self.columnas,[],[],[])
|
301
|
+
temp.valor = self.valor
|
302
|
+
temp.posx = self.posx
|
303
|
+
temp.posy = self.posy
|
304
|
+
|
305
|
+
for j in (0...other.posx.size.to_i)
|
306
|
+
encontrado = false
|
307
|
+
for k in (0...self.posx.size.to_i)
|
308
|
+
if(other.posx[j] == temp.posx[k] and other.posy[j] == temp.posy[k])
|
309
|
+
temp.valor[k] = temp.valor[k] - other.valor[j]
|
310
|
+
encontrado = true
|
311
|
+
end
|
312
|
+
|
313
|
+
end
|
314
|
+
if (encontrado == false)
|
315
|
+
temp.posx << other.posx[j]
|
316
|
+
temp.posy << other.posy[j]
|
317
|
+
temp.valor << other.valor[j]
|
318
|
+
end
|
319
|
+
end
|
320
|
+
end
|
321
|
+
end
|
322
|
+
|
323
|
+
return temp
|
324
|
+
else
|
325
|
+
return nil
|
326
|
+
end
|
327
|
+
end
|
328
|
+
|
329
|
+
end
|
330
|
+
|
331
|
+
# === Clase Dispersa
|
332
|
+
#
|
333
|
+
# Definición de la clase _Dispersa_ compuesta por
|
334
|
+
# * metodo initialize
|
335
|
+
# * metodo to_s(other)
|
336
|
+
# * metodo max(other)
|
337
|
+
# * metodo min(other)
|
338
|
+
# * método pos
|
339
|
+
#
|
340
|
+
class Dispersa < Matrices
|
341
|
+
attr_accessor :posx, :posy, :valor
|
342
|
+
def initialize(f,c,posx, posy, valor)
|
343
|
+
super(f,c)
|
344
|
+
@posx = posx
|
345
|
+
@posy = posy
|
346
|
+
@valor = valor
|
347
|
+
|
348
|
+
end
|
349
|
+
|
350
|
+
def to_s
|
351
|
+
s=String.new
|
352
|
+
s << "["
|
353
|
+
for i in (0...@filas.to_i)
|
354
|
+
s << "[#{posx[i]},#{posy[i]},#{valor[i]}]"
|
355
|
+
end
|
356
|
+
s << "]"
|
357
|
+
end
|
358
|
+
|
359
|
+
def max
|
360
|
+
m = self.valor[0]
|
361
|
+
for i in (0...self.valor.size.to_i)
|
362
|
+
if (self.valor[i]> m)
|
363
|
+
m = self.valor[i]
|
364
|
+
end
|
365
|
+
end
|
366
|
+
return m
|
367
|
+
end
|
368
|
+
|
369
|
+
def min
|
370
|
+
m = self.valor[0]
|
371
|
+
for i in (0...self.valor.size.to_i)
|
372
|
+
if (self.valor[i]< m)
|
373
|
+
m = self.valor[i]
|
374
|
+
end
|
375
|
+
end
|
376
|
+
return m
|
377
|
+
end
|
378
|
+
|
379
|
+
def pos(a,b)
|
380
|
+
for i in (0...self.posx.size)
|
381
|
+
if(posx[i]==a and posy[i]==b)
|
382
|
+
return valor[i]
|
383
|
+
end
|
384
|
+
end
|
385
|
+
return nil
|
386
|
+
end
|
387
|
+
|
388
|
+
end
|
389
|
+
# === Clase Densa
|
390
|
+
#
|
391
|
+
# Definición de la clase _Matrices_ compuesta por
|
392
|
+
# * metodo initialize
|
393
|
+
# * metodo pos(a,b)
|
394
|
+
# * metodo to_s
|
395
|
+
# * metodo max
|
396
|
+
# * metodo min
|
397
|
+
#
|
398
|
+
class Densa < Matrices
|
399
|
+
attr_accessor :matriz
|
400
|
+
|
401
|
+
def initialize(f,c,m)#Estructura de datos de la matriz densa
|
402
|
+
super(f,c)
|
403
|
+
@matriz = Array.new(@filas.to_i){Array.new(@columnas.to_i)}
|
404
|
+
|
405
|
+
if (m != nil)
|
406
|
+
#Rellenamos la matriz con lo valores recibidos
|
407
|
+
for i in (0...@filas.to_i)
|
408
|
+
for j in (0...@columnas.to_i)
|
409
|
+
@matriz[i][j]=m[i*@columnas.to_i+j];
|
410
|
+
end
|
411
|
+
end
|
412
|
+
end
|
413
|
+
end
|
414
|
+
|
415
|
+
|
416
|
+
def pos(a,b)# Metodos getter devuelve el valor de una posicion determinada
|
417
|
+
@matriz[a][b]
|
418
|
+
end
|
419
|
+
|
420
|
+
|
421
|
+
def to_s#Metodo que devuelve la matriz en forma de string
|
422
|
+
"#{@matriz}"
|
423
|
+
end
|
424
|
+
|
425
|
+
|
426
|
+
def traspuesta#Método que calcula la traspuesta de una matriz
|
427
|
+
i=0
|
428
|
+
mtrasp = Array.new(@filas) {Array.new(self.columnas)}
|
429
|
+
while i < @filas
|
430
|
+
j=0
|
431
|
+
while j < @columnas
|
432
|
+
mtrasp[i][j] = self.matriz[j][i]
|
433
|
+
j+=1
|
434
|
+
end
|
435
|
+
i+=1
|
436
|
+
end
|
437
|
+
Matrices.new(mtrasp)
|
438
|
+
end
|
439
|
+
|
440
|
+
|
441
|
+
|
442
|
+
def opuesta#Método que calcula el opuesto de una matriz
|
443
|
+
|
444
|
+
i=0
|
445
|
+
mop = Array.new(@filas) {Array.new(self.columnas)}
|
446
|
+
while i < @filas
|
447
|
+
j=0
|
448
|
+
while j < @columnas
|
449
|
+
mop[i][j] = (self.matriz[i][j]) * (- 1)
|
450
|
+
j+=1
|
451
|
+
end
|
452
|
+
i+=1
|
453
|
+
end
|
454
|
+
Matrices.new(mop)
|
455
|
+
|
456
|
+
end
|
457
|
+
|
458
|
+
def minimo#Método que devuelve el elemento menor de la matriz
|
459
|
+
|
460
|
+
min = self.matriz[0][0]
|
461
|
+
i=0
|
462
|
+
while i < @filas
|
463
|
+
j=0
|
464
|
+
while j < @columnas
|
465
|
+
if(self.matriz[i][j] < min)
|
466
|
+
min = self.matriz[i][j]
|
467
|
+
end
|
468
|
+
j+= 1
|
469
|
+
end
|
470
|
+
i+= 1
|
471
|
+
end
|
472
|
+
return min
|
473
|
+
end
|
474
|
+
|
475
|
+
def maximo#Método que devuelve el elemento mayor de la matriz
|
476
|
+
|
477
|
+
max = self.matriz[0][0]
|
478
|
+
i = 0
|
479
|
+
while i < @filas
|
480
|
+
j = 0
|
481
|
+
while j < @columnas
|
482
|
+
if(self.matriz[i][j] > max)
|
483
|
+
max = self.matriz[i][j]
|
484
|
+
end
|
485
|
+
j+= 1
|
486
|
+
end
|
487
|
+
i+= 1
|
488
|
+
end
|
489
|
+
return max
|
490
|
+
end
|
491
|
+
end
|
492
|
+
|
493
|
+
|
494
|
+
den1 = Densa.new(3,3,[1,2,3,4,5,6,7,8,9])
|
495
|
+
den2 = Densa.new(3,3,[1,2,3,4,5,6,7,8,9])
|
496
|
+
den3 = Densa.new(3,3,[0,0,0,0,0,0,0,0,0])
|
497
|
+
disp1= Dispersa.new(3,3,[0,1,2],[0,1,2],[4,12,30])
|
498
|
+
disp2= Dispersa.new(3,3,[1,1,2],[0,1,2],[44,16,32])
|
499
|
+
|
500
|
+
#puts den1.to_s
|
501
|
+
#puts den2.to_s
|
502
|
+
|
503
|
+
|
504
|
+
|
505
|
+
puts "#{(disp1*den1).to_s}"
|
506
|
+
|
507
|
+
|
data/lib/racional.rb
ADDED
@@ -0,0 +1,146 @@
|
|
1
|
+
#=racional.rb
|
2
|
+
#
|
3
|
+
# Autores:: Aarón José Vera Cerdeña,Jacobo Saavedra Valdes
|
4
|
+
#
|
5
|
+
# == Este fichero contiene:
|
6
|
+
#
|
7
|
+
#
|
8
|
+
#
|
9
|
+
#
|
10
|
+
require "./lib/gcd.rb"
|
11
|
+
# === Clase Racional
|
12
|
+
#
|
13
|
+
# Definición de la clase _Racional_ compuesta por
|
14
|
+
# * metodo initialize
|
15
|
+
# * metodo to_s(other)
|
16
|
+
# * metodo +(other)
|
17
|
+
# * metodo *(other)
|
18
|
+
# * metodo /(other)
|
19
|
+
# * metodo num
|
20
|
+
# * metodo denom
|
21
|
+
# * metodo to_f
|
22
|
+
# * metodo abs
|
23
|
+
# * metodo %
|
24
|
+
# * metodo reciprocal
|
25
|
+
# * metodo -@
|
26
|
+
# * metodo <=>
|
27
|
+
#
|
28
|
+
class Racional
|
29
|
+
include Comparable
|
30
|
+
|
31
|
+
def initialize(numerador, denominador)
|
32
|
+
@numerador = numerador
|
33
|
+
@denominador = denominador
|
34
|
+
end
|
35
|
+
attr_reader :numerador, :denominador
|
36
|
+
|
37
|
+
|
38
|
+
def to_s#Método que devuelve un String
|
39
|
+
"#{@numerador}/#{@denominador}"
|
40
|
+
end
|
41
|
+
|
42
|
+
def +(other)#Calcular la suma
|
43
|
+
if (@denominador == other.denominador )
|
44
|
+
nume=@numerador + other.numerador
|
45
|
+
deno=@denominador
|
46
|
+
mcd=gcd(nume,deno)
|
47
|
+
else
|
48
|
+
aux= @denominador * other.denominador
|
49
|
+
nume = ((aux / @denominador ) * @numerador) + ((aux / other.denominador ) * other.numerador)
|
50
|
+
deno = @denominador*other.denominador
|
51
|
+
mcd=gcd(nume,deno)
|
52
|
+
end
|
53
|
+
|
54
|
+
temp = Racional.new(nume/mcd, deno/mcd)
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
def -(other)#Calcular la resta de un Racional
|
59
|
+
if (@denominador == other.denominador )
|
60
|
+
nume=@numerador - other.numerador
|
61
|
+
deno=@denominador
|
62
|
+
mcd=gcd(nume,deno)
|
63
|
+
|
64
|
+
else
|
65
|
+
aux= @denominador * other.denominador
|
66
|
+
nume = ((aux / @denominador ) * @numerador) - ((aux / other.denominador ) * other.numerador)
|
67
|
+
deno=@denominador*other.denominador
|
68
|
+
mcd=gcd(nume,deno)
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
|
73
|
+
temp = Racional.new(nume/mcd, deno/mcd)
|
74
|
+
end
|
75
|
+
|
76
|
+
|
77
|
+
def * (other)#Calcular el producto de un Racional
|
78
|
+
nume = @numerador * other.numerador
|
79
|
+
deno = @denominador * other.denominador
|
80
|
+
mcd=gcd(nume,deno)
|
81
|
+
temp = Racional.new(nume / mcd ,deno / mcd )
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
#####Calcular la division
|
86
|
+
def / (other)
|
87
|
+
|
88
|
+
nume = @numerador *other.denominador
|
89
|
+
deno = @denominador * other.numerador
|
90
|
+
mcd= gcd(nume,deno)
|
91
|
+
temp = Racional.new(nume/mcd, deno/mcd )
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
|
96
|
+
def num#####Devolver el numerador
|
97
|
+
@numerador
|
98
|
+
end
|
99
|
+
|
100
|
+
|
101
|
+
def denom #####Devolver el denominador
|
102
|
+
@denominador
|
103
|
+
end
|
104
|
+
|
105
|
+
|
106
|
+
def to_f#####Flotante
|
107
|
+
flotante = numerador.to_f/denominador
|
108
|
+
end
|
109
|
+
|
110
|
+
|
111
|
+
def abs#####Calcular el absoluto de la fraccion
|
112
|
+
absnum = numerador.abs
|
113
|
+
absdenom = denominador.abs
|
114
|
+
abs = Racional.new(absnum, absdenom)
|
115
|
+
end
|
116
|
+
|
117
|
+
|
118
|
+
def % (other)#####Calcular el resto
|
119
|
+
nume = @numerador * other.denominador
|
120
|
+
deno = @denominador * other.numerador
|
121
|
+
mcd = gcd(nume,deno)
|
122
|
+
|
123
|
+
temp = ((nume/mcd) % (deno/mcd))
|
124
|
+
|
125
|
+
|
126
|
+
end
|
127
|
+
|
128
|
+
|
129
|
+
def reciprocal#####Calcular el reciproco de la fraccion
|
130
|
+
Racional.new(@denominador,@numerador)
|
131
|
+
end
|
132
|
+
|
133
|
+
|
134
|
+
def -@#####Calcular el opuesto de la fraccion
|
135
|
+
negado = Racional.new(-denominador, numerador)
|
136
|
+
end
|
137
|
+
|
138
|
+
def <=> (other)
|
139
|
+
if other.instance_of? Racional
|
140
|
+
return (numerador.to_f/denominador)<=>(other.numerador.to_f/other.denominador)
|
141
|
+
else
|
142
|
+
false
|
143
|
+
end
|
144
|
+
|
145
|
+
end
|
146
|
+
end
|