p10lppt13 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/.travis.yml +8 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +58 -0
- data/Guardfile +52 -0
- data/README.md +58 -0
- data/Rakefile +27 -0
- data/lib/fraccion.rb +143 -0
- data/lib/gcd.rb +7 -0
- data/lib/p10lppt13/version.rb +3 -0
- data/lib/p10lppt13.rb +277 -0
- data/p10lppt13.gemspec +20 -0
- data/spec/p10lppt13_spec.rb +134 -0
- data/test/tc_p10lppt13.rb +21 -0
- metadata +58 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8436d25fe500897f8296082a082742d4c4c11540
|
4
|
+
data.tar.gz: 1a08d13f564ab578c876318ee537270ec64c8fd9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 96eae38e7cfd7f988edfb41b821d83bdcbe85f8df50751734b430cb774241b69bb0c68e323ee955c315551d841d4a00e8ff8274a2937ffe8a3b295210831e97b
|
7
|
+
data.tar.gz: 7dfa726681d6ce045daf61c0ce78ee3d2583e8dade0c2ef055e589c65c4dbb1126f6b822cbc2b3a2a363372d888d7cbe08fa9d554e6dde70dc78032a8a0fb472
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
p10lppt13 (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://rubygems.org/
|
8
|
+
specs:
|
9
|
+
celluloid (0.15.2)
|
10
|
+
timers (~> 1.1.0)
|
11
|
+
coderay (1.0.9)
|
12
|
+
diff-lcs (1.2.4)
|
13
|
+
ffi (1.9.3)
|
14
|
+
formatador (0.2.4)
|
15
|
+
guard (2.2.3)
|
16
|
+
formatador (>= 0.2.4)
|
17
|
+
listen (~> 2.1)
|
18
|
+
lumberjack (~> 1.0)
|
19
|
+
pry (>= 0.9.12)
|
20
|
+
thor (>= 0.18.1)
|
21
|
+
guard-rspec (4.0.3)
|
22
|
+
guard (>= 2.1.1)
|
23
|
+
rspec (~> 2.14)
|
24
|
+
listen (2.2.0)
|
25
|
+
celluloid (>= 0.15.2)
|
26
|
+
rb-fsevent (>= 0.9.3)
|
27
|
+
rb-inotify (>= 0.9)
|
28
|
+
lumberjack (1.0.4)
|
29
|
+
method_source (0.8.2)
|
30
|
+
pry (0.9.12.2)
|
31
|
+
coderay (~> 1.0.5)
|
32
|
+
method_source (~> 0.8)
|
33
|
+
slop (~> 3.4)
|
34
|
+
rake (10.1.0)
|
35
|
+
rb-fsevent (0.9.3)
|
36
|
+
rb-inotify (0.9.2)
|
37
|
+
ffi (>= 0.5.0)
|
38
|
+
rspec (2.14.1)
|
39
|
+
rspec-core (~> 2.14.0)
|
40
|
+
rspec-expectations (~> 2.14.0)
|
41
|
+
rspec-mocks (~> 2.14.0)
|
42
|
+
rspec-core (2.14.7)
|
43
|
+
rspec-expectations (2.14.3)
|
44
|
+
diff-lcs (< 2.0, >= 1.1.3)
|
45
|
+
rspec-mocks (2.14.4)
|
46
|
+
slop (3.4.6)
|
47
|
+
thor (0.18.1)
|
48
|
+
timers (1.1.0)
|
49
|
+
|
50
|
+
PLATFORMS
|
51
|
+
ruby
|
52
|
+
|
53
|
+
DEPENDENCIES
|
54
|
+
guard
|
55
|
+
guard-rspec
|
56
|
+
p10lppt13!
|
57
|
+
rake
|
58
|
+
rspec
|
data/Guardfile
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
guard 'bundler' do
|
2
|
+
watch('Gemfile')
|
3
|
+
end
|
4
|
+
|
5
|
+
guard 'rspec', :version => 2 do
|
6
|
+
watch(%r{^spec/.+_spec\.rb$})
|
7
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/p10lppt13_spec.rb" }
|
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
|
+
|
31
|
+
|
32
|
+
guard :rspec do
|
33
|
+
watch(%r{^spec/.+_spec\.rb$})
|
34
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
35
|
+
watch('spec/spec_helper.rb') { "spec" }
|
36
|
+
|
37
|
+
# Rails example
|
38
|
+
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
39
|
+
watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
|
40
|
+
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"] }
|
41
|
+
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
|
42
|
+
watch('config/routes.rb') { "spec/routing" }
|
43
|
+
watch('app/controllers/application_controller.rb') { "spec/controllers" }
|
44
|
+
|
45
|
+
# Capybara features specs
|
46
|
+
watch(%r{^app/views/(.+)/.*\.(erb|haml|slim)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
|
47
|
+
|
48
|
+
# Turnip features and steps
|
49
|
+
watch(%r{^spec/acceptance/(.+)\.feature$})
|
50
|
+
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
|
51
|
+
end
|
52
|
+
|
data/README.md
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
[Práctica 10 de LPP](http://campusvirtual.ull.es/1314/mod/assign/view.php?id=95635)
|
2
|
+
=================
|
3
|
+
|
4
|
+
Alumnos:
|
5
|
+
- [José Alexander Delgado Hernández](http://campusvirtual.ull.es/1314/user/view.php?id=5990&course=1102) ([alu0100596403@ull.edu.es](mailto:alu0100596403@ull.edu.es))
|
6
|
+
- [Sem Ramos Herrera](http://campusvirtual.ull.es/1314/user/view.php?id=2455&course=1102) ([alu0100603194@ull.edu.es](mailto:alu0100603194@ull.edu.es))
|
7
|
+
|
8
|
+
=================
|
9
|
+
|
10
|
+
##Jerarquía de la práctica
|
11
|
+
.
|
12
|
+
├── bin
|
13
|
+
│ └── P10lppt13
|
14
|
+
├── Gemfile
|
15
|
+
├── .git
|
16
|
+
├── .gitignore
|
17
|
+
├── lib
|
18
|
+
│ ├── P10lppt13
|
19
|
+
│ │ └── version.rb
|
20
|
+
│ └── P10lppt13.rb
|
21
|
+
├── LICENSE.txt
|
22
|
+
├── P10lppt13.gemspec
|
23
|
+
├── Rakefile
|
24
|
+
├── README.md
|
25
|
+
├── .README.md.kate-swp
|
26
|
+
└── .travis.yml
|
27
|
+
|
28
|
+
##Clases que contendrá la práctica
|
29
|
+
|
30
|
+
En primer lugar, existirá una clase base "Matriz" abstracta, de la que heredarán "Dispera" y "Densa",
|
31
|
+
y en la que se encontrarán los métodos comunes a ambas subclases, que según el porcentaje de
|
32
|
+
elementos que sean igual a cero, se tratará de un tipo de matriz u otro.
|
33
|
+
|
34
|
+
## Installation
|
35
|
+
|
36
|
+
Add this line to your application's Gemfile:
|
37
|
+
|
38
|
+
gem 'Practica9'
|
39
|
+
|
40
|
+
And then execute:
|
41
|
+
|
42
|
+
$ bundle
|
43
|
+
|
44
|
+
Or install it yourself as:
|
45
|
+
|
46
|
+
$ gem install Practica9
|
47
|
+
|
48
|
+
## Usage
|
49
|
+
|
50
|
+
TODO: Write usage instructions here
|
51
|
+
|
52
|
+
## Contributing
|
53
|
+
|
54
|
+
1. Fork it
|
55
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
56
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
57
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
58
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
$:.unshift File.dirname(__FILE__) + 'lib'
|
2
|
+
require "bundler/gem_tasks"
|
3
|
+
require 'rspec/core/rake_task'
|
4
|
+
|
5
|
+
require 'rspec/core/rake_task'
|
6
|
+
RSpec::Core::RakeTask.new
|
7
|
+
task :default => :spec
|
8
|
+
|
9
|
+
desc "Ejecutando las pruebas para el codigo de Matrices"
|
10
|
+
task :speclocal do
|
11
|
+
sh "rspec -I. spec/p10lppt13_spec.rb"
|
12
|
+
end
|
13
|
+
|
14
|
+
desc "Ejecutando las pruebas para el codigo de Matrices con documentacion"
|
15
|
+
task :run do
|
16
|
+
sh "rspec -I. spec/p10lppt13_spec.rb --format documentation"
|
17
|
+
end
|
18
|
+
|
19
|
+
desc "Ejecutando las pruebas para el codigo de Matrices con documentacion"
|
20
|
+
task :thtml do
|
21
|
+
sh "rspec -I. spec/p10lppt13_spec.rb --format html"
|
22
|
+
end
|
23
|
+
|
24
|
+
desc "Ejecutando las pruebas unitarias para el codigo de Matrices"
|
25
|
+
task :test do
|
26
|
+
sh "ruby -Ilib -Itest test/tc_p10lppt13.rb"
|
27
|
+
end
|
data/lib/fraccion.rb
ADDED
@@ -0,0 +1,143 @@
|
|
1
|
+
require "gcd.rb"
|
2
|
+
|
3
|
+
#############################################################
|
4
|
+
# Clase Fraccion de la practica #7 de LPP #
|
5
|
+
#############################################################
|
6
|
+
|
7
|
+
class Fraccion
|
8
|
+
|
9
|
+
attr_reader :numer, :denomin
|
10
|
+
|
11
|
+
# Incluimos el módulo Comparable
|
12
|
+
include Comparable
|
13
|
+
|
14
|
+
def initialize(n,d)
|
15
|
+
@numer, @denomin = n, d
|
16
|
+
end
|
17
|
+
|
18
|
+
#############################################################
|
19
|
+
# Funciones basicas de la clase #
|
20
|
+
#############################################################
|
21
|
+
|
22
|
+
# Getter del num
|
23
|
+
def num()
|
24
|
+
return @numer
|
25
|
+
end
|
26
|
+
|
27
|
+
# Getter del denom
|
28
|
+
def denom()
|
29
|
+
return @denomin
|
30
|
+
end
|
31
|
+
|
32
|
+
# Funcion para simplificar y mostrar la fraccion en formato reducido
|
33
|
+
def simplificar
|
34
|
+
aux = gcd(self.numer, self.denomin) # MCD entre num y denom
|
35
|
+
Fraccion.new(self.numer/aux, self.denomin/aux) # Simplificamos la fraccion que le pasemos
|
36
|
+
end
|
37
|
+
|
38
|
+
#Mostramos los atributos en un string
|
39
|
+
def to_s
|
40
|
+
"#{@numer}/#{@denomin}"
|
41
|
+
end
|
42
|
+
|
43
|
+
#Mostramos los atributos en float
|
44
|
+
def to_f
|
45
|
+
@numer/@denomin.to_f
|
46
|
+
end
|
47
|
+
|
48
|
+
#############################################################
|
49
|
+
# Funciones de igualdad, absoluto, reciproco y opuesto #
|
50
|
+
#############################################################
|
51
|
+
|
52
|
+
# Funcion de comparar igualdad: Comparamos la fraccion implicita, con la que le pasamos como parametro
|
53
|
+
def ==(other)
|
54
|
+
if other.class.to_s != "Fraccion"
|
55
|
+
other = Fraccion.new(other,1)
|
56
|
+
end
|
57
|
+
if ((@numer == other.numer) and (@denomin == other.denomin))
|
58
|
+
true # Si el numerador y el denominador de la implicita coinciden con los
|
59
|
+
# de la pasada por parametro, entonces las fracciones son iguales (true)
|
60
|
+
else
|
61
|
+
false # Si no, son distintas (false)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
# Funcion que devuelve la funcion en valor absoluto
|
66
|
+
def absoluto
|
67
|
+
@numer = @numer.abs
|
68
|
+
@denomin = @denomin.abs
|
69
|
+
Fraccion.new(@numer,@denomin)
|
70
|
+
end
|
71
|
+
|
72
|
+
#Obtiene la fraccion reciproca a la implicita
|
73
|
+
def reciprocal
|
74
|
+
Fraccion.new(@denomin,@numer)
|
75
|
+
end
|
76
|
+
|
77
|
+
# Funcion que calcula la fraccion opuesta
|
78
|
+
def opuesto
|
79
|
+
Fraccion.new(-@numer,@denomin)
|
80
|
+
end
|
81
|
+
|
82
|
+
#############################################################
|
83
|
+
# Funciones de suma, resta, multiplicacion y division #
|
84
|
+
#############################################################
|
85
|
+
|
86
|
+
# Necesitamos hallar el minimo comun multiplo para la suma y la resta
|
87
|
+
def mcm(n, m)
|
88
|
+
aux=gcd(n,m)
|
89
|
+
(n/aux)*m
|
90
|
+
end
|
91
|
+
|
92
|
+
# Realizamos la division de la fraccion implicita con recibida como parametro
|
93
|
+
def /(other)
|
94
|
+
temp = Fraccion.new(@numer*other.denomin, @denomin*other.numer)
|
95
|
+
temp.simplificar
|
96
|
+
end
|
97
|
+
|
98
|
+
# Realizamos la suma de la fraccion implicita con recibida como parametro
|
99
|
+
def +(other)
|
100
|
+
if(other.instance_of?Fixnum)
|
101
|
+
temp=other
|
102
|
+
other=Fraccion.new(temp,1)
|
103
|
+
end
|
104
|
+
|
105
|
+
aux = mcm(@denomin, other.denomin)
|
106
|
+
temp = Fraccion.new(((aux/@denomin)*@numer)+((aux/other.denomin)*other.numer),aux)
|
107
|
+
temp.simplificar
|
108
|
+
end
|
109
|
+
|
110
|
+
# Realizamos la resta de la fraccion implicita con recibida como parametro
|
111
|
+
def -(other)
|
112
|
+
aux = mcm(@denomin, other.denomin)
|
113
|
+
temp = Fraccion.new(((aux/@denomin)*@numer)-((aux/other.denomin)*other.numer),aux)
|
114
|
+
temp.simplificar
|
115
|
+
end
|
116
|
+
|
117
|
+
# Realizamos la multiplicacion de la fraccion implicita con recibida como parametro
|
118
|
+
def *(other)
|
119
|
+
temp=Fraccion.new(@numer*other.numer, @denomin*other.denomin)
|
120
|
+
temp.simplificar
|
121
|
+
end
|
122
|
+
|
123
|
+
#############################################################
|
124
|
+
# Funciones de modulo y comparacionales (menor, mayor, #
|
125
|
+
# menor o igual y mayor o igual)
|
126
|
+
#############################################################
|
127
|
+
|
128
|
+
def %(other) #Obtenemos el resto de la division de dos fracciones
|
129
|
+
aux1 = self.numer * other.denomin #Multiplicamos el numerador de la primera entre el denominador de la segunda
|
130
|
+
aux2 = self.denomin * other.numer #Multiplicamos el denominador de la primera entre el numerador de la segunda
|
131
|
+
resto = aux1%aux2 #Obtenemos el Resto de la fraccion resultante
|
132
|
+
end
|
133
|
+
|
134
|
+
def <=>(other)
|
135
|
+
self.to_f <=> other.to_f
|
136
|
+
end
|
137
|
+
|
138
|
+
def coerce(other)
|
139
|
+
#aux = Fraccion.new(other,1)
|
140
|
+
[self,other]
|
141
|
+
end
|
142
|
+
|
143
|
+
end
|
data/lib/gcd.rb
ADDED
data/lib/p10lppt13.rb
ADDED
@@ -0,0 +1,277 @@
|
|
1
|
+
require "fraccion.rb"
|
2
|
+
|
3
|
+
class Matriz
|
4
|
+
|
5
|
+
attr_reader :nfil, :ncol
|
6
|
+
# Metodo initilize de la clase base
|
7
|
+
$acum
|
8
|
+
def initialize(nfil, ncol)
|
9
|
+
@nfil = nfil
|
10
|
+
@ncol = ncol
|
11
|
+
end
|
12
|
+
|
13
|
+
# Getter de nfil
|
14
|
+
def filas
|
15
|
+
@nfil
|
16
|
+
end
|
17
|
+
|
18
|
+
# Getter de ncol
|
19
|
+
def colum
|
20
|
+
@ncol
|
21
|
+
end
|
22
|
+
|
23
|
+
# Metodo para convertir la matriz a string
|
24
|
+
def to_s
|
25
|
+
aux = ""
|
26
|
+
@nfil.times do |i|
|
27
|
+
@ncol.times do |j|
|
28
|
+
aux << "#{pos[i][j]}\t"
|
29
|
+
end
|
30
|
+
aux << "\n"
|
31
|
+
end
|
32
|
+
aux
|
33
|
+
end
|
34
|
+
|
35
|
+
# Metodo para sumar dos matrices
|
36
|
+
def +(other)
|
37
|
+
if ((@nfil != other.nfil) || (@ncol != other.ncol))
|
38
|
+
puts "No se pueden sumar las matrices"
|
39
|
+
else
|
40
|
+
elemento = Array.new(0)
|
41
|
+
for i in 0...filas
|
42
|
+
fila = Array.new(0)
|
43
|
+
for j in 0...colum
|
44
|
+
if self.pos[i][j] == nil && other.pos[i][j] != nil
|
45
|
+
fila << other.pos[i][j]
|
46
|
+
elsif self.pos[i][j] != nil && other.pos[i][j] == nil
|
47
|
+
fila << pos[i][j]
|
48
|
+
elsif self.pos[i][j] == nil && other.pos[i][j] == nil
|
49
|
+
fila << 0
|
50
|
+
else
|
51
|
+
fila << pos[i][j] + other.pos[i][j]
|
52
|
+
end
|
53
|
+
end
|
54
|
+
elemento << fila
|
55
|
+
end
|
56
|
+
Densa.new(@nfil, @ncol, elemento)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# Metodo para restar dos matrices
|
61
|
+
def -(other)
|
62
|
+
if ((@nfil != other.nfil) || (@ncol != other.ncol))
|
63
|
+
puts "No se pueden restar las matrices"
|
64
|
+
else
|
65
|
+
elemento = Array.new(0)
|
66
|
+
for i in 0...filas
|
67
|
+
fila = Array.new(0)
|
68
|
+
for j in 0...colum
|
69
|
+
if self.pos[i][j] == nil && other.pos[i][j] != nil
|
70
|
+
fila << -other.pos[i][j]
|
71
|
+
elsif self.pos[i][j] != nil && other.pos[i][j] == nil
|
72
|
+
fila << pos[i][j]
|
73
|
+
elsif self.pos[i][j] == nil && other.pos[i][j] == nil
|
74
|
+
fila << 0
|
75
|
+
else
|
76
|
+
fila << pos[i][j] - other.pos[i][j]
|
77
|
+
end
|
78
|
+
end
|
79
|
+
elemento << fila
|
80
|
+
end
|
81
|
+
Densa.new(@nfil, @ncol, elemento)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def primervalor
|
86
|
+
for i in 0...@nfil
|
87
|
+
for j in 0...@nncol
|
88
|
+
if (self.pos[i][j] != nil)
|
89
|
+
return self.pos[i][j]
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
# Metodo para hallar el maximo de una matriz
|
96
|
+
def max
|
97
|
+
acum = pos[0][0]
|
98
|
+
for i in 0...filas
|
99
|
+
for j in 0...colum
|
100
|
+
if self.pos[i][j] != nil
|
101
|
+
if pos[i][j] > acum
|
102
|
+
acum = pos[i][j]
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
acum
|
108
|
+
end
|
109
|
+
|
110
|
+
# Metodo para hallar el minimo de una matriz
|
111
|
+
def min
|
112
|
+
acum = pos[0][0]
|
113
|
+
for i in 0...filas
|
114
|
+
for j in 0...colum
|
115
|
+
if self.pos[i][j] != nil
|
116
|
+
if pos[i][j] < acum
|
117
|
+
acum = pos[i][j]
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
acum
|
123
|
+
end
|
124
|
+
|
125
|
+
# Metodo para multiplicacion dos matrices
|
126
|
+
def por(other)
|
127
|
+
if ((@nfil != other.ncol) || (@ncol != other.nfil))
|
128
|
+
puts "No se pueden multiplicarr las matrices"
|
129
|
+
else
|
130
|
+
elemento = Array.new(0)
|
131
|
+
for i in 0...nfil
|
132
|
+
fila = Array.new(0)
|
133
|
+
for j in 0...other.ncol
|
134
|
+
aux = 0
|
135
|
+
for k in 0...ncol
|
136
|
+
aux += pos[i][k] * other.pos[k][j]
|
137
|
+
end
|
138
|
+
fila << aux
|
139
|
+
end
|
140
|
+
elemento << fila
|
141
|
+
end
|
142
|
+
end
|
143
|
+
Matriz.new(@nfil, other.ncol, elemento)
|
144
|
+
end
|
145
|
+
|
146
|
+
# Metodo para multiplicacion dos matrices
|
147
|
+
def porf(other)
|
148
|
+
if ((@nfil != other.ncol) || (@ncol != other.nfil))
|
149
|
+
puts "No se pueden multiplicarr las matrices"
|
150
|
+
else
|
151
|
+
elemento = Array.new(0)
|
152
|
+
for i in 0...nfil
|
153
|
+
fila = Array.new(0)
|
154
|
+
for j in 0...other.ncol
|
155
|
+
aux = Fraccion.new(1,1)
|
156
|
+
aux = aux - aux
|
157
|
+
for k in 0...ncol
|
158
|
+
aux += pos[i][k] * other.pos[k][j]
|
159
|
+
end
|
160
|
+
fila << aux
|
161
|
+
end
|
162
|
+
elemento << fila
|
163
|
+
end
|
164
|
+
end
|
165
|
+
Densa.new(@nfil, other.ncol, elemento)
|
166
|
+
end
|
167
|
+
|
168
|
+
# Metodo para multiplicar una matriz por un escalar
|
169
|
+
def *(other)
|
170
|
+
elemento = Array.new(0)
|
171
|
+
for i in 0...filas
|
172
|
+
fila = Array.new(0)
|
173
|
+
for j in 0...colum
|
174
|
+
fila << pos[i][j]*other
|
175
|
+
end
|
176
|
+
elemento << fila
|
177
|
+
end
|
178
|
+
Matriz.new(@nfil, @ncol, elemento)
|
179
|
+
end
|
180
|
+
|
181
|
+
# Metodo para hallar la traspuesta de una matriz
|
182
|
+
def trasponer
|
183
|
+
elemento = Array.new(0)
|
184
|
+
for i in 0...colum
|
185
|
+
fila = Array.new(0)
|
186
|
+
for j in 0...filas
|
187
|
+
fila << pos[j][i]
|
188
|
+
end
|
189
|
+
elemento << fila
|
190
|
+
end
|
191
|
+
Densa.new(@ncol, @nfil, elemento)
|
192
|
+
end
|
193
|
+
|
194
|
+
|
195
|
+
# Metodo para convertir la matriz en un vector
|
196
|
+
def vectorizar
|
197
|
+
aux = Array.new(0)
|
198
|
+
for i in 0...colum
|
199
|
+
for j in 0...filas
|
200
|
+
aux << pos[i][j]
|
201
|
+
end
|
202
|
+
end
|
203
|
+
Array.new(aux)
|
204
|
+
end
|
205
|
+
|
206
|
+
end
|
207
|
+
|
208
|
+
# Clase de Matrices Densas
|
209
|
+
class Densa < Matriz
|
210
|
+
|
211
|
+
attr_reader :pos
|
212
|
+
|
213
|
+
|
214
|
+
def initialize(nfil, ncol, pos)
|
215
|
+
super(nfil, ncol)
|
216
|
+
@pos = Array.new(pos)
|
217
|
+
end
|
218
|
+
|
219
|
+
def [](i)
|
220
|
+
@data[i]
|
221
|
+
end
|
222
|
+
|
223
|
+
def []=(i,value)
|
224
|
+
@data[i] = value
|
225
|
+
end
|
226
|
+
|
227
|
+
end
|
228
|
+
|
229
|
+
# Clase de Vectores dispersos
|
230
|
+
class VectorDisperso
|
231
|
+
attr_reader :vector
|
232
|
+
|
233
|
+
def initialize(h = {})
|
234
|
+
@vector = Hash.new(0)
|
235
|
+
@vector = @vector.merge!(h)
|
236
|
+
end
|
237
|
+
|
238
|
+
def [](i)
|
239
|
+
@vector[i]
|
240
|
+
end
|
241
|
+
|
242
|
+
def to_s
|
243
|
+
@vector.to_s
|
244
|
+
end
|
245
|
+
end
|
246
|
+
|
247
|
+
# Clase de Matrices Dispersas
|
248
|
+
class Dispersa < Matriz
|
249
|
+
attr_reader :pos
|
250
|
+
|
251
|
+
def initialize(nfil, ncol, h = {})
|
252
|
+
super(nfil, ncol)
|
253
|
+
@pos = Hash.new({})
|
254
|
+
for k in h.keys do
|
255
|
+
@pos[k] = if h[k].is_a? VectorDisperso
|
256
|
+
h[k]
|
257
|
+
else
|
258
|
+
@pos[k] = VectorDisperso.new(h[k])
|
259
|
+
end
|
260
|
+
end
|
261
|
+
end
|
262
|
+
=begin
|
263
|
+
for i in self.pos.keys do
|
264
|
+
=end
|
265
|
+
def [](i)
|
266
|
+
@pos[i]
|
267
|
+
end
|
268
|
+
|
269
|
+
def col(j)
|
270
|
+
c = {}
|
271
|
+
for r in @pos.keys do
|
272
|
+
c[r] = @pos[r].vector[j] if @pos[r].vector.keys.include? j
|
273
|
+
end
|
274
|
+
VectorDisperso.new c
|
275
|
+
end
|
276
|
+
|
277
|
+
end
|
data/p10lppt13.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "p10lppt13/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "p10lppt13"
|
7
|
+
s.version = P10lppt13::VERSION
|
8
|
+
s.authors = ["José Alexander Delgado Hernández, Sem Ramos Herrera"]
|
9
|
+
s.email = ["alu0100603194@ull.edu.es"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Gema de matrices densas y dispersas}
|
12
|
+
s.description = %q{Implementación de varias clases con matrices densas y dispersas}
|
13
|
+
|
14
|
+
s.rubyforge_project = "p10lppt13"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
end
|
@@ -0,0 +1,134 @@
|
|
1
|
+
require "p10lppt13.rb"
|
2
|
+
|
3
|
+
describe Matriz do
|
4
|
+
before :each do
|
5
|
+
@m1 = Densa.new(2,2,[[1,2],[3,4]])
|
6
|
+
@m2 = Densa.new(3,3,[[1,2,4],[6,5,8],[2,5,3]])
|
7
|
+
@m3 = Dispersa.new(3,3, 0 => { 0 => 2, 2 => 1 })
|
8
|
+
@m4 = Densa.new(3,3,[[-1,-2,-4],[-3,-5,-1],[-2,-5,-3]])
|
9
|
+
@f1 = Fraccion.new(1,2)
|
10
|
+
@f2 = Fraccion.new(3,2)
|
11
|
+
@f3 = Fraccion.new(5,2)
|
12
|
+
@mf1 = Densa.new(2,2,[[@f1,@f2],[@f3,@f1]])
|
13
|
+
@mf2 = Densa.new(2,2,[[@f1,@f2],[@f1,@f2]])
|
14
|
+
@disper = Dispersa.new(2,2, 0 => { 0 => @f1 })
|
15
|
+
end
|
16
|
+
|
17
|
+
# Comprobacion de que se crea bien la matriz
|
18
|
+
describe "Comprobamos el numero de...\n" do
|
19
|
+
it " - Filas para una matriz densa 3x3\n" do
|
20
|
+
@m2.nfil.should eq(3)
|
21
|
+
end
|
22
|
+
it " - Columnas de una matriz densa 3x3\n" do
|
23
|
+
@m2.ncol.should eq(3)
|
24
|
+
end
|
25
|
+
it "Acceso al elemento [0][0]" do
|
26
|
+
@m1.pos[0][0].should eq(1)
|
27
|
+
end
|
28
|
+
it "Acceso al elemento [0][1]" do
|
29
|
+
@m1.pos[0][1].should eq(2)
|
30
|
+
end
|
31
|
+
it "Acceso al elemento [1][0]" do
|
32
|
+
@m1.pos[1][0].should eq(3)
|
33
|
+
end
|
34
|
+
it "Acceso al elemento [1][1]" do
|
35
|
+
@m1.pos[1][1].should eq(4)
|
36
|
+
end
|
37
|
+
it " - Filas para una matriz dispersa 3x3\n" do
|
38
|
+
@m3.nfil.should eq(3)
|
39
|
+
end
|
40
|
+
it " - Columnas de una matriz dispersa 3x3\n" do
|
41
|
+
@m3.ncol.should eq(3)
|
42
|
+
end
|
43
|
+
it "Acceso al elemento [0][2]" do
|
44
|
+
@m3[0][2].should eq(1)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe "Operaciones aritmeticas" do
|
49
|
+
it "Suma de dos matrices densas" do
|
50
|
+
@aux= @m1+@m1
|
51
|
+
@aux.to_s.should eq("2\t4\t\n6\t8\t\n")
|
52
|
+
end
|
53
|
+
|
54
|
+
it "Suma de matriz densa y matriz dispersa" do
|
55
|
+
@aux=@m2+@m3
|
56
|
+
@aux.to_s.should eq("3\t2\t5\t\n6\t5\t8\t\n2\t5\t3\t\n")
|
57
|
+
end
|
58
|
+
|
59
|
+
it "Suma de dos matrices dispersas" do
|
60
|
+
@aux=@m3+@m3
|
61
|
+
@aux.to_s.should eq("4\t0\t2\t\n0\t0\t0\t\n0\t0\t0\t\n")
|
62
|
+
end
|
63
|
+
it "Resta de dos matrices densas" do
|
64
|
+
@aux= @m1+@m1
|
65
|
+
@aux2 = @aux-@m1
|
66
|
+
@aux2.to_s.should eq(@m1.to_s)
|
67
|
+
end
|
68
|
+
|
69
|
+
it "Resta de matriz densa y matriz dispersa" do
|
70
|
+
@aux=@m2-@m3
|
71
|
+
@aux.to_s.should eq("-1\t2\t3\t\n6\t5\t8\t\n2\t5\t3\t\n")
|
72
|
+
end
|
73
|
+
|
74
|
+
it "Resta de dos matrices dispersas" do
|
75
|
+
@aux=@m3+@m3
|
76
|
+
@aux2=@aux-@m3
|
77
|
+
@aux2.to_s.should eq("2\t0\t1\t\n0\t0\t0\t\n0\t0\t0\t\n")
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe "Maximo y minimo" do
|
82
|
+
it "Maximo dispersa" do
|
83
|
+
@m3.max.to_s.should eq("2")
|
84
|
+
end
|
85
|
+
it "Minimo dispersa" do
|
86
|
+
@m3.min.to_s.should eq("0")
|
87
|
+
end
|
88
|
+
it "Maximo densa" do
|
89
|
+
@m1.max.to_s.should eq("4")
|
90
|
+
end
|
91
|
+
it "Minimo densa" do
|
92
|
+
@m1.min.to_s.should eq("1")
|
93
|
+
end
|
94
|
+
it "Maximo densa con valores negativos" do
|
95
|
+
@m4.max.should eq(-1)
|
96
|
+
end
|
97
|
+
it "Minimo densa con valores negativos" do
|
98
|
+
@m4.min.should eq(-5)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
describe "Operaciones con matrices con fracciones" do
|
103
|
+
it "Comprobamos que realiza bien la traspuesta" do
|
104
|
+
@aux = Densa.new(2,2,[[@f1,@f3],[@f2,@f1]])
|
105
|
+
@mf1.trasponer.to_s.should eq(@aux.to_s)
|
106
|
+
end
|
107
|
+
it "Comprobamos la suma" do
|
108
|
+
@aux = Densa.new(2,2,[[1,2],[3,4]])
|
109
|
+
@af1 = Fraccion.new(3,2)
|
110
|
+
@af2 = Fraccion.new(7,2)
|
111
|
+
@af3 = Fraccion.new(11,2)
|
112
|
+
@af4 = Fraccion.new(9,2)
|
113
|
+
@aux3 = Densa.new(2,2,[[@af1,@af2],[@af3,@af4]])
|
114
|
+
@aux2 = @aux+@mf1
|
115
|
+
@aux2.to_s.should eq(@aux3.to_s)
|
116
|
+
end
|
117
|
+
it "Comprobamos la multiplicacion" do
|
118
|
+
@aux = Densa.new(2,2,[[@f1,@f1],[@f1,@f1]])
|
119
|
+
@aux2 = @aux.porf(@aux)
|
120
|
+
@aux3 = Densa.new(2,2,[[@f1,@f1],[@f1,@f1]])
|
121
|
+
@aux2.to_s.should eq(@aux3.to_s)
|
122
|
+
end
|
123
|
+
it "Comprobamos el maximo" do
|
124
|
+
@aux = Densa.new(2,2,[[@f1,1],[@f3,@f1]])
|
125
|
+
@aux.max.should eq(@f3)
|
126
|
+
end
|
127
|
+
it "Comprobamos el minimo" do
|
128
|
+
@aux = Densa.new(2,2,[[1,@f2],[@f3,@f1]])
|
129
|
+
@aux.min.should eq(@f1)
|
130
|
+
end
|
131
|
+
|
132
|
+
end
|
133
|
+
|
134
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
|
2
|
+
require "p10lppt13.rb"
|
3
|
+
|
4
|
+
require "test/unit"
|
5
|
+
|
6
|
+
class TestP10lppt13 < Test::Unit::TestCase
|
7
|
+
|
8
|
+
def test_prueba
|
9
|
+
|
10
|
+
@m1 = Densa.new(2,2,[[1,2],[3,4]])
|
11
|
+
@m2 = Densa.new(3,3,[[1,2,4],[6,5,8],[2,5,3]])
|
12
|
+
@m3 = Dispersa.new(3,3, 0 => { 0 => 2, 2 => 1 })
|
13
|
+
|
14
|
+
assert_equal "1\t2\t\n3\t4\t\n", @m1.to_s
|
15
|
+
assert_not_equal "4\t2\t\n3\t4\t\n", @m1.to_s
|
16
|
+
assert_not_nil @m3.pos[0][0]
|
17
|
+
assert_not_same @m3.pos[0][0], @m1.pos[0][2]
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: p10lppt13
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- José Alexander Delgado Hernández, Sem Ramos Herrera
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-11-22 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Implementación de varias clases con matrices densas y dispersas
|
14
|
+
email:
|
15
|
+
- alu0100603194@ull.edu.es
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- .gitignore
|
21
|
+
- .travis.yml
|
22
|
+
- Gemfile
|
23
|
+
- Gemfile.lock
|
24
|
+
- Guardfile
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- lib/fraccion.rb
|
28
|
+
- lib/gcd.rb
|
29
|
+
- lib/p10lppt13.rb
|
30
|
+
- lib/p10lppt13/version.rb
|
31
|
+
- p10lppt13.gemspec
|
32
|
+
- spec/p10lppt13_spec.rb
|
33
|
+
- test/tc_p10lppt13.rb
|
34
|
+
homepage: ''
|
35
|
+
licenses: []
|
36
|
+
metadata: {}
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - '>='
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
requirements: []
|
52
|
+
rubyforge_project: p10lppt13
|
53
|
+
rubygems_version: 2.0.3
|
54
|
+
signing_key:
|
55
|
+
specification_version: 4
|
56
|
+
summary: Gema de matrices densas y dispersas
|
57
|
+
test_files: []
|
58
|
+
has_rdoc:
|