prct09 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 +18 -0
- data/.travis.yml +8 -0
- data/Gemfile +4 -0
- data/Guarfile +29 -0
- data/LICENSE.txt +22 -0
- data/README.md +78 -0
- data/Rakefile +29 -0
- data/bin/prct09 +3 -0
- data/lib/Fraccion.rb +87 -0
- data/lib/Matriz.rb +103 -0
- data/lib/Matriz_densa.rb +121 -0
- data/lib/Matriz_dispersa.rb +201 -0
- data/lib/mcd.rb +9 -0
- data/lib/prct09/version.rb +3 -0
- data/lib/prct09.rb +5 -0
- data/prct09.gemspec +26 -0
- data/spec/Matriz_spec.rb +61 -0
- data/test/tc_matriz.rb +70 -0
- metadata +148 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Guarfile
ADDED
@@ -0,0 +1,29 @@
|
|
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
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Mauricio Cavalleri, katerine Cardona
|
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,78 @@
|
|
1
|
+
# PRCT09
|
2
|
+
|
3
|
+
DESCRIPCIÓN:
|
4
|
+
|
5
|
+
Esta práctica es la creación del "diseño" de la jerarquía de clases para representar 'matrices densas' y 'matrices dispersas'.
|
6
|
+
|
7
|
+
## INSTALACION
|
8
|
+
|
9
|
+
Añade esta línea a Gemfile de la aplicación:
|
10
|
+
|
11
|
+
gem 'prct09'
|
12
|
+
|
13
|
+
Y luego ejecutar:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
O instalarlo usted mismo como:
|
18
|
+
|
19
|
+
$ Gem install prct09
|
20
|
+
|
21
|
+
## CREACION DE GEMA
|
22
|
+
|
23
|
+
:~/$ bundle gem -b Prct9
|
24
|
+
create Prct9/Gemfile
|
25
|
+
create Prct9/Rakefile
|
26
|
+
create Prct9/LICENSE
|
27
|
+
create Prct9/README.md
|
28
|
+
create Prct9/.gitignore
|
29
|
+
create Prct9/Prct9.gemspec
|
30
|
+
create Prct9/lib/Prct9.rb
|
31
|
+
create Prct9/lib/Prct9/version.rb
|
32
|
+
create Prct9/bin/Prct9
|
33
|
+
|
34
|
+
## ARBOL DE LA GEMA
|
35
|
+
.
|
36
|
+
├── bin
|
37
|
+
│ └── prct09
|
38
|
+
├── Gemfile
|
39
|
+
├── lib
|
40
|
+
│ ├── prct09
|
41
|
+
│ │ └── version.rb
|
42
|
+
│ └── prct09.rb
|
43
|
+
├── LICENSE.txt
|
44
|
+
├── prct09.gemspec
|
45
|
+
├── Rakefile
|
46
|
+
└── README.md
|
47
|
+
|
48
|
+
|
49
|
+
## CONTRIBUCION
|
50
|
+
|
51
|
+
|
52
|
+
La implementación de esta práctica se hara en un fichero dentro de la carpeta lib donde reutilizaremos la clase matriz ya creada y de ella derivaran la clase matriz dispersa (Una matriz se considerará dispersa si tiene más de un 60% de ceros) y la clase matriz densa donde heredaran los metodos +, -, *, to_s y se sobreescribiran en caso de que queramos otro tipo de implementacion.
|
53
|
+
|
54
|
+
class Matriz
|
55
|
+
end
|
56
|
+
|
57
|
+
class MatrizDispersa < Matriz
|
58
|
+
end
|
59
|
+
|
60
|
+
class MatrizDensa < Matriz
|
61
|
+
end
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
|
78
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
|
3
|
+
$:.unshift File.dirname(__FILE__) + 'lib'
|
4
|
+
|
5
|
+
require 'rspec/core/rake_task'
|
6
|
+
RSpec::Core::RakeTask.new
|
7
|
+
|
8
|
+
task :default => :spec
|
9
|
+
|
10
|
+
desc "test con --format documentation"
|
11
|
+
task :spec do
|
12
|
+
sh "rspec spec/Matriz_spec.rb --format documentation"
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
desc "Ejecutar matriz.rb"
|
17
|
+
task :bin do
|
18
|
+
sh "ruby lib/Matriz.rb"
|
19
|
+
end
|
20
|
+
|
21
|
+
desc "Ejecutar test con formato html"
|
22
|
+
task :thtml do
|
23
|
+
sh "rspec spec/Matriz_spec.rb --format html"
|
24
|
+
end
|
25
|
+
|
26
|
+
desc "Test Unitarios"
|
27
|
+
task :test do
|
28
|
+
sh "ruby -Ilib test/tc_matriz.rb"
|
29
|
+
end
|
data/bin/prct09
ADDED
data/lib/Fraccion.rb
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
|
2
|
+
class Fraccion
|
3
|
+
attr_reader :n, :d
|
4
|
+
|
5
|
+
include Comparable
|
6
|
+
|
7
|
+
def mcd(a,b)
|
8
|
+
d = a.abs, b.abs
|
9
|
+
while d.min != 0
|
10
|
+
d = d.min, d.max%d.min
|
11
|
+
end
|
12
|
+
d.max
|
13
|
+
end
|
14
|
+
private :mcd
|
15
|
+
|
16
|
+
#Construccion
|
17
|
+
def initialize(n, d)
|
18
|
+
@n = n / mcd(n,d)
|
19
|
+
@d = d / mcd(n,d)
|
20
|
+
end
|
21
|
+
|
22
|
+
#Pasar a string el objeto Fraccion
|
23
|
+
def imprimirFraccion
|
24
|
+
"#{@n}/#{@d}"
|
25
|
+
end
|
26
|
+
|
27
|
+
#Pasar a flotante el objeto Fraccion
|
28
|
+
def imprimirFlotante
|
29
|
+
@n.to_f/@d.to_f
|
30
|
+
end
|
31
|
+
|
32
|
+
#Sobrecarga de operadores
|
33
|
+
|
34
|
+
def + (other)
|
35
|
+
Fraccion.new(@n* other.d + other.n*@d, @d * other.d)
|
36
|
+
end
|
37
|
+
|
38
|
+
def - (other)
|
39
|
+
Fraccion.new(@n* other.d - other.n*@d, @d * other.d)
|
40
|
+
end
|
41
|
+
|
42
|
+
def * (other)
|
43
|
+
Fraccion.new(@n* other.n, @d * other.d)
|
44
|
+
end
|
45
|
+
|
46
|
+
def / (other)
|
47
|
+
Fraccion.new(@n* other.d, @d * other.n)
|
48
|
+
end
|
49
|
+
|
50
|
+
def % (other)
|
51
|
+
Fraccion.new((imprimirFlotante % other.imprimirFlotante*1000).to_i, 1000)
|
52
|
+
end
|
53
|
+
|
54
|
+
def <=> (other)
|
55
|
+
imprimirFlotante <=> other.imprimirFlotante
|
56
|
+
end
|
57
|
+
|
58
|
+
#valor absoluto
|
59
|
+
def abs
|
60
|
+
if (@n < 0) ^ (@d < 0)
|
61
|
+
if @n < 0
|
62
|
+
Fraccion.new(@n*-1, @d)
|
63
|
+
else
|
64
|
+
Fraccion.new(@n, @d*-1)
|
65
|
+
end
|
66
|
+
|
67
|
+
elsif (@n < 0) && (@d < 0)
|
68
|
+
Fraccion.new(@n*-1, @d*-1)
|
69
|
+
|
70
|
+
else
|
71
|
+
Fraccion.new(@n, @d)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
|
76
|
+
#inverso de una fraccion
|
77
|
+
def reciprocal
|
78
|
+
Fraccion.new(@d, @n)
|
79
|
+
end
|
80
|
+
|
81
|
+
|
82
|
+
#opuesta de una fraccion
|
83
|
+
def -@
|
84
|
+
Fraccion.new(@n*-1, @d)
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
data/lib/Matriz.rb
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
require "Fraccion.rb"
|
2
|
+
class Matriz
|
3
|
+
|
4
|
+
attr_accessor :filas, :cols, :matriz
|
5
|
+
|
6
|
+
def initialize(m)
|
7
|
+
@filas = m.size
|
8
|
+
@cols = m[1].size
|
9
|
+
@matriz = m
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_s ()
|
13
|
+
@matriz.each do |fila|
|
14
|
+
puts fila.join(" ")
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def + (other)
|
19
|
+
aux_m = Array.new
|
20
|
+
for i in 0...@filas do
|
21
|
+
aux_m[i] = Array.new
|
22
|
+
for j in 0...@cols do
|
23
|
+
aux_m[i][j] = 0;
|
24
|
+
end
|
25
|
+
end
|
26
|
+
for i in 0...@filas do
|
27
|
+
for j in 0...@cols do
|
28
|
+
aux_m[i][j] = @matriz[i][j] + other.matriz[i][j]
|
29
|
+
end
|
30
|
+
end
|
31
|
+
Matriz.new(aux_m).matriz
|
32
|
+
end
|
33
|
+
|
34
|
+
def - (other)
|
35
|
+
aux_m = Array.new
|
36
|
+
for i in 0...@filas do
|
37
|
+
aux_m[i] = Array.new
|
38
|
+
for j in 0...@cols do
|
39
|
+
aux_m[i][j] = 0;
|
40
|
+
end
|
41
|
+
end
|
42
|
+
for i in 0...@filas do
|
43
|
+
for j in 0...@cols do
|
44
|
+
aux_m[i][j] = @matriz[i][j] - other.matriz[i][j]
|
45
|
+
end
|
46
|
+
end
|
47
|
+
Matriz.new(aux_m).matriz
|
48
|
+
end
|
49
|
+
|
50
|
+
def * (other)
|
51
|
+
aux_m = Array.new
|
52
|
+
for i in 0...@filas do
|
53
|
+
aux_m[i] = Array.new
|
54
|
+
for j in 0...@cols do
|
55
|
+
aux_m[i][j] = 0;
|
56
|
+
end
|
57
|
+
end
|
58
|
+
for i in 0...@filas do
|
59
|
+
for j in 0...other.cols do
|
60
|
+
for k in 0...other.filas do
|
61
|
+
aux_m[i][j] += @matriz[i][k] * other.matriz[k][j]
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
Matriz.new(aux_m).matriz
|
66
|
+
end
|
67
|
+
|
68
|
+
def traspuesta ()
|
69
|
+
aux_m = Array.new
|
70
|
+
for i in 0...@filas do
|
71
|
+
aux_m[i] = Array.new
|
72
|
+
for j in 0...@cols do
|
73
|
+
aux_m[i][j] = @matriz[(@filas-1)-i][(@cols-1)-j]
|
74
|
+
end
|
75
|
+
end
|
76
|
+
Matriz.new(aux_m).comprobar
|
77
|
+
end
|
78
|
+
|
79
|
+
def comprobar()
|
80
|
+
cont_total = 0
|
81
|
+
cont_ceros = 0
|
82
|
+
for i in 0...@filas do
|
83
|
+
for j in 0...@cols do
|
84
|
+
if @matriz[i][j] == 0
|
85
|
+
cont_ceros = cont_ceros + 1
|
86
|
+
end
|
87
|
+
cont_total = cont_total + 1
|
88
|
+
end
|
89
|
+
end
|
90
|
+
if cont_ceros >= (cont_total * 0.6)
|
91
|
+
Matriz_dispersa.new(@matriz)
|
92
|
+
else
|
93
|
+
Matriz_densa.new(@matriz)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def max()
|
98
|
+
end
|
99
|
+
|
100
|
+
def min()
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
data/lib/Matriz_densa.rb
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
require "Fraccion.rb"
|
2
|
+
require "Matriz.rb"
|
3
|
+
class Matriz_densa < Matriz
|
4
|
+
|
5
|
+
def + (other)
|
6
|
+
aux_m = Array.new
|
7
|
+
for i in 0...@filas do
|
8
|
+
aux_m[i] = Array.new
|
9
|
+
for j in 0...@cols do
|
10
|
+
aux_m[i][j] = 0;
|
11
|
+
end
|
12
|
+
end
|
13
|
+
if other.is_a?(Matriz_dispersa)
|
14
|
+
for i in 0...@filas do
|
15
|
+
for j in 0...@cols do
|
16
|
+
if other.matriz[i.to_s+"_"+j.to_s] != nil
|
17
|
+
aux_m[i][j] = @matriz[i][j] + other.matriz[i.to_s+"_"+j.to_s]
|
18
|
+
else
|
19
|
+
aux_m[i][j] = @matriz[i][j]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
elsif other.is_a?(Matriz_densa)
|
24
|
+
for i in 0...@filas do
|
25
|
+
for j in 0...@cols do
|
26
|
+
aux_m[i][j] = @matriz[i][j] + other.matriz[i][j]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
Matriz.new(aux_m).comprobar()
|
31
|
+
end
|
32
|
+
|
33
|
+
def - (other)
|
34
|
+
aux_m = Array.new
|
35
|
+
for i in 0...@filas do
|
36
|
+
aux_m[i] = Array.new
|
37
|
+
for j in 0...@cols do
|
38
|
+
aux_m[i][j] = 0;
|
39
|
+
end
|
40
|
+
end
|
41
|
+
if other.is_a?(Matriz_dispersa)
|
42
|
+
for i in 0...@filas do
|
43
|
+
for j in 0...@cols do
|
44
|
+
if other.matriz[i.to_s+"_"+j.to_s] != nil
|
45
|
+
aux_m[i][j] = @matriz[i][j] - other.matriz[i.to_s+"_"+j.to_s]
|
46
|
+
else
|
47
|
+
aux_m[i][j] = @matriz[i][j]
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
elsif other.is_a?(Matriz_densa)
|
52
|
+
for i in 0...@filas do
|
53
|
+
for j in 0...@cols do
|
54
|
+
aux_m[i][j] = @matriz[i][j] - other.matriz[i][j]
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
Matriz.new(aux_m).comprobar()
|
59
|
+
end
|
60
|
+
|
61
|
+
def * (other)
|
62
|
+
aux_m = Array.new
|
63
|
+
for i in 0...@filas do
|
64
|
+
aux_m[i] = Array.new
|
65
|
+
for j in 0...@cols do
|
66
|
+
aux_m[i][j] = 0;
|
67
|
+
end
|
68
|
+
end
|
69
|
+
if other.is_a?(Matriz_dispersa)
|
70
|
+
for i in 0...@filas do
|
71
|
+
for j in 0...other.cols do
|
72
|
+
for k in 0...other.filas do
|
73
|
+
if other.matriz[k.to_s+"_"+j.to_s] != nil
|
74
|
+
aux_m[i][j] += @matriz[i][j] * other.matriz[k.to_s+"_"+j.to_s]
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
elsif other.is_a?(Matriz_densa)
|
80
|
+
for i in 0...@filas do
|
81
|
+
for j in 0...other.cols do
|
82
|
+
for k in 0...other.filas do
|
83
|
+
aux_m[i][j] += @matriz[i][k] * other.matriz[k][j]
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
Matriz.new(aux_m).comprobar()
|
89
|
+
end
|
90
|
+
|
91
|
+
def max()
|
92
|
+
|
93
|
+
aux_max = @matriz[0][0]
|
94
|
+
|
95
|
+
for i in 0...@filas do
|
96
|
+
for j in 0...@cols do
|
97
|
+
if aux_max < @matriz[i][j]
|
98
|
+
aux_max = @matriz[i][j]
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
aux_max
|
104
|
+
end
|
105
|
+
|
106
|
+
def min()
|
107
|
+
|
108
|
+
aux_min = @matriz[0][0]
|
109
|
+
|
110
|
+
for i in 0...@filas do
|
111
|
+
for j in 0...@cols do
|
112
|
+
if aux_min > @matriz[i][j]
|
113
|
+
aux_min = @matriz[i][j]
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
aux_min
|
119
|
+
end
|
120
|
+
|
121
|
+
end
|
@@ -0,0 +1,201 @@
|
|
1
|
+
require "Matriz.rb"
|
2
|
+
require "Fraccion.rb"
|
3
|
+
class Matriz_dispersa < Matriz
|
4
|
+
|
5
|
+
attr_accessor :filas, :cols, :matriz
|
6
|
+
|
7
|
+
def initialize(m)
|
8
|
+
@filas = m.size
|
9
|
+
@cols = m[1].size
|
10
|
+
|
11
|
+
aux_m = Hash.new
|
12
|
+
for i in 0...@filas do
|
13
|
+
for j in 0...@cols do
|
14
|
+
if m[i][j] != 0
|
15
|
+
aux_m[i.to_s+"_"+j.to_s] = m[i][j]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
@matriz = aux_m
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_s
|
23
|
+
|
24
|
+
aux_m = Array.new
|
25
|
+
for i in 0...@filas do
|
26
|
+
aux_m[i] = Array.new
|
27
|
+
for j in 0...@cols do
|
28
|
+
if m[i.to_s+"_"+j.to_s] != nil
|
29
|
+
aux_m[i][j] = @matriz[i.to_s+"_"+j.to_s]
|
30
|
+
else
|
31
|
+
aux_m[i][j] = 0
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
@matriz.each do |fila|
|
37
|
+
puts fila.join(" ")
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
def + (other)
|
43
|
+
aux_m = Array.new
|
44
|
+
for i in 0...@filas do
|
45
|
+
aux_m[i] = Array.new
|
46
|
+
for j in 0...@cols do
|
47
|
+
aux_m[i][j] = 0;
|
48
|
+
end
|
49
|
+
end
|
50
|
+
if other.is_a?(Matriz_dispersa)
|
51
|
+
for i in 0...@filas do
|
52
|
+
for j in 0...@cols do
|
53
|
+
if @matriz[i.to_s+"_"+j.to_s] != nil
|
54
|
+
if other.matriz[i.to_s+"_"+j.to_s] != nil
|
55
|
+
aux_m[i][j] = @matriz[i.to_s+"_"+j.to_s] + other.matriz[i.to_s+"_"+j.to_s]
|
56
|
+
else
|
57
|
+
aux_m[i][j] = @matriz[i.to_s+"_"+j.to_s]
|
58
|
+
end
|
59
|
+
else
|
60
|
+
if other.matriz[i.to_s+"_"+j.to_s] != nil
|
61
|
+
aux_m[i][j] = other.matriz[i.to_s+"_"+j.to_s]
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
elsif other.is_a?(Matriz_densa)
|
67
|
+
for i in 0...@filas do
|
68
|
+
for j in 0...@cols do
|
69
|
+
if @matriz[i.to_s+"_"+j.to_s] != nil
|
70
|
+
aux_m[i][j] = @matriz[i.to_s+"_"+j.to_s] + other.matriz[i][j]
|
71
|
+
else
|
72
|
+
aux_m[i][j] = other.matriz[i][j]
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
Matriz.new(aux_m).comprobar()
|
78
|
+
end
|
79
|
+
|
80
|
+
def - (other)
|
81
|
+
aux_m = Array.new
|
82
|
+
for i in 0...@filas do
|
83
|
+
aux_m[i] = Array.new
|
84
|
+
for j in 0...@cols do
|
85
|
+
aux_m[i][j] = 0;
|
86
|
+
end
|
87
|
+
end
|
88
|
+
if other.is_a?(Matriz_dispersa)
|
89
|
+
for i in 0...@filas do
|
90
|
+
for j in 0...@cols do
|
91
|
+
if @matriz[i.to_s+"_"+j.to_s] != nil
|
92
|
+
if other.matriz[i.to_s+"_"+j.to_s] != nil
|
93
|
+
aux_m[i][j] = @matriz[i.to_s+"_"+j.to_s] - other.matriz[i.to_s+"_"+j.to_s]
|
94
|
+
else
|
95
|
+
aux_m[i][j] = @matriz[i.to_s+"_"+j.to_s]
|
96
|
+
end
|
97
|
+
else
|
98
|
+
if other.matriz[i.to_s+"_"+j.to_s] != nil
|
99
|
+
aux_m[i][j] = -(other.matriz[i.to_s+"_"+j.to_s])
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
elsif other.is_a?(Matriz_densa)
|
105
|
+
for i in 0...@filas do
|
106
|
+
for j in 0...@cols do
|
107
|
+
if @matriz[i.to_s+"_"+j.to_s] != nil
|
108
|
+
aux_m[i][j] = @matriz[i.to_s+"_"+j.to_s] - other.matriz[i][j]
|
109
|
+
else
|
110
|
+
aux_m[i][j] = -(other.matriz[i][j])
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
Matriz.new(aux_m).comprobar()
|
116
|
+
end
|
117
|
+
|
118
|
+
def * (other)
|
119
|
+
aux_m = Array.new
|
120
|
+
for i in 0...@filas do
|
121
|
+
aux_m[i] = Array.new
|
122
|
+
for j in 0...@cols do
|
123
|
+
aux_m[i][j] = 0;
|
124
|
+
end
|
125
|
+
end
|
126
|
+
if other.is_a?(Matriz_dispersa)
|
127
|
+
for i in 0...@filas do
|
128
|
+
for j in 0...other.cols do
|
129
|
+
for k in 0...other.filas do
|
130
|
+
if @matriz[i.to_s+"_"+k.to_s] != nil
|
131
|
+
if other.matriz[k.to_s+"_"+j.to_s] != nil
|
132
|
+
aux_m[i][j] += @matriz[i.to_s+"_"+k.to_s] * other.matriz[k.to_s+"_"+j.to_s]
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
elsif other.is_a?(Matriz_densa)
|
139
|
+
for i in 0...@filas do
|
140
|
+
for j in 0...other.cols do
|
141
|
+
for k in 0...other.filas do
|
142
|
+
if @matriz[i.to_s+"_"+k.to_s] != nil
|
143
|
+
aux_m[i][j] += @matriz[i.to_s+"_"+k.to_s] * other.matriz[k][j]
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
Matriz.new(aux_m).comprobar()
|
150
|
+
end
|
151
|
+
|
152
|
+
def traspuesta ()
|
153
|
+
aux_m = Array.new
|
154
|
+
for i in 0...@filas do
|
155
|
+
aux_m[i] = Array.new
|
156
|
+
for j in 0...@cols do
|
157
|
+
if @matriz[((@filas-1)-i).to_s+"_"+((@cols-1)-j).to_s] != nil
|
158
|
+
aux_m[i][j] = @matriz[((@filas-1)-i).to_s+"_"+((@cols-1)-j).to_s]
|
159
|
+
else
|
160
|
+
aux_m[i][j] = 0
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
Matriz.new(aux_m).comprobar
|
165
|
+
end
|
166
|
+
|
167
|
+
def max()
|
168
|
+
|
169
|
+
aux_max = nil
|
170
|
+
|
171
|
+
for i in 0...@filas do
|
172
|
+
for j in 0...@cols do
|
173
|
+
if @matriz[i.to_s+"_"+j.to_s] != nil and aux_max == nil
|
174
|
+
aux_max = 0
|
175
|
+
elsif @matriz[i.to_s+"_"+j.to_s] != nil and aux_max < @matriz[i.to_s+"_"+j.to_s]
|
176
|
+
aux_max = @matriz[i.to_s+"_"+j.to_s]
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
aux_max
|
182
|
+
end
|
183
|
+
|
184
|
+
def min()
|
185
|
+
|
186
|
+
aux_min = nil
|
187
|
+
|
188
|
+
for i in 0...@filas do
|
189
|
+
for j in 0...@cols do
|
190
|
+
if @matriz[i.to_s+"_"+j.to_s] != nil and aux_min == nil
|
191
|
+
aux_min = 0
|
192
|
+
elsif @matriz[i.to_s+"_"+j.to_s] != nil and aux_min > @matriz[i.to_s+"_"+j.to_s]
|
193
|
+
aux_min = @matriz[i.to_s+"_"+j.to_s]
|
194
|
+
end
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
aux_min
|
199
|
+
end
|
200
|
+
|
201
|
+
end
|
data/lib/mcd.rb
ADDED
data/lib/prct09.rb
ADDED
data/prct09.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'prct09/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "prct09"
|
8
|
+
spec.version = Prct09::VERSION
|
9
|
+
spec.authors = ["Mauricio Cavalleri y katerine cardona"]
|
10
|
+
spec.email = ["alu0100683578@ull.edu.es"]
|
11
|
+
spec.description = %q{creacion de la gema para practica de matrices}
|
12
|
+
spec.summary = %q{implementación de los directorios de la gema para la práctica y realización de matrices densas y dispersas}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
22
|
+
spec.add_development_dependency 'rake'
|
23
|
+
spec.add_development_dependency 'rspec'
|
24
|
+
spec.add_development_dependency 'guard-rspec'
|
25
|
+
spec.add_development_dependency 'guard-bundler'
|
26
|
+
end
|
data/spec/Matriz_spec.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
#fichero de expectativas de la clase fraccion
|
2
|
+
require "Matriz.rb"
|
3
|
+
require "Fraccion.rb"
|
4
|
+
require "Matriz_densa.rb"
|
5
|
+
require "Matriz_dispersa.rb"
|
6
|
+
|
7
|
+
describe Matriz do
|
8
|
+
before :each do
|
9
|
+
#Objeto Matriz para trabajar con las pruebas
|
10
|
+
@m1 = Matriz_dispersa.new([[1,0,0],[0,0,0],[0,0,1]]) #dispersa
|
11
|
+
@m2 = Matriz_densa.new([[1,2/4,1],[2,1/4,3],[3,5/4,4]]) #densa
|
12
|
+
@m3 = Matriz_densa.new([[2,0,1],[3,0,0],[5,1,1]]) #densa
|
13
|
+
@m4 = Matriz_dispersa.new([[0,0,0],[1,0,0],[0,1,0]])#dispersa
|
14
|
+
@m5 = Matriz_densa.new([[1,0,1],[3,0,0],[5,1,0]]) #densa
|
15
|
+
@m6 = Matriz_densa.new([[1,2/4,1],[1,2/4,1],[1,2/4,1]]) #densa
|
16
|
+
end
|
17
|
+
|
18
|
+
it " Se debe poder sumar dos matrices de enteros" do
|
19
|
+
@aux = Matriz_densa.new([[1,1,1],[1,1,1],[1,1,1]])
|
20
|
+
(@m1 + @aux).matriz.should == [[2,1,1],[1,1,1],[1,1,2]]
|
21
|
+
end
|
22
|
+
|
23
|
+
it " Se debe poder restar dos matrices de enteros" do
|
24
|
+
@aux = Matriz_densa.new([[1,1,1],[1,1,1],[1,1,1]])
|
25
|
+
(@m1 - @aux).matriz.should == [[0,-1,-1],[-1,-1,-1],[-1,-1,0]]
|
26
|
+
end
|
27
|
+
|
28
|
+
it " Se debe poder multiplicar dos matrices de enteros" do
|
29
|
+
@aux = Matriz_densa.new([[1,2,3],[1,2,3],[1,2,3]])
|
30
|
+
(@m1 * @aux).matriz.should == [[1,2,3],[0,0,0],[1,2,3]]
|
31
|
+
end
|
32
|
+
|
33
|
+
it " Se debe poder hacer la traspuesta de una matriz" do
|
34
|
+
@aux = Matriz_densa.new([[1,6,2],[2,5,5],[4,8,3]])
|
35
|
+
@aux.traspuesta.matriz.should == [[3,8,4],[5,5,2],[2,6,1]]
|
36
|
+
@m4.traspuesta.matriz.should == Matriz_dispersa.new([[0,1,0],[0,0,1],[0,0,0]]).matriz
|
37
|
+
end
|
38
|
+
|
39
|
+
it " Se debe poder sumar matrices de fracciones" do
|
40
|
+
@aux = Matriz_densa.new([[1,1,1],[1,1,1],[1,1,1]])
|
41
|
+
(@m2 + @aux).matriz.should == [[2,3/2,2],[3,5/4,4],[4,9/4,5]]
|
42
|
+
end
|
43
|
+
|
44
|
+
it "Se debe poder multiplicar dos matrices de racionales" do
|
45
|
+
@aux = Matriz_densa.new([[1,1,1],[1,1,1],[1,1,1]])
|
46
|
+
(@m6 * @aux).matriz.should == [[2,2,2],[2,2,2],[2,2,2]]
|
47
|
+
end
|
48
|
+
|
49
|
+
it "Se debe poder comprobar el tipo de matriz del resultado de las operaciones" do
|
50
|
+
(@m1 + @m4).is_a?(Matriz_densa).should == true
|
51
|
+
(@m3 - @m5).is_a?(Matriz_dispersa).should == true
|
52
|
+
(@m3 * @m2).is_a?(Matriz_densa).should == true
|
53
|
+
end
|
54
|
+
|
55
|
+
it "Se debe poder encontrar el maximo y el minimo de una matriz" do
|
56
|
+
@m5.max.should == 5
|
57
|
+
@m5.min.should == 0
|
58
|
+
@m1.max.should == 1
|
59
|
+
@m1.min.should == 0
|
60
|
+
end
|
61
|
+
end
|
data/test/tc_matriz.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require "Matriz.rb"
|
3
|
+
require "Fraccion.rb"
|
4
|
+
require "Matriz_densa.rb"
|
5
|
+
require "Matriz_dispersa.rb"
|
6
|
+
|
7
|
+
class Test_Matriz < Test::Unit::TestCase
|
8
|
+
|
9
|
+
def setup
|
10
|
+
@m1=Matriz_dispersa.new([[1,0,0],[0,1,0],[0,0,0]]) #dispersa
|
11
|
+
@m2=Matriz_densa.new([[1,2,1],[2,1,3],[3,5,4]]) #densa
|
12
|
+
@m3=Matriz_dispersa.new([[1,0,0],[0,0,0],[1,0,0]]) #dispersa
|
13
|
+
@m4=Matriz_densa.new([[2,3,1],[9,3,4],[2,7,5]]) #densa
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
#comprobacion que se crean bien las matrices
|
18
|
+
|
19
|
+
def test_Comprobacion
|
20
|
+
|
21
|
+
assert_equal(@m1.filas,3)
|
22
|
+
assert_equal(@m1.cols,3)
|
23
|
+
|
24
|
+
assert_equal(@m2.filas,3)
|
25
|
+
assert_equal(@m2.cols,3)
|
26
|
+
|
27
|
+
assert_equal(@m3.filas,3)
|
28
|
+
assert_equal(@m3.cols,3)
|
29
|
+
|
30
|
+
assert_equal(@m4.filas,3)
|
31
|
+
assert_equal(@m4.cols,3)
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
#comprobar tipo de matrices
|
37
|
+
|
38
|
+
def test_Tipo
|
39
|
+
|
40
|
+
assert_equal(true, @m1.is_a?(Matriz_dispersa))
|
41
|
+
assert_equal(true, @m2.is_a?(Matriz_densa))
|
42
|
+
assert_equal(true, @m3.is_a?(Matriz_dispersa))
|
43
|
+
assert_equal(true, @m4.is_a?(Matriz_densa))
|
44
|
+
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
#comprobar operaciones
|
49
|
+
|
50
|
+
def test_Operacion
|
51
|
+
|
52
|
+
#suma
|
53
|
+
assert_equal(Matriz_dispersa.new([[2,0,0],[0,1,0],[1,0,0]]).matriz, (@m1 + @m3).matriz)
|
54
|
+
assert_equal(Matriz_densa.new([[2,2,1],[2,2,3],[3,5,4]]).matriz,(@m1+@m2).matriz)
|
55
|
+
|
56
|
+
#resta
|
57
|
+
|
58
|
+
assert_equal(Matriz_densa.new([[0,-2,-1],[-2,0,-3],[-3,-5,-4]]).matriz,(@m1-@m2).matriz)
|
59
|
+
assert_equal(Matriz_dispersa.new([[0,0,0],[0,1,0],[-1,0,0]]).matriz,(@m1-@m3).matriz)
|
60
|
+
|
61
|
+
#multiplicacion
|
62
|
+
|
63
|
+
assert_equal(Matriz_densa.new([[2,3,1],[0,0,0],[2,3,1]]).matriz,(@m3*@m4).matriz)
|
64
|
+
assert_equal(Matriz_densa.new([[22,16,14],[19,30,21],[59,52,43]]).matriz,(@m2*@m4).matriz)
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
|
70
|
+
|
metadata
ADDED
@@ -0,0 +1,148 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: prct09
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Mauricio Cavalleri y katerine cardona
|
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: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
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: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: guard-rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: guard-bundler
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
description: creacion de la gema para practica de matrices
|
95
|
+
email:
|
96
|
+
- alu0100683578@ull.edu.es
|
97
|
+
executables:
|
98
|
+
- prct09
|
99
|
+
extensions: []
|
100
|
+
extra_rdoc_files: []
|
101
|
+
files:
|
102
|
+
- .gitignore
|
103
|
+
- .travis.yml
|
104
|
+
- Gemfile
|
105
|
+
- Guarfile
|
106
|
+
- LICENSE.txt
|
107
|
+
- README.md
|
108
|
+
- Rakefile
|
109
|
+
- bin/prct09
|
110
|
+
- lib/Fraccion.rb
|
111
|
+
- lib/Matriz.rb
|
112
|
+
- lib/Matriz_densa.rb
|
113
|
+
- lib/Matriz_dispersa.rb
|
114
|
+
- lib/mcd.rb
|
115
|
+
- lib/prct09.rb
|
116
|
+
- lib/prct09/version.rb
|
117
|
+
- prct09.gemspec
|
118
|
+
- spec/Matriz_spec.rb
|
119
|
+
- test/tc_matriz.rb
|
120
|
+
homepage: ''
|
121
|
+
licenses:
|
122
|
+
- MIT
|
123
|
+
post_install_message:
|
124
|
+
rdoc_options: []
|
125
|
+
require_paths:
|
126
|
+
- lib
|
127
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
128
|
+
none: false
|
129
|
+
requirements:
|
130
|
+
- - ! '>='
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '0'
|
133
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
134
|
+
none: false
|
135
|
+
requirements:
|
136
|
+
- - ! '>='
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
requirements: []
|
140
|
+
rubyforge_project:
|
141
|
+
rubygems_version: 1.8.23
|
142
|
+
signing_key:
|
143
|
+
specification_version: 3
|
144
|
+
summary: implementación de los directorios de la gema para la práctica y realización
|
145
|
+
de matrices densas y dispersas
|
146
|
+
test_files:
|
147
|
+
- spec/Matriz_spec.rb
|
148
|
+
- test/tc_matriz.rb
|