matrix_expansion 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 +17 -0
- data/.travis.yml +15 -0
- data/Gemfile +7 -0
- data/Guardfile +30 -0
- data/LICENSE +22 -0
- data/README.md +23 -0
- data/Rakefile +30 -0
- data/lib/matrix_expansion.rb +11 -0
- data/lib/matrix_expansion/fraccion.rb +118 -0
- data/lib/matrix_expansion/gcd.rb +18 -0
- data/lib/matrix_expansion/matrix.rb +31 -0
- data/lib/matrix_expansion/matrix_densa.rb +232 -0
- data/lib/matrix_expansion/matrix_dispersa.rb +268 -0
- data/lib/matrix_expansion/version.rb +3 -0
- data/matrix_expansion.gemspec +17 -0
- data/spec/matrix_spec.rb +206 -0
- data/test/tc_matrix.rb +87 -0
- metadata +64 -0
data/.gitignore
ADDED
data/.travis.yml
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
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2013 TODO: Write your name
|
|
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,23 @@
|
|
|
1
|
+
# MatrixExpansion
|
|
2
|
+
|
|
3
|
+
## Introducción
|
|
4
|
+
|
|
5
|
+
Gema que permite el uso de matrices densas y dispersas a través de la herencia de una matriz simple. Todas las operaciones con números y fracciones incluidas.
|
|
6
|
+
|
|
7
|
+
## Instalación
|
|
8
|
+
|
|
9
|
+
Add this line to your application's Gemfile:
|
|
10
|
+
|
|
11
|
+
gem 'matrix_expansion'
|
|
12
|
+
|
|
13
|
+
And then execute:
|
|
14
|
+
|
|
15
|
+
$ bundle
|
|
16
|
+
|
|
17
|
+
Or install it yourself as:
|
|
18
|
+
|
|
19
|
+
$ gem install matrix_expansion
|
|
20
|
+
|
|
21
|
+
## Diseño
|
|
22
|
+
|
|
23
|
+
A la hora de realizar las matrices densas se ha realizado de forma natural (como para cualquier matriz simple). El planteamiento importante es para la representación de las matrices dispersas. Hemos decidido finalmente, tras haber pensado en implementarlo con varios vectores, en que era más sencillo, tanto para el acceso como para las operaciones, implementarlo con un array de hash. Cada fila es un array que contiene hashes, donde la clave es la columna del elemento no nulo y el valor es el propio valor del elemento no nulo. De esta forma, solamente se almacenan los valores nulos. Si se accediera a otra posición se devolvería 0 (aunque este no este almacenado en la matriz dispersa para ahorrar memoria).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
|
|
2
|
+
$:.unshift File.dirname(__FILE__) + 'lib'
|
|
3
|
+
$:.unshift './lib', './spec'
|
|
4
|
+
|
|
5
|
+
require "bundler/gem_tasks"
|
|
6
|
+
|
|
7
|
+
require 'rspec/core/rake_task'
|
|
8
|
+
RSpec::Core::RakeTask.new
|
|
9
|
+
|
|
10
|
+
task :default => :spec
|
|
11
|
+
|
|
12
|
+
desc "Expectativas de la clase Matriz"
|
|
13
|
+
task :spec do
|
|
14
|
+
sh "rspec -I. spec/matrix_spec.rb"
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
desc "Expectativas de la clase Matriz, con documentacion"
|
|
18
|
+
task :test do
|
|
19
|
+
sh "rspec -I. spec/matrix_spec.rb --format documentation"
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
desc "Expectativas de la clase Matriz, con documentacion HTML"
|
|
23
|
+
task :thtml do
|
|
24
|
+
sh "rspec -I. spec/matrix_spec.rb --format html"
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
desc "Pruebas unitarias de las clases Matriz_Densa y Matriz_Dispersa"
|
|
28
|
+
task :tc do
|
|
29
|
+
sh "ruby -i. test/tc_matrix.rb"
|
|
30
|
+
end
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
require "./lib/matrix_expansion/version"
|
|
2
|
+
require "./lib/matrix_expansion/matrix.rb"
|
|
3
|
+
require "./lib/matrix_expansion/matrix_densa.rb"
|
|
4
|
+
require "./lib/matrix_expansion/matrix_dispersa.rb"
|
|
5
|
+
|
|
6
|
+
require "./lib/matrix_expansion/fraccion.rb"
|
|
7
|
+
require "./lib/matrix_expansion/gcd.rb"
|
|
8
|
+
|
|
9
|
+
module MatrixExpansion
|
|
10
|
+
# Your code goes here...
|
|
11
|
+
end
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
require "./lib/matrix_expansion/gcd.rb"
|
|
2
|
+
|
|
3
|
+
class Fraccion
|
|
4
|
+
include Comparable
|
|
5
|
+
attr_reader :a, :b #a: numerador, b: denominador
|
|
6
|
+
def initialize(a,b)
|
|
7
|
+
#comprobamos que los datos de entradas sean numericos
|
|
8
|
+
raise unless a.is_a?(Numeric)
|
|
9
|
+
raise unless b.is_a?(Numeric)
|
|
10
|
+
#comprobamos que el dominador no sea cero
|
|
11
|
+
raise unless (b != 0)
|
|
12
|
+
#inicializamos las variables
|
|
13
|
+
@a, @b = a, b
|
|
14
|
+
end
|
|
15
|
+
#formateo de la salida (a/b)
|
|
16
|
+
def to_s
|
|
17
|
+
"#{@a}/#{@b}"
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
#metodo que retorna el numerador
|
|
21
|
+
def num
|
|
22
|
+
return @a
|
|
23
|
+
end
|
|
24
|
+
#metedo que retorna el denominador
|
|
25
|
+
def denom
|
|
26
|
+
return @b
|
|
27
|
+
end
|
|
28
|
+
#Fraccion en modo flotante
|
|
29
|
+
def to_f
|
|
30
|
+
@a.to_f / @b.to_f #conversion de tipo a flotante mediante to_f
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
#Fracción pasada a float. (ej: 1.25)
|
|
34
|
+
def abs
|
|
35
|
+
f= Fraccion.new(@a.abs, @b.abs)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
#Fracción reciproca o inversa (a/b -> b/a)
|
|
39
|
+
def reciprocal
|
|
40
|
+
f= Fraccion.new(@b, @a)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
#Suma de fracciones
|
|
44
|
+
def + (other)
|
|
45
|
+
comun = @b * other.denom
|
|
46
|
+
a1 = @a * other.denom
|
|
47
|
+
a2 = other.num * @b
|
|
48
|
+
a3 = a1 + a2
|
|
49
|
+
k = gcd(a3,comun)
|
|
50
|
+
a3 /= k
|
|
51
|
+
comun /= k
|
|
52
|
+
Fraccion.new(a3,comun)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
#Resta de fracciones
|
|
56
|
+
def - (other)
|
|
57
|
+
comun = @b * other.denom
|
|
58
|
+
a1 = @a * other.denom
|
|
59
|
+
a2 = other.num * @b
|
|
60
|
+
a3 = a1 - a2
|
|
61
|
+
k = gcd(a3,comun)
|
|
62
|
+
a3 /= k
|
|
63
|
+
comun /= k
|
|
64
|
+
Fraccion.new(a3,comun)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
#Opuesto de fracción. La suma de una fracción y su opuesto debe ser 0.
|
|
68
|
+
def -@
|
|
69
|
+
k = gcd(@a, @b)
|
|
70
|
+
a = @a / k
|
|
71
|
+
b = @b / k
|
|
72
|
+
if (b < 0) #Si el denominador es negativo
|
|
73
|
+
return Fraccion.new(a,-b)
|
|
74
|
+
else
|
|
75
|
+
return Fraccion.new(-a,b)
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
#Producto de fracciones
|
|
80
|
+
def * (other)
|
|
81
|
+
a = @a * other.num
|
|
82
|
+
b = @b * other.denom
|
|
83
|
+
k = gcd(a,b)
|
|
84
|
+
a /= k
|
|
85
|
+
b /= k
|
|
86
|
+
Fraccion.new(a,b)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
#División de fracciones
|
|
90
|
+
def / (other)
|
|
91
|
+
a = @a * other.denom
|
|
92
|
+
b = @b * other.num
|
|
93
|
+
k = gcd(a,b)
|
|
94
|
+
a /= k
|
|
95
|
+
b /= k
|
|
96
|
+
Fraccion.new(a,b)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
#Resto de fracciones
|
|
100
|
+
def % (other)
|
|
101
|
+
r = Fraccion.new(0,1) #Inicializa objeto
|
|
102
|
+
r = self
|
|
103
|
+
while (r.to_f >= other.to_f) do #Hasta que no se pueda restar más. Queda en r el resto.
|
|
104
|
+
r = r - other
|
|
105
|
+
end
|
|
106
|
+
return r
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def <=> (other)
|
|
111
|
+
self.to_f <=> other.to_f
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def coerce (other)
|
|
115
|
+
[Fraccion.new(other,1),self]
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
=begin
|
|
2
|
+
**********************************************************
|
|
3
|
+
* Autores:
|
|
4
|
+
* Teno González Dos Santos
|
|
5
|
+
* Albano José Yanes Reyes
|
|
6
|
+
* Fecha: 18/10/2013
|
|
7
|
+
* Asignatura: Lenguejes y Paradigmas de Programación
|
|
8
|
+
* Tercero de Grado en Ingeniería Informática
|
|
9
|
+
* Universidad de la Laguna
|
|
10
|
+
***********************************************************
|
|
11
|
+
=end
|
|
12
|
+
def gcd(u, v)
|
|
13
|
+
u, v = u.abs, v.abs
|
|
14
|
+
while v != 0
|
|
15
|
+
u, v = v, u % v
|
|
16
|
+
end
|
|
17
|
+
u
|
|
18
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
=begin
|
|
2
|
+
*******************************************************************************************
|
|
3
|
+
* Autores:
|
|
4
|
+
Teno González Dos Santos
|
|
5
|
+
Albano José Yanes Reyes
|
|
6
|
+
* Fecha: 19/11/2013 -
|
|
7
|
+
* Asignatura: Lenguujes y Paradigmas de Programación
|
|
8
|
+
* Tercero de Grado en Ingeniería Informática
|
|
9
|
+
* Universidad de la Laguna
|
|
10
|
+
*
|
|
11
|
+
* Practica 10: Matrices densas y dispersas
|
|
12
|
+
********************************************************************************************
|
|
13
|
+
=end
|
|
14
|
+
|
|
15
|
+
# Clase Matriz
|
|
16
|
+
|
|
17
|
+
require './lib/matrix_expansion/fraccion.rb'
|
|
18
|
+
|
|
19
|
+
module MatrixExpansion
|
|
20
|
+
class Matriz
|
|
21
|
+
attr_reader :fil, :col
|
|
22
|
+
attr_accessor :matrix
|
|
23
|
+
|
|
24
|
+
def initialize(n, m)
|
|
25
|
+
raise ArgumentError, 'Valores para filas y columnas incorrectas' unless n.is_a? Fixnum and n > 0 and m.is_a? Fixnum and m > 0
|
|
26
|
+
|
|
27
|
+
@fil, @col= n, m
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
=begin
|
|
2
|
+
*******************************************************************************************
|
|
3
|
+
* Autores:
|
|
4
|
+
Teno González Dos Santos
|
|
5
|
+
Albano José Yanes Reyes
|
|
6
|
+
* Fecha: 19/11/2013 -
|
|
7
|
+
* Asignatura: Lenguujes y Paradigmas de Programación
|
|
8
|
+
* Tercero de Grado en Ingeniería Informática
|
|
9
|
+
* Universidad de la Laguna
|
|
10
|
+
*
|
|
11
|
+
* Practica 10: Matrices densas y dispersas
|
|
12
|
+
********************************************************************************************
|
|
13
|
+
=end
|
|
14
|
+
|
|
15
|
+
# Clase Matriz_Densa
|
|
16
|
+
|
|
17
|
+
require "./lib/matrix_expansion/matrix.rb"
|
|
18
|
+
|
|
19
|
+
module MatrixExpansion
|
|
20
|
+
class Matriz_Densa < Matriz
|
|
21
|
+
|
|
22
|
+
# Crea una matriz anidando array e inicializa los valores a 0
|
|
23
|
+
def initialize(n, m)
|
|
24
|
+
super
|
|
25
|
+
|
|
26
|
+
@matrix = Array.new(@fil,0)
|
|
27
|
+
i = 0
|
|
28
|
+
while i < @fil
|
|
29
|
+
@matrix[i] = Array.new(@col,0)
|
|
30
|
+
i += 1
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Da valores numericos a la matriz densa
|
|
35
|
+
def set_valores_num
|
|
36
|
+
valor = 1
|
|
37
|
+
i = 0
|
|
38
|
+
while(i < @col)
|
|
39
|
+
j = 0
|
|
40
|
+
while(j < @fil)
|
|
41
|
+
@matrix[i][j] = valor
|
|
42
|
+
valor += 1
|
|
43
|
+
j += 1
|
|
44
|
+
end
|
|
45
|
+
i += 1
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Da valores fraccionales a la matriz densa
|
|
50
|
+
def set_valores_fracc
|
|
51
|
+
a = 1
|
|
52
|
+
b = 2
|
|
53
|
+
i = 0
|
|
54
|
+
while(i < @col)
|
|
55
|
+
j = 0
|
|
56
|
+
while(j < @fil)
|
|
57
|
+
@matrix[i][j] = Fraccion.new(a,b)
|
|
58
|
+
a += 1
|
|
59
|
+
b += 1
|
|
60
|
+
j += 1
|
|
61
|
+
end
|
|
62
|
+
i += 1
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
#Comprueba que se accede dentro de la matriz y devuelve el valor
|
|
67
|
+
def get(i,j)
|
|
68
|
+
if( !(i.is_a? Fixnum) or i < 0 or i >=@fil or !(j.is_a? Fixnum) or j < 0 or j >= @col)
|
|
69
|
+
return nil
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
return @matrix[i][j]
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# Calcula el porcentaje de valores nulos
|
|
76
|
+
def porcentaje_nulos
|
|
77
|
+
total = @fil*@col
|
|
78
|
+
no_nulos = 0
|
|
79
|
+
|
|
80
|
+
i = 0
|
|
81
|
+
while(i < @fil)
|
|
82
|
+
j = 0
|
|
83
|
+
while(j < @col)
|
|
84
|
+
if(@matrix[i][j] != 0)
|
|
85
|
+
no_nulos += 1
|
|
86
|
+
end
|
|
87
|
+
j += 1
|
|
88
|
+
end
|
|
89
|
+
i += 1
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
nulos = total - no_nulos
|
|
93
|
+
nulos.to_f/total.to_f
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# Muestra la matriz densa
|
|
97
|
+
def to_s
|
|
98
|
+
s = ""
|
|
99
|
+
i = 0
|
|
100
|
+
while(i < @col)
|
|
101
|
+
j = 0
|
|
102
|
+
while(j < @fil)
|
|
103
|
+
s += "#{@matrix[i][j].to_s}\t"
|
|
104
|
+
j += 1
|
|
105
|
+
end
|
|
106
|
+
s += "\n"
|
|
107
|
+
i += 1
|
|
108
|
+
end
|
|
109
|
+
s
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# Suma de matrices densas
|
|
113
|
+
def +(other)
|
|
114
|
+
raise ArgumentError , 'El argumento debe ser una matriz' unless other.is_a? Matriz
|
|
115
|
+
raise ArgumentError , 'Las matrices deben ser del mismo tamano' unless @fil == other.fil and @col == other.col
|
|
116
|
+
|
|
117
|
+
c = Matriz_Densa.new(@fil, @col)
|
|
118
|
+
i = 0
|
|
119
|
+
while(i < @fil)
|
|
120
|
+
j = 0
|
|
121
|
+
while(j < @col)
|
|
122
|
+
c.matrix[i][j] = @matrix[i][j] + other.matrix[i][j]
|
|
123
|
+
j += 1
|
|
124
|
+
end
|
|
125
|
+
i += 1
|
|
126
|
+
end
|
|
127
|
+
c
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
# Resta de matrices densas
|
|
131
|
+
def -(other)
|
|
132
|
+
raise ArgumentError , 'El argumento debe ser una matriz' unless other.is_a? Matriz
|
|
133
|
+
raise ArgumentError , 'Las matrices deben ser del mismo tamano' unless @fil == other.fil and @col == other.col
|
|
134
|
+
|
|
135
|
+
c = Matriz_Densa.new(@fil, @col)
|
|
136
|
+
i = 0
|
|
137
|
+
while(i < @fil)
|
|
138
|
+
j = 0
|
|
139
|
+
while(j < @col)
|
|
140
|
+
c.matrix[i][j] = @matrix[i][j] - other.matrix[i][j]
|
|
141
|
+
j += 1
|
|
142
|
+
end
|
|
143
|
+
i += 1
|
|
144
|
+
end
|
|
145
|
+
c
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
#Producto de matrices densas
|
|
150
|
+
def *(other)
|
|
151
|
+
raise ArgumentError , 'El argumento debe ser una matriz' unless other.is_a? Numeric or other.is_a? Matriz
|
|
152
|
+
|
|
153
|
+
#Si el argumento es un numero
|
|
154
|
+
if(other.is_a? Numeric)
|
|
155
|
+
c = Matriz_Densa.new(@fil, @col)
|
|
156
|
+
i = 0
|
|
157
|
+
while(i < @N)
|
|
158
|
+
j = 0
|
|
159
|
+
while(j < @M)
|
|
160
|
+
c.matrix = @matrix[i][j] * other
|
|
161
|
+
j += 1
|
|
162
|
+
end
|
|
163
|
+
i += 1
|
|
164
|
+
end
|
|
165
|
+
#Si el arguumento es una matriz
|
|
166
|
+
else
|
|
167
|
+
raise ArgumentError , 'Matriz no compatible (A.fil == B.col)' unless @col == other.fil
|
|
168
|
+
|
|
169
|
+
c = Matriz_Densa.new(@fil, other.col)
|
|
170
|
+
i = 0
|
|
171
|
+
while(i < @fil)
|
|
172
|
+
j = 0
|
|
173
|
+
while(j < other.col)
|
|
174
|
+
k = 0
|
|
175
|
+
c.matrix[i][j] = 0
|
|
176
|
+
while(k < @col)
|
|
177
|
+
c.matrix[i][j] += @matrix[i][k] * other.matrix[k][j]
|
|
178
|
+
k += 1
|
|
179
|
+
end
|
|
180
|
+
j += 1
|
|
181
|
+
end
|
|
182
|
+
i += 1
|
|
183
|
+
end
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
c
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
# Calcula el minimo elemento de la matriz
|
|
191
|
+
def min
|
|
192
|
+
# Establecemos valor del primer elemento
|
|
193
|
+
min = @matrix[0][0]
|
|
194
|
+
i = 0
|
|
195
|
+
|
|
196
|
+
# Fila a fila actualizando el valor minimo
|
|
197
|
+
while (i < @fil)
|
|
198
|
+
j = 0
|
|
199
|
+
while (j < @col)
|
|
200
|
+
if (@matrix[i][j] < min)
|
|
201
|
+
min = @matrix[i][j]
|
|
202
|
+
end
|
|
203
|
+
j += 1
|
|
204
|
+
end
|
|
205
|
+
i += 1
|
|
206
|
+
end
|
|
207
|
+
min
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
# Calcula el maximo elemento de la matriz
|
|
211
|
+
def max
|
|
212
|
+
|
|
213
|
+
# Establecemos valor del primer elemento
|
|
214
|
+
max = @matrix[0][0]
|
|
215
|
+
i = 0
|
|
216
|
+
|
|
217
|
+
#Fila a fila actualizando el valor maximo
|
|
218
|
+
while (i < @fil)
|
|
219
|
+
j = 0
|
|
220
|
+
while (j < @col)
|
|
221
|
+
if (@matrix[i][j] > max)
|
|
222
|
+
max = @matrix[i][j]
|
|
223
|
+
end
|
|
224
|
+
j += 1
|
|
225
|
+
end
|
|
226
|
+
i += 1
|
|
227
|
+
end
|
|
228
|
+
max
|
|
229
|
+
end
|
|
230
|
+
end
|
|
231
|
+
end
|
|
232
|
+
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
=begin
|
|
2
|
+
*******************************************************************************************
|
|
3
|
+
* Autores:
|
|
4
|
+
Teno González Dos Santos
|
|
5
|
+
Albano José Yanes Reyes
|
|
6
|
+
* Fecha: 19/11/2013 -
|
|
7
|
+
* Asignatura: Lenguujes y Paradigmas de Programación
|
|
8
|
+
* Tercero de Grado en Ingeniería Informática
|
|
9
|
+
* Universidad de la Laguna
|
|
10
|
+
*
|
|
11
|
+
* Practica 10: Matrices densas y dispersas
|
|
12
|
+
********************************************************************************************
|
|
13
|
+
=end
|
|
14
|
+
|
|
15
|
+
# Clase Matriz_Dispersa
|
|
16
|
+
|
|
17
|
+
require "./lib/matrix_expansion/matrix.rb"
|
|
18
|
+
require "./lib/matrix_expansion/matrix_densa.rb"
|
|
19
|
+
|
|
20
|
+
module MatrixExpansion
|
|
21
|
+
class Matriz_Dispersa < Matriz
|
|
22
|
+
|
|
23
|
+
# Se crea un array de hashes en el que en cada fila i, por cada elemento no nulo hay un hash de clave j y valor no_nulo
|
|
24
|
+
def initialize(n, m)
|
|
25
|
+
super
|
|
26
|
+
@matrix = Array.new(@fil)
|
|
27
|
+
i = 0
|
|
28
|
+
while(i < @fil)
|
|
29
|
+
@matrix[i] = {}
|
|
30
|
+
i += 1
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Metodo de la clase que permite convertir una matriz densa en una dispersa
|
|
35
|
+
def self.densa_a_dispersa(matriz)
|
|
36
|
+
raise ArgumentError, 'El parametro debe ser una matriz densa' unless matriz.is_a? MatrixExpansion::Matriz_Densa
|
|
37
|
+
|
|
38
|
+
obj = new(matriz.fil, matriz.col)
|
|
39
|
+
|
|
40
|
+
i = 0
|
|
41
|
+
while(i < matriz.fil)
|
|
42
|
+
j = 0
|
|
43
|
+
while(j < matriz.col)
|
|
44
|
+
value = matriz.matrix[i][j]
|
|
45
|
+
|
|
46
|
+
if( value != 0)
|
|
47
|
+
obj.matrix[i][j] = value
|
|
48
|
+
end
|
|
49
|
+
j += 1
|
|
50
|
+
end
|
|
51
|
+
i += 1
|
|
52
|
+
end
|
|
53
|
+
obj
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
#Calcula el procentaje de nulos en la matriz dispersa
|
|
57
|
+
def porcentaje_nulos
|
|
58
|
+
total = @fil*@col
|
|
59
|
+
no_nulos = 0
|
|
60
|
+
|
|
61
|
+
i = 0
|
|
62
|
+
while(i < @fil)
|
|
63
|
+
# El tamaño de cada fila es el numero de elementos no nulos que hay en ellas
|
|
64
|
+
no_nulos += @matrix[i].size
|
|
65
|
+
i += 1
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
nulos = total - no_nulos
|
|
69
|
+
nulos.to_f/total.to_f
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Metodo para asignar valores a la matriz dispersa que permite controlar que siga siendo dispersa en todo momento
|
|
73
|
+
def set(i, j, valor)
|
|
74
|
+
# Se accede dentro de los limites de la matriz
|
|
75
|
+
if( !(i.is_a? Fixnum) or i < 0 or i >=@fil or !(j.is_a? Fixnum) or j < 0 or j >= @col)
|
|
76
|
+
return nil
|
|
77
|
+
end
|
|
78
|
+
# Borrar elemento si es un valor nulo. Si no, introducirlo
|
|
79
|
+
if(valor == nil or valor == 0)
|
|
80
|
+
@matrix[i].delete(j)
|
|
81
|
+
else
|
|
82
|
+
@matrix[i][j] = valor
|
|
83
|
+
end
|
|
84
|
+
# Si se ha sobrepasado el número de elementos nulos, borramos el último elemento modificado
|
|
85
|
+
if(porcentaje_nulos < 0.6)
|
|
86
|
+
@matrix[i].delete(j)
|
|
87
|
+
puts "Borrado el elemento #{i},#{j} por sobrepasar el numero de elementos no nulos (Porcentaje actual: #{porcentaje_nulos}"
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# Metodo que muestra la matriz dispersa de la forma: (0,1)=>2 (0,2)=>3
|
|
93
|
+
# (1,3)=>1
|
|
94
|
+
def to_s
|
|
95
|
+
|
|
96
|
+
i = 0
|
|
97
|
+
salida = ""
|
|
98
|
+
while(i < @fil)
|
|
99
|
+
@matrix[i].sort.each{|k, v| salida += "(#{i},#{k.to_s})=>#{v.to_s} "}
|
|
100
|
+
salida += "\n"
|
|
101
|
+
i += 1
|
|
102
|
+
end
|
|
103
|
+
salida
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# Metodo que devuelve el valor de la posicion dentro de la matriz si es no nulo, o 0 eoc
|
|
107
|
+
def get(i, j)
|
|
108
|
+
if( !(i.is_a? Fixnum) or i < 0 or i >=@fil or !(j.is_a? Fixnum) or j < 0 or j >= @col)
|
|
109
|
+
return nil
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
if(@matrix[i][j] != nil)
|
|
113
|
+
return @matrix[i][j]
|
|
114
|
+
else
|
|
115
|
+
return 0
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# Suma de matrices
|
|
120
|
+
def +(other)
|
|
121
|
+
raise ArgumentError , 'El argumento debe ser una matriz' unless other.is_a? Matriz
|
|
122
|
+
raise ArgumentError , 'Las matrices deben ser del mismo tamano' unless @fil == other.fil and @col == other.col
|
|
123
|
+
|
|
124
|
+
c = Matriz_Densa.new(@fil, @col)
|
|
125
|
+
i = 0
|
|
126
|
+
while(i < @fil)
|
|
127
|
+
j = 0
|
|
128
|
+
while(j < @col)
|
|
129
|
+
c.matrix[i][j] = get(i,j) + other.get(i,j)
|
|
130
|
+
j += 1
|
|
131
|
+
end
|
|
132
|
+
i += 1
|
|
133
|
+
end
|
|
134
|
+
# Si el resultado es una matriz dispersa se convierte la densa
|
|
135
|
+
if(c.porcentaje_nulos > 0.6)
|
|
136
|
+
c = Matriz_Dispersa.densa_a_dispersa(c)
|
|
137
|
+
end
|
|
138
|
+
c
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
# Resta de matrices
|
|
142
|
+
def -(other)
|
|
143
|
+
raise ArgumentError , 'El argumento debe ser una matriz' unless other.is_a? Matriz
|
|
144
|
+
raise ArgumentError , 'Las matrices deben ser del mismo tamano' unless @fil == other.fil and @col == other.col
|
|
145
|
+
|
|
146
|
+
c = Matriz_Densa.new(@fil, @col)
|
|
147
|
+
i = 0
|
|
148
|
+
while(i < @fil)
|
|
149
|
+
j = 0
|
|
150
|
+
while(j < @col)
|
|
151
|
+
c.matrix[i][j] = get(i,j) - other.get(i,j)
|
|
152
|
+
j += 1
|
|
153
|
+
end
|
|
154
|
+
i += 1
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
# Si el resultado es una matriz dispersa se convierte la densa
|
|
158
|
+
if(c.porcentaje_nulos > 0.6)
|
|
159
|
+
c = Matriz_Dispersa.densa_a_dispersa(c)
|
|
160
|
+
end
|
|
161
|
+
c
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
# Producto de matrices
|
|
165
|
+
def *(other)
|
|
166
|
+
raise ArgumentError , 'El argumento debe ser una matriz' unless other.is_a? Numeric or other.is_a? Matriz
|
|
167
|
+
|
|
168
|
+
# Si el arguento es un numero
|
|
169
|
+
if(other.is_a? Numeric)
|
|
170
|
+
c = Matriz_Densa.new(@fil, @col)
|
|
171
|
+
i = 0
|
|
172
|
+
while(i < @fil)
|
|
173
|
+
j = 0
|
|
174
|
+
while(j < @col)
|
|
175
|
+
c.matrix[i][j] = get(i,j) * other
|
|
176
|
+
j += 1
|
|
177
|
+
end
|
|
178
|
+
i += 1
|
|
179
|
+
end
|
|
180
|
+
# Si el argumento es una matriz
|
|
181
|
+
else
|
|
182
|
+
raise ArgumentError , 'Matriz no compatible (A.fil == B.col)' unless @col == other.fil
|
|
183
|
+
c = Matriz_Densa.new(@fil, other.col)
|
|
184
|
+
i = 0
|
|
185
|
+
while(i < @fil)
|
|
186
|
+
j = 0
|
|
187
|
+
while(j < other.fil)
|
|
188
|
+
k = 0
|
|
189
|
+
c.matrix[i][j] = 0
|
|
190
|
+
while(k < @col)
|
|
191
|
+
c.matrix[i][j] = c.matrix[i][j] + (get(i,j) * other.get(i,j))
|
|
192
|
+
k += 1
|
|
193
|
+
end
|
|
194
|
+
j += 1
|
|
195
|
+
end
|
|
196
|
+
i += 1
|
|
197
|
+
end
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
# Si el resultado es una matriz dispersa se convierte la densa
|
|
201
|
+
if(c.porcentaje_nulos > 0.6)
|
|
202
|
+
c = Matriz_Dispersa.densa_a_dispersa(c)
|
|
203
|
+
end
|
|
204
|
+
c
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
# Calcula el valor maximo de la matriz dispersa
|
|
208
|
+
def max
|
|
209
|
+
# Si toda la matriz es 0, el maximo sera 0
|
|
210
|
+
if(porcentaje_nulos == 1.0)
|
|
211
|
+
return 0
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
max = nil
|
|
215
|
+
|
|
216
|
+
# Asignar al primer valor no-nulo de la matriz (el primero que encuentre hasta que deje de ser nil)
|
|
217
|
+
i = 0
|
|
218
|
+
while(max == nil)
|
|
219
|
+
if(@matrix[i].size != 0)
|
|
220
|
+
max = @matrix[i].values[0]
|
|
221
|
+
end
|
|
222
|
+
i += 1
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
# Iterar por todos los elementos no nulos para encontrar el maximo
|
|
226
|
+
i = 0
|
|
227
|
+
while(i < @matrix.size)
|
|
228
|
+
if(@matrix[i].values.max != nil and @matrix[i].values.max > max)
|
|
229
|
+
max = @matrix[i].values.max
|
|
230
|
+
end
|
|
231
|
+
i += 1
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
max
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
# Calcula el valor maximo de la matriz dispersa
|
|
238
|
+
def min
|
|
239
|
+
# Si toda la matriz es 0, el minimo sera 0
|
|
240
|
+
if (porcentaje_nulos == 1.0)
|
|
241
|
+
return 0
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
min = nil
|
|
245
|
+
|
|
246
|
+
# Asignar al primer valor no-nulo de la matriz (el primero que encuentre hasta que deje de ser nil)
|
|
247
|
+
i = 0
|
|
248
|
+
while(min == nil)
|
|
249
|
+
if(@matrix[i].size != 0)
|
|
250
|
+
min = @matrix[i].values[0]
|
|
251
|
+
end
|
|
252
|
+
i += 1
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
# Iterar por todos los elementos no nulos para encontrar el maximo
|
|
256
|
+
i = 0
|
|
257
|
+
while(i < @matrix.size)
|
|
258
|
+
if(@matrix[i].values.min != nil and @matrix[i].values.min < min)
|
|
259
|
+
min = @matrix[i].values.min
|
|
260
|
+
end
|
|
261
|
+
i += 1
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
min
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
end
|
|
268
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
require File.expand_path('../lib/matrix_expansion/version', __FILE__)
|
|
3
|
+
|
|
4
|
+
Gem::Specification.new do |gem|
|
|
5
|
+
gem.authors = ["Teno Gonzalez Dos Santos / Albano Jose Yanes Reyes"]
|
|
6
|
+
gem.email = ["alu0100536829@ull.edu.es / alu0100457094@ull.edu.es"]
|
|
7
|
+
gem.description = %q{Gema de matrices densas y dispersas}
|
|
8
|
+
gem.summary = %q{Matrices densas y dispersas.}
|
|
9
|
+
gem.homepage = "https://github.com/alu01005368291/prct10.git"
|
|
10
|
+
|
|
11
|
+
gem.files = `git ls-files`.split($\)
|
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
|
14
|
+
gem.name = "matrix_expansion"
|
|
15
|
+
gem.require_paths = ["lib"]
|
|
16
|
+
gem.version = MatrixExpansion::VERSION
|
|
17
|
+
end
|
data/spec/matrix_spec.rb
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
=begin
|
|
2
|
+
*******************************************************************************************
|
|
3
|
+
* Autores:
|
|
4
|
+
Teno González Dos Santos
|
|
5
|
+
Albano José Yanes Reyes
|
|
6
|
+
* Fecha: 19/11/2013 -
|
|
7
|
+
* Asignatura: Lenguujes y Paradigmas de Programación
|
|
8
|
+
* Tercero de Grado en Ingeniería Informática
|
|
9
|
+
* Universidad de la Laguna
|
|
10
|
+
*
|
|
11
|
+
* Practica 10: Matrices densas y dispersas
|
|
12
|
+
********************************************************************************************
|
|
13
|
+
=end
|
|
14
|
+
|
|
15
|
+
# Pruebas rspec
|
|
16
|
+
|
|
17
|
+
require "./lib/matrix_expansion.rb"
|
|
18
|
+
|
|
19
|
+
describe MatrixExpansion::Matriz do
|
|
20
|
+
before :each do
|
|
21
|
+
@m1 = MatrixExpansion::Matriz.new(5, 5)
|
|
22
|
+
end
|
|
23
|
+
describe " # Almacenamiento de matrices. " do
|
|
24
|
+
it " # Se debe almacenar el numero de filas." do
|
|
25
|
+
@m1.fil
|
|
26
|
+
end
|
|
27
|
+
it " # Se debe almacenar el numero de columnas." do
|
|
28
|
+
@m1.col
|
|
29
|
+
end
|
|
30
|
+
it " # Se debe almacenar un contenido." do
|
|
31
|
+
@m1.matrix
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
describe MatrixExpansion::Matriz_Densa do
|
|
37
|
+
|
|
38
|
+
before :each do
|
|
39
|
+
@m1 = MatrixExpansion::Matriz_Densa.new(2,2)
|
|
40
|
+
@m2 = MatrixExpansion::Matriz_Densa.new(2,2)
|
|
41
|
+
@m3 = MatrixExpansion::Matriz_Densa.new(2,2)
|
|
42
|
+
@m4 = MatrixExpansion::Matriz_Densa.new(2,2)
|
|
43
|
+
|
|
44
|
+
@m1.set_valores_num
|
|
45
|
+
@m2.set_valores_num
|
|
46
|
+
@m3.set_valores_fracc
|
|
47
|
+
@m4.set_valores_fracc
|
|
48
|
+
|
|
49
|
+
end
|
|
50
|
+
describe " # Almacenamiento de matrices. " do
|
|
51
|
+
it " # Se debe poder acceder a los datos almacenados en la matriz " do
|
|
52
|
+
@m1.matrix[0][0].should eq(1)
|
|
53
|
+
@m1.matrix[0][1].should eq(2)
|
|
54
|
+
end
|
|
55
|
+
it " # Se deben poder modificar los datos almacenados en la matriz " do
|
|
56
|
+
@m1.matrix[0][0] = 5
|
|
57
|
+
@m1.matrix[0][0].should eq(5)
|
|
58
|
+
|
|
59
|
+
@m1.matrix[0][1] = 8
|
|
60
|
+
@m1.matrix[0][1].should eq(8)
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
describe " # Representacion de la matriz." do
|
|
65
|
+
it " # Mostrar la matriz con numeros." do
|
|
66
|
+
@m1.to_s.should == "1\t2\t\n3\t4\t\n"
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
it " # Mostrar la matriz con fracciones. " do
|
|
70
|
+
@m3.to_s.should == "1/2\t2/3\t\n3/4\t4/5\t\n"
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
describe " # Operaciones con matrices. " do
|
|
75
|
+
it " # Suma de matrices. " do
|
|
76
|
+
(@m1 + @m2).to_s.should == "2\t4\t\n6\t8\t\n"
|
|
77
|
+
(@m3 + @m4).to_s.should == "1/1\t4/3\t\n3/2\t8/5\t\n"
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
it " # Resta de matrices." do
|
|
81
|
+
(@m1 - @m2).to_s.should == "0\t0\t\n0\t0\t\n"
|
|
82
|
+
(@m3 - @m4).to_s.should == "0/1\t0/1\t\n0/1\t0/1\t\n"
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
it " # Producto de matrices." do
|
|
86
|
+
(@m1 * @m2).to_s.should == "7\t10\t\n15\t22\t\n"
|
|
87
|
+
(@m3 * @m4).to_s.should == "3/4\t13/15\t\n39/40\t57/50\t\n"
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
it " # Suma de matrices de fracciones y numeros." do
|
|
91
|
+
(@m1 + @m3).to_s.should == "3/2\t8/3\t\n15/4\t24/5\t\n"
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
it " # Resta de matrices de fracciones y numeros." do
|
|
95
|
+
(@m1 - @m3).to_s.should == "1/2\t4/3\t\n9/4\t16/5\t\n"
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
it " # Producto de matrices de fracciones y numeros." do
|
|
99
|
+
(@m1 * @m3).to_s.should == "2/1\t34/15\t\n9/2\t26/5\t\n"
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
describe " # Otras operaciones. " do
|
|
104
|
+
it " # Minimo de una matriz. " do
|
|
105
|
+
@m1.min.should eq(1)
|
|
106
|
+
@m3.min.to_s.should eq("1/2")
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
it " # Maximo de una matriz. " do
|
|
110
|
+
@m1.max.should eq(4)
|
|
111
|
+
@m3.max.to_s.should eq("4/5")
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
describe MatrixExpansion::Matriz_Densa do
|
|
119
|
+
|
|
120
|
+
before :each do
|
|
121
|
+
@mden = MatrixExpansion::Matriz_Densa.new(2,2)
|
|
122
|
+
@mden.matrix[0][1] = 3
|
|
123
|
+
|
|
124
|
+
@mdis1 = MatrixExpansion::Matriz_Dispersa.densa_a_dispersa(@mden)
|
|
125
|
+
|
|
126
|
+
@mdis2 = MatrixExpansion::Matriz_Dispersa.new(2,2)
|
|
127
|
+
@mdis2.set(1,1,2)
|
|
128
|
+
|
|
129
|
+
@mdis3 = MatrixExpansion::Matriz_Dispersa.new(2,2)
|
|
130
|
+
@mdis3.set(1,1,Fraccion.new(1,2))
|
|
131
|
+
|
|
132
|
+
end
|
|
133
|
+
describe " # Almacenamiento de matrices. " do
|
|
134
|
+
it " # Se debe poder crear matrices dispersas vacias o a partir de matrices densas." do
|
|
135
|
+
MatrixExpansion::Matriz_Dispersa.new(5, 5)
|
|
136
|
+
MatrixExpansion::Matriz_Dispersa.densa_a_dispersa(@mden)
|
|
137
|
+
end
|
|
138
|
+
it " # Se debe poder acceder a los datos almacenados en la matriz " do
|
|
139
|
+
@mdis1.get(0,1).should eq(3)
|
|
140
|
+
@mdis2.get(1,1).should eq(2)
|
|
141
|
+
end
|
|
142
|
+
it " # Se deben poder modificar los datos almacenados en la matriz " do
|
|
143
|
+
@mdis1.set(0,1,5)
|
|
144
|
+
@mdis1.get(0,1).should eq(5)
|
|
145
|
+
|
|
146
|
+
@mdis2.set(1,1,8)
|
|
147
|
+
@mdis2.get(1,1).should eq(8)
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
describe " # Representacion de la matriz." do
|
|
152
|
+
it " # Mostrar la matriz con numeros." do
|
|
153
|
+
@mdis1.to_s.should == "(0,1)=>3 \n\n"
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
it " # Mostrar la matriz con fracciones. " do
|
|
157
|
+
@mdis3.to_s.should == "\n(1,1)=>1/2 \n"
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
describe " # Operaciones con matrices. " do
|
|
161
|
+
it " # Suma de matrices. " do
|
|
162
|
+
(@mdis1 + @mdis2).to_s.should == "0\t3\t\n0\t2\t\n"
|
|
163
|
+
(@mdis3 + @mdis3).to_s.should == "\n(1,1)=>1/1 \n"
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
it " # Resta de matrices." do
|
|
167
|
+
(@mdis1 - @mdis2).to_s.should == "0\t3\t\n0\t-2\t\n"
|
|
168
|
+
(@mdis3 - @mdis3).to_s.should == "\n\n"
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
it " # Producto de matrices." do
|
|
172
|
+
(@mdis1 * @mdis1).to_s.should == "(0,1)=>18 \n\n"
|
|
173
|
+
(@mdis3 * @mdis3).to_s.should == "\n(1,1)=>1/2 \n"
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
it " # Suma de matrices de fracciones y numeros." do
|
|
177
|
+
(@mdis2 + @mdis3).to_s.should == "\n(1,1)=>5/2 \n"
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
it " # Resta de matrices de fracciones y numeros." do
|
|
181
|
+
(@mdis2 - @mdis3).to_s.should == "\n(1,1)=>3/2 \n"
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
it " # Producto de matrices de fracciones y numeros." do
|
|
185
|
+
(@mdis2 * @mdis3).to_s.should == "\n(1,1)=>2/1 \n"
|
|
186
|
+
end
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
describe " # Otras operaciones. " do
|
|
190
|
+
it " # Minimo de una matriz. " do
|
|
191
|
+
@mdis1.min.should eq(3)
|
|
192
|
+
@mdis3.min.to_s.should eq("1/2")
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
it " # Maximo de una matriz. " do
|
|
196
|
+
@mdis1.max.should eq(3)
|
|
197
|
+
@mdis3.max.to_s.should eq("1/2")
|
|
198
|
+
end
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
end
|
data/test/tc_matrix.rb
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
=begin
|
|
2
|
+
*******************************************************************************************
|
|
3
|
+
* Autores:
|
|
4
|
+
Teno González Dos Santos
|
|
5
|
+
Albano José Yanes Reyes
|
|
6
|
+
* Fecha: 19/11/2013 -
|
|
7
|
+
* Asignatura: Lenguujes y Paradigmas de Programación
|
|
8
|
+
* Tercero de Grado en Ingeniería Informática
|
|
9
|
+
* Universidad de la Laguna
|
|
10
|
+
*
|
|
11
|
+
* Practica 10: Matrices densas y dispersas
|
|
12
|
+
********************************************************************************************
|
|
13
|
+
=end
|
|
14
|
+
|
|
15
|
+
# Clase Test_Matriz
|
|
16
|
+
|
|
17
|
+
require "./lib/matrix_expansion.rb"
|
|
18
|
+
require "test/unit"
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class Test_Matriz < Test::Unit::TestCase
|
|
23
|
+
def setup
|
|
24
|
+
|
|
25
|
+
#matrices densas
|
|
26
|
+
@m1 = MatrixExpansion::Matriz_Densa.new(2,2)
|
|
27
|
+
@m2 = MatrixExpansion::Matriz_Densa.new(2,2)
|
|
28
|
+
@m3 = MatrixExpansion::Matriz_Densa.new(2,2)
|
|
29
|
+
|
|
30
|
+
@m1.set_valores_num
|
|
31
|
+
|
|
32
|
+
@m2.set_valores_num
|
|
33
|
+
|
|
34
|
+
@m3.matrix[0][0] = 2
|
|
35
|
+
@m3.matrix[0][1] = 4
|
|
36
|
+
@m3.matrix[1][0] = 6
|
|
37
|
+
@m3.matrix[1][1] = 8
|
|
38
|
+
|
|
39
|
+
#matrices dispersas
|
|
40
|
+
@md1 = MatrixExpansion::Matriz_Dispersa.new(2,2)
|
|
41
|
+
@md2 = MatrixExpansion::Matriz_Dispersa.new(2,2)
|
|
42
|
+
@md3 = MatrixExpansion::Matriz_Dispersa.new(2,2)
|
|
43
|
+
|
|
44
|
+
@md1.set(0,0,0)
|
|
45
|
+
@md1.set(0,1,0)
|
|
46
|
+
@md1.set(1,0,5)
|
|
47
|
+
@md1.set(1,1,0)
|
|
48
|
+
|
|
49
|
+
@md2.set(0,0,0)
|
|
50
|
+
@md2.set(0,1,0)
|
|
51
|
+
@md2.set(1,0,3)
|
|
52
|
+
@md2.set(1,1,0)
|
|
53
|
+
|
|
54
|
+
@md3.set(0,0,0)
|
|
55
|
+
@md3.set(0,1,0)
|
|
56
|
+
@md3.set(1,0,2)
|
|
57
|
+
@md3.set(1,1,0)
|
|
58
|
+
|
|
59
|
+
#matrices densas para provocar fallo typecheck
|
|
60
|
+
@merr1 = MatrixExpansion::Matriz_Densa.new(1,1)
|
|
61
|
+
@merr2 = MatrixExpansion::Matriz_Densa.new(1,1)
|
|
62
|
+
@merr1.matrix[0][0] = 5
|
|
63
|
+
@merr2.matrix[0][0] ="error"
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
#matrices densas para provocar error en valor de argumentos
|
|
67
|
+
@mfail1 = MatrixExpansion::Matriz_Densa.new(1,1)
|
|
68
|
+
@mfail2 = MatrixExpansion::Matriz_Densa.new(2,2)
|
|
69
|
+
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def test_simple
|
|
73
|
+
assert_equal(@m3.to_s,(@m1+@m2).to_s)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def test_simple2
|
|
77
|
+
assert_equal(@md3.to_s,(@md1-@md2).to_s)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def test_typecheck
|
|
81
|
+
assert_raise(TypeError) {@merr1+@merr2}
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def test_failure
|
|
85
|
+
assert_raise(ArgumentError) {@mfail1*@mfail2}
|
|
86
|
+
end
|
|
87
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: matrix_expansion
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Teno Gonzalez Dos Santos / Albano Jose Yanes Reyes
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2013-11-20 00:00:00.000000000 Z
|
|
13
|
+
dependencies: []
|
|
14
|
+
description: Gema de matrices densas y dispersas
|
|
15
|
+
email:
|
|
16
|
+
- alu0100536829@ull.edu.es / alu0100457094@ull.edu.es
|
|
17
|
+
executables: []
|
|
18
|
+
extensions: []
|
|
19
|
+
extra_rdoc_files: []
|
|
20
|
+
files:
|
|
21
|
+
- .gitignore
|
|
22
|
+
- .travis.yml
|
|
23
|
+
- Gemfile
|
|
24
|
+
- Guardfile
|
|
25
|
+
- LICENSE
|
|
26
|
+
- README.md
|
|
27
|
+
- Rakefile
|
|
28
|
+
- lib/matrix_expansion.rb
|
|
29
|
+
- lib/matrix_expansion/fraccion.rb
|
|
30
|
+
- lib/matrix_expansion/gcd.rb
|
|
31
|
+
- lib/matrix_expansion/matrix.rb
|
|
32
|
+
- lib/matrix_expansion/matrix_densa.rb
|
|
33
|
+
- lib/matrix_expansion/matrix_dispersa.rb
|
|
34
|
+
- lib/matrix_expansion/version.rb
|
|
35
|
+
- matrix_expansion.gemspec
|
|
36
|
+
- spec/matrix_spec.rb
|
|
37
|
+
- test/tc_matrix.rb
|
|
38
|
+
homepage: https://github.com/alu01005368291/prct10.git
|
|
39
|
+
licenses: []
|
|
40
|
+
post_install_message:
|
|
41
|
+
rdoc_options: []
|
|
42
|
+
require_paths:
|
|
43
|
+
- lib
|
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
45
|
+
none: false
|
|
46
|
+
requirements:
|
|
47
|
+
- - ! '>='
|
|
48
|
+
- !ruby/object:Gem::Version
|
|
49
|
+
version: '0'
|
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
51
|
+
none: false
|
|
52
|
+
requirements:
|
|
53
|
+
- - ! '>='
|
|
54
|
+
- !ruby/object:Gem::Version
|
|
55
|
+
version: '0'
|
|
56
|
+
requirements: []
|
|
57
|
+
rubyforge_project:
|
|
58
|
+
rubygems_version: 1.8.24
|
|
59
|
+
signing_key:
|
|
60
|
+
specification_version: 3
|
|
61
|
+
summary: Matrices densas y dispersas.
|
|
62
|
+
test_files:
|
|
63
|
+
- spec/matrix_spec.rb
|
|
64
|
+
- test/tc_matrix.rb
|