ETSII_GEM 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,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3a6db5130ab93431d09f642e57cd5bd5dac5f45b
4
+ data.tar.gz: e9ebefe9e3ca066beb462d9f36b65b67a7ca95b8
5
+ SHA512:
6
+ metadata.gz: 1494c21ceca1b1c6b324c880955ee715ab00acdc8949315d56d2aaea8e1ab4803b05127ac6f40310ab9e282c98bb405d2ebf05c07cc8ce6b9138217faf820894
7
+ data.tar.gz: 677c423ac770dca03ef2d176485c3a54b686e985b50e20067c02d4b98dd7b7c5f274740d3e978b0cd380520d1863587dd7e93e06f1c2fa1280168b8798831e84
@@ -0,0 +1,31 @@
1
+ *~
2
+ *.gem
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ <<<<<<< HEAD
7
+ .yardoc
8
+ Gemfile.lock
9
+ InstalledFiles
10
+ _yardoc
11
+ coverage
12
+ doc/
13
+ =======
14
+ coverage
15
+ InstalledFiles
16
+ >>>>>>> 40ea3c0ca5c25881b4fda80527eb85ce14184147
17
+ lib/bundler/man
18
+ pkg
19
+ rdoc
20
+ spec/reports
21
+ test/tmp
22
+ test/version_tmp
23
+ tmp
24
+ <<<<<<< HEAD
25
+ =======
26
+
27
+ # YARD artifacts
28
+ .yardoc
29
+ _yardoc
30
+ doc/
31
+ >>>>>>> 40ea3c0ca5c25881b4fda80527eb85ce14184147
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 1.9.2
5
+ - jruby
6
+ - rbx
7
+ # uncomment this line if your project needs to run something other than `rake`:
8
+ # script: bundle exec rspec spec
9
+ script: "bundle exec rake"
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ETSII_GEM/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ETSII_GEM"
8
+ spec.version = ETSIIGEM::VERSION
9
+ spec.authors = ["Héctor J. & Enrique Tejera"]
10
+ spec.email = ["alu0100603170@ull.edu.es"]
11
+ spec.description = %q{"Gema para la representación de matrices, tanto densas como dispersas."}
12
+ spec.summary = %q{"Existe una clase madre llamada 'Matriz' de la que heredan dos subclases, 'Densa' para las matrices densas y 'Dispersa' para las matrices dispersas."}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Especifica your gem's dependencies in ETSII_GEM.gemspec
4
+ gemspec
5
+
6
+ gem 'guard'
7
+ gem 'guard-rspec'
8
+ gem 'guard-bundler'
9
+ gem 'rake'
10
+ gem 'rspec'
11
+
@@ -0,0 +1,32 @@
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 :rspec do
13
+ watch(%r{^spec/.+_spec\.rb$})
14
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
15
+ watch('spec/spec_helper.rb') { "spec" }
16
+
17
+ # Rails example
18
+ watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
19
+ watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
20
+ 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"] }
21
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
22
+ watch('config/routes.rb') { "spec/routing" }
23
+ watch('app/controllers/application_controller.rb') { "spec/controllers" }
24
+
25
+ # Capybara features specs
26
+ watch(%r{^app/views/(.+)/.*\.(erb|haml|slim)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
27
+
28
+ # Turnip features and steps
29
+ watch(%r{^spec/acceptance/(.+)\.feature$})
30
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
31
+ end
32
+
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Héctor J.
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,41 @@
1
+ # ETSIIGEM
2
+
3
+ Gema para la representación de matrices, tanto densas como dispersas.
4
+
5
+ #ESTRUCTURA
6
+
7
+ Existe una clase madre llamada "Matriz" de la que heredan dos subclases, "Densa" para las matrices densas y "Dispersa" para las matrices dispersas.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'ETSII_GEM'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install ETSII_GEM
22
+
23
+ ## Usage
24
+
25
+ En proceso...
26
+
27
+ ## Contributing
28
+
29
+ 1. Fork it
30
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
31
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
32
+ 4. Push to the branch (`git push origin my-new-feature`)
33
+ 5. Create new Pull Request
34
+
35
+ =======
36
+ ## prct10 by:
37
+ ======
38
+
39
+ Héctor José Ravelo García
40
+
41
+ Enrique Tejera González
@@ -0,0 +1,23 @@
1
+ $:.unshift File.dirname(__FILE__) + 'lib'
2
+
3
+ #Required files
4
+ require "bundler/gem_tasks"
5
+ require 'rspec/core/rake_task'
6
+ RSpec::Core::RakeTask.new
7
+
8
+ task :default => :doc
9
+
10
+ desc "Pruebas unitarias"
11
+ task :test do
12
+ sh "ruby -I. test/tc_ETSII_GEM.rb"
13
+ end
14
+
15
+ desc "Ejecutar las espectativas de la gema"
16
+ task :spec do
17
+ sh "rspec -I. spec/ETSII_GEM_spec.rb"
18
+ end
19
+
20
+ desc "Ejecutar con documentacion"
21
+ task :doc do
22
+ sh "rspec -I. spec/ETSII_GEM_spec.rb --format documentation"
23
+ end
@@ -0,0 +1,237 @@
1
+ require "ETSII_GEM/version"
2
+
3
+ module EtsiiGem
4
+ class Matriz #Superclase matriz------------------------------------------------------------------------------------------
5
+ attr_accessor :rows, :cols, :data
6
+
7
+ undef rows=, cols=
8
+
9
+ def initialize(rows, cols)
10
+ @rows, @cols = rows, cols
11
+ end
12
+ def +(other)
13
+ raise ArgumentError, "El tamaño de las matrices debe ser igual" unless @rows == other.rows && @cols == other.cols
14
+ c = Densa.new(@rows, @cols)
15
+ @rows.times do |i|
16
+ @cols.times do |j|
17
+ if self[i][j] == nil && other[i][j] == nil
18
+ c[i][j] = 0
19
+ elsif self[i][j] == nil && other[i][j] != nil
20
+ c[i][j] = other[i][j]
21
+ elsif self[i][j] != nil && other[i][j] == nil
22
+ c[i][j] = self[i][j]
23
+ else
24
+ c[i][j] = self[i][j] + other[i][j]
25
+ end
26
+ end
27
+ end
28
+ c
29
+ end
30
+
31
+ def -(other)
32
+ raise ArgumentError, "El tamaño de las matrices debe ser igual" unless @rows == other.rows && @cols == other.cols
33
+ c = Densa.new(@rows, @cols)
34
+ @rows.times do |i|
35
+ @cols.times do |j|
36
+ if self[i][j] == nil && other[i][j] == nil
37
+ c[i][j] = 0
38
+ elsif self[i][j] == nil && other[i][j] != nil
39
+ c[i][j] = -other[i][j]
40
+ elsif self[i][j] != nil && other[i][j] == nil
41
+ c[i][j] = self[i][j]
42
+ else
43
+ c[i][j] = self[i][j] - other[i][j]
44
+ end
45
+ end
46
+ end
47
+ c
48
+ end
49
+
50
+ def *(other)
51
+ raise ArgumentError, "Columns and Rows must be equal" unless (@cols == other.rows)
52
+ c = Densa.new(@rows,other.cols)
53
+ @rows.times do |i|
54
+ other.cols.times do |j|
55
+ ac = 0
56
+ @cols.times do |k|
57
+ ac += self[i][k] * other[k][j] if (self[i][k] != nil && other[k][j] != nil)
58
+ end
59
+ c[i][j] = ac
60
+ end
61
+ end
62
+ c
63
+ end
64
+
65
+ def max
66
+ value = 0
67
+ @rows.times do |i|
68
+ @cols.times do |j|
69
+ if self[i][j] != nil
70
+ value = self[i][j] if self[i][j] > value
71
+ end
72
+ end
73
+ end
74
+ value
75
+ end
76
+
77
+ def min
78
+ value = self.max
79
+ @rows.times do |i|
80
+ @cols.times do |j|
81
+ if self[i][j] != nil
82
+ value = self[i][j] if self[i][j] < value
83
+ end
84
+ end
85
+ end
86
+ value
87
+ end
88
+
89
+ end
90
+
91
+ class Fraccion
92
+ include Comparable
93
+
94
+ attr_accessor :num, :denom
95
+
96
+ def initialize(a, b)
97
+ x = mcd(a,b)
98
+ @num = a/x
99
+ @denom = b/x
100
+
101
+ if (@num < 0 && @denom < 0)
102
+ @num = @num * -1
103
+ @denom = @denom * -1
104
+ end
105
+
106
+ if (@denom < 0)
107
+ @denom = @denom * -1
108
+ @num = @num * -1
109
+ end
110
+ end
111
+
112
+ def mcd(u, v)
113
+ u, v = u.abs, v.abs
114
+ while v != 0
115
+ u, v = v, u % v
116
+ end
117
+ u
118
+ end
119
+
120
+ def to_s
121
+ "#{@num}/#{@denom}"
122
+ end
123
+
124
+ def to_f
125
+ @num.to_f/@denom.to_f
126
+ end
127
+
128
+ def +(other)
129
+ if other.instance_of? Fixnum
130
+ c = Fraccion.new(other,1)
131
+ Fraccion.new(@num * c.denom + @denom * c.num, @denom * c.denom)
132
+ else
133
+ Fraccion.new(@num * other.denom + @denom * other.num, @denom * other.denom)
134
+ end
135
+ end
136
+
137
+ def -(other)
138
+ if other.instance_of? Fixnum
139
+ c = Fraccion.new(other,1)
140
+ Fraccion.new(@num * c.denom - @denom * c.num, @denom * c.denom)
141
+ else
142
+ Fraccion.new(@num * other.denom - @denom * other.num, @denom * other.denom)
143
+ end
144
+ end
145
+
146
+ def *(other)
147
+ if other.instance_of? Fixnum
148
+ c = Fraccion.new(other,1)
149
+ Fraccion.new(@num * c.num, @denom * c.denom)
150
+ else
151
+ Fraccion.new(@num * other.num, @denom * other.denom)
152
+ end
153
+ end
154
+
155
+ def <=>(other)
156
+ return nil unless (other.instance_of? Fraccion) || (other.instance_of? Fixnum)
157
+ if other.instance_of? Fixnum
158
+ c = Fraccion.new(other,1)
159
+ (c.num.to_f / c.denom.to_f) <=> (self.num.to_f/self.denom.to_f)
160
+ else
161
+ (self.num.to_f/self.denom.to_f) <=> (other.num.to_f/other.denom.to_f)
162
+ end
163
+ end
164
+
165
+ def coerce(other)
166
+ [self,other]
167
+ end
168
+ end
169
+
170
+ class Densa < Matriz #Matriz densa---------------------------------------------------------------------------------------
171
+ attr_reader :data
172
+
173
+ def initialize(rows,cols)
174
+ @data = Array.new(rows) {Array.new(cols)}
175
+ super
176
+ end
177
+
178
+ def [](i)
179
+ @data[i]
180
+ end
181
+
182
+ def []=(i,value)
183
+ @data[i] = value
184
+ end
185
+
186
+ def tras()
187
+ c = Densa.new(@cols, @rows)
188
+ c.rows.times do |i|
189
+ c.cols.times do |j|
190
+ c[i][j] = self[j][i]
191
+ end
192
+ end
193
+ c
194
+ end
195
+
196
+ def x(value)
197
+ self.rows.times do |i|
198
+ self.cols.times do |j|
199
+ self[i][j] *= 2
200
+ end
201
+ end
202
+ end
203
+ end
204
+
205
+ class VectorDisperso
206
+ attr_reader :vector
207
+
208
+ def initialize (h = {})
209
+ @vector = Hash.new(0)
210
+ @vector = @vector.merge!(h)
211
+ end
212
+
213
+ def [](i)
214
+ @vector[i]
215
+ end
216
+ end
217
+
218
+ class Dispersa < Matriz #Matriz dispersa--------------------------------------------------------------------------------
219
+ attr_reader :data
220
+
221
+ def initialize(rows,cols, h = {})
222
+ @data = Hash.new({})
223
+ for k in h.keys do
224
+ if h[k].is_a? VectorDisperso
225
+ @data[k] = h[k]
226
+ else
227
+ @data[k] = VectorDisperso.new(h[k])
228
+ end
229
+ end
230
+ super(rows,cols)
231
+ end
232
+
233
+ def [](i)
234
+ @data[i]
235
+ end
236
+ end
237
+ end
@@ -0,0 +1,3 @@
1
+ module ETSIIGEM
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,211 @@
1
+ require './lib/ETSII_GEM.rb'
2
+
3
+ describe EtsiiGem do
4
+
5
+ before :each do
6
+ @a = EtsiiGem::Densa.new(2,2)
7
+ @a[0][0] = EtsiiGem::Fraccion.new(1,1)
8
+ @a[0][1] = EtsiiGem::Fraccion.new(2,1)
9
+ @a[1][0] = EtsiiGem::Fraccion.new(3,1)
10
+ @a[1][1] = 4
11
+
12
+ @b = EtsiiGem::Dispersa.new(2,2,0 => { 0 => 1, 1 => 2})
13
+ end
14
+
15
+ describe "\n INFO de matrices \n" do
16
+ it "La matriz densa tiene guardado el numero de filas" do
17
+ @a.rows.should eq(2)
18
+ end
19
+
20
+ it "La matriz densa tiene guardado el numero de columnas " do
21
+ @a.cols.should eq(2)
22
+ end
23
+
24
+ it "La matriz dispersa tiene guardado el numero de filas" do
25
+ @b.rows.should eq(2)
26
+ end
27
+
28
+ it "La matriz dispersa tiene guardado el numero de columnas" do
29
+ @b.cols.should eq(2)
30
+ end
31
+ end
32
+
33
+ describe "\n OPERANDO CON MATRICES DENSAS" do
34
+ describe "\n SUMA \n" do
35
+ it "Es posible sumar matrices de igual tamano" do
36
+ x = EtsiiGem::Densa.new(2,2)
37
+
38
+ x[0][0] = 1
39
+ x[0][1] = 2
40
+ x[1][0] = 3
41
+ x[1][1] = 4
42
+
43
+ y = @a + x
44
+
45
+ y[0][0].should eq(2)
46
+ y[0][1].should eq(4)
47
+ y[1][0].should eq(6)
48
+ y[1][1].should eq(8)
49
+ end
50
+
51
+ it "No es posible sumar matrices de tamanos diferentes" do
52
+ x = EtsiiGem::Densa.new(2,1)
53
+
54
+ x[0][0] = 1
55
+ x[1][0] = 3
56
+
57
+ expect {@a + x}.to raise_error(ArgumentError)
58
+ end
59
+
60
+ it "Al sumar dos matrices densas se debe obtener otra matriz densa" do
61
+ x = EtsiiGem::Densa.new(2,2)
62
+
63
+ x[0][0] = 1
64
+ x[0][1] = 2
65
+ x[1][0] = 3
66
+ x[1][1] = 4
67
+
68
+ y = @a + x
69
+
70
+ y.should be_an_instance_of EtsiiGem::Densa
71
+ end
72
+
73
+ end
74
+
75
+ describe "\n RESTA \n" do
76
+ it "Es posible restar matrices de igual tamano" do
77
+ x = EtsiiGem::Densa.new(2,2)
78
+
79
+ x[0][0] = EtsiiGem::Fraccion.new(1,1)
80
+ x[0][1] = EtsiiGem::Fraccion.new(2,1)
81
+ x[1][0] = EtsiiGem::Fraccion.new(3,1)
82
+ x[1][1] = EtsiiGem::Fraccion.new(4,1)
83
+
84
+ y = @a - x
85
+
86
+ y[0][0].should eq(EtsiiGem::Fraccion.new(0,1))
87
+ y[0][1].should eq(EtsiiGem::Fraccion.new(0,1))
88
+ y[1][0].should eq(EtsiiGem::Fraccion.new(0,1))
89
+ y[1][1].should eq(EtsiiGem::Fraccion.new(0,1))
90
+ end
91
+
92
+ it "No es posible restar matrices de tamanos diferentes" do
93
+ x = EtsiiGem::Densa.new(2,1)
94
+
95
+ x[0][0] = EtsiiGem::Fraccion.new(1,1)
96
+ x[1][0] = EtsiiGem::Fraccion.new(3,1)
97
+
98
+ expect {@a - x}.to raise_error(ArgumentError)
99
+ end
100
+
101
+ it "Al restar dos matrices densas se debe obtener otra matriz densa" do
102
+ x = EtsiiGem::Densa.new(2,2)
103
+
104
+ x[0][0] = 1
105
+ x[0][1] = 2
106
+ x[1][0] = 3
107
+ x[1][1] = 4
108
+
109
+ y = @a - x
110
+
111
+ y.should be_an_instance_of EtsiiGem::Densa
112
+ end
113
+
114
+ end
115
+
116
+ describe "\n MULTIPLICACION \n" do
117
+ it "Dos matrices son multiplicables si el numero de columnas de la 1ª coincide con el numero de filas de la 2ª" do
118
+ x = EtsiiGem::Densa.new(2,5)
119
+
120
+ x[0][0] = EtsiiGem::Fraccion.new(1,1)
121
+ x[0][1] = EtsiiGem::Fraccion.new(2,1)
122
+ x[0][2] = EtsiiGem::Fraccion.new(3,1)
123
+ x[0][3] = EtsiiGem::Fraccion.new(4,1)
124
+ x[0][4] = EtsiiGem::Fraccion.new(5,1)
125
+ x[1][0] = EtsiiGem::Fraccion.new(6,1)
126
+ x[1][1] = EtsiiGem::Fraccion.new(7,1)
127
+ x[1][2] = EtsiiGem::Fraccion.new(8,1)
128
+ x[1][3] = EtsiiGem::Fraccion.new(9,1)
129
+ x[1][4] = EtsiiGem::Fraccion.new(10,1)
130
+
131
+ y = @a * x
132
+
133
+ y[0][0].should eq(EtsiiGem::Fraccion.new(13,1))
134
+ y[0][1].should eq(EtsiiGem::Fraccion.new(16,1))
135
+ y[0][2].should eq(EtsiiGem::Fraccion.new(19,1))
136
+ y[0][3].should eq(EtsiiGem::Fraccion.new(22,1))
137
+ y[0][4].should eq(EtsiiGem::Fraccion.new(25,1))
138
+ y[1][0].should eq(EtsiiGem::Fraccion.new(27,1))
139
+ y[1][1].should eq(EtsiiGem::Fraccion.new(34,1))
140
+ y[1][2].should eq(EtsiiGem::Fraccion.new(41,1))
141
+ y[1][3].should eq(EtsiiGem::Fraccion.new(48,1))
142
+ y[1][4].should eq(EtsiiGem::Fraccion.new(55,1))
143
+ end
144
+
145
+ it "No se pueden multiplicar dos matrices si el numero de columnas de la 1ª es diferente al numero de filas de la 2ª" do
146
+ x = EtsiiGem::Densa.new(1,2)
147
+
148
+ x[0][0] = 1
149
+ x[0][1] = EtsiiGem::Fraccion.new(2,1)
150
+
151
+ expect {@a * x}.to raise_error(ArgumentError)
152
+ end
153
+
154
+ it "Una matriz densa se puede multiplicar por un valor determinado" do
155
+ @a.x(2)
156
+
157
+ @a[0][0].should eq(2)
158
+ @a[0][1].should eq(4)
159
+ @a[1][0].should eq(6)
160
+ @a[1][1].should eq(8)
161
+ end
162
+ end
163
+
164
+ describe "\n VALORES MAX Y MIN DE UNA MATRIZ \n" do
165
+ it "Valor maximo" do
166
+ @a.max.should eq(4)
167
+ end
168
+
169
+ it "Valor minimo" do
170
+ @a.min.should eq(4)
171
+ end
172
+ end
173
+
174
+ describe "\n OPERANDO CON MATRICES DISPERSAS \n" do
175
+ it "Es posible sumar dos matrices dispersas" do
176
+ x = EtsiiGem::Dispersa.new(2,2,0 => { 0 => 1, 1 => 2}, 1 => { 0 => 3, 1 => 4})
177
+ y = EtsiiGem::Dispersa.new(2,2,0 => { 0 => 1, 1 => 2}, 1 => { 0 => 3, 1 => 4})
178
+
179
+ z = x + y
180
+
181
+ z[0][0].should eq(2)
182
+ z[0][1].should eq(4)
183
+ z[1][0].should eq(6)
184
+ z[1][1].should eq(8)
185
+ end
186
+
187
+ it "Es posible sumar una matriz dispersa y una densa" do
188
+ x = EtsiiGem::Dispersa.new(2,2,0 => { 0 => 1, 1 => 2}, 1 => { 0 => 3, 1 => 4})
189
+
190
+ y = @a + x
191
+
192
+ y[0][0].should eq(2)
193
+ y[0][1].should eq(4)
194
+ y[1][0].should eq(6)
195
+ y[1][1].should eq(8)
196
+ end
197
+
198
+ it "Es posible multiplicar una matriz dispersa y una densa" do
199
+ x = EtsiiGem::Dispersa.new(2,2,0 => { 0 => EtsiiGem::Fraccion.new(5,1), 1 => 6})
200
+
201
+ y = @a * x
202
+
203
+ y[0][0].should eq(5)
204
+ y[0][1].should eq(6)
205
+ y[1][0].should eq(15)
206
+ y[1][1].should eq(EtsiiGem::Fraccion.new(18,1))
207
+ end
208
+
209
+ end
210
+ end
211
+ end
@@ -0,0 +1,87 @@
1
+ require 'test/unit'
2
+ require './lib/ETSII_GEM.rb'
3
+
4
+ class Test_ETSII_GEM < Test::Unit::TestCase
5
+ def setup
6
+ @a = EtsiiGem::Densa.new(2,2)
7
+ @a[0][0] = EtsiiGem::Fraccion.new(1,1)
8
+ @a[0][1] = EtsiiGem::Fraccion.new(2,1)
9
+ @a[1][0] = EtsiiGem::Fraccion.new(3,1)
10
+ @a[1][1] = 4
11
+
12
+ @b = EtsiiGem::Densa.new(2,2)
13
+ @b[0][0] = EtsiiGem::Fraccion.new(5,1)
14
+ @b[0][1] = EtsiiGem::Fraccion.new(6,1)
15
+ @b[1][0] = 7
16
+ @b[1][1] = 8
17
+
18
+ @c = EtsiiGem::Dispersa.new(2,3,0 => { 0 => EtsiiGem::Fraccion.new(5,1), 1 => 6})
19
+ end
20
+
21
+ def test_rows
22
+ assert_equal(2,@a.rows)
23
+ assert_equal(2,@b.rows)
24
+ assert_not_equal(3,@a.rows)
25
+ assert_not_equal(3,@b.rows)
26
+ end
27
+
28
+ def test_cols
29
+ assert_equal(2,@a.cols)
30
+ assert_equal(2,@b.cols)
31
+ assert_not_equal(3,@a.cols)
32
+ assert_not_equal(3,@b.cols)
33
+ end
34
+
35
+ def test_values
36
+ assert_equal(EtsiiGem::Fraccion.new(1,1),@a[0][0])
37
+ assert_equal(EtsiiGem::Fraccion.new(2,1),@a[0][1])
38
+ assert_equal(3,@a[1][0])
39
+ assert_equal(4,@a[1][1])
40
+ end
41
+
42
+ def test_plus
43
+ d = @a + @b
44
+ assert_equal(2,d.rows)
45
+ assert_equal(2,d.cols)
46
+ assert_equal(6,d[0][0])
47
+ assert_equal(8,d[0][1])
48
+ assert_equal(10,d[1][0])
49
+ assert_equal(EtsiiGem::Fraccion.new(12,1),d[1][1])
50
+
51
+ assert_raise(ArgumentError) {@a + @c}
52
+ end
53
+
54
+ def test_minus
55
+ d = @a - @b
56
+ assert_equal(2,d.rows)
57
+ assert_equal(2,d.cols)
58
+ assert_equal(-4,d[0][0])
59
+ assert_equal(-4,d[0][1])
60
+ assert_equal(-4,d[1][0])
61
+ assert_equal(EtsiiGem::Fraccion.new(4,-1),d[1][1])
62
+
63
+ assert_raise(ArgumentError) {@a - @c}
64
+ end
65
+
66
+ def test_product
67
+ d = @a * @c
68
+ assert_equal(2,d.rows)
69
+ assert_equal(3,d.cols)
70
+ assert_equal(5,d[0][0])
71
+ assert_equal(6,d[0][1])
72
+ assert_equal(0,d[0][2])
73
+ assert_equal(15,d[1][0])
74
+ assert_equal(18,d[1][1])
75
+ assert_equal(0,d[1][2])
76
+ end
77
+
78
+ def test_max
79
+ assert_equal(4,@a.max)
80
+ assert_equal(6,@c.max)
81
+ end
82
+
83
+ def test_min
84
+ assert_equal(EtsiiGem::Fraccion.new(4,1),@a.min)
85
+ assert_equal(EtsiiGem::Fraccion.new(0,1),@c.min)
86
+ end
87
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ETSII_GEM
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Héctor J. & Enrique Tejera
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: '"Gema para la representación de matrices, tanto densas como dispersas."'
42
+ email:
43
+ - alu0100603170@ull.edu.es
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - .travis.yml
50
+ - ETSII_GEM.gemspec
51
+ - Gemfile
52
+ - Guardfile
53
+ - LICENSE.txt
54
+ - README.md
55
+ - Rakefile
56
+ - lib/ETSII_GEM.rb
57
+ - lib/ETSII_GEM/version.rb
58
+ - spec/ETSII_GEM_spec.rb
59
+ - test/tc_ETSII_GEM.rb
60
+ homepage: ''
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.0.3
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: '"Existe una clase madre llamada ''Matriz'' de la que heredan dos subclases,
84
+ ''Densa'' para las matrices densas y ''Dispersa'' para las matrices dispersas."'
85
+ test_files:
86
+ - spec/ETSII_GEM_spec.rb
87
+ - test/tc_ETSII_GEM.rb
88
+ has_rdoc: