maths_matrix_ull_etsii_lpp_t10 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 +17 -0
- data/Gemfile +9 -0
- data/Guardfile +30 -0
- data/LICENSE.txt +22 -0
- data/README.md +6 -0
- data/Rakefile +28 -0
- data/bin/maths_matrix_ull_etsii_lpp_t10 +3 -0
- data/lib/densa.rb +214 -0
- data/lib/dispersa.rb +170 -0
- data/lib/gcd.rb +9 -0
- data/lib/maths_matrix_ull_etsii_lpp_t10.rb +5 -0
- data/lib/maths_matrix_ull_etsii_lpp_t10/version.rb +3 -0
- data/lib/matriz.rb +43 -0
- data/lib/racional.rb +105 -0
- data/maths_matrix_ull_etsii_lpp_t10.gemspec +23 -0
- data/spec/matriz_spec.rb +181 -0
- data/test/tc_matrices.rb +39 -0
- metadata +93 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0742f72e63f2bdbe3d242636ad83f10cf1857ab6
|
4
|
+
data.tar.gz: ab7b5f4992195838f3b6f0ab9f0e12be996e5e42
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 48782a0f15afc10d2b9f2d8d6b63784241cf5c6f4c0ebbcf7e5c7eb676aa7aa8bd80f40e258fbfbc9f57ba6250a3324903dd4011fe3c9bffb8aa963f893dd761
|
7
|
+
data.tar.gz: 7556f6bc4cbb9b0107ba2c7e7cb52f3e09fab0747f1047a3406df792ead9b502e872736ab4c9dc4f99e1f46de6541f15ca7f5d7a23a354c8e97d86c94ef52be9
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
guard :bundler do
|
5
|
+
watch('Gemfile')
|
6
|
+
# Uncomment next line if your Gemfile contains the `gemspec' command.
|
7
|
+
# watch(/^.+\.gemspec/)
|
8
|
+
end
|
9
|
+
|
10
|
+
guard :rspec do
|
11
|
+
watch(%r{^spec/.+_spec\.rb$})
|
12
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
13
|
+
watch('spec/spec_helper.rb') { "spec" }
|
14
|
+
|
15
|
+
# Rails example
|
16
|
+
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
17
|
+
watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
|
18
|
+
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
|
19
|
+
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
|
20
|
+
watch('config/routes.rb') { "spec/routing" }
|
21
|
+
watch('app/controllers/application_controller.rb') { "spec/controllers" }
|
22
|
+
|
23
|
+
# Capybara features specs
|
24
|
+
watch(%r{^app/views/(.+)/.*\.(erb|haml|slim)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
|
25
|
+
|
26
|
+
# Turnip features and steps
|
27
|
+
watch(%r{^spec/acceptance/(.+)\.feature$})
|
28
|
+
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
|
29
|
+
end
|
30
|
+
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Jonathan Trujillo Estevez
|
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,6 @@
|
|
1
|
+
|
2
|
+
Esta practica consiste en la implementacion varias clases para representar tanto la matrices densas
|
3
|
+
como las matrices dispersas.
|
4
|
+
|
5
|
+
Por lo que hemos optado por elegir una jerarquía de clases, en la que tendremos como clase madre la
|
6
|
+
clase Matrix y como clases hijas de esta tanto la clase Densa como la clase Dispersa.
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
$:.unshift File.dirname(__FILE__) + 'lib'
|
2
|
+
$:.unshift './lib', './spec'
|
3
|
+
|
4
|
+
require "bundler/gem_tasks"
|
5
|
+
require 'rspec/core/rake_task'
|
6
|
+
RSpec::Core::RakeTask.new
|
7
|
+
task :default => :spec
|
8
|
+
|
9
|
+
desc "Pruebas unitarias de la clase Matriz"
|
10
|
+
task :p do
|
11
|
+
sh "rspec -I. spec/matriz_spec.rb"
|
12
|
+
end
|
13
|
+
|
14
|
+
desc "Pruebas unitarias de la clase Matriz"
|
15
|
+
task :doc do
|
16
|
+
sh "rspec -I. spec/matriz_spec.rb --format documentation"
|
17
|
+
end
|
18
|
+
|
19
|
+
desc "Pruebas unitarias de la clase Matriz"
|
20
|
+
task :html do
|
21
|
+
sh "rspec -I. spec/matriz_spec.rb --format html"
|
22
|
+
end
|
23
|
+
|
24
|
+
desc "Pruebas unitarias de Matriz"
|
25
|
+
task :test do
|
26
|
+
sh "ruby -Ilib test/tc_matrices.rb"
|
27
|
+
end
|
28
|
+
|
data/lib/densa.rb
ADDED
@@ -0,0 +1,214 @@
|
|
1
|
+
require 'matriz.rb'
|
2
|
+
|
3
|
+
class Densa < Matriz
|
4
|
+
|
5
|
+
attr_accessor :matriz, :f, :c
|
6
|
+
|
7
|
+
def initialize(mat) #constructor
|
8
|
+
@matriz = mat #array de arrays con la matriz
|
9
|
+
@f = mat.size #filas de la matriz
|
10
|
+
@c = mat[0].size #columnas de la matriz
|
11
|
+
end
|
12
|
+
|
13
|
+
def coerce (dato)
|
14
|
+
[self, dato]
|
15
|
+
end
|
16
|
+
# Metodo para sumar dos matrices
|
17
|
+
def +(mat)
|
18
|
+
if(mat.class == Densa)
|
19
|
+
resultado = Densa.new(@matriz)
|
20
|
+
for i in 0...@f do
|
21
|
+
for j in 0...@c do
|
22
|
+
resultado.matriz[i][j] =@matriz[i][j] + mat.matriz[i][j]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
else
|
26
|
+
resultado = Densa.new(@matriz)
|
27
|
+
for i in 0...@f
|
28
|
+
for j in 0...@c
|
29
|
+
if((!mat.matriz[i].nil?) && (!mat.matriz[i][j].nil?))
|
30
|
+
resultado.matriz = resultado.matriz[i][j] + mat.matriz[i][j]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
resultado
|
36
|
+
end
|
37
|
+
# Metodo para restar dos matrices
|
38
|
+
def -(mat)
|
39
|
+
if(mat.class == Densa)
|
40
|
+
resultado = Densa.new(@matriz)
|
41
|
+
for i in 0...@f do
|
42
|
+
for j in 0...@c do
|
43
|
+
resultado.matriz[i][j] =@matriz[i][j] - mat.matriz[i][j]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
else
|
47
|
+
resultado = Densa.new(@matriz)
|
48
|
+
for i in 0...@f
|
49
|
+
for j in 0...@c
|
50
|
+
if((!mat.matriz[i].nil?) && (!mat.matriz[i][j].nil?))
|
51
|
+
resultado.matriz = resultado.matriz[i][j] - mat.matriz[i][j]
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
resultado
|
57
|
+
end
|
58
|
+
# Metodo para multiplicacion dos matrices
|
59
|
+
def *(mat)
|
60
|
+
if(mat.class == Densa)
|
61
|
+
if (@c == mat.f)
|
62
|
+
aux = Array.new
|
63
|
+
for i in 0...@f do
|
64
|
+
aux[i] = Array.new
|
65
|
+
for j in 0...mat.c do
|
66
|
+
if(mat.matriz[0][0].class == Fraccion)
|
67
|
+
aux[i][j] = Fraccion.new(0,1)
|
68
|
+
else
|
69
|
+
aux[i][j] = 0
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
resultado = Densa.new(aux)
|
74
|
+
for i in 0...@f do
|
75
|
+
for j in 0...mat.c do
|
76
|
+
for k in 0...@c do
|
77
|
+
resultado.matriz[i][j] += @matriz[i][k] * mat.matriz[k][j]
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
else
|
82
|
+
puts "error"
|
83
|
+
end
|
84
|
+
else
|
85
|
+
if(@c == mat.f)
|
86
|
+
aux = Array.new
|
87
|
+
for i in 0...@f do
|
88
|
+
aux[i] = Array.new
|
89
|
+
for j in 0...mat.c do
|
90
|
+
if(((!mat.matriz[i].nil?) && (!mat.matriz[i][j].nil?) && (mat.matriz[i][j].class == Fraccion)) || (@matriz[0][0].class == Fraccion))
|
91
|
+
aux[i][j] = Fraccion.new(0,1)
|
92
|
+
else
|
93
|
+
aux[i][j] = 0
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
resultado = Densa.new(aux)
|
98
|
+
for i in 0...@f do
|
99
|
+
for j in 0...mat.c do
|
100
|
+
for k in 0...@c do
|
101
|
+
if((!mat.matriz[k].nil?) && (!mat.matriz[k][j].nil?))
|
102
|
+
resultado.matriz[i][j] += @matriz[i][k] * mat.matriz[k][j]
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
else
|
108
|
+
puts "error"
|
109
|
+
end
|
110
|
+
end
|
111
|
+
return resultado
|
112
|
+
end
|
113
|
+
|
114
|
+
# Metodo para multiplicar una matriz por un escalar
|
115
|
+
def x(n)
|
116
|
+
resultado = Densa.new(@matriz)
|
117
|
+
for i in 0...@f do
|
118
|
+
for j in 0...@c do
|
119
|
+
resultado.matriz[i][j] =@matriz[i][j] *n
|
120
|
+
end
|
121
|
+
end
|
122
|
+
resultado
|
123
|
+
end
|
124
|
+
|
125
|
+
#Metodo para comprobar si dos matrices son iguales
|
126
|
+
def igual (mat)
|
127
|
+
if(@f == mat.f && @c == mat.c)
|
128
|
+
for i in 0...@f
|
129
|
+
for j in 0...@c
|
130
|
+
if(@matriz[i][j] != mat.matriz[i][j])
|
131
|
+
return -1
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
return 1
|
137
|
+
end
|
138
|
+
#Metodo para calcular el determinante de una matriz
|
139
|
+
def deter
|
140
|
+
det = @matriz[0][0]
|
141
|
+
aux = Densa.new(@matriz)
|
142
|
+
for k in 0...@f do
|
143
|
+
l = k+1
|
144
|
+
for i in l...@c do
|
145
|
+
for j in l...@c do
|
146
|
+
aux.matriz[i][j] = (aux.matriz[k][k] * aux.matriz[i][j] - aux.matriz[k][j] * aux.matriz[i][k])/ aux.matriz[k][k]
|
147
|
+
end
|
148
|
+
end
|
149
|
+
det = det * aux.matriz[k][k]
|
150
|
+
end
|
151
|
+
det
|
152
|
+
end
|
153
|
+
|
154
|
+
#Metodo para calcular la traspuesta de una matriz
|
155
|
+
def t
|
156
|
+
resultado = Array.new
|
157
|
+
for i in 0...@c
|
158
|
+
resultado[i] = Array.new
|
159
|
+
for j in 0...@f
|
160
|
+
resultado[i][j] = matriz[j][i]
|
161
|
+
end
|
162
|
+
end
|
163
|
+
resultado
|
164
|
+
end
|
165
|
+
|
166
|
+
# Metodo para convertir la matriz a string
|
167
|
+
def to_s
|
168
|
+
aux = ""
|
169
|
+
@f.times do |i|
|
170
|
+
@c.times do |j|
|
171
|
+
aux << "#{@matriz[i][j]}\t"
|
172
|
+
end
|
173
|
+
aux << "\n"
|
174
|
+
end
|
175
|
+
aux
|
176
|
+
end
|
177
|
+
|
178
|
+
def mayor
|
179
|
+
max = @matriz[0][0]
|
180
|
+
for i in 0...@f
|
181
|
+
for j in 0...@c
|
182
|
+
if(max < @matriz[i][j])
|
183
|
+
max = @matriz[i][j]
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
def menor
|
190
|
+
min = @matriz[0][0]
|
191
|
+
for i in 0...@f
|
192
|
+
for j in 0...@c
|
193
|
+
if(min > @matriz[i][j])
|
194
|
+
min = @matriz[i][j]
|
195
|
+
end
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
def to_densa (mat)
|
201
|
+
aux = Array.new
|
202
|
+
for i in 0...@f
|
203
|
+
aux[i] = Array.new
|
204
|
+
for j in 0...@c
|
205
|
+
if(!mat[i].nil? && !mat[i][j].nil?)
|
206
|
+
aux[i][j] = mat[i][j]
|
207
|
+
else
|
208
|
+
aux[i][j] = 0
|
209
|
+
end
|
210
|
+
end
|
211
|
+
end
|
212
|
+
Densa.new(aux)
|
213
|
+
end
|
214
|
+
end
|
data/lib/dispersa.rb
ADDED
@@ -0,0 +1,170 @@
|
|
1
|
+
require 'matriz.rb'
|
2
|
+
|
3
|
+
class Dispersa < Matriz
|
4
|
+
|
5
|
+
attr_accessor :f, :c, :matriz
|
6
|
+
|
7
|
+
def initialize (n,m,mat)
|
8
|
+
@f = n
|
9
|
+
@c = m
|
10
|
+
@matriz = mat
|
11
|
+
end
|
12
|
+
#Funcion para mostrar una matriz
|
13
|
+
def to_s
|
14
|
+
aux = ""
|
15
|
+
for i in 0...@f
|
16
|
+
for j in 0...@c
|
17
|
+
if((!@matriz[i].nil?) && (!@matriz[i][j].nil?))
|
18
|
+
aux = aux + @matriz[i][j].to_s + "\t"
|
19
|
+
else
|
20
|
+
aux = aux + "0\t"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
aux = aux + "\n"
|
24
|
+
end
|
25
|
+
aux
|
26
|
+
end
|
27
|
+
|
28
|
+
def +(mat)
|
29
|
+
if (mat.f == @f && mat.c == @c)
|
30
|
+
resultado = Array.new
|
31
|
+
for i in 0...@f do
|
32
|
+
resultado[i] = Array.new
|
33
|
+
for j in 0...@c do
|
34
|
+
resultado[i][j] = 0
|
35
|
+
end
|
36
|
+
end
|
37
|
+
aux = Densa.new(resultado)
|
38
|
+
nElementos = 0
|
39
|
+
for i in 0...@f do
|
40
|
+
for j in 0...@c do
|
41
|
+
if ((!@matriz[i].nil?) && (!@matriz[i][j].nil?) && (!mat.matriz[i].nil?) && (!mat.matriz[i][j].nil?))
|
42
|
+
aux.matriz[i][j] = @matriz[i][j] + mat.matriz[i][j]
|
43
|
+
nElementos += 1
|
44
|
+
elsif ((!@matriz[i].nil?) && (!@matriz[i][j].nil?) && ((!mat.matriz[i].nil?) || (!mat.matriz[i].nil? && !mat.matriz[i][j].nil?)))
|
45
|
+
aux.matriz[i][j] = @matriz[i][j]
|
46
|
+
nElementos += 1
|
47
|
+
elsif ((!mat.matriz[i].nil?) && (!mat.matriz[i][j].nil?) && ((!@matriz[i].nil?) || (!@matriz[i].nil? && !@matriz[i][j].nil?)))
|
48
|
+
aux.matriz[i][j] = mat.matriz[i][j]
|
49
|
+
nElementos += 1
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
if ((@f * @c) * 0.4 > nElementos)
|
54
|
+
aux = to_dispersa(aux)
|
55
|
+
end
|
56
|
+
else
|
57
|
+
aux = "No se pueden sumar"
|
58
|
+
end
|
59
|
+
aux
|
60
|
+
end
|
61
|
+
|
62
|
+
def -(mat)
|
63
|
+
if (mat.f == @f && mat.c == @c)
|
64
|
+
result = Array.new
|
65
|
+
for i in 0...@f do
|
66
|
+
result[i] = Array.new
|
67
|
+
for j in 0...@c do
|
68
|
+
result[i][j] = 0
|
69
|
+
end
|
70
|
+
end
|
71
|
+
aux = Densa.new(result)
|
72
|
+
nElementos = 0
|
73
|
+
for i in 0...@f do
|
74
|
+
for j in 0...@c do
|
75
|
+
if ((!@matriz[i].nil?) && (!@matriz[i][j].nil?) && (!mat.matriz[i].nil?) && (!mat.matriz[i][j].nil?))
|
76
|
+
aux.matriz[i][j] = @matriz[i][j] - mat.matriz[i][j]
|
77
|
+
nElementos += 1
|
78
|
+
elsif ((!@matriz[i].nil?) && (!@matriz[i][j].nil?) && ((!mat.matriz[i].nil?) || (!mat.matriz[i].nil? && !mat.matriz[i][j].nil?)))
|
79
|
+
aux.matriz[i][j] = @matriz[i][j]
|
80
|
+
nElementos += 1
|
81
|
+
elsif ((!mat.matriz[i].nil?) && (!mat.matriz[i][j].nil?) && ((!@matriz[i].nil?) || (!@matriz[i].nil? && !@matriz[i][j].nil?)))
|
82
|
+
aux.matriz[i][j] = - mat.matriz[i][j]
|
83
|
+
nElementos += 1
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
if ((@f * @c) * 0.4 > nElementos)
|
88
|
+
aux = to_dispersa(aux)
|
89
|
+
end
|
90
|
+
else
|
91
|
+
aux = "No se pueden sumar"
|
92
|
+
end
|
93
|
+
aux
|
94
|
+
end
|
95
|
+
|
96
|
+
def to_dispersa(mat)
|
97
|
+
resultado = {}
|
98
|
+
for i in 0...mat.f do
|
99
|
+
for j in 0...mat.c do
|
100
|
+
if (mat.matriz[i][j] != 0)
|
101
|
+
if (resultado[i].nil?)
|
102
|
+
resultado[i] = {}
|
103
|
+
end
|
104
|
+
resultado[i][j] = mat.matriz[i][j]
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
aux = Dispersa.new(mat.f, mat.c, resultado)
|
109
|
+
end
|
110
|
+
|
111
|
+
def *(mat)
|
112
|
+
if (@c == mat.f)
|
113
|
+
result = Array.new
|
114
|
+
for i in 0...@f do
|
115
|
+
result[i] = Array.new
|
116
|
+
for j in 0...@c do
|
117
|
+
result[i][j] = 0
|
118
|
+
end
|
119
|
+
end
|
120
|
+
aux = Densa.new(result)
|
121
|
+
nElementos = 0
|
122
|
+
for i in 0...@f do
|
123
|
+
for j in 0...mat.c do
|
124
|
+
for z in 0...@c do
|
125
|
+
if ((!@matriz[i].nil?) && (!@matriz[i][z].nil?) && (!mat.matriz[z].nil?) && (!mat.matriz[z][j].nil?))
|
126
|
+
aux.matriz[i][j] += @matriz[i][z] * mat.matriz[z][j]
|
127
|
+
nElementos += 1
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
if ((@f * @c) * 0.4 > nElementos)
|
133
|
+
aux = to_dispersa(aux)
|
134
|
+
end
|
135
|
+
else
|
136
|
+
aux = "No se pueden multiplicar"
|
137
|
+
end
|
138
|
+
aux
|
139
|
+
end
|
140
|
+
|
141
|
+
def mayor
|
142
|
+
max = 0
|
143
|
+
for i in 0...@f do
|
144
|
+
for j in 0...@c do
|
145
|
+
if((!@matriz[i].nil?) && (!@matriz[i][j].nil?))
|
146
|
+
if (max < @matriz[i][j])
|
147
|
+
max = @matriz[i][j]
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
|
155
|
+
def menor
|
156
|
+
min = 0
|
157
|
+
for i in 0...@f do
|
158
|
+
for j in 0...@c do
|
159
|
+
if((!@matriz[i].nil?) && (!@matriz[i][j].nil?))
|
160
|
+
if (min > @matriz[i][j])
|
161
|
+
min = @matriz[i][j]
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
end
|
169
|
+
|
170
|
+
|
data/lib/gcd.rb
ADDED
data/lib/matriz.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'racional.rb'
|
2
|
+
|
3
|
+
class Matriz
|
4
|
+
|
5
|
+
attr_accessor :nFil, :mCol, :matriz
|
6
|
+
|
7
|
+
def initialize(mat) #constructor
|
8
|
+
end
|
9
|
+
# Metodo para sumar dos matrices
|
10
|
+
|
11
|
+
def +(mat)
|
12
|
+
end
|
13
|
+
|
14
|
+
# Metodo para restar dos matrices
|
15
|
+
def -(mat)
|
16
|
+
end
|
17
|
+
|
18
|
+
# Metodo para multiplicacion dos matrices
|
19
|
+
def *(mat)
|
20
|
+
end
|
21
|
+
|
22
|
+
# Metodo para multiplicar una matriz por un escalar
|
23
|
+
def x(n)
|
24
|
+
end
|
25
|
+
|
26
|
+
#Metodo para comprobar si dos matrices son iguales
|
27
|
+
def igual (mat)
|
28
|
+
end
|
29
|
+
|
30
|
+
#Metodo para calcular el determinante de una matriz
|
31
|
+
def deter
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
#Metodo para calcular la traspuesta de una matriz
|
36
|
+
def t
|
37
|
+
end
|
38
|
+
|
39
|
+
# Metodo para convertir la matriz a string
|
40
|
+
def to_s
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
data/lib/racional.rb
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
# Implementar en este fichero la clase para crear objetos racionales
|
2
|
+
|
3
|
+
require "gcd.rb" #incluimos el fichero con la funcion de maximo comun divisor
|
4
|
+
|
5
|
+
include Comparable
|
6
|
+
|
7
|
+
|
8
|
+
#clase para definir el objeto fraccion
|
9
|
+
class Fraccion
|
10
|
+
|
11
|
+
attr_accessor :num, :den #nos permite el uso de los getter y setter sobre num y den
|
12
|
+
|
13
|
+
def coerce (dato)
|
14
|
+
[self, dato]
|
15
|
+
end
|
16
|
+
|
17
|
+
# Funcion initialize
|
18
|
+
def initialize(numerador, denominador)
|
19
|
+
mcd = gcd(numerador, denominador) #Minimo comun divisor
|
20
|
+
@num = numerador/mcd
|
21
|
+
@den = denominador/mcd
|
22
|
+
end
|
23
|
+
#Funcion para devolver un string
|
24
|
+
def to_s
|
25
|
+
return "#{@num}/#{@den}"
|
26
|
+
end
|
27
|
+
#Fucion para devolver un flotante
|
28
|
+
def to_f
|
29
|
+
return @num/@den.to_f
|
30
|
+
end
|
31
|
+
#Fucion para comparar dos fracciones
|
32
|
+
def ==(fraccion)
|
33
|
+
return @num == fraccion.num && @den == fraccion.den
|
34
|
+
end
|
35
|
+
#Fucion para transformar una fraccion a su valor absoluto
|
36
|
+
def abs
|
37
|
+
Fraccion.new(@num.abs,@den.abs)
|
38
|
+
end
|
39
|
+
#Funcion para obtener el reciproco de un fraccion
|
40
|
+
def reciproco
|
41
|
+
Fraccion.new(@den,@num)
|
42
|
+
end
|
43
|
+
#Fucion para calcular el inverso de una fraccion
|
44
|
+
def -@
|
45
|
+
Fraccion.new(-@num,@den)
|
46
|
+
end
|
47
|
+
#Fucion para sumar dos fracciones o un escalar con una fraccion
|
48
|
+
def +(dato)
|
49
|
+
if (dato.class == Fraccion)
|
50
|
+
n = dato.num * @den + dato.den * @num
|
51
|
+
d = @den * dato.den
|
52
|
+
Fraccion.new(n,d)
|
53
|
+
else
|
54
|
+
n = (dato * @den) + @num
|
55
|
+
d = @den
|
56
|
+
Fraccion.new(n,d)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
#Fucion para restar dos fracciones o restar un escalar con una fraccion
|
60
|
+
def -(dato)
|
61
|
+
if(dato.class == Fraccion)
|
62
|
+
n = dato.den * @num - dato.num * @den
|
63
|
+
d = @den * dato.den
|
64
|
+
Fraccion.new(n,d)
|
65
|
+
else
|
66
|
+
n = (dato * @den) - @num
|
67
|
+
d = @den
|
68
|
+
Fraccion.new(n,d)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
#Fucion para multiplicar dos fracciones
|
72
|
+
def *(dato)
|
73
|
+
if (dato.class == Fraccion)
|
74
|
+
n = dato.num * @num
|
75
|
+
d = dato.den * @den
|
76
|
+
Fraccion.new(n,d)
|
77
|
+
else
|
78
|
+
n = dato * @num
|
79
|
+
d = @den
|
80
|
+
Fraccion.new(n,d)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
#Funcion para dividir dos fracciones
|
84
|
+
def /(fraccion) #/
|
85
|
+
n = fraccion.den * @num
|
86
|
+
d = fraccion.num * @den
|
87
|
+
Fraccion.new(n,d)
|
88
|
+
end
|
89
|
+
#Funcion para calcular el resto de dos fracciones
|
90
|
+
def %(fraccion)
|
91
|
+
re = @num * fraccion.den % @den * fraccion.num
|
92
|
+
end
|
93
|
+
#Funcion operador guerra de las galaxias
|
94
|
+
def <=>(fraccion)
|
95
|
+
if (@num * fraccion.den < @den * fraccion.num)
|
96
|
+
return -1
|
97
|
+
end
|
98
|
+
if (@num * fraccion.den > @den * fraccion.num)
|
99
|
+
return 1
|
100
|
+
end
|
101
|
+
if(@num * fraccion.den == @den * fraccion.num)
|
102
|
+
return 0
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'maths_matrix_ull_etsii_lpp_t10/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "maths_matrix_ull_etsii_lpp_t10"
|
8
|
+
spec.version = MathsMatrixUllEtsiiLppT10::VERSION
|
9
|
+
spec.authors = ["Jonathan Trujillo Estevez"]
|
10
|
+
spec.email = ["alu0100606324@ull.edu.es"]
|
11
|
+
spec.description = %q{clase matrices}
|
12
|
+
spec.summary = %q{operaciones entre matrices densas y matrices dispersas}
|
13
|
+
spec.homepage = "https://github.com/alu0100606324/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
|
+
end
|
data/spec/matriz_spec.rb
ADDED
@@ -0,0 +1,181 @@
|
|
1
|
+
require 'matriz.rb'
|
2
|
+
require 'densa.rb'
|
3
|
+
require 'dispersa.rb'
|
4
|
+
|
5
|
+
require 'rspec'
|
6
|
+
|
7
|
+
describe Matriz do
|
8
|
+
|
9
|
+
before :each do
|
10
|
+
@m1 = Densa.new([[1,2],[3,4]])
|
11
|
+
@m2 = Densa.new([[1,1],[1,1]])
|
12
|
+
@m3 = Densa.new([[4,3],[2,1]])
|
13
|
+
@m4 = Densa.new([[1,2,3],[4,5,6]])
|
14
|
+
@m5 = Densa.new([[Fraccion.new(1,2),Fraccion.new(1,2)],[Fraccion.new(1,2),Fraccion.new(1,2)]])
|
15
|
+
@m6 = Densa.new([[Fraccion.new(2,3),Fraccion.new(2,3)],[Fraccion.new(2,3),Fraccion.new(2,3)]])
|
16
|
+
@m7 = Fraccion.new(7,6)
|
17
|
+
@m8 = Fraccion.new(1,6)
|
18
|
+
@m9 = Fraccion.new(2,3)
|
19
|
+
@m10 = Fraccion.new(3,2)
|
20
|
+
@m11 = Fraccion.new(5,2)
|
21
|
+
@m12 = Fraccion.new(7,2)
|
22
|
+
@m13 = Fraccion.new(9,2)
|
23
|
+
@m14 = Fraccion.new(1,2)
|
24
|
+
@m15 = Fraccion.new(1,1)
|
25
|
+
@m16 = Fraccion.new(2,1)
|
26
|
+
@m17 = Fraccion.new(7,2)
|
27
|
+
@n = 2
|
28
|
+
@mdi1 = Dispersa.new(3,3,{0 => {0=> 5, 1 => 3, 2 => 2}, 2 => {1 => 13}})
|
29
|
+
@mdi2 = Dispersa.new(3,3,{1 => {0 => 2, 1 => 6, 2 => 1}, 2 => {1 => 4, 2 => 4}})
|
30
|
+
@mdi3 = Dispersa.new(2,2,{0 => {0=> 5}})
|
31
|
+
@mdi4 = Dispersa.new(2,2,{0 => {0 => Fraccion.new(1,2)}})
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "Pruebas constructor" do
|
35
|
+
|
36
|
+
it "Debe existir una variable que almacene el numero de filas" do
|
37
|
+
@m1.f.should == 2
|
38
|
+
@m5.f.should == 2
|
39
|
+
end
|
40
|
+
|
41
|
+
it "Debe existir una variable que almacene el numero de columnas" do
|
42
|
+
@m1.c.should == 2
|
43
|
+
@m5.c.should == 2
|
44
|
+
end
|
45
|
+
|
46
|
+
it "Se debe invocar al metodo f() para obtener el numero de filas" do
|
47
|
+
@m1.respond_to?("f").should be_true
|
48
|
+
@m5.respond_to?("f").should be_true
|
49
|
+
end
|
50
|
+
|
51
|
+
it "Se debe invocar al metodo c() para obtener el numero de columnas" do
|
52
|
+
@m1.respond_to?("c").should be_true
|
53
|
+
@m5.respond_to?("c").should be_true
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe "Puebas operaciones binarias" do
|
58
|
+
|
59
|
+
it "Se debe sumar dos matrices con + y dar el resultado" do
|
60
|
+
(@m1 + @m2).matriz == [[2,3],[4,5]]
|
61
|
+
(@m5 + @m6).matriz == [[@m7,@m7],[@m7,@m7]]
|
62
|
+
end
|
63
|
+
|
64
|
+
it "Se debe sumar una matriz de racionales con una matriz de enteros" do
|
65
|
+
(@m5 + @m1).matriz == [[@m10,@m11],[@m12,@m13]]
|
66
|
+
end
|
67
|
+
|
68
|
+
it "Se debe sumar una matriz de enteros con una matriz de racionales" do
|
69
|
+
(@m1 + @m5).matriz == [[@m10,@m11],[@m12,@m13]]
|
70
|
+
end
|
71
|
+
|
72
|
+
it "Se debe restar dos matrices con - y dar el resultado" do
|
73
|
+
(@m1 - @m2).matriz == [[0,1],[2,3]]
|
74
|
+
(@m6 - @m5).matriz == [[@m8,@m8],[@m8,@m8]]
|
75
|
+
end
|
76
|
+
|
77
|
+
it "Se debe restar una matriz de racionales con una matriz de enteros" do
|
78
|
+
(@m1 - @m5).matriz == [[@m14,@m10],[@m11,@m16]]
|
79
|
+
(@m5 - @m1).matriz == [[@m14,@m10],[@m11,@m16]]
|
80
|
+
end
|
81
|
+
|
82
|
+
it "Se debe multiplicar dos matrices con * y dar el resultado" do
|
83
|
+
(@m1 * @m3).matriz == [[8,5],[20,13]]
|
84
|
+
(@m5 * @m6).matriz == [[@m9,@m9],[@m9,@m9]]
|
85
|
+
end
|
86
|
+
|
87
|
+
it "Se debe multiplicar una matriz de enteros por una matriz de fracciones y darl el resultado" do
|
88
|
+
(@m1 * @m5).matriz == [[@m10,@m10],[@m12,@m12]]
|
89
|
+
(@m5 * @m1).matriz == [[@m10,@m10],[@m12,@m12]]
|
90
|
+
end
|
91
|
+
|
92
|
+
it "Se debe multiplicar una matriz pro un escalar y dar el resultado" do
|
93
|
+
@m1.x(@n).matriz == [[2,4],[6,8]]
|
94
|
+
end
|
95
|
+
|
96
|
+
it "Se debe comparar si dos matrices son iguales" do
|
97
|
+
(@m1.igual(@m1)).should == 1
|
98
|
+
(@m5.igual(@m6)).should == -1
|
99
|
+
(@m5.igual(@m5)).should == 1
|
100
|
+
end
|
101
|
+
|
102
|
+
it "Se deben sumar 2 matrices dispersas" do
|
103
|
+
(@mdi1 + @mdi2).matriz == [[5,3,2],[2,6,1],[0,17,4]]
|
104
|
+
end
|
105
|
+
|
106
|
+
it "Se deben restar 2 matrices dispersas" do
|
107
|
+
(@mdi1 - @mdi2).matriz == [[5,3,2],[-2,-6,-1],[0,9,-4]]
|
108
|
+
end
|
109
|
+
|
110
|
+
it "Se deben multiplicar 2 matrices dispersas" do
|
111
|
+
(@mdi1 * @mdi2).matriz == [[6,26,11],[0,0,0],[26,78,13]]
|
112
|
+
end
|
113
|
+
|
114
|
+
it "Se debe sumar una densa con una dispersa" do
|
115
|
+
(@m1 + @mdi3).matriz == [[6,2],[3,4]]
|
116
|
+
(@mdi3 + @m1).matriz == [[6,2],[3,4]]
|
117
|
+
end
|
118
|
+
|
119
|
+
it "Se debe restar una densa con una dispersa" do
|
120
|
+
(@m1 - @mdi3).matriz == [[-4,2],[3,4]]
|
121
|
+
(@mdi3 - @m1).matriz == [[-4,2],[3,4]]
|
122
|
+
end
|
123
|
+
|
124
|
+
it "Se debe multiplicar dos matrices densas" do
|
125
|
+
(@m1 * @mdi3).matriz == [[5,0],[15,0]]
|
126
|
+
(@mdi3 * @m1).matriz == [[5,0],[15,0]]
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
describe "Pruebas unarias" do
|
131
|
+
|
132
|
+
it "Se debe calcular el determinante de una matriz" do
|
133
|
+
@m1.deter == -2
|
134
|
+
end
|
135
|
+
|
136
|
+
it "Se debe mostar por la consola la Matrix Densa en forma de string"do
|
137
|
+
@m1.to_s.should == "1\t2\t\n3\t4\t\n"
|
138
|
+
end
|
139
|
+
|
140
|
+
|
141
|
+
it "Se debe calcular la traspuesta de una matriz" do
|
142
|
+
@m4.t.should == [[1,4],[2,5],[3,6]]
|
143
|
+
end
|
144
|
+
|
145
|
+
|
146
|
+
it " Se debe calcular el menor y mayor de una matriz densa" do
|
147
|
+
@m1.menor == 1
|
148
|
+
@m1.mayor == 4
|
149
|
+
end
|
150
|
+
|
151
|
+
it "Se debe mostrar por la consola la Matrix Dispersa en forma de string"do
|
152
|
+
@mdi1.to_s.should == "5\t3\t2\t\n0\t0\t0\t\n0\t13\t0\t\n"
|
153
|
+
end
|
154
|
+
|
155
|
+
it "Se debe calcular el menor y mayor de una matriz dispersa" do
|
156
|
+
@mdi1.menor == 0
|
157
|
+
@mdi2.mayor == 13
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
end
|
data/test/tc_matrices.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require "lib/matriz"
|
2
|
+
require "lib/densa"
|
3
|
+
require "lib/dispersa"
|
4
|
+
require "test/unit"
|
5
|
+
|
6
|
+
class Test_Matrices < Test::Unit::TestCase
|
7
|
+
def setup
|
8
|
+
@m1 = Densa.new([[1,2],[3,4]])
|
9
|
+
@m2 = Densa.new([[1,1],[1,1]])
|
10
|
+
# @m3 = Densa.new([[4,3],[2,1]])
|
11
|
+
# @m4 = Densa.new([[1,2,3],[4,5,6]])
|
12
|
+
# @m5 = Densa.new([[Fraccion.new(1,2),Fraccion.new(1,2)],[Fraccion.new(1,2),Fraccion.new(1,2)]])
|
13
|
+
# @m6 = Densa.new([[Fraccion.new(2,3),Fraccion.new(2,3)],[Fraccion.new(2,3),Fraccion.new(2,3)]])
|
14
|
+
# @m7 = Fraccion.new(7,6)
|
15
|
+
# @m8 = Fraccion.new(1,6)
|
16
|
+
# @m9 = Fraccion.new(2,3)
|
17
|
+
# @m10 = Fraccion.new(3,2)
|
18
|
+
# @m11 = Fraccion.new(5,2)
|
19
|
+
# @m12 = Fraccion.new(7,2)
|
20
|
+
# @m13 = Fraccion.new(9,2)
|
21
|
+
# @m14 = Fraccion.new(1,2)
|
22
|
+
# @m15 = Fraccion.new(1,1)
|
23
|
+
# @m16 = Fraccion.new(2,1)
|
24
|
+
# @m17 = Fraccion.new(7,2)
|
25
|
+
# @n = 2
|
26
|
+
# @mdi1 = Dispersa.new(3,3,{0 => {0=> 5, 1 => 3, 2 => 2}, 2 => {1 => 13}})
|
27
|
+
# @mdi2 = Dispersa.new(3,3,{1 => {0 => 2, 1 => 6, 2 => 1}, 2 => {1 => 4, 2 => 4}})
|
28
|
+
# @mdi3 = Dispersa.new(2,2,{0 => {0=> 5}})
|
29
|
+
# @mdi4 = Dispersa.new(2,2,{0 => {0 => Fraccion.new(1,2)}})
|
30
|
+
end
|
31
|
+
|
32
|
+
def tear_down
|
33
|
+
#nothing
|
34
|
+
end
|
35
|
+
|
36
|
+
def suma_densas
|
37
|
+
assert_equal((@m1+@m2), ([[2,3],[4,5]]))
|
38
|
+
end
|
39
|
+
end
|
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: maths_matrix_ull_etsii_lpp_t10
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jonathan Trujillo Estevez
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-11-22 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
|
+
description: clase matrices
|
42
|
+
email:
|
43
|
+
- alu0100606324@ull.edu.es
|
44
|
+
executables:
|
45
|
+
- maths_matrix_ull_etsii_lpp_t10
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- .gitignore
|
50
|
+
- Gemfile
|
51
|
+
- Guardfile
|
52
|
+
- LICENSE.txt
|
53
|
+
- README.md
|
54
|
+
- Rakefile
|
55
|
+
- bin/maths_matrix_ull_etsii_lpp_t10
|
56
|
+
- lib/densa.rb
|
57
|
+
- lib/dispersa.rb
|
58
|
+
- lib/gcd.rb
|
59
|
+
- lib/maths_matrix_ull_etsii_lpp_t10.rb
|
60
|
+
- lib/maths_matrix_ull_etsii_lpp_t10/version.rb
|
61
|
+
- lib/matriz.rb
|
62
|
+
- lib/racional.rb
|
63
|
+
- maths_matrix_ull_etsii_lpp_t10.gemspec
|
64
|
+
- spec/matriz_spec.rb
|
65
|
+
- test/tc_matrices.rb
|
66
|
+
homepage: https://github.com/alu0100606324/prct09
|
67
|
+
licenses:
|
68
|
+
- MIT
|
69
|
+
metadata: {}
|
70
|
+
post_install_message:
|
71
|
+
rdoc_options: []
|
72
|
+
require_paths:
|
73
|
+
- lib
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - '>='
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - '>='
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
requirements: []
|
85
|
+
rubyforge_project:
|
86
|
+
rubygems_version: 2.0.3
|
87
|
+
signing_key:
|
88
|
+
specification_version: 4
|
89
|
+
summary: operaciones entre matrices densas y matrices dispersas
|
90
|
+
test_files:
|
91
|
+
- spec/matriz_spec.rb
|
92
|
+
- test/tc_matrices.rb
|
93
|
+
has_rdoc:
|