prac09 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.
@@ -0,0 +1,18 @@
1
+
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.rb~
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 1.9.3
5
+ - jruby-19mode
6
+ - rbx-19mode
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in prac09.gemspec
4
+ gemspec
@@ -0,0 +1,12 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'bundler' do
5
+ watch('Gemfile')
6
+ end
7
+
8
+ guard 'rspec', :version => 2 do
9
+ watch(%r{^spec/.+_spec\.rb$})
10
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
11
+ watch('spec/spec_helper.rb') { "spec" }
12
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Leonor Priego, Carlos Martin
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.
@@ -0,0 +1,47 @@
1
+ ==============================================================
2
+ Prac09
3
+ Estado actual de TRAVIS [![Build Status](https://travis-ci.org/alu0100699906/pract09.png?branch=master)](https://travis-ci.org/alu0100699906/pract09)
4
+ ==============================================================
5
+
6
+
7
+ La Gema Prac09 consta de clases que nos permiten crear, gestionar y trabajar, de manera distinta, con matrices densas y matrices dispersas.
8
+
9
+ Como dato, cabe añadir que una matriz se considerará dispersa si tiene más de un 60% de ceros.
10
+
11
+ ## Instalación
12
+
13
+ Añade esta línea al Gemfile de tu aplicación:
14
+
15
+ gem 'prac09'
16
+
17
+ Luego ejecuta:
18
+
19
+ $ bundle
20
+
21
+ O instálalo tú mismo de esta forma:
22
+
23
+ $ gem install prac09
24
+
25
+ ## Uso
26
+
27
+ Si quieres usar esta gema en tu aplicación Ruby, incluye el fichero "pract09"
28
+
29
+ ## Contribución
30
+
31
+ 1. Haz un fork
32
+ 2. Crea tu rama de características (`git checkout -b my-new-feature`)
33
+ 3. Haz un commit de tus cambios (`git commit -am 'Add some feature'`)
34
+ 4. Empuja los cambios a la rama(`git push origin my-new-feature`)
35
+ 5. Crea un nuevo Pull Request.
36
+
37
+ ## Documentación
38
+
39
+ ### Herencia de clases
40
+
41
+ clase Matriz //clase base
42
+ .________|______.
43
+ ↓ ↓
44
+ clase Mdispersa clase Mdensa //clases derivadas
45
+
46
+ La clase Matriz contendrá la implementacion de unos métodos generales para las operaciones +, -, *, to_s, == (método que pasa a una cadena de caracteres). Las clases derivadas implementarán para cada una los métodos de comparación y de acceso ([]=, []) ya que éstos tienen funcionamientos distintos debido a cómo almacena la información cada una de las clases.
47
+
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+
5
+ RSpec::Core::RakeTask.new
6
+ task :default => :spec
7
+
8
+ desc "Ejecutamos las pruebas unitarias"
9
+ task :test do
10
+ sh "ruby -Ilib test/tc_matrices.rb"
11
+ end
@@ -0,0 +1,9 @@
1
+ require "prac09/version"
2
+
3
+
4
+ module Prac09
5
+ require "prac09/fraccion"
6
+ require "prac09/matriz"
7
+ require "prac09/matrizdispersa"
8
+ require "prac09/matrizdensa"
9
+ end
@@ -0,0 +1,149 @@
1
+ class Fraccion
2
+
3
+ #el modulo comparable contiene todos los metodos que permiten hacer comparaciones
4
+ include Comparable
5
+ #metodo que permite acceder a los atributos de la clase
6
+ attr_reader :numerador, :denominador
7
+
8
+ def initialize(n,d)
9
+ simp=gcd(n,d) #guarda el maximo comun divisor
10
+
11
+ if d!=0
12
+ @numerador=n
13
+ @denominador=d
14
+ end
15
+
16
+ #utiliza el maximo comun divisor para simplificar la fraccion
17
+ @numerador, @denominador = n/simp, d/simp
18
+ end
19
+
20
+ #metodo que devuelve el numerador
21
+ def num()
22
+ @numerador
23
+ end
24
+
25
+ #metodo que devuelve el denominador
26
+ def denom()
27
+ @denominador
28
+ end
29
+
30
+ #metodo para mostrar por la consola la fraccion
31
+ def to_s
32
+ "#{@numerador}/#{@denominador}"
33
+ end
34
+
35
+ #metodo para mostrar por la consola la fraccion en punto flotante
36
+ def to_float
37
+ (@numerador.to_f()/@denominador.to_f)
38
+ end
39
+
40
+ #metodo para calcular el valor absoluto de una fraccion
41
+ def abs ()
42
+ Fraccion.new(@numerador.abs,@denominador.abs)
43
+ end
44
+
45
+ ##metodo para calcular el reciproco de una fraccion
46
+ def reciprocal ()
47
+ Fraccion.new(@denominador,@numerador)
48
+ end
49
+
50
+ #metodo para calcular el opuesto de una fraccion
51
+ def -@ ()
52
+ if(@numerador>0 && @denominador<0)
53
+ @numerador = @numerador.abs
54
+ @denominador = @denominador.abs
55
+
56
+ elsif(@numerador<0 && @denominador>0)
57
+ @numerador = @numerador.abs
58
+ @denominador = @denominador.abs
59
+
60
+ else
61
+ @numerador = -(@numerador.abs)
62
+ @denominador = @denominador.abs
63
+ end
64
+ Fraccion.new(@numerador,@denominador)
65
+ end
66
+
67
+ #metodo para calcular la suma de dos fracciones
68
+ def +(object)
69
+ if (object.instance_of?(Fraccion)==true)
70
+
71
+ aux=mcm(@denominador,object.denominador)
72
+ return Fraccion.new((((aux*@numerador)/@denominador)+(aux*object.numerador)/object.denominador),aux)
73
+
74
+ end
75
+ if (object.instance_of?( Fixnum )==true)
76
+ aux=Fraccion.new(object, 1)
77
+ m=mcm(@denominador,aux.denominador)
78
+ return Fraccion.new((((m*@numerador)/@denominador) + (m*aux.numerador)/aux.denominador),m)
79
+ end
80
+ end
81
+
82
+ #metodo para calcular la resta de dos fracciones
83
+ def -(object)
84
+ aux=mcm(@denominador,object.denominador)
85
+ Fraccion.new((((aux*@numerador)/@denominador)-(aux*object.numerador)/object.denominador),aux)
86
+ end
87
+
88
+ #metodo para calcular la multiplicacion de dos fracciones
89
+ def *(object)
90
+ if(object.is_a?(Numeric))
91
+ object = Fraccion.new(object,1)
92
+ end
93
+ Fraccion.new(@numerador*object.numerador,@denominador*object.denominador)
94
+ end
95
+
96
+ #metodo para calcular la division de dos fracciones
97
+ def /(object)
98
+ #multiplica numerador con numerador y denominador con denominador
99
+ Fraccion.new(@numerador*object.denominador,@denominador*object.numerador)
100
+
101
+
102
+ end
103
+
104
+ #metodo para calcular el resto de la division de dos fracciones
105
+ def % (object)
106
+ #guarda la division de las fracciones
107
+ division=self.abs/object.abs
108
+ #calcula el resto
109
+ resto=object.abs * (division - Fraccion.new(division.to_float.to_i,1))
110
+ if (object.numerador < 0)
111
+ #si el numerador es negativo imprime el opuesto del resto
112
+ return -resto
113
+ end
114
+ #lo imprime
115
+ resto
116
+ end
117
+
118
+
119
+ #Utilizamos el metodo del modulo Comparable para realizar operaciones comparacionales de fracciones
120
+ def <=> (object)
121
+ self.to_float<=>object.to_float
122
+ end
123
+
124
+ def gcd(n, d)
125
+ n1,n2 = n.abs,d.abs
126
+ gcd = 1
127
+ k = 1
128
+ while (k <= n1 and k <= n2)
129
+ if (n1 % k == 0 and n2 % k == 0)
130
+ gcd = k
131
+ end
132
+ k += 1
133
+ end
134
+ return gcd
135
+ end
136
+
137
+
138
+ #metodo para hallar el minimo comun multiplo de los dos numeros pasados como parametros
139
+ def mcm(a,b)
140
+ #mediante el metodo de euclides, resolvemos el minimo comun multiplo mediante el
141
+ #maximo comun divisor
142
+ aux=gcd(a,b)
143
+ (a/aux)*b
144
+ end
145
+
146
+ def coerce(object)
147
+ [self,object]
148
+ end
149
+ end
@@ -0,0 +1,190 @@
1
+
2
+ class Matriz
3
+
4
+ def initialize(filas, columnas)
5
+ @filas, @columnas = filas , columnas
6
+ end
7
+
8
+ #get filas
9
+ def filas()
10
+ @filas
11
+ end
12
+
13
+ #get columnas
14
+ def columnas()
15
+ @columnas
16
+ end
17
+
18
+
19
+ def Matriz.constructor(filas, columnas, velementos)
20
+
21
+ longitud=filas*columnas
22
+ if (((velementos.count(0)*100)/(longitud)) >= 60)
23
+
24
+ MatrizDispersa.new(filas,columnas,velementos)
25
+ else
26
+
27
+ MatrizDensa.new(filas,columnas,velementos)
28
+ end
29
+ end
30
+
31
+
32
+ def +(object)
33
+
34
+ if ( (@filas == object.filas) && (@columnas == object.columnas))
35
+
36
+ matrizresultado=Array.new
37
+ i = 0
38
+ while (i < @filas) do
39
+
40
+ j=0
41
+ while (j < @columnas) do
42
+
43
+ matrizresultado << (self[i,j] + object[i,j])
44
+ j=j+1
45
+ end
46
+ i=i+1
47
+ end
48
+ Matriz.constructor(@filas, @columnas, matrizresultado)
49
+ end
50
+ end
51
+
52
+
53
+ def -(object)
54
+
55
+ if ( (@filas == object.filas) && (@columnas == object.columnas))
56
+
57
+ matrizresultado=Array.new
58
+ i = 0
59
+ while (i < @filas) do
60
+
61
+ j=0
62
+ while (j < @columnas) do
63
+
64
+ matrizresultado << (self[i,j] - object[i,j])
65
+ j=j+1
66
+ end
67
+ i=i+1
68
+ end
69
+ Matriz.constructor(@filas, @columnas, matrizresultado)
70
+ end
71
+ end
72
+
73
+
74
+
75
+ def ==(object)
76
+ if ((@filas == object.filas) && (@columnas == object.columnas))
77
+ i = 0
78
+ while (i < @filas) do
79
+
80
+ j = 0
81
+ while (j < @columnas) do
82
+
83
+ if (self[i,j] == object[i,j]) #comparamos elemento a elemento
84
+ es_igual = true
85
+ else
86
+ return es_igual = false #si solo uno es distinto la funcion devuelve falso
87
+ end
88
+ j = j + 1
89
+ end
90
+ i = i + 1
91
+ end
92
+ end
93
+ return es_igual #si compara todos los elementos y son iguales devuelve verdadero
94
+ end
95
+
96
+ def *(object)
97
+
98
+ if(object.is_a? Numeric)
99
+ matrizresultado=Array.new
100
+ i=0
101
+ while (i<@filas)
102
+ j=0
103
+ while (j<@columnas)
104
+ matrizresultado << (self[i,j]*object)
105
+ j=j+1
106
+ end
107
+ i=i+1
108
+ end
109
+ return Matriz.constructor(@filas,@columnas,matrizresultado)
110
+
111
+
112
+ elsif ((object.is_a?(Matriz)==true) && (@columnas==object.filas()))
113
+ i=0
114
+ matrizresultado = Array.new
115
+ while (i < @filas)
116
+ j = 0
117
+ while (j < object.columnas())
118
+ k = 0
119
+ matrizresultado<<0
120
+ while (k < @columnas) #itera en las columnas de la primera matriz y las filas de la segunda
121
+ matrizresultado[(matrizresultado.size)-1]= (matrizresultado.last + (self[i,k] * object[k,j]))
122
+ k = k + 1
123
+ end
124
+ j = j + 1
125
+ end
126
+ i = i + 1
127
+ end
128
+ return Matriz.constructor(@filas,object.columnas,matrizresultado)
129
+ end
130
+ end
131
+
132
+ def max()
133
+ maximo = 0 # maximo provisional
134
+ i, j = 0,0
135
+ while i<=@filas
136
+ while j<=@columnas
137
+ if (self[i,j]>maximo) #si el valor es mayor que el provisional almacena el nuevo
138
+ maximo = self[i,j]
139
+ end
140
+ j += 1;
141
+ end
142
+ i += 1;
143
+ end
144
+ return maximo
145
+ end
146
+
147
+ def min()
148
+ minimo = 0 # maximo provisional
149
+ i, j = 0,0
150
+ while i<=@filas
151
+ while j<=@columnas
152
+ if (self[i,j]<minimo) #si el valor es mayor que el provisional almacena el nuevo
153
+ minimo = self[i,j]
154
+ end
155
+ j += 1;
156
+ end
157
+ i += 1;
158
+ end
159
+ return minimo
160
+ end
161
+
162
+ def to_s
163
+ string= "[" ##corchete de matriz
164
+
165
+ fil=0
166
+ while (fil<@filas)
167
+ string = string + "[" #corchete de fila
168
+ col=0
169
+ while (col<@columnas)
170
+ string = string + "#{self[fil,col]}"
171
+ col += 1
172
+ if(col < @columnas)
173
+ string = string + ","
174
+ end
175
+ end
176
+ string = string + "]" #cerramos el corchete de fila
177
+ fil += 1
178
+ if (fil < @filas)
179
+ string = string + ","
180
+ end
181
+ end
182
+ string = string +"]" ##cerramos el corchete de matriz
183
+ end
184
+
185
+
186
+ def coerce(object)
187
+ [self,object]
188
+ end
189
+
190
+ end
@@ -0,0 +1,22 @@
1
+
2
+
3
+ class MatrizDensa < Matriz
4
+
5
+ def initialize(filas, columnas, velementos)
6
+ super(filas, columnas)
7
+ @matriz=Array.new(velementos)
8
+ end
9
+
10
+ def pos(i,j)
11
+ (i*@columnas)+j
12
+ end
13
+
14
+ def [](i,j)
15
+ @matriz[pos(i,j)]
16
+ end
17
+
18
+ def []=(i,j,valor)
19
+ @matriz[pos(i,j)] = valor
20
+ end
21
+
22
+ end
@@ -0,0 +1,59 @@
1
+
2
+ class MatrizDispersa < Matriz
3
+
4
+ def initialize(filas, columnas, velementos)
5
+ super(filas, columnas)
6
+ @vvalores= Array.new
7
+ @vfil= Array.new
8
+ @vcol = Array.new
9
+
10
+ longitud=(@filas*@columnas)
11
+ i=0
12
+ while(i<longitud)
13
+
14
+ if(velementos[i]!=0)
15
+
16
+ @vvalores<< (velementos[i])
17
+ @vfil << (i/@columnas)
18
+ @vcol<< (i%@columnas)
19
+ end
20
+ i=i+1
21
+ end
22
+ end
23
+
24
+ def [](f,c)
25
+ no_nulo = @vvalores.size
26
+ i=0
27
+ while(i < no_nulo)
28
+
29
+ if (@vfil[i] == f && @vcol[i] == c)
30
+ return @vvalores[i]
31
+ end
32
+ i=i+1
33
+ end
34
+ return 0
35
+ end
36
+
37
+ def []= (i,j,nvalor) #setter
38
+ ind = 0
39
+ while ind<@vvalores.size
40
+ if (i == @vfil[ind] && j == @vcol[ind] && nvalor != 0) #para poner un nuevo valor no nulo
41
+ @vvalor[ind]=nvalor
42
+ return
43
+ end
44
+ if (i == @vfil[ind] && j == @vcol[ind] && nvalor == 0) #para poner un nuevo valor nulo en una posicion ocupada anteriormente
45
+ @vvalores.delete_at(ind) #simplemente eliminamos el valor que habia antes pues los nulos no se almacenan
46
+ @vfil.delete_at(ind)
47
+ @vcol.delete_at(ind)
48
+ return
49
+ end
50
+ ind += 1
51
+ end #si la posicion no existia la anyadimos con el correspondiente valor
52
+ @vvalores << (nvalor)
53
+ @vfil << (i)
54
+ @vcol << (j)
55
+
56
+ end
57
+
58
+
59
+ end
@@ -0,0 +1,3 @@
1
+ module Prac09
2
+ VERSION = "0.0.1"
3
+ end
Binary file
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'prac09/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "prac09"
8
+ spec.version = Prac09::VERSION
9
+ spec.authors = ["Leonor Priego","Carlos Martin"]
10
+ spec.email = ["alu0100699906@ull.edu.es","alu0100702293@ull.edu.es"]
11
+ spec.description = %q{Clases que permiten gestionar matrices densas y dispersas de manera distinta}
12
+ spec.summary = %q{Aplicación para trabajar con matrices densas y dispersas}
13
+ spec.homepage = "https://github.com/alu0100699906/pract09"
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
@@ -0,0 +1,169 @@
1
+ require "prac09/fraccion.rb"
2
+
3
+ describe Fraccion do #espectativa para la clase
4
+ ##
5
+ describe "#se asignan bien los valores de denominador y numerador"do
6
+ it "Creacion correcta de fraccion con numerador y denom." do
7
+ fa = Fraccion.new(1,2)
8
+ fa.numerador.should == 1
9
+ fa.denominador.should == 2
10
+ end
11
+ end
12
+ ##
13
+ describe "#la fraccion esta en su minima expresion" do
14
+ it "Fraccion simplificada" do
15
+ fa = Fraccion.new(6,4)
16
+ fa.numerador.should == 3
17
+ fa.denominador.should == 2
18
+ end
19
+ end
20
+ ##
21
+ describe "#el valor del numerador y denominador se obtiene mediante num() y denom()" do
22
+ it ".num() existe" do
23
+ fa = Fraccion.new(1,2)
24
+ fa.num() == 1
25
+ end
26
+ it ".denom() existe" do
27
+ fa = Fraccion.new(1,2)
28
+ fa.denom() == 2
29
+ end
30
+ end
31
+ ##
32
+ describe " #Se debe mostrar num/denom" do
33
+ it " .to_s()" do
34
+ fa = Fraccion.new(1,2)
35
+ fa.to_s().should == "1/2"
36
+ end
37
+ end
38
+ ##
39
+ describe " #Se debe mostrar en formato flotante" do
40
+ it " .to_float()" do
41
+ fa = Fraccion.new(1,2)
42
+ fa.to_float().should == 0.5
43
+ end
44
+ end
45
+ ##
46
+ describe " #comparacion con == "do
47
+ it "\n comparando"do
48
+ fa = Fraccion.new(6,4)
49
+ fb = Fraccion.new(3,2)
50
+ fc = Fraccion.new(1,5)
51
+
52
+ (fa == fb).should be_true
53
+ (fa == fc).should be_false
54
+ (fb == fc).should be_false
55
+ end
56
+ end
57
+ ##
58
+ describe " #valor absoluto con metodo abs"do
59
+ it ".abs"do
60
+ fa = Fraccion.new(-6,4)
61
+ fb = Fraccion.new(1,-5)
62
+ fc = Fraccion.new(1,5)
63
+
64
+ fa.abs.num.should == 3
65
+ fa.abs.denom.should == 2
66
+ fb.abs.should == fc
67
+ end
68
+ end
69
+
70
+ ##
71
+ describe " #Operaciones con fracciones"do
72
+ it ".recriprocal calcula inversa" do
73
+ fa = Fraccion.new(3,2)
74
+ fb = Fraccion.new(2,3)
75
+
76
+ (fa.reciprocal == fb).should be_true
77
+ end
78
+ it "-@ opuesto" do
79
+ fa = Fraccion.new(2,4)
80
+ fb = Fraccion.new(-1,2)
81
+ fc = Fraccion.new(1,-4)
82
+
83
+ ((-fa) == fa).should be_true
84
+ (-fb).to_s.should eq("1/2")
85
+ (-fc).should eq(Fraccion.new(1,4))
86
+ end
87
+ it "@+@ operacion suma" do
88
+ fa = Fraccion.new(3,2)
89
+ fb = Fraccion.new(1,4)
90
+ fc = Fraccion.new(7,4)
91
+
92
+ (fa + fb).should == fc
93
+ (fb + fc).should eq(Fraccion.new(2,1))
94
+ end
95
+ it "@-@ operacion resta" do
96
+ fa = Fraccion.new(2,3)
97
+ fb = Fraccion.new(1,4)
98
+ fc = Fraccion.new(5,12)
99
+
100
+ (fa - fb).should == fc
101
+ (fb - fc).should eq(Fraccion.new(-1,6))
102
+ end
103
+ it "@*@ operacion producto" do
104
+ fa = Fraccion.new(2,3)
105
+ fb = Fraccion.new(1,4)
106
+ fc = Fraccion.new(5,2)
107
+
108
+ (fa * fb).should eq(Fraccion.new(1,6))
109
+ (fa * fc).should eq(Fraccion.new(5,3))
110
+ (fb * fc).should eq(Fraccion.new(5,8))
111
+ end
112
+ it "@/@ operacion division" do
113
+ fa = Fraccion.new(2,3)
114
+ fb = Fraccion.new(5,2)
115
+ fc = Fraccion.new(4,15)
116
+
117
+ (fa / fb == fc).should be_true
118
+ (fa / fc == fb).should be_true
119
+ end
120
+ it "@%@ operacion modulo" do
121
+ fa = Fraccion.new(2,3)
122
+ fb = Fraccion.new(5,2)
123
+ fc = Fraccion.new(4,15)
124
+
125
+ (fa % fb).to_s.should =="2/3"
126
+ (fa % fc).to_s.should =="2/15"
127
+ end
128
+ it "@<@ operacion menor que" do
129
+ fa = Fraccion.new(1,4)
130
+ fb = Fraccion.new(5,2)
131
+ fc = Fraccion.new(3,4)
132
+
133
+ (fa < fb).should be_true
134
+ (fa < fc).should be_true
135
+ (fc < fb).should be_true
136
+ end
137
+ it "@>@ operacion mayor que" do
138
+ fa = Fraccion.new(1,4)
139
+ fb = Fraccion.new(5,2)
140
+ fc = Fraccion.new(3,4)
141
+
142
+ (fb > fa).should be_true
143
+ (fc > fa).should be_true
144
+ (fb > fc).should be_true
145
+ end
146
+ it "@<=@ operacion menor o igual que" do
147
+ fa = Fraccion.new(1,4)
148
+ fb = Fraccion.new(5,2)
149
+ fc = Fraccion.new(1,4)
150
+
151
+ (fa <= fb).should be_true
152
+ (fa <= fc).should be_true
153
+ (fc <= fb).should be_true
154
+ end
155
+ it "@>=@ operacion mayor o igual que" do
156
+ fa = Fraccion.new(1,4)
157
+ fb = Fraccion.new(5,2)
158
+ fc = Fraccion.new(1,4)
159
+
160
+ (fb >= fa).should be_true
161
+ (fc >= fa).should be_true
162
+ (fb >= fc).should be_true
163
+ end
164
+ end
165
+
166
+
167
+ end
168
+
169
+
@@ -0,0 +1,128 @@
1
+
2
+ require 'prac09.rb'
3
+
4
+ describe Matriz do
5
+
6
+ before (:each) do
7
+
8
+ @ma = Matriz.constructor(2,2,[1,1,2,2])
9
+ @mz = Matriz.constructor(2,2,[1,1,2,2])
10
+ @md = Matriz.constructor(2,2,[2,2,4,4])
11
+ @dis = Matriz.constructor(2,2,[2,0,0,0])
12
+
13
+ @mb = Matriz.constructor(3,3,[0,0,0,0,0,1,0,0,0])
14
+ @mg = Matriz.constructor(3,3,[0,0,0,0,0,1,0,0,0])
15
+ @mo = Matriz.constructor(3,3,[0,0,0,0,0,2,0,0,0])
16
+ @mp = Matriz.constructor(3,3,[1,2,3,0,0,0,0,0,4])
17
+ @mq = Matriz.constructor(3,3,[2,4,0,0,0,0,2,0,0])
18
+
19
+ @fa = Fraccion.new(1,2)
20
+ @fb = Fraccion.new(3,4)
21
+ @fc = Fraccion.new(5,6)
22
+ @fd = Fraccion.new(7,8)
23
+ @ra = Fraccion.new(11,8)
24
+ @ro = Fraccion.new(19,12)
25
+
26
+ @a = Matriz.constructor(2,2, [@fa,@fb,@fc,@fd])
27
+ @b = Matriz.constructor(2,2,[@fd, @fc, @fb, @fa])
28
+ @c = Matriz.constructor(2,2,[@ra,@ro,@ro,@ra])
29
+
30
+ @pro1 = Matriz.constructor(3,3,[1,1,1,2,2,2,3,3,3])
31
+ @pro2 = Matriz.constructor(3,3,[1,2,3,1,2,3,1,2,3])
32
+
33
+ @multip = Matriz.constructor(3,2,[2,3,0,1,0,3])
34
+ @multip1 = Matriz.constructor(2,4,[0,1,0,0,7,0,2,0])#dispersa
35
+
36
+ @l1=Matriz.constructor(2,2,[0,0,0,1])
37
+ @l2=Matriz.constructor(2,2,[0,1,0,0])
38
+
39
+ @l4=Matriz.constructor(3,3,[0,1,2,1,0,0,0,0,0])
40
+ @l5=Matriz.constructor(3,3,[2,0,2,2,0,0,0,0,0])
41
+
42
+
43
+ end
44
+
45
+ describe "# pruebas " do
46
+
47
+ it "instanciacion de un racional" do
48
+ @fa = Fraccion.new(1,2)
49
+ @fa.numerador.should == 1
50
+ @fa.denominador.should == 2
51
+ end
52
+
53
+ it "suma de matrices con fracciones" do
54
+ (@a + @b).should == @c
55
+ end
56
+
57
+
58
+ it "#comprobacion de clases" do
59
+ @mb.instance_of?(MatrizDispersa)
60
+ @ma.instance_of?(MatrizDensa)
61
+ @dis.instance_of?(MatrizDispersa)
62
+ end
63
+
64
+ it " igualdad densa "do
65
+ @ma.should == @mz
66
+ end
67
+
68
+ it " igualdad dispersa "do
69
+ @mb.should == @mg
70
+ end
71
+
72
+ it " suma " do
73
+
74
+ (@ma + @mz).should == @md #suma densas
75
+ (@mg + @mb).should == @mo #suma dispersas
76
+ (@mp + @mq).to_s.should == ('[[3,6,3],[0,0,0],[2,0,4]]') #suma dispersas
77
+ (@a + @md).to_s.should == ('[[5/2,11/4],[29/6,39/8]]')#suma racional con densa
78
+ (@a + @dis).to_s.should ==('[[5/2,3/4],[5/6,7/8]]') #suma racional con dispersa
79
+ (@dis+ @a).to_s.should == ('[[5/2,3/4],[5/6,7/8]]') #suma dispersa con racional
80
+ (@ma + @dis).to_s.should == ('[[3,1],[2,2]]') #suma dispersa con densa
81
+
82
+ end
83
+
84
+ it "resta" do
85
+ (@md -@mz).should eq(@ma)
86
+ (@mo - @mg).should eq(@mb)
87
+ end
88
+
89
+
90
+ it " producto "do
91
+ (2*@pro1).to_s.should ==('[[2,2,2],[4,4,4],[6,6,6]]')
92
+ (@ma * @a).to_s.should == ('[[4/3,13/8],[8/3,13/4]]')
93
+ (@a * @l1).to_s.should == ('[[0/4,3/4],[0/24,7/8]]')
94
+ ( @a * @a ).to_s.should == ('[[7/8,33/32],[55/48,89/64]]') #multiplica 2 racionales
95
+ (@pro1 * @pro2).to_s.should == ('[[3,6,9],[6,12,18],[9,18,27]]')
96
+ (@multip * @multip1).to_s.should ==('[[21,2,6,0],[7,0,2,0],[21,0,6,0]]') #multiplica densa con dispersa
97
+ (@l1*@l2).to_s.should ==('[[0,0],[0,0]]') #multiplica dispersas
98
+ (@l4*@l5).to_s.should ==('[[2,0,0],[2,0,2],[0,0,0]]') #multiplica dispersas
99
+ end
100
+
101
+ it " getter y setter"do
102
+ @mz[0,0].should == 1
103
+ @ma[0,1]= 2
104
+ @ma[0,1].should == 2
105
+
106
+ @multip[0,0].should == 2
107
+ @multip[1,2]= 5
108
+ @mq[0,1].should == 4
109
+
110
+
111
+ @mb[1,2].should == 1
112
+ @mb[0,0]= 5
113
+ @mb[0,0].should == 5
114
+
115
+
116
+
117
+ end
118
+ it " maximo y minimo"do
119
+ @mq.max.should == 4
120
+ @md.max.should == 4
121
+
122
+
123
+ end
124
+
125
+ end
126
+
127
+ end
128
+
@@ -0,0 +1,69 @@
1
+ require 'prac09.rb'
2
+ require 'test/unit'
3
+
4
+ class Test_Matrices < Test::Unit::TestCase
5
+ def setup
6
+ @ma = Matriz.constructor(2,2,[1,1,2,2])
7
+ @mz = Matriz.constructor(2,2,[1,1,2,2])
8
+ @md = Matriz.constructor(2,2,[2,2,4,4])
9
+ @mc = Matriz.constructor(3,3,[8,8,4,5,4,7,1,2,9])
10
+
11
+ @mb = Matriz.constructor(3,3,[0,0,0,0,0,1,0,0,0])
12
+ @mg = Matriz.constructor(3,3,[0,0,0,0,0,1,0,0,0])
13
+ @mo = Matriz.constructor(3,3,[0,0,0,0,0,2,0,0,0])
14
+ @mp = Matriz.constructor(3,3,[1,2,3,0,0,0,0,0,4])
15
+ @mq = Matriz.constructor(3,3,[2,4,0,0,0,0,2,0,0])
16
+ @mr = Matriz.constructor(3,3,[3,6,3,0,0,0,2,0,4])
17
+
18
+ @fa = Fraccion.new(1,2)
19
+ @fb = Fraccion.new(3,4)
20
+ @fc = Fraccion.new(5,6)
21
+ @fd = Fraccion.new(7,8)
22
+ @ra = Fraccion.new(11,8)
23
+ @ro = Fraccion.new(19,12)
24
+ @rb = Fraccion.new(33,32)
25
+ @rc = Fraccion.new(55,48)
26
+ @rd = Fraccion.new(89,64)
27
+
28
+ @a = Matriz.constructor(2,2, [@fa,@fb,@fc,@fd])
29
+ @b = Matriz.constructor(2,2,[@fd, @fc, @fb, @fa])
30
+ @c = Matriz.constructor(2,2,[@ra,@ro,@ro,@ra])
31
+
32
+ @pro1 = Matriz.constructor(3,3,[1,1,1,2,2,2,3,3,3])
33
+ @pro2 = Matriz.constructor(3,3,[1,2,3,1,2,3,1,2,3])
34
+ @pro3 = Matriz.constructor(3,3,[3,6,9,6,12,18,9,18,27])
35
+ end
36
+
37
+ def tear_down
38
+ #nothing
39
+ end
40
+
41
+ #pruebas para el metodo suma
42
+ def test_suma
43
+ assert_equal(@mr,(@mq+@mp)) #dispersas
44
+ assert_equal(@c,(@a+@b)) #fraccionales
45
+ assert_equal(@md,(@ma+@mz)) #densa
46
+ end
47
+
48
+ #pruebas para el metodo resta
49
+ def test_resta
50
+ assert_equal(@ma, (@md-@mz)) #densa
51
+ assert_equal(@a, (@c-@b)) #fraccionales
52
+ assert_equal(@mp, (@mr-@mq)) #dispersa
53
+ end
54
+
55
+ #pruebas para el metodo producto
56
+ def test_producto
57
+ assert_equal(@pro3, (@pro1*@pro2))
58
+ assert_equal((@ma * @a).to_s,('[[4/3,13/8],[8/3,13/4]]'))
59
+ end
60
+
61
+ #pruebas para los fallos
62
+ def test_failure
63
+ assert_equal(@c,(@a+@b))
64
+ end
65
+
66
+ def test_maximo_minimo
67
+ assert_equal(@mq.max,4)
68
+ end
69
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: prac09
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Leonor Priego
9
+ - Carlos Martin
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-11-19 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: bundler
17
+ requirement: &9760260 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: '1.3'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: *9760260
26
+ - !ruby/object:Gem::Dependency
27
+ name: rake
28
+ requirement: &9759780 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *9759780
37
+ - !ruby/object:Gem::Dependency
38
+ name: rspec
39
+ requirement: &9759260 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *9759260
48
+ - !ruby/object:Gem::Dependency
49
+ name: guard-rspec
50
+ requirement: &9758660 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *9758660
59
+ - !ruby/object:Gem::Dependency
60
+ name: guard-bundler
61
+ requirement: &9758080 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: *9758080
70
+ description: Clases que permiten gestionar matrices densas y dispersas de manera distinta
71
+ email:
72
+ - alu0100699906@ull.edu.es
73
+ - alu0100702293@ull.edu.es
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - .gitignore
79
+ - .travis.yml
80
+ - Gemfile
81
+ - Guardfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - lib/prac09.rb
86
+ - lib/prac09/fraccion.rb
87
+ - lib/prac09/matriz.rb
88
+ - lib/prac09/matrizdensa.rb
89
+ - lib/prac09/matrizdispersa.rb
90
+ - lib/prac09/version.rb
91
+ - pkg/prac09-0.0.1.gem
92
+ - prac09.gemspec
93
+ - spec/fraccion_spec.rb
94
+ - spec/matrices_spec.rb
95
+ - test/tc_matrices.rb
96
+ homepage: https://github.com/alu0100699906/pract09
97
+ licenses:
98
+ - MIT
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ! '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 1.8.11
118
+ signing_key:
119
+ specification_version: 3
120
+ summary: Aplicación para trabajar con matrices densas y dispersas
121
+ test_files:
122
+ - spec/fraccion_spec.rb
123
+ - spec/matrices_spec.rb
124
+ - test/tc_matrices.rb