lpp_t_04_matrix 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ rvm:
2
+ - 1.9.3
3
+ - jruby
4
+ - 2.0.0
5
+
6
+ script: "bundle exec rake"
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in lpp_t_04_matrix.gemspec
4
+ gemspec
5
+
6
+ gem 'guard'
7
+ gem 'guard-rspec'
8
+ gem 'guard-bundler'
9
+ gem 'guard-test'
data/Guardfile ADDED
@@ -0,0 +1,33 @@
1
+ guard 'bundler' do
2
+ watch('Gemfile')
3
+ watch(/^.+\.gemspec/)
4
+ end
5
+
6
+ guard 'rspec', :version => 2 do
7
+ watch(%r{^spec/.+_spec\.rb$})
8
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
9
+ watch('spec/spec_helper.rb') { "spec" }
10
+ end
11
+
12
+ guard :test do
13
+ watch('tc_lpp_t_04_matrix.rb')
14
+ watch(%r{^test/.+_test\.rb$})
15
+ watch('test/test_helper.rb') { 'test' }
16
+
17
+ # Non-rails
18
+ watch(%r{^lib/(.+)\.rb$}) { |m| "test/#{m[1]}_test.rb" }
19
+
20
+ # Rails 4
21
+ # watch(%r{^app/(.+)\.rb}) { |m| "test/#{m[1]}_test.rb" }
22
+ # watch(%r{^app/controllers/application_controller\.rb}) { 'test/controllers' }
23
+ # watch(%r{^app/controllers/(.+)_controller\.rb}) { |m| "test/integration/#{m[1]}_test.rb" }
24
+ # watch(%r{^app/views/(.+)_mailer/.+}) { |m| "test/mailers/#{m[1]}_mailer_test.rb" }
25
+ # watch(%r{^lib/(.+)\.rb}) { |m| "test/lib/#{m[1]}_test.rb" }
26
+
27
+ # Rails < 4
28
+ # watch(%r{^app/models/(.+)\.rb$}) { |m| "test/unit/#{m[1]}_test.rb" }
29
+ # watch(%r{^app/controllers/(.+)\.rb$}) { |m| "test/functional/#{m[1]}_test.rb" }
30
+ # watch(%r{^app/views/(.+)/.+\.erb$}) { |m| "test/functional/#{m[1]}_controller_test.rb" }
31
+ # watch(%r{^app/views/.+$}) { 'test/integration' }
32
+ # watch('app/controllers/application_controller.rb') { ['test/functional', 'test/integration'] }
33
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 EliezerCruzSuarez
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,83 @@
1
+ # LppT04Matrix
2
+
3
+ :~/LPP/prct09/tree
4
+ .
5
+ ├── Gemfile
6
+ ├── Gemfile.lock
7
+ ├── Guardfile
8
+ ├── lib
9
+ │ ├── lpp_t_04_matrix
10
+ │ │ └── version.rb
11
+ │ └── lpp_t_04_matrix.rb
12
+ ├── LICENSE.txt
13
+ ├── lpp_t_04_matrix.gemspec
14
+ ├── Rakefile
15
+ ├── README.md
16
+ └── spec
17
+ ├── lpp_t_04_matrix_spec.rb
18
+ └── spec_helper.rb
19
+
20
+ ## Descripción de los objetivos de la práctica
21
+
22
+ Para realización de esta práctica se ha creado una jerarquía de clases.Teniendo en cuenta que tenemos una clase abstracta de nombre Matriz, y de la cual han heredado las otras dos clases que definen el tipo de objetos que tenemos, MatrizDensa y MatrizDispersa, se han implementado los métodos para operar con ambos tipos de matrices sin importar que objeto sea. Además se ha incluido el tratamiento de fracciones desarrollado en la práctica anterior con la idea de que la matriz sea capaz de operar con elementos del tipo Franction. Los métodos implemnetados son:
23
+ Suma, Resta, Multiplicación y Traspuesta.
24
+ ***
25
+
26
+
27
+ ## Jerarquía de clases para la creación las matrices:
28
+
29
+ class Matriz<pre>
30
+ @filas
31
+ @columnas
32
+ </pre>
33
+ end
34
+
35
+ Class MatrizDensa < Matriz
36
+ <pre> #Se definen los métodos que trabajan con matrices densas
37
+ </pre>
38
+ end
39
+
40
+ Class MatrizDispersa < Matriz
41
+ <pre> #Se definen los métodos que trabajan con matrices dispersas
42
+ </pre>
43
+ end
44
+
45
+ ## Trabajo con matrices
46
+
47
+ Un ejemplo de como construiremos una matriz densa es:
48
+ m3 = MatrizDensa.new(2,2,[[7,10],[15,22]])
49
+ Deberemos llamar a la clase MatrizDensa.
50
+ Los dos primero parametros son el número de filas y el número de columnas.
51
+ A continuación se le pasa un arrary que contiene todos los elementos de la matriz.
52
+
53
+ Un ejemplo de como construiremos una matriz dispersa es:
54
+ m2 = MatrizDispersa.new(2, 2, {0 => {0 => 1, 1 => 2}, 1 => {0 => 3, 1 => 4}})
55
+ Se llama a la clase MatrizDispersa.
56
+ Los dos primero parametros son el número de filas y el número de columnas.
57
+ A continuación se le pasa un hash que contiene los elementos no nulos de la matriz del tipo:
58
+ [FILA] => {[COLUMNA] => [VALOR], [COLUMNA] => [VALOR], ...}, ...
59
+
60
+ ## Installation
61
+
62
+ Add this line to your application's Gemfile:
63
+
64
+ gem 'lpp_t_04_matrix'
65
+
66
+ And then execute:
67
+
68
+ $ bundle
69
+
70
+ Or install it yourself as:
71
+
72
+ $ gem install lpp_t_04_matrix
73
+
74
+ ## Usage
75
+
76
+
77
+ ## Contributing
78
+
79
+ 1. Fork it
80
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
81
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
82
+ 4. Push to the branch (`git push origin my-new-feature`)
83
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+ RSpec::Core::RakeTask.new
4
+ task :default => :spec
@@ -0,0 +1,3 @@
1
+ module LppT04Matrix
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,375 @@
1
+ require "lpp_t_04_matrix/version"
2
+
3
+ module LppT04Matrix
4
+ class Matriz
5
+ attr_accessor :filas, :columnas
6
+ def initialize (filas, columnas)
7
+ @filas = filas
8
+ @columnas = columnas
9
+ end
10
+ end
11
+
12
+ class MatrizDensa < Matriz
13
+ attr_accessor :elemento
14
+ def initialize (filas, columnas, elemento)
15
+ super(filas, columnas)
16
+ @elemento = elemento
17
+ end
18
+
19
+ def [](i)
20
+ @elemento[i]
21
+ end
22
+
23
+ def indice(i,j)
24
+ @elemento[i][j]
25
+ end
26
+
27
+ def to_s
28
+ imprimir = ""
29
+ @filas.times do |i|
30
+ @columnas.times do |j|
31
+ imprimir << "#{elemento[i][j]} "
32
+ end
33
+ imprimir << "\n"
34
+ end
35
+ imprimir
36
+ end
37
+
38
+ def +(other)
39
+ raise ArgumentError, "La longitud de las matrices no coincide." unless @filas == other.filas && @columnas == other.columnas
40
+ elemento = Array.new
41
+ @filas.times do |i|
42
+ elemento_fila = Array.new
43
+ @columnas.times do |j|
44
+ elemento_fila << @elemento[i][j].+(other.elemento[i][j])
45
+ end
46
+ elemento << elemento_fila
47
+ end
48
+ MatrizDensa.new(@filas, @columnas,elemento)
49
+ end
50
+
51
+ def -(other)
52
+ raise ArgumentError, "La longitud de las matrices no coincide." unless @filas == other.filas && @columnas == other.columnas
53
+ elemento = Array.new
54
+ @filas.times do |i|
55
+ elemento_fila = Array.new
56
+ @columnas.times do |j|
57
+ elemento_fila << @elemento[i][j] - other.elemento[i][j]
58
+ end
59
+ elemento << elemento_fila
60
+ end
61
+ MatrizDensa.new(@filas, @columnas,elemento)
62
+ end
63
+
64
+ def *(other)
65
+ raise ArgumentError, "La longitud de las matrices no coincide." unless @columnas == other.filas
66
+ elemento = Array.new
67
+ acumulado = 0
68
+ @filas.times do |i|
69
+ elemento_fila = Array.new
70
+ other.columnas.times do |j|
71
+ acumulado = 0
72
+ @columnas.times do |k|
73
+ suma = @elemento[i][k] * other.indice(k,j)
74
+ acumulado = suma + acumulado
75
+ end
76
+ elemento_fila << acumulado
77
+ end
78
+ elemento << elemento_fila
79
+ end
80
+ MatrizDensa.new(@filas, other.columnas, elemento)
81
+ end
82
+
83
+ def traspuesta
84
+ elemento = Array.new
85
+ @columnas.times do |i|
86
+ elemento_fila = Array.new
87
+ @filas.times do |j|
88
+ elemento_fila << @elemento[j][i]
89
+ end
90
+ elemento << elemento_fila
91
+ end
92
+ MatrizDensa.new(@columnas, @filas, elemento)
93
+ end
94
+ def maximo
95
+ aux = @elemento[0][0]
96
+ @columnas.times do |i|
97
+ @filas.times do |j|
98
+ aux = @elemento[i][j] if @elemento[i][j] > aux
99
+ end
100
+ end
101
+ aux
102
+ end
103
+ def minimo
104
+ aux = @elemento[0][0]
105
+ @columnas.times do |i|
106
+ @filas.times do |j|
107
+ aux = @elemento[i][j] if @elemento[i][j] < aux
108
+ end
109
+ end
110
+ aux
111
+ end
112
+ end
113
+
114
+
115
+ class MatrizDispersa < Matriz
116
+ attr_accessor :elemento
117
+ def initialize (filas, columnas, elemento)
118
+ super(filas, columnas)
119
+ @elemento = elemento
120
+ end
121
+
122
+ def [](i)
123
+ @elemento[i]
124
+ end
125
+ def indice(i,j)
126
+ elemento = @elemento.fetch(i,0)
127
+ if elemento!= 0
128
+ elemento.fetch(j,0)
129
+ else
130
+ 0
131
+ end
132
+ end
133
+
134
+ def to_s
135
+ @elemento
136
+ end
137
+
138
+ def +(other)
139
+ raise ArgumentError, "La longitud de las matrices no coincide." unless @filas == other.filas && @columnas == other.columnas
140
+ case other
141
+ when MatrizDensa
142
+ other.+(self)
143
+ when MatrizDispersa
144
+ elemento = @elemento.merge(other.elemento){|key, oldval, newval| oldval.merge(newval){|key2, oldval2, newval2|oldval2 + newval2}}
145
+ MatrizDispersa.new(@filas, other.columnas, elemento)
146
+ else
147
+ raise TypeError.new("Cannot coerce #{other.inspect} to a Matriz")
148
+ end
149
+ end
150
+
151
+ def -(other)
152
+ raise ArgumentError, "La longitud de las matrices no coincide." unless @filas == other.filas && @columnas == other.columnas
153
+ case other
154
+ when MatrizDensa
155
+ other.-(self)
156
+ when MatrizDispersa
157
+ elemento = @elemento.merge(other.elemento){|key, oldval, newval| oldval.merge(newval){|key2, oldval2, newval2|oldval2 - newval2}}
158
+ MatrizDispersa.new(@filas, other.columnas, elemento)
159
+ else
160
+ raise TypeError.new("Cannot coerce #{other.inspect} to a Matriz")
161
+ end
162
+ end
163
+
164
+ def *(other)
165
+ raise ArgumentError, "La longitud de las matrices no coincide." unless @columnas == other.filas
166
+ case other
167
+ when MatrizDensa
168
+ other.*(self)
169
+ when MatrizDispersa
170
+ other = other.traspuesta
171
+ elemento = Hash.new(Hash.new())
172
+ @elemento.each {
173
+ |key, value|
174
+ other.elemento.each {
175
+ |key1, value1|
176
+ acumulado = 0
177
+ value.each {
178
+ |key2, value2|
179
+ if(value1[key2] != nil)
180
+ acumulado += value2 * value1[key2]
181
+ end
182
+ }
183
+ if(acumulado != 0)
184
+ hash = { key1 => acumulado}
185
+ hash2 = {key => hash}
186
+ elemento.merge!(hash2){|key3, oldval, newval| oldval.merge!(newval)}
187
+ end
188
+ }
189
+ }
190
+ elemento
191
+ MatrizDispersa.new(@filas, other.columnas, elemento)
192
+ else
193
+ raise TypeError.new("Cannot coerce #{other.inspect} to a Matriz")
194
+ end
195
+ end
196
+
197
+ def traspuesta
198
+ elemento = Hash.new(Hash.new())
199
+ @elemento.each {
200
+ |key, value| value.each {
201
+ |key2, value2| hash = { key => value2}
202
+ hash2 = {key2 => hash}
203
+ elemento.merge!(hash2){|key3, oldval, newval| oldval.merge!(newval)}
204
+ }
205
+ }
206
+ elemento
207
+ MatrizDispersa.new(@filas, @columnas, elemento)
208
+ end
209
+ def maximo
210
+ aux = @elemento.keys
211
+ aux1 = aux[0]
212
+ aux2 = @elemento[aux1].values
213
+ mayor = aux2[0]
214
+ @elemento.each {
215
+ |key, value| value.each {
216
+ |key2, value2|
217
+ mayor = indice(key,key2) if indice(key,key2) > mayor
218
+ }
219
+ }
220
+ mayor
221
+ end
222
+ def minimo
223
+ aux = @elemento.keys
224
+ aux1 = aux[0]
225
+ aux2 = @elemento[aux1].values
226
+ mayor = aux2[0]
227
+ @elemento.each {
228
+ |key, value| value.each {
229
+ |key2, value2|
230
+ mayor = indice(key,key2) if indice(key,key2) < mayor
231
+ }
232
+ }
233
+ mayor
234
+ end
235
+ end
236
+ end
237
+
238
+ class Frac
239
+ attr_reader :numerador, :denominador # Se definen set y get para acceder a las variables de la clase.
240
+ include Comparable # Se incluye el modulo de nombre "comparable" utilizado para el operador <=>.
241
+
242
+ def initialize(numerador, denominador) # Se define el constructor.
243
+ mcd = gcd(numerador,denominador)
244
+ @numerador , @denominador = numerador/mcd, denominador/mcd
245
+ end
246
+
247
+ def num() # Devuelve el numerador.
248
+ @numerador
249
+ end
250
+
251
+ def denom() # Devuelve el denominador.
252
+ @denominador
253
+ end
254
+
255
+ def to_s
256
+ if @denominador == 1 #Si el denominador es 1, solo se imprimer el numerador
257
+ "#{@numerador}"
258
+ elsif @denominador == -1 #En el caso de que b=-1 y a>0 ó a=-1
259
+ if @numerador>0 || @numerador==-1
260
+ "#{-@numerador}"
261
+ else #Si b=-1 y a es menor que cero, imprime el valor de a
262
+ "#{@numerador}"
263
+ end
264
+ elsif (@numerador>0 && @denominador>0 && @denominador!=1) #Caso nomal, a y b son mayores que cero, imprime a/b
265
+ "#{@numerador}/#{@denominador}"
266
+ elsif (@numerador>0 && @denominador<0 && @denominador!=-1) # b es menor que cero, se le cambia el signo a todo para que se imprima: -a/b
267
+ "#{-@numerador}/#{-@denominador}"
268
+ elsif (@numerador<0 && @denominador<0 && @denominador!=-1) # a y b son menor que cero, se le cambia el signo a todo para que imprima a/b
269
+ "#{-@numerador}/#{-@denominador}"
270
+ elsif (@numerador<0 && @denominador>0 && @denominador!=1) # a es menor que cero, se imprime igual: -a/b
271
+ "#{@numerador}/#{@denominador}"
272
+ end
273
+ end
274
+
275
+ def to_f() # Imprime el resultado de la fraccion como un float.
276
+ @numerador.to_f/@denominador
277
+ end
278
+
279
+ def <=>(other) # Se define el operador <=> para el modulo comparable.
280
+ @numerador.to_f/@denominador <=> other.numerador.to_f/other.denominador
281
+ end
282
+
283
+ def abs() # Se calcula el valor absoluto de una fraccion.
284
+ if (@numerador > 0 && @denominador > 0)
285
+ Frac.new(@numerador, @denominador).to_s
286
+ elsif (@numerador > 0 && @denominador < 0)
287
+ Frac.new(@numerador, -1 * @denominador).to_s
288
+ elsif (@numerador < 0 && @denominador > 0)
289
+ Frac.new(-1 * @numerador, @denominador).to_s
290
+ else (@numerador < 0 && @denominador < 0)
291
+ Frac.new(-1 * @numerador,-1 * @denominador).to_s
292
+ end
293
+ end
294
+
295
+ def reciprocal() # Calcula el reciproco de una fraccion.
296
+ Frac.new(@denominador, @numerador)
297
+ end
298
+
299
+ def +(other) # Calcula la suma de dos fracciones.
300
+ if other.class == Frac
301
+ Frac.new(@numerador*other.denominador + other.numerador*@denominador , @denominador*other.denominador)
302
+ elsif other.class == Fixnum
303
+ Frac.new(@numerador + other*@denominador , @denominador)
304
+ end
305
+
306
+ end
307
+
308
+ def -(other) # Calcula la resta de dos fracciones.
309
+ if (other.class == Frac)
310
+ Frac.new(@numerador*other.denominador - other.numerador*@denominador , @denominador*other.denominador)
311
+ else
312
+ Frac.new(@numerador - other*@denominador , @denominador)
313
+ end
314
+ end
315
+
316
+ def *(other) # Calcula la multiplicacion de dos fracciones.
317
+ if other.class == Frac
318
+ Frac.new(@numerador * other.numerador, @denominador * other.denominador)
319
+ elsif other.class == Fixnum
320
+ Frac.new(@numerador * other, @denominador)
321
+ end
322
+ end
323
+
324
+ def /(other) # Calcula la division de dos fracciones.
325
+ Frac.new(@numerador * other.denominador, @denominador * other.numerador)
326
+ end
327
+
328
+ def %(other) # Calcula el resto al dividir dos fracciones (modulo)
329
+ result = self./(other)
330
+ result = (result.numerador%result.denominador).to_i
331
+ end
332
+
333
+ def coerce(other)
334
+ [self,other]
335
+ end
336
+ end
337
+
338
+ def gcd(u, v)
339
+ u, v = u.abs, v.abs
340
+ while v != 0
341
+ u, v = v, u % v
342
+ end
343
+ u
344
+ end
345
+
346
+ if __FILE__ == $0
347
+ # Trabajo con la clase:
348
+ include LppT04Matrix
349
+
350
+ m1 = MatrizDensa.new(2,2,[[1,2],[3,4]])
351
+ m2 = MatrizDispersa.new(2, 2, {0 => {0 => 1, 1 => 2}, 1 => {0 => 3, 1 => 4}})
352
+ m3 = MatrizDensa.new(2,2,[[7,10],[15,22]])
353
+ m4 = MatrizDispersa.new(2, 2, {0 => {0 => Frac.new(1,2), 1 => Frac.new(1,2)}, 1 => {0 => Frac.new(1,2), 1 => Frac.new(1,2)}})
354
+ a = Frac.new(1,2)
355
+ b = Frac.new(2,2)
356
+ c = Frac.new(3,2)
357
+ d = Frac.new(4,2)
358
+ e = Frac.new(3,2)
359
+ f = Frac.new(9,2)
360
+ g = Frac.new(3,1)
361
+ h = Frac.new(6,1)
362
+ m9 = MatrizDensa.new(2,2,[[a,b],[c,d]])
363
+ m10 = MatrizDensa.new(2,2,[[e,3],[f,6]])
364
+ puts m9-(m1)
365
+ puts
366
+ puts m1-(m9)
367
+ puts
368
+ puts m4.to_s
369
+ puts m4*m1
370
+ puts m1*m4
371
+ puts m1*m1
372
+ puts (m2*m4).to_s
373
+ puts (m4*m2).to_s
374
+ puts m1
375
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'lpp_t_04_matrix/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "lpp_t_04_matrix"
8
+ spec.version = LppT04Matrix::VERSION
9
+ spec.authors = ["EliezerCruzSuarez"]
10
+ spec.email = ["alu0100611298@ull.edu.es"]
11
+ spec.description = %q{Para realizacion de esta practica se ha creado una jerarquia de clases.
12
+ Teniendo en cuenta que tenemos una clase abstracta de nombre Matriz, y de la cual han heredado las
13
+ otras dos clases que definen el tipo de objetos que tenemos, MatrizDensa y MatrizDispersa, se han
14
+ implementado los metodos para operar con ambos tipos de matrices, sin importar que objeto sea.
15
+ Además se ha incluido el tratamiento de fracciones desarrollado en la practica anterior con la idea
16
+ de que la matriz sea capaz de operar con elementos del tipo Franction. Los metodos implemnetados son:
17
+ suma, resta, multiplicacion y traspuesta.}
18
+ spec.summary = %q{Operacion con matrices densas y dispersas}
19
+ spec.homepage = ""
20
+ spec.license = "MIT"
21
+
22
+ spec.files = `git ls-files`.split($/)
23
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
24
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.3"
28
+ spec.add_development_dependency "rake"
29
+ spec.add_development_dependency 'rspec', '~> 2.9'
30
+ end
@@ -0,0 +1,256 @@
1
+ require 'spec_helper'
2
+ include LppT04Matrix
3
+
4
+ describe LppT04Matrix do
5
+
6
+ describe Matriz do
7
+ before :all do
8
+ @m1 = Matriz.new(2,2)
9
+ end
10
+ describe "Se comprueba el numero de:" do
11
+ it "filas de la matriz" do
12
+ @m1.filas.should eq(2)
13
+ end
14
+ it "columnas de la matriz" do
15
+ @m1.columnas.should eq(2)
16
+ end
17
+ end
18
+ end
19
+
20
+ describe MatrizDensa do
21
+ before :all do
22
+ @m1 = MatrizDensa.new(2,2,[[1,2],[3,4]])
23
+ @m2 = MatrizDensa.new(2,2,[[2,4],[6,8]])
24
+ @m3 = MatrizDensa.new(2,2,[[7,10],[15,22]])
25
+ @m4 = MatrizDensa.new(2,2,[[1,3],[2,4]])
26
+ a = Frac.new(1,2)
27
+ b = Frac.new(2,2)
28
+ c = Frac.new(3,2)
29
+ d = Frac.new(4,2)
30
+ @m9 = MatrizDensa.new(2,2,[[a,b],[c,d]])
31
+ @m10 = MatrizDensa.new(2,2,[[0,0],[0,0]])
32
+ @m11 = MatrizDensa.new(2,2,[[a,c],[b,d]])
33
+ e = Frac.new(7,4)
34
+ f = Frac.new(10,4)
35
+ g = Frac.new(15,4)
36
+ h = Frac.new(22,4)
37
+ @m12 = MatrizDensa.new(2,2,[[e,f],[g,h]])
38
+ i = Frac.new(3,2)
39
+ j = Frac.new(9,2)
40
+ k = Frac.new(-1,2)
41
+ l = Frac.new(-3,2)
42
+ @m13 = MatrizDensa.new(2,2,[[i,3],[j,6]])
43
+ @m14 = MatrizDensa.new(2,2,[[k,-1],[l,-2]])
44
+
45
+ @m15 = MatrizDensa.new(2,2,[[Frac.new(7,2),5],[Frac.new(15,2),11]])
46
+
47
+ end
48
+ describe "Acceder al subindice;" do
49
+ it "Para acceder al [0,0]" do
50
+ @m1.elemento[0][0].should eq(1)
51
+ end
52
+ it "Para acceder al [0,0]" do
53
+ @m1.elemento[0][1].should eq(2)
54
+ end
55
+ it "Para acceder al [0,0]" do
56
+ @m1.elemento[1][0].should eq(3)
57
+ end
58
+ it "Para acceder al [0,0]" do
59
+ @m1.elemento[1][1].should eq(4)
60
+ end
61
+ end
62
+
63
+ describe "Imprimir la matriz" do
64
+ it "del orden 2x2" do
65
+ @m1.to_s.should eq("1 2 \n3 4 \n")
66
+ end
67
+ end
68
+ describe "Suma de dos matrices" do
69
+ it "del orden mxn" do
70
+ @m1.+(@m1).to_s.should eq(@m2.to_s)
71
+ end
72
+ end
73
+ describe "Suma de matrices de fracciones y enteros" do
74
+ it "del orden mxn" do
75
+ @m9.+(@m1).to_s.should eq(@m13.to_s)
76
+ end
77
+ end
78
+ describe "Suma de matrices de enteros y fracciones" do
79
+ it "del orden mxn" do
80
+ @m1.+(@m9).to_s.should eq(@m13.to_s)
81
+ end
82
+ end
83
+ describe "Resta de dos matrices" do
84
+ it "del orden mxn" do
85
+ @m2.-(@m1).to_s.should eq(@m1.to_s)
86
+ end
87
+ end
88
+ describe "Resta de matrices de fracciones y enteros" do
89
+ it "del orden mxn" do
90
+ @m9.-(@m1).to_s.should eq(@m14.to_s)
91
+ end
92
+ end
93
+ describe "Resta de matrices de enteros y fracciones" do
94
+ it "del orden mxn" do
95
+ @m1.-(@m9).to_s.should eq(@m14.to_s)
96
+ end
97
+ end
98
+ describe "Matriz traspuesta " do
99
+ it "del orden nxn." do
100
+ @m1.traspuesta.to_s.should eq(@m4.to_s)
101
+ end
102
+ end
103
+
104
+ describe "Multiplicacion de matrices del orden axn x nxb" do
105
+ it "del orden 2x2 x 2x2" do
106
+ @m1.*(@m1).to_s.should eq(@m3.to_s)
107
+ end
108
+ end
109
+ describe "Multiplicacion de matrices de fracciones y enteros" do
110
+ it "del orden mxn" do
111
+ @m9.*(@m1).to_s.should eq(@m15.to_s)
112
+ end
113
+ end
114
+ describe "Multiplicacion de matrices de enteros y fracciones" do
115
+ it "del orden mxn" do
116
+ @m1.*(@m9).to_s.should eq(@m15.to_s)
117
+ end
118
+ end
119
+ describe "Trabajo con fracciones" do
120
+ it "suma" do
121
+ @m9.+(@m9).to_s.should eq (@m1.to_s)
122
+ end
123
+ it "resta" do
124
+ @m9.-(@m9).to_s.should eq (@m10.to_s)
125
+ end
126
+ it "Multiplicacion" do
127
+ @m9.*(@m9).to_s.should eq (@m12.to_s)
128
+ end
129
+ it "traspuesta" do
130
+ @m9.traspuesta.to_s.should eq (@m11.to_s)
131
+ end
132
+ end
133
+ describe "Calculo de maximo y minimo" do
134
+ it "Maximo" do
135
+ @m1.maximo.should eq (4)
136
+ end
137
+ it "Minimo" do
138
+ @m1.minimo.should eq (1)
139
+ end
140
+ end
141
+ end
142
+
143
+ describe MatrizDispersa do
144
+ before :all do
145
+ @m1 = MatrizDispersa.new(250, 250, {100 => {10 => 1, 50 => 200}, 200 => {10 => 1, 50 => 200}})
146
+ @m2 = MatrizDispersa.new(250, 250, {100 => {10 => 2, 50 => 400}, 200 => {10 => 2, 50 => 400}})
147
+ @m3 = MatrizDispersa.new(2, 2, {1 => {1 => 1, 2 => 2}, 2 => {1 => 3, 2 => 4}})
148
+ @m4 = MatrizDispersa.new(2, 2, {1 => {1 => 7, 2 => 10}, 2 => {1 => 15, 2 => 22}})
149
+ @m5 = MatrizDispersa.new(2, 2, {1 => {1 => 1, 2 => 3}, 2 => {1 => 2, 2 => 4}})
150
+ @m6 = MatrizDispersa.new(3, 2, {2 => {1 => 1}, 3 => { 2 => 4}})
151
+ @m7 = MatrizDispersa.new(2, 3, {1 => {2 => 6}, 2 => {1 => 7}})
152
+ @m8 = MatrizDispersa.new(3, 3, {2=>{2=>6}, 3=>{1=>28}})
153
+
154
+ end
155
+ describe "Acceder al subindice;" do
156
+ it "Para acceder al [0,0]" do
157
+ @m1.elemento[100][10].should eq(1)
158
+ end
159
+ it "Para acceder al [0,0]" do
160
+ @m1.elemento[100][50].should eq(200)
161
+ end
162
+ it "Para acceder al [0,0]" do
163
+ @m1.elemento[200][10].should eq(1)
164
+ end
165
+ it "Para acceder al [0,0]" do
166
+ @m1.elemento[200][50].should eq(200)
167
+ end
168
+ end
169
+
170
+ describe "Imprimir la matriz" do
171
+ it "del orden 250x250" do
172
+ @m1.to_s.should eq({100=>{10=>1, 50=>200}, 200=>{10=>1, 50=>200}})
173
+ end
174
+ end
175
+
176
+ describe "Suma de dos matrices" do
177
+ it "del orden mxn" do
178
+ @m1.+(@m1).to_s.should eq(@m2.to_s)
179
+ end
180
+ end
181
+
182
+ describe "Resta de dos matrices" do
183
+ it "del orden mxn" do
184
+ @m2.-(@m1).to_s.should eq(@m1.to_s)
185
+ end
186
+ end
187
+
188
+ describe "Multiplicacion de matrices del orden axn x nxb" do
189
+ it "del orden 2x2 x 2x2" do
190
+ @m3.*(@m3).to_s.should eq(@m4.to_s)
191
+ end
192
+ it "del orden 3x2 x 2x3" do
193
+ @m6.*(@m7).to_s.should eq(@m8.to_s)
194
+ end
195
+ end
196
+
197
+ describe "Matriz traspuesta " do
198
+ it "del orden nxn." do
199
+ @m3.traspuesta.to_s.should eq(@m5.to_s)
200
+ end
201
+ end
202
+ describe "Calculo de maximo y minimo" do
203
+ it "Maximo" do
204
+ @m1.maximo.should eq (200)
205
+ end
206
+ it "Minimo" do
207
+ @m1.minimo.should eq (1)
208
+ end
209
+ end
210
+ end
211
+
212
+ before :all do
213
+ @m1 = MatrizDensa.new(2,2,[[1,2],[3,4]])
214
+ @m2 = MatrizDispersa.new(2, 2, {0 => {0 => 1, 1 => 2}, 1 => {0 => 3, 1 => 4}})
215
+ @m3 = MatrizDensa.new(2,2,[[7,10],[15,22]])
216
+ @m4 = MatrizDensa.new(2,2,[[2,4],[6,8]])
217
+ @m5 = MatrizDensa.new(2,2,[[0,0],[0,0]])
218
+ @m6 = MatrizDispersa.new(2, 2, {0 => {0 => Frac.new(1,2), 1 => Frac.new(1,2)}, 1 => {0 => Frac.new(1,2), 1 => Frac.new(1,2)}})
219
+ @m7 = MatrizDensa.new(2,2,[[Frac.new(3,2),Frac.new(3,2)],[Frac.new(7,2),Frac.new(7,2)]])
220
+ end
221
+
222
+ describe "Trabajo con matrices de diferente tipo" do
223
+ describe "Matriz Densa -> Matriz Dispersa" do
224
+ it "Suma de dos matrices del orden mxn" do
225
+ @m1.+(@m2).to_s.should eq(@m4.to_s)
226
+ end
227
+ it "Resta de dos matrices del orden mxn" do
228
+ @m1.-(@m2).to_s.should eq(@m5.to_s)
229
+ end
230
+ it "Multiplicacion del orden 2x2 x 2x2" do
231
+ @m1.*(@m2).to_s.should eq(@m3.to_s)
232
+ end
233
+ it "Multiplicacion del orden 2x2 x 2x2 con fracciones y enteros" do
234
+ @m1.*(@m6).to_s.should eq(@m7.to_s)
235
+ end
236
+ end
237
+ end
238
+
239
+ describe "Trabajo con matrices de diferente tipo" do
240
+ describe "Matriz Dispersa -> Matriz Densa" do
241
+ it "Suma de dos matrices del orden mxn" do
242
+ @m2.+(@m1).to_s.should eq(@m4.to_s)
243
+ end
244
+ it "Resta de dos matrices del orden mxn" do
245
+ @m2.-(@m1).to_s.should eq(@m5.to_s)
246
+ end
247
+ it "Multiplicacion del orden 2x2 x 2x2" do
248
+ @m2.*(@m1).to_s.should eq(@m3.to_s)
249
+ end
250
+ it "Multiplicacion del orden 2x2 x 2x2 con fracciones y enteros" do
251
+ @m6.*(@m1).to_s.should eq(@m7.to_s)
252
+ end
253
+ end
254
+ end
255
+ end
256
+
@@ -0,0 +1,8 @@
1
+ $:.unshift File.dirname(__FILE__) + '/../lib'
2
+ require 'lpp_t_04_matrix'
3
+
4
+ RSpec.configure do |config|
5
+ config.treat_symbols_as_metadata_keys_with_true_values = true
6
+ config.run_all_when_everything_filtered = true
7
+ config.filter_run :focus
8
+ end
@@ -0,0 +1,57 @@
1
+ $:.unshift File.dirname(__FILE__) + '/../lib'
2
+ require 'lpp_t_04_matrix'
3
+ require "test/unit"
4
+ include LppT04Matrix
5
+
6
+ class Test_Matriz < Test::Unit::TestCase
7
+
8
+ def setup
9
+ @m1 = MatrizDensa.new(2,2,[[1,2],[3,4]])
10
+ @m2 = MatrizDispersa.new(250, 250, {100 => {10 => 1, 50 => 200}, 200 => {10 => 1, 50 => 200}})
11
+ @m3 = MatrizDensa.new(2,2,[[2,4],[6,8]])
12
+ @m4 = MatrizDispersa.new(250, 250, {100 => {10 => 2, 50 => 400}, 200 => {10 => 2, 50 => 400}})
13
+ @m5 = MatrizDispersa.new(2, 2, {1 => {1 => 1, 2 => 2}, 2 => {1 => 3, 2 => 4}})
14
+ @m6 = MatrizDispersa.new(2, 2, {1 => {1 => 7, 2 => 10}, 2 => {1 => 15, 2 => 22}})
15
+ @m7 = MatrizDensa.new(2,2,[[7,10],[15,22]])
16
+ @m8 = MatrizDispersa.new(2, 2, {0 => {0 => 1, 1 => 2}, 1 => {0 => 3, 1 => 4}})
17
+ @m9 = MatrizDensa.new(2,2,[[7,10],[15,22]])
18
+ @m10 = MatrizDispersa.new(2, 2, {0 => {0 => Frac.new(1,2), 1 => Frac.new(1,2)}, 1 => {0 => Frac.new(1,2), 1 => Frac.new(1,2)}})
19
+ @m11 = MatrizDensa.new(2,2,[[Frac.new(3,2),Frac.new(3,2)],[Frac.new(7,2),Frac.new(7,2)]])
20
+
21
+ end
22
+ def test_MatrizDensa
23
+ assert_equal("1", @m1.elemento[0][0].to_s)
24
+ assert_equal("2", @m1.elemento[0][1].to_s)
25
+ assert_equal("3", @m1.elemento[1][0].to_s)
26
+ assert_equal("4", @m1.elemento[1][1].to_s)
27
+ assert_equal(@m3.to_s, @m1.+(@m1).to_s)
28
+ assert_equal(@m1.to_s, @m3.-(@m1).to_s)
29
+ assert_equal(@m7.to_s, @m1.*(@m1).to_s)
30
+ end
31
+
32
+ def test_MatrizDispersa
33
+ assert_equal("1", @m2.elemento[100][10].to_s)
34
+ assert_equal("200", @m2.elemento[100][50].to_s)
35
+ assert_equal("1", @m2.elemento[200][10].to_s)
36
+ assert_equal("200", @m2.elemento[200][50].to_s)
37
+ assert_equal(@m4.to_s, @m2.+(@m2).to_s)
38
+ assert_equal(@m2.to_s, @m4.-(@m2).to_s)
39
+ assert_equal(@m6.to_s, @m5.*(@m5).to_s)
40
+ end
41
+
42
+ def test_DensaDispersa
43
+ assert_equal(@m3.to_s, @m1.+(@m8).to_s)
44
+ assert_equal(@m1.to_s, @m3.-(@m8).to_s)
45
+ assert_equal(@m9.to_s, @m1.*(@m8).to_s)
46
+ end
47
+
48
+ def test_DispersaDensa
49
+ assert_equal(@m3.to_s, @m8.+(@m1).to_s)
50
+ assert_equal(@m1.to_s, @m8.-(@m3).to_s)
51
+ assert_equal(@m9.to_s, @m8.*(@m1).to_s)
52
+ end
53
+
54
+ def test_Fracciones
55
+ assert_equal(@m11.to_s, @m1.*(@m10).to_s)
56
+ end
57
+ end
@@ -0,0 +1 @@
1
+ require "tc_lpp_t_04_matrix.rb"
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lpp_t_04_matrix
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - EliezerCruzSuarez
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: '2.9'
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: '2.9'
62
+ description: ! "Para realizacion de esta practica se ha creado una jerarquia de clases.
63
+ \n Teniendo en cuenta que tenemos una clase abstracta de nombre Matriz, y de
64
+ la cual han heredado las\n otras dos clases que definen el tipo de objetos que
65
+ tenemos, MatrizDensa y MatrizDispersa, se han\n implementado los metodos para
66
+ operar con ambos tipos de matrices, sin importar que objeto sea.\n Además se
67
+ ha incluido el tratamiento de fracciones desarrollado en la practica anterior con
68
+ la idea\n de que la matriz sea capaz de operar con elementos del tipo Franction.
69
+ Los metodos implemnetados son: \n suma, resta, multiplicacion y traspuesta."
70
+ email:
71
+ - alu0100611298@ull.edu.es
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .rspec
78
+ - .travis.yml
79
+ - Gemfile
80
+ - Guardfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - lib/lpp_t_04_matrix.rb
85
+ - lib/lpp_t_04_matrix/version.rb
86
+ - lpp_t_04_matrix.gemspec
87
+ - spec/lpp_t_04_matrix_spec.rb
88
+ - spec/spec_helper.rb
89
+ - test/tc_lpp_t_04_matrix.rb
90
+ - test/tc_lpp_t_04_matrix_test.rb
91
+ homepage: ''
92
+ licenses:
93
+ - MIT
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 1.8.23
113
+ signing_key:
114
+ specification_version: 3
115
+ summary: Operacion con matrices densas y dispersas
116
+ test_files:
117
+ - spec/lpp_t_04_matrix_spec.rb
118
+ - spec/spec_helper.rb
119
+ - test/tc_lpp_t_04_matrix.rb
120
+ - test/tc_lpp_t_04_matrix_test.rb