matriz 0.1.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/.travis.yml +8 -0
- data/Gemfile +3 -0
- data/Guardfile +8 -0
- data/LICENSE +22 -0
- data/README.md +25 -0
- data/Rakefile +36 -0
- data/Rakefile~ +33 -0
- data/bin/main.rb +256 -0
- data/lib/gcd.rb +13 -0
- data/lib/gcd.rb~ +14 -0
- data/lib/matriz.rb +37 -0
- data/lib/matriz_densa.rb +221 -0
- data/lib/matriz_dispersa.rb +323 -0
- data/lib/racionales.rb +172 -0
- data/lib/racionales.rb~ +172 -0
- data/lib/version.rb +3 -0
- data/lib/version.rb~ +3 -0
- data/matriz.gemspec +25 -0
- data/spec/matriz_spec.rb +265 -0
- data/test/tc_matriz.rb +50 -0
- metadata +140 -0
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Mª Belen Armas Torres - Aaron Socas Gaspar
|
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,25 @@
|
|
1
|
+
pract11
|
2
|
+
=======
|
3
|
+
|
4
|
+
Partiendo de la practica anterior, vamos a incluir las siguientes modificaciones para matrices densas y dispersas.
|
5
|
+
|
6
|
+
|
7
|
+
- La documentacion de la gema (utlizamos RDOC).
|
8
|
+
|
9
|
+
- Utilizaremos métodos con la filosofía de la programación funcional, aunque Ruby está diseñado
|
10
|
+
para utilizar programación orientada a objetos. Este tipo de programación lo hemos implementado
|
11
|
+
en las siguientes funciones (tanto en matrices densas como dispersas):
|
12
|
+
- calculo del maximo
|
13
|
+
- calculo del minimo
|
14
|
+
|
15
|
+
- Para hacer la suma (y resta) y el producto entre matrices (y escalar) hemos usado el método 'time',
|
16
|
+
aunque también lo hemos hecho con el método 'upto' (está comentado en la misma línea). Los usamos
|
17
|
+
para recorrer los arrays y operar.
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
Grupo (M-15)
|
22
|
+
============
|
23
|
+
|
24
|
+
- María Belén Armas Torres
|
25
|
+
- Aarón Socas Gaspar
|
data/Rakefile
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
#Fichero de Rakefile para Guard
|
2
|
+
|
3
|
+
#!/bin/env rake
|
4
|
+
require "bundler/gem_tasks"
|
5
|
+
|
6
|
+
$:.unshift File.dirname(__FILE__) + 'lib'
|
7
|
+
$:.unshift './lib', './spec'
|
8
|
+
|
9
|
+
require 'rspec/core/rake_task'
|
10
|
+
RSpec::Core::RakeTask.new
|
11
|
+
task :default => :spec
|
12
|
+
|
13
|
+
desc "Ejecutar las espectativas de la clase Matriz e hijas"
|
14
|
+
task :spec_local do
|
15
|
+
sh "rspec -I. spec/matriz_spec.rb"
|
16
|
+
end
|
17
|
+
|
18
|
+
desc "Ejecutar tests"
|
19
|
+
task :test do
|
20
|
+
sh "ruby test/tc_matriz.rb"
|
21
|
+
end
|
22
|
+
|
23
|
+
desc "Ejecutar con documentacion"
|
24
|
+
task :doc do
|
25
|
+
sh "rspec -I. spec/matriz_spec.rb --format documentation"
|
26
|
+
end
|
27
|
+
|
28
|
+
desc "Ejecutar programa"
|
29
|
+
task :bin do
|
30
|
+
sh "ruby bin/main.rb"
|
31
|
+
end
|
32
|
+
|
33
|
+
desc "Ejecutar las espectativas de la clase Matriz version html"
|
34
|
+
task :thtml do
|
35
|
+
sh "rspec -I. spec/matriz_spec.rb --format html"
|
36
|
+
end
|
data/Rakefile~
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
#Fichero de Rakefile para Guard
|
2
|
+
|
3
|
+
$:.unshift File.dirname(__FILE__) + 'lib'
|
4
|
+
$:.unshift './lib', './spec'
|
5
|
+
|
6
|
+
require 'rspec/core/rake_task'
|
7
|
+
RSpec::Core::RakeTask.new
|
8
|
+
task :default => :spec
|
9
|
+
|
10
|
+
desc "Ejecutar las espectativas de la clase Matriz e hijas"
|
11
|
+
task :spec_local do
|
12
|
+
sh "rspec -I. spec/matriz_spec.rb"
|
13
|
+
end
|
14
|
+
|
15
|
+
desc "Ejecutar tests"
|
16
|
+
task :test do
|
17
|
+
sh "ruby test/tc_matriz.rb"
|
18
|
+
end
|
19
|
+
|
20
|
+
desc "Ejecutar con documentacion"
|
21
|
+
task :doc do
|
22
|
+
sh "rspec -I. spec/matriz_spec.rb --format documentation"
|
23
|
+
end
|
24
|
+
|
25
|
+
desc "Ejecutar programa"
|
26
|
+
task :bin do
|
27
|
+
sh "ruby bin/main.rb"
|
28
|
+
end
|
29
|
+
|
30
|
+
desc "Ejecutar las espectativas de la clase Matriz version html"
|
31
|
+
task :thtml do
|
32
|
+
sh "rspec -I. spec/matriz_spec.rb --format html"
|
33
|
+
end
|
data/bin/main.rb
ADDED
@@ -0,0 +1,256 @@
|
|
1
|
+
#Funcion menu
|
2
|
+
require './lib/matriz.rb'
|
3
|
+
require './lib/matriz_densa.rb'
|
4
|
+
require './lib/matriz_dispersa.rb'
|
5
|
+
|
6
|
+
def menu1
|
7
|
+
system ("clear")
|
8
|
+
z=0
|
9
|
+
while (z<1 or z>5)
|
10
|
+
op=["\t\tMENU","\t1. Crear matrices","\t2. Generar matrices con valores aleatorios","\t3. Operaciones con matrices","\t4. Mostrar matrices","\t5. Salir"]
|
11
|
+
op.each{|op| puts op}
|
12
|
+
z=gets.chomp
|
13
|
+
z=z.to_i
|
14
|
+
if (z<1 or z>5)
|
15
|
+
puts ("Escoja una opcion entre [1..5]")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
z
|
19
|
+
end
|
20
|
+
|
21
|
+
def menu2
|
22
|
+
system ("clear")
|
23
|
+
z=0
|
24
|
+
while (z<1 or z>5)
|
25
|
+
op=["\t\tOPERACIONES CON MATRICES","\t1. Suma y resta de matrices","\t2. Producto escalar y entre matrices","\t3. Traspuesta de una matriz","\t4. Calcular maximo y minimo", "\t5. Volver"]
|
26
|
+
op.each{|op| puts op}
|
27
|
+
z=gets.chomp
|
28
|
+
z=z.to_i
|
29
|
+
if (z<1 or z>5)
|
30
|
+
puts ("Escoja una opcion entre [1..4]")
|
31
|
+
end
|
32
|
+
end
|
33
|
+
z
|
34
|
+
end
|
35
|
+
|
36
|
+
def menu3
|
37
|
+
z=0
|
38
|
+
while (z<1 or z>2)
|
39
|
+
op=["\tQuiere trabajar con enteros o fracciones?","\t1. Numeros enteros","\t2. Numeros racionales"]
|
40
|
+
op.each{|op| puts op}
|
41
|
+
z=gets.chomp
|
42
|
+
z=z.to_i
|
43
|
+
if (z<1 or z>2)
|
44
|
+
puts ("Escoja una opcion entre [1..2]")
|
45
|
+
end
|
46
|
+
end
|
47
|
+
z
|
48
|
+
end
|
49
|
+
|
50
|
+
def menu4
|
51
|
+
z=0
|
52
|
+
while (z<1 or z>2)
|
53
|
+
op=["\tIntroducir matriz densa o dispersa?","\t1. Matriz densa","\t2. Matriz dispersa"]
|
54
|
+
op.each{|op| puts op}
|
55
|
+
z=gets.chomp
|
56
|
+
z=z.to_i
|
57
|
+
if (z<1 or z>2)
|
58
|
+
puts ("Escoja una opcion entre [1..2]")
|
59
|
+
end
|
60
|
+
end
|
61
|
+
z
|
62
|
+
end
|
63
|
+
|
64
|
+
#Esta funcion la utilizamos para pausar del programa
|
65
|
+
def pausa
|
66
|
+
# while line=gets
|
67
|
+
while gets
|
68
|
+
break
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
#Ejecucion del programa principal
|
73
|
+
def principal
|
74
|
+
op=0
|
75
|
+
while (op!=5)
|
76
|
+
op= menu1
|
77
|
+
case op
|
78
|
+
when 1
|
79
|
+
puts "CREAR MATRICES"
|
80
|
+
puts "Introduce la dimension de las matriz 1: "
|
81
|
+
print "Filas? "
|
82
|
+
STDOUT.flush
|
83
|
+
f=gets.chomp
|
84
|
+
print "Columnas? "
|
85
|
+
c=gets.chomp
|
86
|
+
op4=menu4
|
87
|
+
case op4
|
88
|
+
when 1
|
89
|
+
a= Matriz_densa.new(f.to_i,c.to_i)
|
90
|
+
when 2
|
91
|
+
a= Matriz_dispersa.new(f.to_i,c.to_i)
|
92
|
+
end
|
93
|
+
op3=menu3
|
94
|
+
case op3
|
95
|
+
when 1
|
96
|
+
a.introducir_datos(1)
|
97
|
+
when 2
|
98
|
+
a.introducir_datos(2)
|
99
|
+
end
|
100
|
+
|
101
|
+
puts "Introduce la dimension de las matriz 2: "
|
102
|
+
print "Filas? "
|
103
|
+
STDOUT.flush
|
104
|
+
f=gets.chomp
|
105
|
+
print "Columnas? "
|
106
|
+
c=gets.chomp
|
107
|
+
op4=menu4
|
108
|
+
case op4
|
109
|
+
when 1
|
110
|
+
b= Matriz_densa.new(f.to_i,c.to_i)
|
111
|
+
when 2
|
112
|
+
b= Matriz_dispersa.new(f.to_i,c.to_i)
|
113
|
+
end
|
114
|
+
op3=menu3
|
115
|
+
case op3
|
116
|
+
when 1
|
117
|
+
b.introducir_datos(1)
|
118
|
+
when 2
|
119
|
+
b.introducir_datos(2)
|
120
|
+
end
|
121
|
+
puts a.to_s
|
122
|
+
puts b.to_s
|
123
|
+
|
124
|
+
when 2
|
125
|
+
puts "GENERAR MATRICES CON VALORES ALEATORIOS"
|
126
|
+
puts "Introduce la dimension de la matriz 1: "
|
127
|
+
print "Filas? "
|
128
|
+
STDOUT.flush
|
129
|
+
f=gets.chomp
|
130
|
+
print "Columnas? "
|
131
|
+
c=gets.chomp
|
132
|
+
op4=menu4
|
133
|
+
case op4
|
134
|
+
when 1
|
135
|
+
a= Matriz_densa.new(f.to_i,c.to_i)
|
136
|
+
when 2
|
137
|
+
a= Matriz_dispersa.new(f.to_i,c.to_i)
|
138
|
+
end
|
139
|
+
op3=menu3
|
140
|
+
case op3
|
141
|
+
when 1
|
142
|
+
a.generar(1)
|
143
|
+
when 2
|
144
|
+
a.generar(2)
|
145
|
+
end
|
146
|
+
|
147
|
+
puts "Introduce la dimension de la matriz 2: "
|
148
|
+
print "Filas? "
|
149
|
+
STDOUT.flush
|
150
|
+
f=gets.chomp
|
151
|
+
print "Columnas? "
|
152
|
+
c=gets.chomp
|
153
|
+
op4=menu4
|
154
|
+
case op4
|
155
|
+
when 1 #matriz densa
|
156
|
+
b= Matriz_densa.new(f.to_i,c.to_i)
|
157
|
+
when 2 #matriz dispersa
|
158
|
+
b= Matriz_dispersa.new(f.to_i,c.to_i)
|
159
|
+
end
|
160
|
+
op3=menu3
|
161
|
+
case op3
|
162
|
+
when 1
|
163
|
+
b.generar(1)
|
164
|
+
when 2
|
165
|
+
b.generar(2)
|
166
|
+
end
|
167
|
+
puts a.to_s
|
168
|
+
puts b.to_s
|
169
|
+
|
170
|
+
when 3
|
171
|
+
op2=0
|
172
|
+
while (op2!=5)
|
173
|
+
op2= menu2
|
174
|
+
case op2
|
175
|
+
when 1
|
176
|
+
puts "Suma"
|
177
|
+
puts "M1 + M2 = M3"
|
178
|
+
puts a.to_s
|
179
|
+
puts "+"
|
180
|
+
puts b.to_s
|
181
|
+
puts "="
|
182
|
+
c = a+b
|
183
|
+
puts c.to_s
|
184
|
+
|
185
|
+
puts "Resta"
|
186
|
+
puts "M1 - M2 = M3"
|
187
|
+
puts a.to_s
|
188
|
+
puts "-"
|
189
|
+
puts b.to_s
|
190
|
+
puts "="
|
191
|
+
c = a-b
|
192
|
+
puts c.to_s
|
193
|
+
pausa
|
194
|
+
|
195
|
+
when 2
|
196
|
+
puts "Mutiplicacion por un escalar"
|
197
|
+
print "Numero a multiplicar?: "
|
198
|
+
STDOUT.flush
|
199
|
+
num=gets.chomp
|
200
|
+
num = num.to_i
|
201
|
+
puts "#{num.to_i} * M1 = M3"
|
202
|
+
puts a.to_s
|
203
|
+
c= a*num
|
204
|
+
puts c.to_s
|
205
|
+
puts "Producto de matrices"
|
206
|
+
puts "M1 * M2 = M3"
|
207
|
+
puts a.to_s
|
208
|
+
puts "*"
|
209
|
+
puts b.to_s
|
210
|
+
puts "="
|
211
|
+
d=a*b
|
212
|
+
puts d.to_s
|
213
|
+
pausa
|
214
|
+
|
215
|
+
when 3
|
216
|
+
puts "Traspuesta de matrices"
|
217
|
+
puts "M1"
|
218
|
+
puts a.to_s
|
219
|
+
puts "traspuesta M1"
|
220
|
+
puts "#{a.t.to_s}"
|
221
|
+
puts "M2"
|
222
|
+
puts b.to_s
|
223
|
+
puts "traspuesta M2"
|
224
|
+
puts "#{b.t.to_s}"
|
225
|
+
pausa
|
226
|
+
|
227
|
+
when 4
|
228
|
+
puts "Elementos maximos y minimos"
|
229
|
+
puts "Maximo y minimo de M1"
|
230
|
+
puts a.to_s
|
231
|
+
puts "Max= #{a.max}"
|
232
|
+
puts "Min= #{a.min}"
|
233
|
+
puts "Maximo y minimo de M2"
|
234
|
+
puts b.to_s
|
235
|
+
puts "Max= #{b.max}"
|
236
|
+
puts "Min= #{b.min}"
|
237
|
+
pausa
|
238
|
+
end
|
239
|
+
end
|
240
|
+
when 4
|
241
|
+
puts "MOSTRAR"
|
242
|
+
puts "M1 = "
|
243
|
+
puts a.to_s
|
244
|
+
puts "M2 = "
|
245
|
+
puts b.to_s
|
246
|
+
if a==b
|
247
|
+
puts "Las matrices son iguales"
|
248
|
+
else
|
249
|
+
puts "Las matrices son diferentes"
|
250
|
+
end
|
251
|
+
end
|
252
|
+
pausa
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
256
|
+
principal
|
data/lib/gcd.rb
ADDED
data/lib/gcd.rb~
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
def gcd(u,v) #gcd = MCD = max. común divisor
|
2
|
+
u, v = u.abs, v.abs
|
3
|
+
while (v != 0)
|
4
|
+
u, v = v, u % v
|
5
|
+
end
|
6
|
+
u
|
7
|
+
end
|
8
|
+
|
9
|
+
def mcm (a, b) #mcm = MCM = mín. común múltiplo
|
10
|
+
(a*b)/gcd(a,b)
|
11
|
+
end
|
12
|
+
|
13
|
+
# puts "MCD = #{gcd(ARGV[0].to_i,ARGV[1].to_i)}"
|
14
|
+
# puts "MCM = #{mcm(ARGV[0].to_i,ARGV[1].to_i)}"
|
data/lib/matriz.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
|
2
|
+
class Matriz
|
3
|
+
attr_reader :fil, :col
|
4
|
+
|
5
|
+
def initialize (m,n) #Crea la matriz con las dimensiones especificadas por parametro
|
6
|
+
raise TypeError, "Error. Tipo de dimensiones incorrectas" if ((m < 0) or (n < 0))
|
7
|
+
@fil, @col = m, n
|
8
|
+
end
|
9
|
+
|
10
|
+
def to_s
|
11
|
+
end
|
12
|
+
|
13
|
+
def introducir_datos (o)
|
14
|
+
end
|
15
|
+
|
16
|
+
def generar
|
17
|
+
end
|
18
|
+
|
19
|
+
def +(other)
|
20
|
+
end
|
21
|
+
|
22
|
+
def -(other)
|
23
|
+
end
|
24
|
+
|
25
|
+
def *(other)
|
26
|
+
end
|
27
|
+
|
28
|
+
def t
|
29
|
+
end
|
30
|
+
|
31
|
+
def det
|
32
|
+
end
|
33
|
+
|
34
|
+
def ==(other)
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|