matrices_p9 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.
- data/.gitignore +27 -0
- data/.travis.yml +8 -0
- data/Gemfile +8 -0
- data/Guardfile +8 -0
- data/LICENSE.txt +22 -0
- data/README.md +12 -0
- data/README.md~ +29 -0
- data/Rakefile +17 -0
- data/lib/fraccion.rb +227 -0
- data/lib/matrices_p9/version.rb +3 -0
- data/lib/matrices_p9.rb +359 -0
- data/matrices_p9.gemspec +21 -0
- data/spec/matrices_p9_spec.rb +123 -0
- data/test/tc_MatrizDensa.rb +26 -0
- data/test/tc_MatrizDispersa.rb +16 -0
- metadata +79 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Luis Orta
|
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,12 @@
|
|
1
|
+
# MatricesP9
|
2
|
+
|
3
|
+
La clase matriz se implementará mediante una clase abstracta, que contendrá los métodos básicos como el numero de filas y columnas de la matriz. Esta será la clase padre.
|
4
|
+
|
5
|
+
De esta clase derivarán las clases Matriz_Densa y Matriz_Dispersa.
|
6
|
+
|
7
|
+
La clase Matriz_Densa será la desarrollada en la práctica anterior.
|
8
|
+
|
9
|
+
La clase Matriz_Dispersa será una nueva clase desarrollada para aquellas matrices con más de un 60 % de ceros y se tratarán de manera distinta que las densas. Su estructura no será una matriz de elementos (dos arrays anidados con todas las posiciones ocupadas) sino la mezcla de un array de posiciones y un array de elementos, lo que reducirá la cantidad de memoria utilizada para almacenar los elementos distintos de cero. Tendrá su propio metodo de suma, resta y multiplicación, además de ser comparable (Modulo comparable).
|
10
|
+
|
11
|
+
|
12
|
+
|
data/README.md~
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# MatricesP9
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'matrices_p9'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install matrices_p9
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
$:.unshift File.dirname(__FILE__) + 'lib'
|
2
|
+
require "bundler/gem_tasks"
|
3
|
+
require 'rspec/core/rake_task'
|
4
|
+
|
5
|
+
RSpec::Core::RakeTask.new
|
6
|
+
task :default => :spec
|
7
|
+
|
8
|
+
desc "Run spec"
|
9
|
+
task :spec do
|
10
|
+
sh "cd spec \n rspec -I. matrices_spec.rb"
|
11
|
+
end
|
12
|
+
|
13
|
+
desc "Run UnitTest"
|
14
|
+
task :test do
|
15
|
+
sh "ruby -Ilib -Itest test/tc_MatrizDispersa.rb \n ruby -Ilib -Itest test/tc_MatrizDensa.rb"
|
16
|
+
end
|
17
|
+
|
data/lib/fraccion.rb
ADDED
@@ -0,0 +1,227 @@
|
|
1
|
+
|
2
|
+
#require "./gcd.rb"
|
3
|
+
#Programa que trabaja con fracciones
|
4
|
+
#Adriana Rolo Inchausti, Luis Antonio Orta Mendes
|
5
|
+
|
6
|
+
class Fraccion
|
7
|
+
attr_reader :denominador, :numerador
|
8
|
+
|
9
|
+
def initialize(num, denomin)
|
10
|
+
#atributo
|
11
|
+
mcd=gcd(num,denomin)
|
12
|
+
|
13
|
+
@numerador=num/mcd
|
14
|
+
@denominador=denomin/mcd
|
15
|
+
end
|
16
|
+
|
17
|
+
#Maximo comun divisor, funcion usada para simplificar las matrices.
|
18
|
+
def gcd(u, v)
|
19
|
+
u, v = u.abs, v.abs
|
20
|
+
while v != 0
|
21
|
+
u, v = v, u % v
|
22
|
+
end
|
23
|
+
u
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
# Metodos getter de Numerador y denominador
|
28
|
+
|
29
|
+
def num
|
30
|
+
return @numerador
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
def denom
|
35
|
+
return @denominador
|
36
|
+
end
|
37
|
+
|
38
|
+
#Metodo que devuelve la fraccion en forma de string de la manera a/b
|
39
|
+
def to_s
|
40
|
+
"#{@numerador}/#{@denominador}"
|
41
|
+
end
|
42
|
+
|
43
|
+
#Metodo que devuelve el numero decimal que representa la matriz
|
44
|
+
def to_f
|
45
|
+
a=@numerador.to_f
|
46
|
+
b=@denominador.to_f
|
47
|
+
a/b
|
48
|
+
end
|
49
|
+
#Sobrecarga del operador de comparacion, devuelve true si son iguale y false en caso contrario
|
50
|
+
def == (otro)
|
51
|
+
if (@numerador==otro.numerador) && (@denominador == otro.denominador)
|
52
|
+
return true
|
53
|
+
else
|
54
|
+
return false
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
#Metodo para calcular el valor absoluto de la fraccion (numerador y denominador positivo)
|
59
|
+
def abs
|
60
|
+
if @numerador < 0
|
61
|
+
@numerador = @numerador*-1
|
62
|
+
end
|
63
|
+
if @denominador < 0
|
64
|
+
@denominador = @denominador*-1
|
65
|
+
end
|
66
|
+
return self
|
67
|
+
end
|
68
|
+
|
69
|
+
#Metodo que devuelve la matriz invertida de la forma que si al comienzo era a/b, devolveria b/a
|
70
|
+
def reciprocal
|
71
|
+
aux= @numerador
|
72
|
+
@numerador = @denominador
|
73
|
+
@denominador = aux
|
74
|
+
end
|
75
|
+
|
76
|
+
#Metodo que devuelve la fraccion con el signo contrario al que tenia incialmente
|
77
|
+
def opuesto
|
78
|
+
@numerador = -1*@numerador
|
79
|
+
return self
|
80
|
+
end
|
81
|
+
|
82
|
+
#Calcula el Minimo comun multiplo, se usa para realizar la suma y la resta
|
83
|
+
def mcm(a,b)
|
84
|
+
(a*b)/gcd(a,b)
|
85
|
+
end
|
86
|
+
#Sobrecarga del operador de suma, recibe como parametros dos fracciones y devuelve una fraccion con el resultado de la suma
|
87
|
+
def +(other)
|
88
|
+
if(other.instance_of?Fixnum)
|
89
|
+
temp=other
|
90
|
+
other=Fraccion.new(temp,1)
|
91
|
+
end
|
92
|
+
if (@denominador == other.denominador)
|
93
|
+
nume=@numerador + other.numerador
|
94
|
+
deno=@denominador
|
95
|
+
mcd=gcd(nume,deno)
|
96
|
+
else
|
97
|
+
aux= @denominador * other.denominador
|
98
|
+
nume = ((aux / @denominador) * @numerador) + ((aux / other.denominador) * other.numerador)
|
99
|
+
deno = @denominador*other.denominador
|
100
|
+
mcd=gcd(nume,deno)
|
101
|
+
end
|
102
|
+
|
103
|
+
temp = Fraccion.new(nume/mcd, deno/mcd)
|
104
|
+
end
|
105
|
+
|
106
|
+
|
107
|
+
#Sobrecarga del operador de resta, recibe como parametros dos francciones y devuelve una fraccion con el resultado de la resta
|
108
|
+
def -(other)
|
109
|
+
if(other.instance_of?Fixnum)
|
110
|
+
temp=other
|
111
|
+
other=Fraccion.new(temp,1)
|
112
|
+
end
|
113
|
+
if (@denominador == other.denominador )
|
114
|
+
nume=@numerador - other.numerador
|
115
|
+
deno=@denominador
|
116
|
+
mcd=gcd(nume,deno)
|
117
|
+
|
118
|
+
else
|
119
|
+
aux= @denominador * other.denominador
|
120
|
+
nume = ((aux / @denominador) * @numerador) - ((aux / other.denominador ) * other.numerador)
|
121
|
+
deno=@denominador*other.denominador
|
122
|
+
mcd=gcd(nume,deno)
|
123
|
+
|
124
|
+
end
|
125
|
+
|
126
|
+
|
127
|
+
temp = Fraccion.new(nume/mcd, deno/mcd)
|
128
|
+
end
|
129
|
+
|
130
|
+
#Sobrecarga del operador de multiplicación, recibe como parametros dos francciones y devuelve una fraccion con el resultado de la multiplicación
|
131
|
+
def * (other)
|
132
|
+
nume = @numerador * other.numerador
|
133
|
+
deno = @denominador * other.denominador
|
134
|
+
mcd=gcd(nume,deno)
|
135
|
+
temp = Fraccion.new(nume / mcd ,deno / mcd )
|
136
|
+
|
137
|
+
end
|
138
|
+
|
139
|
+
#Sobrecarga del operador de division, recibe como parametros dos francciones y devuelve una fraccion con el resultado de la division
|
140
|
+
def / (other)
|
141
|
+
|
142
|
+
nume = @numerador *other.denominador
|
143
|
+
deno = @denominador * other.numerador
|
144
|
+
mcd= gcd(nume,deno)
|
145
|
+
temp = Fraccion.new(nume/mcd, deno/mcd )
|
146
|
+
|
147
|
+
end
|
148
|
+
|
149
|
+
#Sobrecarga del operador de modulo, recibe como parametros dos francciones y devuelve el resto de la division de las mismas
|
150
|
+
def % (other)
|
151
|
+
aux=Fraccion.new(1,1)
|
152
|
+
aux=self/other
|
153
|
+
aux.numerador%aux.denominador
|
154
|
+
end
|
155
|
+
|
156
|
+
|
157
|
+
#Sobrecarga de operadores de comparación devuelve true en caso de que sea cierto y falso en caso contrario
|
158
|
+
#Mayor que
|
159
|
+
def > (otro)
|
160
|
+
if (@numerador/@denominador > otro.numerador/otro.denominador)
|
161
|
+
return true
|
162
|
+
else
|
163
|
+
return false
|
164
|
+
end
|
165
|
+
|
166
|
+
end
|
167
|
+
|
168
|
+
#menor que
|
169
|
+
def < (otro)
|
170
|
+
if (@numerador/@denominador < otro.numerador/otro.denominador)
|
171
|
+
return true
|
172
|
+
else
|
173
|
+
return false
|
174
|
+
end
|
175
|
+
|
176
|
+
end
|
177
|
+
#mayor igual que
|
178
|
+
def >= (otro)
|
179
|
+
if (@numerador/@denominador >= otro.numerador/otro.denominador)
|
180
|
+
return true
|
181
|
+
else
|
182
|
+
return false
|
183
|
+
end
|
184
|
+
|
185
|
+
end
|
186
|
+
#menos igual que
|
187
|
+
def <= (otro)
|
188
|
+
if (@numerador/@denominador <= otro.numerador/otro.denominador)
|
189
|
+
return true
|
190
|
+
else
|
191
|
+
return false
|
192
|
+
end
|
193
|
+
|
194
|
+
end
|
195
|
+
#Coerce
|
196
|
+
def coerce(other)
|
197
|
+
[self, other]
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
|
data/lib/matrices_p9.rb
ADDED
@@ -0,0 +1,359 @@
|
|
1
|
+
require "fraccion.rb"
|
2
|
+
|
3
|
+
class Matriz
|
4
|
+
attr_accessor :fil, :col
|
5
|
+
include Comparable
|
6
|
+
|
7
|
+
def initialize(f, c)
|
8
|
+
#atributo
|
9
|
+
@fil=f.to_i; #Numero de filas
|
10
|
+
@col=c.to_i; #Numero de columnas
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
#Sobrecarga del operador de suma, recibe como parametros dos matrices y devuelve una matriz con el resultado de la suma de forma A+B= (Aij+Bij)
|
15
|
+
def +(other)
|
16
|
+
if(self.fil == other.fil and self.col == other.col)
|
17
|
+
# SELF Matrices densas
|
18
|
+
if self.instance_of?MatrizDensa
|
19
|
+
temp = MatrizDensa.new(self.fil, self.col, nil)
|
20
|
+
if other.instance_of?MatrizDensa
|
21
|
+
|
22
|
+
for i in (0...@fil.to_i)
|
23
|
+
for j in (0...@col.to_i)
|
24
|
+
temp.mat[i][j] = (self.mat[i][j]) + (other.mat[i][j])
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
if other.instance_of?MatrizDispersa
|
30
|
+
for i in (0...@fil.to_i)
|
31
|
+
for j in (0...@col.to_i)
|
32
|
+
encontrado = 0
|
33
|
+
for k in (0...other.posx.size)
|
34
|
+
if (i==other.posx[k] and j==other.posy[k] and encontrado==0)
|
35
|
+
temp.mat[i][j] = (self.mat[i][j]) + (other.valor[k])
|
36
|
+
encontrado = 1
|
37
|
+
end
|
38
|
+
end
|
39
|
+
if (encontrado == 0)
|
40
|
+
temp.mat[i][j] = self.mat[i][j]
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# SELF Matriz Dispersa
|
48
|
+
if self.instance_of?MatrizDispersa
|
49
|
+
if other.instance_of?MatrizDensa
|
50
|
+
temp = MatrizDensa.new(self.fil, self.col, nil)
|
51
|
+
for i in (0...@fil.to_i)
|
52
|
+
for j in (0...@col.to_i)
|
53
|
+
encontrado = 0
|
54
|
+
for k in (0...self.posx.size.to_i)
|
55
|
+
if (i==self.posx[k] and j==self.posy[k] and encontrado==0)
|
56
|
+
temp.mat[i][j] = (other.mat[i][j]) + (self.valor[k])
|
57
|
+
encontrado = 1
|
58
|
+
end
|
59
|
+
end
|
60
|
+
if (encontrado == 0)
|
61
|
+
temp.mat[i][j] = other.mat[i][j]
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
if other.instance_of?MatrizDispersa
|
69
|
+
temp = MatrizDispersa.new(self.fil,self.col,[],[],[])
|
70
|
+
temp.valor = self.valor
|
71
|
+
temp.posx = self.posx
|
72
|
+
temp.posy = self.posy
|
73
|
+
|
74
|
+
for j in (0...other.posx.size.to_i)
|
75
|
+
encontrado = false
|
76
|
+
for k in (0...self.posx.size.to_i)
|
77
|
+
if(other.posx[j] == temp.posx[k] and other.posy[j] == temp.posy[k])
|
78
|
+
temp.valor[k] = temp.valor[k] + other.valor[j]
|
79
|
+
encontrado = true
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
if (encontrado == false)
|
84
|
+
temp.posx << other.posx[j]
|
85
|
+
temp.posy << other.posy[j]
|
86
|
+
temp.valor << other.valor[j]
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
return temp
|
93
|
+
else
|
94
|
+
return nil
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def -(other)
|
99
|
+
if(self.fil == other.fil and self.col == other.col)
|
100
|
+
# SELF Matrices densas
|
101
|
+
if self.instance_of?MatrizDensa
|
102
|
+
temp = MatrizDensa.new(self.fil, self.col, nil)
|
103
|
+
if other.instance_of?MatrizDensa
|
104
|
+
for i in (0...@fil.to_i)
|
105
|
+
for j in (0...@col.to_i)
|
106
|
+
temp.mat[i][j] = (self.mat[i][j]) - (other.mat[i][j])
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
if other.instance_of?MatrizDispersa
|
112
|
+
for i in (0...@fil.to_i)
|
113
|
+
for j in (0...@col.to_i)
|
114
|
+
encontrado = 0
|
115
|
+
for k in (0...other.posx.size)
|
116
|
+
if (i==other.posx[k] and j==other.posy[k] and encontrado==0)
|
117
|
+
temp.mat[i][j] = (self.mat[i][j]) - (other.valor[k])
|
118
|
+
encontrado = 1
|
119
|
+
end
|
120
|
+
end
|
121
|
+
if (encontrado == 0)
|
122
|
+
temp.mat[i][j] = self.mat[i][j]
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
# SELF Matriz Dispersa
|
130
|
+
if self.instance_of?MatrizDispersa
|
131
|
+
if other.instance_of?MatrizDensa
|
132
|
+
temp = MatrizDensa.new(self.fil, self.col, nil)
|
133
|
+
for i in (0...@fil.to_i)
|
134
|
+
for j in (0...@col.to_i)
|
135
|
+
encontrado = 0
|
136
|
+
for k in (0...self.posx.size.to_i)
|
137
|
+
if (i==self.posx[k] and j==self.posy[k] and encontrado==0)
|
138
|
+
temp.mat[i][j] = (other.mat[i][j]) - (self.valor[k])
|
139
|
+
encontrado = 1
|
140
|
+
end
|
141
|
+
end
|
142
|
+
if (encontrado == 0)
|
143
|
+
temp.mat[i][j] = other.mat[i][j]
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
|
150
|
+
if other.instance_of?MatrizDispersa
|
151
|
+
temp = MatrizDispersa.new(self.fil,self.col,[],[],[])
|
152
|
+
temp.valor = self.valor
|
153
|
+
temp.posx = self.posx
|
154
|
+
temp.posy = self.posy
|
155
|
+
|
156
|
+
for j in (0...other.posx.size.to_i)
|
157
|
+
encontrado = false
|
158
|
+
for k in (0...self.posx.size.to_i)
|
159
|
+
if(other.posx[j] == temp.posx[k] and other.posy[j] == temp.posy[k])
|
160
|
+
temp.valor[k] = temp.valor[k] - other.valor[j]
|
161
|
+
encontrado = true
|
162
|
+
end
|
163
|
+
|
164
|
+
end
|
165
|
+
if (encontrado == false)
|
166
|
+
temp.posx << other.posx[j]
|
167
|
+
temp.posy << other.posy[j]
|
168
|
+
temp.valor << other.valor[j]
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
return temp
|
175
|
+
else
|
176
|
+
return nil
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
end
|
181
|
+
|
182
|
+
|
183
|
+
class MatrizDensa < Matriz
|
184
|
+
attr_accessor :mat
|
185
|
+
def initialize(f,c,e)
|
186
|
+
super(f,c)
|
187
|
+
@mat = Array.new(@fil.to_i){Array.new(@col.to_i)}
|
188
|
+
|
189
|
+
if (e != nil)
|
190
|
+
#Rellenamos la matriz con lo valores recibidos
|
191
|
+
for i in (0...@fil.to_i)
|
192
|
+
for j in (0...@col.to_i)
|
193
|
+
@mat[i][j]=e[i*@col.to_i+j];
|
194
|
+
end
|
195
|
+
end
|
196
|
+
end
|
197
|
+
end
|
198
|
+
# Metodos getter devuelve el valor de una posicion determinada
|
199
|
+
|
200
|
+
def pos(a,b)
|
201
|
+
@mat[a][b]
|
202
|
+
end
|
203
|
+
|
204
|
+
#Metodo que devuelve la matriz en forma de string
|
205
|
+
def to_s
|
206
|
+
"#{@mat}"
|
207
|
+
end
|
208
|
+
|
209
|
+
def max
|
210
|
+
m = self.mat[0][0]
|
211
|
+
for i in (0...@fil.to_i)
|
212
|
+
for j in (0...@col.to_i)
|
213
|
+
if (self.mat[i][j] > m)
|
214
|
+
m = self.mat[i][j]
|
215
|
+
end
|
216
|
+
end
|
217
|
+
end
|
218
|
+
return m
|
219
|
+
end
|
220
|
+
|
221
|
+
def min
|
222
|
+
m = self.mat[0][0]
|
223
|
+
for i in (0...@fil.to_i)
|
224
|
+
for j in (0...@col.to_i)
|
225
|
+
if (self.mat[i][j] < m)
|
226
|
+
m = self.mat[i][j]
|
227
|
+
end
|
228
|
+
end
|
229
|
+
end
|
230
|
+
return m
|
231
|
+
end
|
232
|
+
|
233
|
+
#Operador de comparacion (Modulo comparable). se comparan por la suma de sus componentes ¡¡¡¡¡¡PONERLO EN LA CLASE MADRE!!!!!!
|
234
|
+
|
235
|
+
def <=> (other)
|
236
|
+
return nil unless other.is_a?MatrizDensa
|
237
|
+
c1=0
|
238
|
+
c2=0
|
239
|
+
for i in (0...@fil.to_i)
|
240
|
+
for j in (0...@col.to_i)
|
241
|
+
if(self.mat[i][j] > other.mat[i][j])
|
242
|
+
c1+=1
|
243
|
+
elsif(self.mat[i][j] < other.mat[i][j])
|
244
|
+
c2+=1
|
245
|
+
end
|
246
|
+
end
|
247
|
+
end
|
248
|
+
(c1)<=>(c2)
|
249
|
+
end
|
250
|
+
|
251
|
+
end
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
|
256
|
+
class MatrizDispersa < Matriz
|
257
|
+
attr_accessor :posx, :posy, :valor
|
258
|
+
def initialize(f,c,posx, posy, valor)
|
259
|
+
super(f,c)
|
260
|
+
@posx = posx
|
261
|
+
@posy = posy
|
262
|
+
@valor = valor
|
263
|
+
|
264
|
+
end
|
265
|
+
|
266
|
+
def to_s
|
267
|
+
s=String.new
|
268
|
+
s << "["
|
269
|
+
for i in (0...@fil.to_i)
|
270
|
+
s << "[#{posx[i]},#{posy[i]},#{valor[i]}]"
|
271
|
+
end
|
272
|
+
s << "]"
|
273
|
+
end
|
274
|
+
|
275
|
+
def max
|
276
|
+
m = self.valor[0]
|
277
|
+
for i in (0...self.valor.size.to_i)
|
278
|
+
if (self.valor[i]> m)
|
279
|
+
m = self.valor[i]
|
280
|
+
end
|
281
|
+
end
|
282
|
+
return m
|
283
|
+
end
|
284
|
+
|
285
|
+
def min
|
286
|
+
m = self.valor[0]
|
287
|
+
for i in (0...self.valor.size.to_i)
|
288
|
+
if (self.valor[i]< m)
|
289
|
+
m = self.valor[i]
|
290
|
+
end
|
291
|
+
end
|
292
|
+
return m
|
293
|
+
end
|
294
|
+
|
295
|
+
def pos(a,b)
|
296
|
+
for i in (0...self.posx.size)
|
297
|
+
if(posx[i]==a and posy[i]==b)
|
298
|
+
return valor[i]
|
299
|
+
end
|
300
|
+
end
|
301
|
+
return nil
|
302
|
+
end
|
303
|
+
|
304
|
+
end
|
305
|
+
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
=begin
|
318
|
+
a=MatrizDensa.new(3,3,[1,2,3,4,5,6,7,8,9])
|
319
|
+
b=MatrizDensa.new(3,3,[1,2,3,4,5,6,7,8,9])
|
320
|
+
puts "#{(a+b).to_s}"
|
321
|
+
puts "#{(a-b).to_s}"
|
322
|
+
|
323
|
+
|
324
|
+
a=MatrizDensa.new(3,3,[1,2,3,4,5,6,7,8,9])
|
325
|
+
b=MatrizDispersa.new(3,3,[0,1,2],[0,1,2],[1,2,3])
|
326
|
+
b=(b+b)
|
327
|
+
puts "#{(b.valor).to_s}"
|
328
|
+
|
329
|
+
|
330
|
+
a=MatrizDensa.new(3,3,[1,2,3,34,5,6,7,8,9])
|
331
|
+
b=MatrizDispersa.new(3,3,[0,1,2],[0,1,2],[1,0,3])
|
332
|
+
puts "#{a.max}"
|
333
|
+
puts "#{b.max}"
|
334
|
+
puts "#{a.min}"
|
335
|
+
puts "#{b.min}"
|
336
|
+
|
337
|
+
b=MatrizDispersa.new(3,3,[0,1,2],[0,1,2],[1,2,3])
|
338
|
+
c=MatrizDispersa.new(3,3,[0,1,2],[0,2,2],[1,2,3])
|
339
|
+
b=(b+c)
|
340
|
+
puts "#{b.valor.to_s}"
|
341
|
+
|
342
|
+
a=MatrizDensa.new(3,3,[1,2,3,4,5,6,7,8,9])
|
343
|
+
b=MatrizDensa.new(3,3,[1,2,3,4,5,6,7,8,9])
|
344
|
+
if(a==b)
|
345
|
+
puts "igual"
|
346
|
+
end
|
347
|
+
if (a>=b)
|
348
|
+
puts "mayor"
|
349
|
+
end
|
350
|
+
|
351
|
+
b=MatrizDispersa.new(3,3,[0,1,2],[0,1,2],[1,2,3])
|
352
|
+
c=MatrizDispersa.new(3,3,[0,1,2],[0,2,2],[1,2,3])
|
353
|
+
b=(b+c)
|
354
|
+
puts "#{b.to_s}"
|
355
|
+
|
356
|
+
|
357
|
+
=end
|
358
|
+
|
359
|
+
|
data/matrices_p9.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'matrices_p9/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "matrices_p9"
|
8
|
+
gem.version = MatricesP9::VERSION
|
9
|
+
gem.authors = ["Luis Orta, Adriana Rolo"]
|
10
|
+
gem.email = ["alu0100611526@ull.edu.es, alu0100658712@ull.edu.es"]
|
11
|
+
gem.description = %q{Gema para realizar suma entre matrices densas y matrices dispersas}
|
12
|
+
gem.summary = %q{matrices densas y dispersas}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_development_dependency 'rspec', '~>2.11'
|
21
|
+
end
|
@@ -0,0 +1,123 @@
|
|
1
|
+
require 'matrices_p9'
|
2
|
+
|
3
|
+
describe Matriz do
|
4
|
+
before :each do
|
5
|
+
#Matrices densas
|
6
|
+
@m1 = MatrizDensa.new(3,3,[1,2,3,4,5,6,7,8,9])
|
7
|
+
@m2 = MatrizDensa.new(3,3,[1,2,3,4,5,6,7,8,9])
|
8
|
+
|
9
|
+
#Matrices dispersas
|
10
|
+
@m3 = MatrizDispersa.new(3,3,[0,1,2],[0,1,2],[1,2,3])
|
11
|
+
@m4 = MatrizDispersa.new(3,3,[0,1,2],[0,1,2],[1,2,3])
|
12
|
+
|
13
|
+
#Matrices densas con fracciones
|
14
|
+
@m5 = MatrizDensa.new(2,2,[Fraccion.new(1,2),Fraccion.new(1,3),Fraccion.new(1,4),Fraccion.new(1,5)])
|
15
|
+
@m6 = MatrizDensa.new(2,2,[Fraccion.new(1,2),Fraccion.new(2,3),Fraccion.new(3,4),Fraccion.new(4,5)])
|
16
|
+
end
|
17
|
+
|
18
|
+
#Initialize
|
19
|
+
describe "#Inicializar la matriz" do
|
20
|
+
it "Se usa pos para acceder a los elementos" do
|
21
|
+
@m1.pos(0,2).should eq(3)
|
22
|
+
@m1.pos(2,2).should eq(9)
|
23
|
+
@m2.pos(2,1).should eq(8)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
=begin
|
27
|
+
#Operaciones básicas
|
28
|
+
describe "#Operaciones elementales" do
|
29
|
+
|
30
|
+
it "Se invoca a to_s" do
|
31
|
+
@m1.to_s.should eq("[[1, 2, 3], [4, 5, 6], [7, 8, 9]]")
|
32
|
+
end
|
33
|
+
|
34
|
+
it "Se invoca a suma" do
|
35
|
+
|
36
|
+
(@m1+@m2).to_s().should eq("[[2, 4, 6], [8, 10, 12], [14, 16, 18]]")
|
37
|
+
end
|
38
|
+
|
39
|
+
it "Se invoca a resta" do
|
40
|
+
(@m1-@m2).to_s().should eq("[[0, 0, 0], [0, 0, 0], [0, 0, 0]]")
|
41
|
+
end
|
42
|
+
|
43
|
+
it "Se invoca a multiplicacion" do
|
44
|
+
(@m1*@m2).to_s().should eq("[[30, 36, 42], [66, 81, 96], [102, 126, 150]]")
|
45
|
+
end
|
46
|
+
|
47
|
+
it "Suma de elementos" do
|
48
|
+
@m1.suma().should eq(45)
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
#Comparaciones
|
54
|
+
describe "#Operadores de comparacion" do
|
55
|
+
it "Mayor que" do
|
56
|
+
(@m1>@m2).should eq(false)
|
57
|
+
end
|
58
|
+
|
59
|
+
it "Menor que" do
|
60
|
+
(@m1<@m2).should eq(false)
|
61
|
+
end
|
62
|
+
|
63
|
+
it "Mayor igual que" do
|
64
|
+
(@m1>=@m2).should eq(true)
|
65
|
+
end
|
66
|
+
|
67
|
+
it "Menor igual que" do
|
68
|
+
(@m1<=@m2).should eq(true)
|
69
|
+
end
|
70
|
+
|
71
|
+
it "Igual que" do
|
72
|
+
(@m1==@m2).should eq(true)
|
73
|
+
end
|
74
|
+
|
75
|
+
it "Distinto de" do
|
76
|
+
(@m1!=@m2).should eq(false)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
=end
|
80
|
+
#Modificacion
|
81
|
+
describe "#Operadores de comparacion" do
|
82
|
+
it "Suma fracciones" do
|
83
|
+
(@m5+@m6).to_s.should eq("[[1/1, 1/1], [1/1, 1/1]]")
|
84
|
+
end
|
85
|
+
|
86
|
+
=begin it "Multiplicacion de fracciones" do
|
87
|
+
(@m5*@m6).to_s.should eq("[[1/2, 3/5], [11/40, 49/150]]")
|
88
|
+
end
|
89
|
+
=end
|
90
|
+
end
|
91
|
+
|
92
|
+
#creacion de matrices dispersas
|
93
|
+
describe "#creando una matriz dispersa" do
|
94
|
+
it "Comprobando que se creo correctamente." do
|
95
|
+
@m3.pos(0,0).should eq(1)
|
96
|
+
@m3.pos(1,1).should eq(2)
|
97
|
+
@m3.pos(2,2).should eq(3)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
#Prueba de matrices densas y dispersas.
|
104
|
+
describe "#Operaciones de suma entre matrices densas y dispersas." do
|
105
|
+
it "Suma de matrices dispersas" do
|
106
|
+
(@m3+@m4).to_s().should eq("[[0,0,2][1,1,4][2,2,6]]")
|
107
|
+
end
|
108
|
+
|
109
|
+
it "Suma de matrices densas y dispersas" do
|
110
|
+
(@m1+@m3).to_s().should eq("[[2, 2, 3], [4, 7, 6], [7, 8, 12]]")
|
111
|
+
end
|
112
|
+
|
113
|
+
it "Suma de matrices dispersas y densas " do
|
114
|
+
(@m3+@m1).to_s().should eq("[[2, 2, 3], [4, 7, 6], [7, 8, 12]]")
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# Pruebas unitarias de la clase matriz densa
|
2
|
+
|
3
|
+
require 'fraccion'
|
4
|
+
require 'matrices_p9'
|
5
|
+
require "test/unit"
|
6
|
+
|
7
|
+
|
8
|
+
class Test_MatrizDensa < Test::Unit::TestCase
|
9
|
+
|
10
|
+
def test_1
|
11
|
+
|
12
|
+
assert_equal "[[2, 4, 6], [8, 10, 12], [14, 16, 18]]", (MatrizDensa.new(3,3,[1,2,3,4,5,6,7,8,9]) + MatrizDensa.new(3,3,[1,2,3,4,5,6,7,8,9])).to_s
|
13
|
+
#assert_equal "[[0, 0, 0], [0, 0, 0], [0, 0, 0]]", (MatrizDensa.new(3,3,[1,2,3,4,5,6,7,8,9]) - MatrizDensa.new(3,3,[1,2,3,4,5,6,7,8,9])).to_s
|
14
|
+
#assert_equal "[[30, 36, 42], [66, 81, 96], [102, 126, 150]]", (MatrizDensa.new(3,3,[1,2,3,4,5,6,7,8,9])* MatrizDensa.new(3,3,[1,2,3,4,5,6,7,8,9])).to_s
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
#pruebas con matrices de fracciones
|
19
|
+
def test2
|
20
|
+
assert_equal "[[1/1, 1/1], [1/1, 1/1]]", (MatrizDensa.new(2,2,[Fraccion.new(1,2),Fraccion.new(1,3),Fraccion.new(1,4),Fraccion.new(1,5)]) + MatrizDensa.new(2,2,[Fraccion.new(1,2),Fraccion.new(2,3),Fraccion.new(3,4),Fraccion.new(4,5)])).to_s
|
21
|
+
#assert_equal "[[1/2, 3/5], [11/40, 49/150]]", (MatrizDensa.new(2,2,[Fraccion.new(1,2),Fraccion.new(1,3),Fraccion.new(1,4),Fraccion.new(1,5)]) * MatrizDensa.new(2,2,[Fraccion.new(1,2),Fraccion.new(2,3),Fraccion.new(3,4),Fraccion.new(4,5)])).to_s
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# Pruebas unitarias de la clase matriz dispersa
|
2
|
+
require "fraccion"
|
3
|
+
require "matrices_p9"
|
4
|
+
require "test/unit"
|
5
|
+
|
6
|
+
|
7
|
+
class Test_MatrizDispersa < Test::Unit::TestCase
|
8
|
+
|
9
|
+
def test_1
|
10
|
+
|
11
|
+
assert_equal "[[0,0,2][1,1,4][2,2,6]]", (MatrizDispersa.new(3,3,[0,1,2],[0,1,2],[1,2,3]) + MatrizDispersa.new(3,3,[0,1,2],[0,1,2],[1,2,3])).to_s
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: matrices_p9
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Luis Orta, Adriana Rolo
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-11-19 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '2.11'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '2.11'
|
30
|
+
description: Gema para realizar suma entre matrices densas y matrices dispersas
|
31
|
+
email:
|
32
|
+
- alu0100611526@ull.edu.es, alu0100658712@ull.edu.es
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- .travis.yml
|
39
|
+
- Gemfile
|
40
|
+
- Guardfile
|
41
|
+
- LICENSE.txt
|
42
|
+
- README.md
|
43
|
+
- README.md~
|
44
|
+
- Rakefile
|
45
|
+
- lib/fraccion.rb
|
46
|
+
- lib/matrices_p9.rb
|
47
|
+
- lib/matrices_p9/version.rb
|
48
|
+
- matrices_p9.gemspec
|
49
|
+
- spec/matrices_p9_spec.rb
|
50
|
+
- test/tc_MatrizDensa.rb
|
51
|
+
- test/tc_MatrizDispersa.rb
|
52
|
+
homepage: ''
|
53
|
+
licenses: []
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ! '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
requirements: []
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 1.8.23
|
73
|
+
signing_key:
|
74
|
+
specification_version: 3
|
75
|
+
summary: matrices densas y dispersas
|
76
|
+
test_files:
|
77
|
+
- spec/matrices_p9_spec.rb
|
78
|
+
- test/tc_MatrizDensa.rb
|
79
|
+
- test/tc_MatrizDispersa.rb
|