GemaMatriz 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.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/.travis.yml +6 -0
- data/GemaMatriz.gemspec +27 -0
- data/Gemfile +5 -0
- data/Guardfile +10 -0
- data/LICENSE.txt +22 -0
- data/README.md +72 -0
- data/Rakefile +4 -0
- data/lib/GemaMatriz/version.rb +3 -0
- data/lib/GemaMatriz.rb +5 -0
- data/lib/gcd.rb +9 -0
- data/lib/matriz.rb +293 -0
- data/lib/racional.rb +116 -0
- data/spec/matriz_spec.rb +160 -0
- data/spec/spec_helper.rb +8 -0
- metadata +147 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: be96d0f8fe867db9ee84ce472b3211f1adbc5c50
|
4
|
+
data.tar.gz: 711a49deebe6ff6fdd11f41c32220f0a566fef54
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6f8f1f93777ccfa708711252bcf636f919f9a2e509de653fc109846e93925b1f316740b48bf4742fc5592db0f7fd7060aa6c3b6d1c4234b940b4b6ef51041840
|
7
|
+
data.tar.gz: 66c441c6fc929a5c10a5d3e04bb0a53ed67410cb5dd418ac600eeab485e75333cd6cce918ba89db3492e4288afd6c928deb6bffa21d1ce5550717a8f0616994e
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/GemaMatriz.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'GemaMatriz/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "GemaMatriz"
|
8
|
+
spec.version = GemaMatriz::VERSION
|
9
|
+
spec.authors = ["Rushil Lakhani Lakhani & Waldo Nazco Torres"]
|
10
|
+
spec.email = ["alu0100537031@ull.edu.es"]
|
11
|
+
spec.description = %q{Construcción de una gema ruby para la creación de matrices dispersas y densas}
|
12
|
+
spec.summary = %q{Practica 9 }
|
13
|
+
spec.homepage = "https://github.com/alu0100537031/prct09"
|
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
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "guard"
|
25
|
+
spec.add_development_dependency "guard-rspec"
|
26
|
+
spec.add_development_dependency "guard-bundler"
|
27
|
+
end
|
data/Gemfile
ADDED
data/Guardfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Rushil Lakhani Lakhani
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
Práctica de Laboratorio #9. Creacion De Una Gema
|
2
|
+
================
|
3
|
+
|
4
|
+
## Componentes Del Grupo:
|
5
|
+
|
6
|
+
- RUSHIL LAKHANI LAKHANI
|
7
|
+
- WALDO NAZCO TORRES
|
8
|
+
|
9
|
+
|
10
|
+
## GemaMatriz
|
11
|
+
|
12
|
+
Construcción de una gema ruby para la creación de matrices dispersas y densas,que heredan de la clase base Matriz
|
13
|
+
|
14
|
+
##Descripción de la práctica.
|
15
|
+
|
16
|
+
La práctica consiste en crear una clase abstracta llamada Matriz, donde pondremos los distintos métodos abstractos de la clase
|
17
|
+
Para ello partimos de la clase base Matriz cuya funcion va a ser que sea heredada por las clases hijas ,y creamos dos clases
|
18
|
+
hijas que van a ser: clase Matriz Densa ya implementada de la práctica anterior con las operaciones basicas
|
19
|
+
correspondientes (+-*/) y clase Matriz Dispersa cuya madre va a ser la clase base Matriz y donde se van a redifinir los metodos de
|
20
|
+
la clase madre.
|
21
|
+
|
22
|
+
class Matriz
|
23
|
+
|
24
|
+
metodos abstractos
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
class MatrizDensa < Matriz
|
29
|
+
|
30
|
+
metodos
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
class MatrizDispersa < Matriz
|
35
|
+
|
36
|
+
metodos
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
En dichas clases hijas se redefiniran los metodos para trabajar con el tipo de matrices indicado.
|
41
|
+
## Installation
|
42
|
+
|
43
|
+
Add this line to your application's Gemfile:
|
44
|
+
|
45
|
+
gem 'GemaMatriz'
|
46
|
+
|
47
|
+
And then execute:
|
48
|
+
|
49
|
+
$ bundle
|
50
|
+
|
51
|
+
Or install it yourself as:
|
52
|
+
|
53
|
+
$ gem install GemaMatriz
|
54
|
+
|
55
|
+
## Usage
|
56
|
+
|
57
|
+
TODO: Write usage instructions here
|
58
|
+
|
59
|
+
## Contributing
|
60
|
+
|
61
|
+
1. Fork it
|
62
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
63
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
64
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
65
|
+
5. Create new Pull Request
|
66
|
+
|
67
|
+
---
|
68
|
+
|
69
|
+
Universidad de La Laguna
|
70
|
+
Escuela Técnica Superior de Ingeniería Informática
|
71
|
+
Lenguajes y Paradigmas de la Programación 2013-14
|
72
|
+
|
data/Rakefile
ADDED
data/lib/GemaMatriz.rb
ADDED
data/lib/gcd.rb
ADDED
data/lib/matriz.rb
ADDED
@@ -0,0 +1,293 @@
|
|
1
|
+
# Clase Base Matriz
|
2
|
+
class Matriz
|
3
|
+
require "./lib/racional.rb"
|
4
|
+
|
5
|
+
attr_reader :nfil, :ncol, :mat # metodos de acceso (getter)
|
6
|
+
|
7
|
+
# metodo que inicializa la matriz
|
8
|
+
|
9
|
+
def initialize(nfil,ncol)
|
10
|
+
@nfil = nfil # inicializo numero de filas
|
11
|
+
@ncol = ncol # inicializo numero de columnas
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
class MatrizDensa < Matriz
|
18
|
+
|
19
|
+
attr_reader :mat # metodos de acceso (getter)
|
20
|
+
|
21
|
+
def initialize(nfil,ncol,mat)
|
22
|
+
super(nfil, ncol)
|
23
|
+
@mat = Array.new(mat) #inicializo la matriz pasando como parametro un objeto de tipo matriz
|
24
|
+
end
|
25
|
+
|
26
|
+
#Funcion que devuelve una posicion i dentro de la matriz
|
27
|
+
|
28
|
+
def [](i)
|
29
|
+
return mat[i]
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
#Funcion que asigna un valor k a una posicion i,j dentro de la matriz
|
34
|
+
|
35
|
+
def []=(i, j, k)
|
36
|
+
return mat[i][j] = k
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
def to_s
|
41
|
+
cad = " "
|
42
|
+
for i in 0...nfil
|
43
|
+
cad << " [ "
|
44
|
+
for j in 0...ncol
|
45
|
+
cad << "#{mat[i][j]} "
|
46
|
+
end
|
47
|
+
cad << "]"
|
48
|
+
cad << "\n "
|
49
|
+
end
|
50
|
+
return cad
|
51
|
+
end
|
52
|
+
|
53
|
+
# metodo que suma dos matrices (Sobrecarga del operador +)
|
54
|
+
|
55
|
+
def +(other)
|
56
|
+
raise ArgumentError, "Las matrices no son cuadradas." unless @nfil == other.nfil && @ncol == other.ncol
|
57
|
+
m = Array.new(@nfil){Array.new(@ncol){0}}
|
58
|
+
for i in 0...nfil
|
59
|
+
for j in 0...ncol
|
60
|
+
m[i][j] = self.mat[i][j]+ other.mat[i][j]
|
61
|
+
end
|
62
|
+
end
|
63
|
+
return MatrizDensa.new(other.nfil,other.ncol,m)
|
64
|
+
end
|
65
|
+
|
66
|
+
# metodo que resta dos matrices (Sobrecarga del operador -)
|
67
|
+
|
68
|
+
def -(other)
|
69
|
+
raise ArgumentError, "Las matrices no son cuadradas." unless @nfil == other.nfil && @ncol == other.ncol
|
70
|
+
m = Array.new(@nfil){Array.new(@ncol){0}}
|
71
|
+
for i in 0...nfil
|
72
|
+
for j in 0...ncol
|
73
|
+
m[i][j] = mat[i][j]- other.mat[i][j]
|
74
|
+
end
|
75
|
+
end
|
76
|
+
return MatrizDensa.new(other.nfil,other.ncol,m)
|
77
|
+
end
|
78
|
+
|
79
|
+
# metodo que multiplica dos matrices (Sobrecarga del operador *)
|
80
|
+
|
81
|
+
|
82
|
+
def *(other)
|
83
|
+
m = Array.new(@nfil){Array.new(@ncol){0}}
|
84
|
+
for i in 0...nfil do
|
85
|
+
for j in 0...other.ncol do
|
86
|
+
for k in 0...ncol do
|
87
|
+
m[i][j] = m[i][j] + self.mat[i][k] * other.mat[k][j]
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
return MatrizDensa.new(self.nfil,other.ncol,m)
|
92
|
+
end
|
93
|
+
|
94
|
+
def max
|
95
|
+
max=0
|
96
|
+
for i in 0...nfil do
|
97
|
+
for j in 0...ncol do
|
98
|
+
if mat[i][j] > max
|
99
|
+
max=mat[i][j]
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
return max
|
104
|
+
end
|
105
|
+
|
106
|
+
def min
|
107
|
+
min=9999;
|
108
|
+
for i in 0...nfil do
|
109
|
+
for j in 0...ncol do
|
110
|
+
if mat[i][j] < min
|
111
|
+
min=mat[i][j]
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
return min
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
|
122
|
+
class MatrizDispersa < Matriz
|
123
|
+
|
124
|
+
attr_reader:hash , :mat
|
125
|
+
|
126
|
+
def initialize (nfil, ncol, mat)
|
127
|
+
super(nfil, ncol)
|
128
|
+
@mat = Array.new(mat)
|
129
|
+
nceros = 0 # numero de elementos nulos de la matriz (0)
|
130
|
+
nelementos= (nfil * ncol)*0.6 # elementos de la matriz aplicado el 60 %
|
131
|
+
psincero = 0 # posiciones de los elementos de la matriz cuyo valor no es nulo (0)
|
132
|
+
@hash = Hash.new(0)
|
133
|
+
for i in 0...nfil do
|
134
|
+
for j in 0...ncol do
|
135
|
+
if (mat[i][j]==0)
|
136
|
+
nceros=nceros+1
|
137
|
+
else
|
138
|
+
psincero="[#{i}][#{j}]"
|
139
|
+
if (mat[i][j].is_a?Fraccion)
|
140
|
+
a = mat[i][j].num
|
141
|
+
b = mat[i][j].denom
|
142
|
+
@hash[psincero] = Rational(a,b)
|
143
|
+
#cad = " "
|
144
|
+
#cad << "#{a}"
|
145
|
+
#cad << "/"
|
146
|
+
#cad << "#{b}"
|
147
|
+
#@hash[psincero] = cad
|
148
|
+
else
|
149
|
+
@hash[psincero] = mat[i][j]
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
if nceros >= nelementos # compruebo que la matriz sea dispersa
|
155
|
+
#puts "La matriz es dispersa"
|
156
|
+
else
|
157
|
+
raise ArgumentError, 'La Matriz no es dispersa'
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
def to_s
|
162
|
+
if (hash.values != nil)
|
163
|
+
cad = ""
|
164
|
+
cad << "#{hash}"
|
165
|
+
return cad
|
166
|
+
else
|
167
|
+
return 0
|
168
|
+
end
|
169
|
+
#return hash
|
170
|
+
end
|
171
|
+
|
172
|
+
def +(other)
|
173
|
+
|
174
|
+
case other
|
175
|
+
when MatrizDensa
|
176
|
+
other.+(self)
|
177
|
+
when MatrizDispersa
|
178
|
+
raise ArgumentError, "Las matrices no son cuadradas." unless @nfil == other.nfil && @ncol == other.ncol
|
179
|
+
suma = MatrizDispersa.new(nfil,ncol,0)
|
180
|
+
suma = hash.merge(other.hash){|key,oldval,newval| oldval+newval }
|
181
|
+
return suma # devuelve un objeto de tipo Matriz Dispersa
|
182
|
+
else
|
183
|
+
raise TypeError, "La matriz no es dispersa ni densa" unless other.instance_of? MatrizDispersa
|
184
|
+
end
|
185
|
+
|
186
|
+
|
187
|
+
end
|
188
|
+
|
189
|
+
def -(other)
|
190
|
+
case other
|
191
|
+
when MatrizDensa
|
192
|
+
other.-(self)
|
193
|
+
when MatrizDispersa
|
194
|
+
raise ArgumentError, "Las matrices no son cuadradas." unless @nfil == other.nfil && @ncol == other.ncol
|
195
|
+
resta = MatrizDispersa.new(nfil,ncol,0)
|
196
|
+
resta = hash.merge(other.hash){|key,oldval,newval| oldval-newval}
|
197
|
+
return resta # devuelve un objeto de tipo Matriz Dispersa
|
198
|
+
else
|
199
|
+
raise TypeError, "La matriz no es dispersa ni densa " unless other.instance_of? MatrizDispersa
|
200
|
+
end
|
201
|
+
|
202
|
+
end
|
203
|
+
|
204
|
+
def max
|
205
|
+
max = 0
|
206
|
+
hash.each do |clave,valor|
|
207
|
+
if (valor > max)
|
208
|
+
max=valor
|
209
|
+
end
|
210
|
+
end
|
211
|
+
return max
|
212
|
+
end
|
213
|
+
|
214
|
+
def min
|
215
|
+
min = 9999
|
216
|
+
hash.each do |clave,valor|
|
217
|
+
if (valor < min)
|
218
|
+
min=valor
|
219
|
+
end
|
220
|
+
end
|
221
|
+
return min
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
frac1 = Fraccion.new(5,3)
|
226
|
+
frac2 = Fraccion.new(4,9)
|
227
|
+
m1 = MatrizDensa.new(3,3,[[1,2,0],[3,4,0],[0,2,3]])
|
228
|
+
m2 = MatrizDensa.new(3,3,[[7,10,5],[15,22,3],[2,3,4]])
|
229
|
+
m3 = MatrizDensa.new(3,3,[[frac1,frac2,frac1],[frac1,frac2,frac1],[frac2,frac2,frac1]])
|
230
|
+
m4 = MatrizDispersa.new(3,3,[[0,0,10],[5,0,0],[0,0,40]])
|
231
|
+
m5 = MatrizDispersa.new(3,3,[[0,0,4],[3,0,0],[0,0,2]])
|
232
|
+
m6 = MatrizDispersa.new(3,3,[[0,0,frac1],[frac2,0,0],[0,0,frac1]])
|
233
|
+
#puts 4-frac2 # Tiene que estar implementado el coerce para que funcione
|
234
|
+
#puts frac2-4
|
235
|
+
#puts 1+frac2
|
236
|
+
#puts m1.to_s
|
237
|
+
#puts m3.to_s
|
238
|
+
#puts m4.to_s
|
239
|
+
#puts m5.to_s
|
240
|
+
#puts m6.to_s
|
241
|
+
#print
|
242
|
+
=begin
|
243
|
+
puts " Matrices Densas "
|
244
|
+
puts " M1 "
|
245
|
+
puts m1.to_s
|
246
|
+
puts " M2 "
|
247
|
+
puts m2.to_s
|
248
|
+
puts " M3 "
|
249
|
+
puts m3.to_s
|
250
|
+
puts " (M1+M3)"
|
251
|
+
puts m1+m3
|
252
|
+
puts " (M1-M3)"
|
253
|
+
puts m1-m3
|
254
|
+
puts " (M1*M3)"
|
255
|
+
puts m1*m3
|
256
|
+
puts " Matrices Dispersas "
|
257
|
+
puts " M4 "
|
258
|
+
m4.to_s
|
259
|
+
puts " M5 "
|
260
|
+
m5.to_s
|
261
|
+
puts
|
262
|
+
puts " (M4+M5)"
|
263
|
+
puts m4+m5 # Matriz Dispersa - Matriz Dispersa = Matriz Dispersa
|
264
|
+
puts
|
265
|
+
puts " (M4-M5)"
|
266
|
+
puts m4-m5 # Matriz Dispersa + Matriz Dispersa = Matriz Dispersa
|
267
|
+
puts " El valor maximo de la matriz M2(densa) es #{m2.max}"
|
268
|
+
puts " El valor minimo de la matriz M2(densa) es #{m2.min}"
|
269
|
+
puts " El valor maximo de la matriz M4(dispersa) es #{m4.max}"
|
270
|
+
puts " El valor minimo de la matriz M4(dispersa) es #{m4.min}"
|
271
|
+
=end
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
|
292
|
+
|
293
|
+
|
data/lib/racional.rb
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
require "./lib/gcd.rb"
|
2
|
+
class Fraccion
|
3
|
+
|
4
|
+
include Comparable # Se incluye el modulo de nombre "comparable" utilizado para el operador <=>.
|
5
|
+
|
6
|
+
attr_reader :num, :denom # metodos de acceso (getter)
|
7
|
+
|
8
|
+
def initialize(num,denom)
|
9
|
+
|
10
|
+
# Se encarga de comprobar que los datos sean numericos, si no lo son lanza una excepcion y sale del programa
|
11
|
+
|
12
|
+
raise ArgumentError, 'El argumento no es numerico' unless num.is_a? Numeric
|
13
|
+
raise ArgumentError, 'El argumento no es numerico' unless denom.is_a? Numeric
|
14
|
+
if (denom == 0)
|
15
|
+
raise ArgumentError, 'El argumento no puede valer 0' unless (denom !=0)
|
16
|
+
end
|
17
|
+
aux = gcd(num,denom)
|
18
|
+
@num = num/aux # numerador en su forma simplificada
|
19
|
+
@denom= denom/aux # denominador en su forma simplificada
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
|
24
|
+
# metodo que muestra la fraccion con formato de salida: a/b
|
25
|
+
|
26
|
+
def to_s
|
27
|
+
"#{@num}/#{@denom}"
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
# metodo que calcula la suma de dos fracciones
|
32
|
+
|
33
|
+
def +(other)
|
34
|
+
if other.is_a? Fraccion
|
35
|
+
if (@denom == other.denom) #si los denominadores son iguales
|
36
|
+
Fraccion.new(@num + other.num, @denom)
|
37
|
+
else #si los denominadores son distintos
|
38
|
+
aux3 = ((@denom/gcd(@denom,other.denom)) * other.denom) # Calculo del mcm usando la propiedad (a*b/mcd(a,b))
|
39
|
+
aux = ((aux3/@denom) * @num) # calculo del numerador de la primera fraccion
|
40
|
+
aux2 = ((aux3/other.denom) * other.num) # calculo del denominador de la segunda fraccion
|
41
|
+
Fraccion.new((aux+aux2),aux3)
|
42
|
+
end
|
43
|
+
else
|
44
|
+
tmp = Fraccion.new(other,1)
|
45
|
+
if (@denom == tmp.denom) #si los denominadores son iguales
|
46
|
+
Fraccion.new(@num + other.num, @denom)
|
47
|
+
else #si los denominadores son distintos
|
48
|
+
aux3 = ((@denom/gcd(@denom,tmp.denom)) * tmp.denom) # Calculo del mcm usando la propiedad (a*b/mcd(a,b))
|
49
|
+
aux = ((aux3/@denom) * @num) # calculo del numerador de la primera fraccion
|
50
|
+
aux2 = ((aux3/tmp.denom) * tmp.num) # calculo del denominador de la segunda fraccion
|
51
|
+
Fraccion.new((aux+aux2),aux3)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
# metodo que calcula la resta de dos fracciones
|
57
|
+
|
58
|
+
def -(other)
|
59
|
+
if other.is_a? Fraccion
|
60
|
+
if (@denom == other.denom) #si los denominadores son iguales
|
61
|
+
Fraccion.new(@num - other.num, @denom)
|
62
|
+
else # si los denominadores son distintos
|
63
|
+
aux3 = ((@denom/gcd(@denom,other.denom)) * other.denom) # Calculo del mcm usando la propiedad (a*b/mcd(a,b))
|
64
|
+
aux = ((aux3/@denom) * @num) # calculo del numerador de la primera fraccion
|
65
|
+
aux2 = ((aux3/other.denom) * other.num) # calculo del denominador de la segunda fraccion
|
66
|
+
Fraccion.new((aux-aux2),aux3)
|
67
|
+
end
|
68
|
+
else
|
69
|
+
tmp = Fraccion.new(other,1)
|
70
|
+
if (@denom == tmp.denom) #si los denominadores son iguales
|
71
|
+
Fraccion.new(@num + other.num, @denom)
|
72
|
+
else #si los denominadores son distintos
|
73
|
+
aux3 = ((@denom/gcd(@denom,tmp.denom)) * tmp.denom) # Calculo del mcm usando la propiedad (a*b/mcd(a,b))
|
74
|
+
aux = ((aux3/@denom) * @num) # calculo del numerador de la primera fraccion
|
75
|
+
aux2 = ((aux3/tmp.denom) * tmp.num) # calculo del denominador de la segunda fraccion
|
76
|
+
Fraccion.new((aux-aux2),aux3)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
# metodo que calcula el producto de dos fracciones
|
82
|
+
|
83
|
+
def *(other)
|
84
|
+
Fraccion.new(@num*other.num,@denom*other.denom)
|
85
|
+
end
|
86
|
+
|
87
|
+
# metodo que calcula la division de dos fracciones
|
88
|
+
|
89
|
+
def /(other)
|
90
|
+
Fraccion.new(@num*other.denom,@denom*other.num)
|
91
|
+
end
|
92
|
+
|
93
|
+
# método que contempla el uso del modulo comparable <, >, <= y >=
|
94
|
+
|
95
|
+
def <=>(other)
|
96
|
+
return nil unless other.instance_of? Fraccion
|
97
|
+
# self.to_f <=> other.to_f
|
98
|
+
@num.to_f/@denom <=> other.num.to_f/other.denom
|
99
|
+
end
|
100
|
+
|
101
|
+
def coerce(other)
|
102
|
+
[Fraccion.new(other,1),self]
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
106
|
+
|
107
|
+
=begin
|
108
|
+
def coerce(other)
|
109
|
+
return [self,other]
|
110
|
+
end
|
111
|
+
=end
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
|
116
|
+
|
data/spec/matriz_spec.rb
ADDED
@@ -0,0 +1,160 @@
|
|
1
|
+
require "./lib/matriz.rb"
|
2
|
+
#require "matriz.rb"
|
3
|
+
|
4
|
+
describe Matriz do
|
5
|
+
before :each do
|
6
|
+
@m1 = Matriz.new(3,3)
|
7
|
+
@m1densa = MatrizDensa.new(3,3,[[1,2,0],[3,4,0],[0,2,3]])
|
8
|
+
@m2densa = MatrizDensa.new(3,3,[[7,10,5],[15,22,3],[2,3,4]])
|
9
|
+
frac1 = Fraccion.new(1,3)
|
10
|
+
frac2 = Fraccion.new(1,4)
|
11
|
+
@m3densa = MatrizDensa.new(3,3,[[frac1,frac2,frac1],[frac1,frac2,frac1],[frac2,frac2,frac1]])
|
12
|
+
@m4dispersa = MatrizDispersa.new(3,3,[[0,0,3],[5,0,0],[0,0,2]])
|
13
|
+
@m5dispersa = MatrizDispersa.new(3,3,[[0,0,4],[3,0,0],[0,0,2]])
|
14
|
+
@m6dispersa = MatrizDispersa.new(3,3,[[0,0,frac1],[frac2,0,0],[0,0,frac1]])
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "Se comprueba el numero de:" do
|
18
|
+
it "filas de la matriz" do
|
19
|
+
@m1.nfil.should eq(3)
|
20
|
+
end
|
21
|
+
it "columnas de la matriz" do
|
22
|
+
@m1.ncol.should eq(3)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
|
28
|
+
describe "se debe comprobar si la " do
|
29
|
+
it " matriz es densa" do
|
30
|
+
(@m1densa.instance_of? MatrizDensa).should be_true
|
31
|
+
end
|
32
|
+
it "matriz dispersa" do
|
33
|
+
(@m6dispersa.instance_of? MatrizDispersa).should be_true
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
describe "se debe comprobar si la matriz ;" do
|
40
|
+
it " densa accede al subindice [0,0]" do
|
41
|
+
@m1densa.mat[0][0].should eq(1)
|
42
|
+
end
|
43
|
+
it "densa accede al subindice [0,1]" do
|
44
|
+
@m1densa.mat[0][1].should eq(2)
|
45
|
+
end
|
46
|
+
it "dispersa accede al subindice[1,0]" do
|
47
|
+
@m4dispersa.mat[1][0].should eq(5)
|
48
|
+
end
|
49
|
+
it "dispersa accede al subindice[2,2]" do
|
50
|
+
@m4dispersa.mat[2][2].should eq(2)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe "se debe mostrar la " do
|
55
|
+
|
56
|
+
it " matriz densa con enteros" do
|
57
|
+
@m2densa.to_s.should eq(" [ 7 10 5 ]\n [ 15 22 3 ]\n [ 2 3 4 ]\n ")
|
58
|
+
end
|
59
|
+
it " matriz densa con racionales" do
|
60
|
+
@m3densa.to_s.should eq(" [ 1/3 1/4 1/3 ]\n [ 1/3 1/4 1/3 ]\n [ 1/4 1/4 1/3 ]\n ")
|
61
|
+
end
|
62
|
+
it "matriz dispersa con enteros" do
|
63
|
+
@m4dispersa.to_s.should eq ("{\"[0][2]\"=>3, \"[1][0]\"=>5, \"[2][2]\"=>2}")
|
64
|
+
end
|
65
|
+
|
66
|
+
it "matriz dispersa con racionales" do
|
67
|
+
@m6dispersa.to_s.should eq ("{\"[0][2]\"=>(1/3), \"[1][0]\"=>(1/4), \"[2][2]\"=>(1/3)}")
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
describe "se debe " do
|
73
|
+
|
74
|
+
it " sumar dos matrices densas con enteros" do
|
75
|
+
sum = @m1densa+@m2densa
|
76
|
+
sum.to_s.should eq(" [ 8 12 5 ]\n [ 18 26 3 ]\n [ 2 5 7 ]\n ")
|
77
|
+
end
|
78
|
+
it " restar una matriz densa de enteros con otra matriz densa de racionales" do
|
79
|
+
res = @m1densa-@m3densa
|
80
|
+
res.to_s.should eq(" [ 2/3 7/4 -1/3 ]\n [ 8/3 15/4 -1/3 ]\n [ -1/4 7/4 8/3 ]\n ")
|
81
|
+
end
|
82
|
+
|
83
|
+
it " multiplicar una matriz densa con otra matriz densa" do
|
84
|
+
mul = @m1densa*@m2densa
|
85
|
+
mul.to_s.should eq(" [ 37 54 11 ]\n [ 81 118 27 ]\n [ 36 53 18 ]\n ")
|
86
|
+
end
|
87
|
+
|
88
|
+
it "calcular el maximo de una matriz Densa " do
|
89
|
+
max = @m2densa.max
|
90
|
+
max.should eq 22
|
91
|
+
end
|
92
|
+
|
93
|
+
it "calcular el minimo de una matriz Densa" do
|
94
|
+
min = @m2densa.min
|
95
|
+
min.should eq 2
|
96
|
+
end
|
97
|
+
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
|
104
|
+
describe "se debe " do
|
105
|
+
|
106
|
+
it " sumar dos matrices dispersas con enteros " do
|
107
|
+
sum = @m4dispersa+@m5dispersa
|
108
|
+
sum.to_s.should eq("{\"[0][2]\"=>7, \"[1][0]\"=>8, \"[2][2]\"=>4}")
|
109
|
+
|
110
|
+
end
|
111
|
+
|
112
|
+
it " restar dos matrices dispersas con enteros " do
|
113
|
+
res = @m4dispersa-@m5dispersa
|
114
|
+
res.to_s.should eq("{\"[0][2]\"=>-1, \"[1][0]\"=>2, \"[2][2]\"=>0}")
|
115
|
+
end
|
116
|
+
|
117
|
+
it " sumar una matriz dispersa de enteros con otra matriz dispersa de racionales" do
|
118
|
+
sum = @m4dispersa+@m6dispersa
|
119
|
+
sum.to_s.should eq("{\"[0][2]\"=>(10/3), \"[1][0]\"=>(21/4), \"[2][2]\"=>(7/3)}")
|
120
|
+
end
|
121
|
+
|
122
|
+
it " restar una matriz dispersa de enteros con otra matriz dispersa de racionales" do
|
123
|
+
res = @m4dispersa-@m6dispersa
|
124
|
+
res.to_s.should eq("{\"[0][2]\"=>(8/3), \"[1][0]\"=>(19/4), \"[2][2]\"=>(5/3)}")
|
125
|
+
|
126
|
+
end
|
127
|
+
|
128
|
+
it "calcular el maximo de una matriz Dispersa " do
|
129
|
+
max = @m4dispersa.max
|
130
|
+
max.should eq 5
|
131
|
+
end
|
132
|
+
|
133
|
+
it "calcular el minimo de una matriz Dispersa" do
|
134
|
+
min = @m4dispersa.min
|
135
|
+
min.should eq 2
|
136
|
+
end
|
137
|
+
|
138
|
+
|
139
|
+
|
140
|
+
end
|
141
|
+
|
142
|
+
describe "se debe " do
|
143
|
+
|
144
|
+
it " sumar una matriz densa con una dispersa " do
|
145
|
+
sum = @m1densa+@m5dispersa
|
146
|
+
sum.to_s.should eq(" [ 1 2 4 ]\n [ 6 4 0 ]\n [ 0 2 5 ]\n ")
|
147
|
+
|
148
|
+
|
149
|
+
end
|
150
|
+
it " restar una matriz dispersa con una densa" do
|
151
|
+
res = @m5dispersa-@m1densa
|
152
|
+
res.to_s.should eq(" [ 1 2 -4 ]\n [ 0 4 0 ]\n [ 0 2 1 ]\n ")
|
153
|
+
end
|
154
|
+
|
155
|
+
end
|
156
|
+
|
157
|
+
end
|
158
|
+
|
159
|
+
|
160
|
+
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,147 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: GemaMatriz
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rushil Lakhani Lakhani & Waldo Nazco Torres
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-11-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: guard
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: guard-rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: guard-bundler
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: Construcción de una gema ruby para la creación de matrices dispersas
|
98
|
+
y densas
|
99
|
+
email:
|
100
|
+
- alu0100537031@ull.edu.es
|
101
|
+
executables: []
|
102
|
+
extensions: []
|
103
|
+
extra_rdoc_files: []
|
104
|
+
files:
|
105
|
+
- .gitignore
|
106
|
+
- .rspec
|
107
|
+
- .travis.yml
|
108
|
+
- GemaMatriz.gemspec
|
109
|
+
- Gemfile
|
110
|
+
- Guardfile
|
111
|
+
- LICENSE.txt
|
112
|
+
- README.md
|
113
|
+
- Rakefile
|
114
|
+
- lib/GemaMatriz.rb
|
115
|
+
- lib/GemaMatriz/version.rb
|
116
|
+
- lib/gcd.rb
|
117
|
+
- lib/matriz.rb
|
118
|
+
- lib/racional.rb
|
119
|
+
- spec/matriz_spec.rb
|
120
|
+
- spec/spec_helper.rb
|
121
|
+
homepage: https://github.com/alu0100537031/prct09
|
122
|
+
licenses:
|
123
|
+
- MIT
|
124
|
+
metadata: {}
|
125
|
+
post_install_message:
|
126
|
+
rdoc_options: []
|
127
|
+
require_paths:
|
128
|
+
- lib
|
129
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - '>='
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - '>='
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
requirements: []
|
140
|
+
rubyforge_project:
|
141
|
+
rubygems_version: 2.0.3
|
142
|
+
signing_key:
|
143
|
+
specification_version: 4
|
144
|
+
summary: Practica 9
|
145
|
+
test_files:
|
146
|
+
- spec/matriz_spec.rb
|
147
|
+
- spec/spec_helper.rb
|