maths_matrix_ull_etsii_lpp_t41 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bf260f217ee95598486dc6185f9c5070b6306d7c
4
+ data.tar.gz: 26788663750b764fb16a10a50b6a9a1ac92a5478
5
+ SHA512:
6
+ metadata.gz: 370018659f3f1f112aa0d9e6e981464246b9a1007ad7697f1a244325f5f36f634d2d6855acec0e5a92c21fb5c6a04a38e170c13fb976f92045ca51a380563edf
7
+ data.tar.gz: e125b9bc00e31772ba61728ab2f4d85104c2281eedfefb42f8fd9dfb5f076bfe98cd345d2eaf445ac2e7df187354558399a0fc8be8b4b35e923e2a4e2941077e
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/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 1.9.3
5
+ - jruby-20mode
6
+ - rbx-20mode
7
+ - jruby-19mode
8
+ - rbx-19mode
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rake'
4
+ gem 'rspec'
5
+ gem 'guard-rspec'
6
+ gem 'guard-bundler'
7
+
8
+
9
+ # Specify your gem's dependencies in maths_matrix_ull_etsii_lpp_t41.gemspec
10
+
data/Guardfile ADDED
@@ -0,0 +1,24 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :rspec do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+
9
+ # Rails example
10
+ watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
11
+ watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
12
+ 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"] }
13
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
14
+ watch('config/routes.rb') { "spec/routing" }
15
+ watch('app/controllers/application_controller.rb') { "spec/controllers" }
16
+
17
+ # Capybara features specs
18
+ watch(%r{^app/views/(.+)/.*\.(erb|haml|slim)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
19
+
20
+ # Turnip features and steps
21
+ watch(%r{^spec/acceptance/(.+)\.feature$})
22
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
23
+ end
24
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Oliver Valido
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,30 @@
1
+ # MathsMatrixUllEtsiiLppT41
2
+
3
+ La jerarquía de clases para implementar las matrices densas y dispersas será:
4
+ Una clase base Matriz de la cual heredarán de ella dos clases, MatrizDispersa y MatrizDensa.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'maths_matrix_ull_etsii_lpp_t41'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install maths_matrix_ull_etsii_lpp_t41
19
+
20
+ ## Usage
21
+
22
+ TODO: Write usage instructions here
23
+
24
+ ## Contributing
25
+
26
+ 1. Fork it
27
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
28
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
29
+ 4. Push to the branch (`git push origin my-new-feature`)
30
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ require "bundler/gem_tasks"
2
+ $:.unshift File.dirname(__FILE__) + 'lib'
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :tu
7
+
8
+ desc "Pruebas unitarias de la clase Matriz"
9
+ task :tu do
10
+ sh "rspec -I. spec/matriz_spec.rb"
11
+ end
12
+
13
+ desc "Ejecutar con documentacion"
14
+ task :doc do
15
+ sh "rspec -I. spec/matriz_spec.rb --format documentation"
16
+ end
17
+
18
+ desc "Test class MatrizDensa"
19
+ task :test do
20
+ sh "ruby -I. test/tc_matrizDensa.rb"
21
+ end
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'maths_matrix_ull_etsii_lpp_t41'
@@ -0,0 +1,5 @@
1
+ require "maths_matrix_ull_etsii_lpp_t41/version"
2
+
3
+ module MathsMatrixUllEtsiiLppT41
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,3 @@
1
+ module MathsMatrixUllEtsiiLppT41
2
+ VERSION = "0.0.1"
3
+ end
data/lib/matriz.rb ADDED
@@ -0,0 +1,32 @@
1
+ require 'racional'
2
+
3
+ class Matriz
4
+ attr_accessor :nFil, :mCol, :matriz
5
+
6
+ def initialize(matriz); end
7
+
8
+ include Comparable
9
+
10
+ def coerce(matriz); end
11
+
12
+ def +(matriz); end
13
+
14
+ def -(matriz); end
15
+
16
+ def *(matriz); end
17
+
18
+ def x(matriz); end
19
+
20
+ def max; end
21
+
22
+ def min; end
23
+
24
+ def <=>(matriz); end
25
+
26
+ def det; end
27
+
28
+ def tras; end
29
+
30
+ def to_s; end
31
+ end
32
+
@@ -0,0 +1,182 @@
1
+ #! /usr/local/ruby/bin/ruby
2
+ # require 'racional.rb'
3
+ require 'matriz.rb'
4
+
5
+ class MatrizDensa < Matriz
6
+
7
+ attr_accessor :nFil , :mCol, :matriz
8
+
9
+ def initialize (matriz)
10
+ @nFil = matriz.size
11
+ @mCol = matriz[0].size
12
+ @matriz = matriz
13
+ end
14
+
15
+ # def coerce(matriz)
16
+ # [self, matriz]
17
+ # end
18
+
19
+ def to_s
20
+ matString = ""
21
+ for i in 0...@nFil do
22
+ for j in 0...@mCol do
23
+ matString = matString + @matriz[i][j].to_s + " "
24
+ end
25
+ matString = matString + "\n"
26
+ end
27
+ matString
28
+ end
29
+
30
+ # Metodo para calcular el determinante de una matriz
31
+ def det
32
+ if (@nFil == @mCol)
33
+ det = @matriz[0][0]
34
+ aux = MatrizDensa.new(@matriz)
35
+ for k in 0...@nFil do
36
+ l = k+1
37
+ for i in l...@mCol do
38
+ for j in l...@mCol do
39
+ aux.matriz[i][j] = (aux.matriz[k][k] * aux.matriz[i][j] - aux.matriz[k][j] * aux.matriz[i][k])/ aux.matriz[k][k]
40
+ end
41
+ end
42
+ det = det * aux.matriz[k][k]
43
+ end
44
+ else
45
+ det = "ERROR, la matriz no es cuadrada"
46
+ end
47
+ det
48
+ end
49
+
50
+ # Método apra calcular la traspuesta de una matriz
51
+ def tras
52
+ result = Array.new
53
+ for i in 0...@nFil do
54
+ result[i] = Array.new
55
+ for j in 0...@mCol do
56
+ result[i][j] = 0
57
+ end
58
+ end
59
+
60
+ aux = MatrizDensa.new(result)
61
+ for i in 0...@nFil do
62
+ for j in 0...@mCol do
63
+ aux.matriz[i][j] = @matriz[j][i]
64
+ end
65
+ end
66
+ aux
67
+ end
68
+
69
+ # Metodo para multiplicar una matriz por un escalar
70
+ def x(escalar)
71
+ aux = MatrizDensa.new(@matriz)
72
+ for i in 0...@nFil do
73
+ for j in 0...@mCol do
74
+ aux.matriz[i][j] = @matriz[i][j] * escalar
75
+ end
76
+ end
77
+ aux
78
+ end
79
+
80
+ #Metodo apra sumar dos matrices
81
+ def +(mat)
82
+ if (mat.nFil == @nFil && mat.mCol == @mCol)
83
+ aux = MatrizDensa.new(@matriz)
84
+ for i in 0...@nFil do
85
+ for j in 0...@mCol do
86
+ aux.matriz[i][j] = @matriz[i][j] + mat.matriz[i][j]
87
+ end
88
+ end
89
+ else
90
+ aux = 0
91
+ end
92
+ aux
93
+ end
94
+
95
+ # Metodo para restar dos matrices
96
+ def -(mat)
97
+ if (mat.nFil == @nFil && mat.mCol == @mCol)
98
+ aux = MatrizDensa.new(@matriz)
99
+ for i in 0...@nFil do
100
+ for j in 0...@mCol do
101
+ aux.matriz[i][j] = @matriz[i][j] - mat.matriz[i][j]
102
+ end
103
+ end
104
+ else
105
+ aux = 0
106
+ end
107
+ aux
108
+ end
109
+
110
+ # Metodo para multiplicar dos matrices
111
+ def *(mat)
112
+ if (@mCol == mat.nFil)
113
+ result = Array.new
114
+ for i in 0...@nFil do
115
+ result[i] = Array.new
116
+ for j in 0...mat.mCol do
117
+ if (mat.matriz[0][0].class == Fraccion)
118
+ result[i][j] = Fraccion.new(0, 1)
119
+ else
120
+ result[i][j] = 0
121
+ end
122
+ end
123
+ end
124
+
125
+ aux = MatrizDensa.new(result)
126
+
127
+ for i in 0...@nFil do
128
+ for j in 0...mat.mCol do
129
+ for z in 0...@mCol do
130
+ aux.matriz[i][j] += @matriz[i][z] * mat.matriz[z][j]
131
+ end
132
+ end
133
+ end
134
+ else
135
+ aux = 0
136
+ end
137
+ aux
138
+ end
139
+
140
+ def <=>(matriz)
141
+ if (@nFil == matriz.nFil && @mCol == matriz.mCol)
142
+ comparacion = 0
143
+ for i in 0...@nFil do
144
+ for j in 0...@mCol do
145
+ comparacion += @matriz[i][j] <=> matriz.matriz[i][j]
146
+ end
147
+ end
148
+ end
149
+ if (comparacion > 0)
150
+ comparacion = 1
151
+ elsif (comparacion < 0)
152
+ comparacion = -1
153
+ end
154
+ comparacion
155
+ end
156
+
157
+ def max
158
+ maxi = @matriz[0][0]
159
+ for i in 0...@nFil do
160
+ for j in 0...@mCol do
161
+ if (maxi < @matriz[i][j])
162
+ maxi = @matriz[i][j]
163
+ end
164
+
165
+ end
166
+ end
167
+ maxi
168
+ end
169
+
170
+ def min
171
+ mini = @matriz[0][0]
172
+ for i in 0...@nFil do
173
+ for j in 0...@mCol do
174
+ if (mini > @matriz[i][j])
175
+ mini = @matriz[i][j]
176
+ end
177
+ end
178
+ end
179
+ mini
180
+ end
181
+ end
182
+
@@ -0,0 +1,297 @@
1
+
2
+ require 'matriz.rb'
3
+ require 'matrizDensa.rb'
4
+
5
+ class MatrizDispersa < Matriz
6
+
7
+ attr_accessor :nFil, :mCol, :matriz
8
+
9
+ def initialize(n, m, matriz)
10
+ @nFil = n
11
+ @mCol = m
12
+ @matriz = matriz
13
+ end
14
+
15
+ def coerce(matriz)
16
+ [self, matriz]
17
+ end
18
+
19
+ def to_s
20
+ matString = ""
21
+ for i in 0...@nFil do
22
+ for j in 0...@mCol do
23
+ if ((!@matriz[i].nil?) && (!@matriz[i][j].nil?))
24
+ matString = matString + @matriz[i][j].to_s + " "
25
+ else
26
+ matString = matString + "0 "
27
+ end
28
+ end
29
+ matString = matString + "\n"
30
+ end
31
+ matString
32
+ end
33
+
34
+ def tras
35
+ aux = MatrizDispersa.new(@mCol, @nFil, {})
36
+ for i in 0...@nFil do
37
+ for j in 0...@mCol do
38
+ if ((!@matriz[j].nil?) && (!@matriz[j][i].nil?))
39
+ if (aux.matriz[i].nil?)
40
+ aux.matriz[i] = {}
41
+ end
42
+ aux.matriz[i][j] = @matriz[j][i]
43
+ end
44
+ end
45
+ end
46
+ aux
47
+ end
48
+
49
+ def det
50
+ if (@nFil == @mCol)
51
+ if ((!@matriz[0].nil?) && (!@matriz[0][0].nil?))
52
+ det = @matriz[0][0]
53
+ else
54
+ det = 0
55
+ end
56
+ aux = MatrizDispersa.new(@nFil, @mCol, @matriz)
57
+ for k in 0...@nFil do
58
+ l = k+1
59
+ for i in l...@mCol do
60
+ for j in l...@mCol do
61
+ aux.matriz[i][j] = (aux.matriz[k][k] * aux.matriz[i][j] - aux.matriz[k][j] * aux.matriz[i][k])/ aux.matriz[k][k]
62
+ end
63
+ end
64
+ det = det * aux.matriz[k][k]
65
+ end
66
+ else
67
+ det = "ERROR, la matriz no es cuadrada"
68
+ end
69
+ det
70
+ end
71
+
72
+ def x(escalar) #multiplicacion por un escalar
73
+ aux = MatrizDispersa.new(@nFil, @mCol, @matriz)
74
+ for i in 0...@nFil do
75
+ for j in 0...@mCol do
76
+ if ((!@matriz[i].nil?) && (!@matriz[i][j].nil?))
77
+ aux.matriz[i][j] = @matriz[i][j] * escalar
78
+ end
79
+ end
80
+ end
81
+ aux
82
+ end
83
+
84
+ def +(mat)
85
+ if (mat.nFil == @nFil && mat.mCol == @mCol)
86
+ result = Array.new
87
+ for i in 0...@nFil do
88
+ result[i] = Array.new
89
+ for j in 0...@mCol do
90
+ result[i][j] = 0
91
+ end
92
+ end
93
+ aux = MatrizDensa.new(result)
94
+ nElementos = 0
95
+ if (mat.class == MatrizDispersa)
96
+ for i in 0...@nFil do
97
+ for j in 0...@mCol do
98
+ if ((!@matriz[i].nil?) && (!@matriz[i][j].nil?) && (!mat.matriz[i].nil?) && (!mat.matriz[i][j].nil?))
99
+ aux.matriz[i][j] = @matriz[i][j] + mat.matriz[i][j]
100
+ nElementos += 1
101
+ elsif ((!@matriz[i].nil?) && (!@matriz[i][j].nil?) && ((mat.matriz[i].nil?) || ((!mat.matriz[i].nil?) && mat.matriz[i][j].nil?)))
102
+ aux.matriz[i][j] = @matriz[i][j]
103
+ nElementos += 1
104
+ elsif ((!mat.matriz[i].nil?) && (!mat.matriz[i][j].nil?) && ((@matriz[i].nil?) || ((!@matriz[i].nil?) && @matriz[i][j].nil?)))
105
+ aux.matriz[i][j] = mat.matriz[i][j]
106
+ nElementos += 1
107
+ end
108
+ end
109
+ end
110
+ elsif (mat.class == MatrizDensa)
111
+ for i in 0...@nFil do
112
+ for j in 0...@mCol do
113
+ if ((!@matriz[i].nil?) && (!@matriz[i][j].nil?))
114
+ aux.matriz[i][j] = @matriz[i][j] + mat.matriz[i][j]
115
+ nElementos += 1
116
+ elsif ((@matriz[i].nil?) || ((!@matriz[i].nil?) && @matriz[i][j].nil?))
117
+ aux.matriz[i][j] = mat.matriz[i][j]
118
+ nElementos += 1
119
+ end
120
+ end
121
+ end
122
+ end
123
+ if ((@nFil * @mCol) * 0.4 > nElementos)
124
+ aux = to_dispersa(aux)
125
+ end
126
+ else
127
+ aux = "No se pueden sumar"
128
+ end
129
+ aux
130
+ end
131
+
132
+ def -(mat)
133
+ if (mat.nFil == @nFil && mat.mCol == @mCol)
134
+ result = Array.new
135
+ for i in 0...@nFil do
136
+ result[i] = Array.new
137
+ for j in 0...@mCol do
138
+ result[i][j] = 0
139
+ end
140
+ end
141
+ aux = MatrizDensa.new(result)
142
+ nElementos = 0
143
+ if (mat.class == MatrizDispersa)
144
+ for i in 0...@nFil do
145
+ for j in 0...@mCol do
146
+ if ((!@matriz[i].nil?) && (!@matriz[i][j].nil?) && (!mat.matriz[i].nil?) && (!mat.matriz[i][j].nil?))
147
+ aux.matriz[i][j] = @matriz[i][j] - mat.matriz[i][j]
148
+ nElementos += 1
149
+ elsif ((!@matriz[i].nil?) && (!@matriz[i][j].nil?) && ((mat.matriz[i].nil?) || ((!mat.matriz[i].nil?) && mat.matriz[i][j].nil?)))
150
+ aux.matriz[i][j] = @matriz[i][j]
151
+ nElementos += 1
152
+ elsif ((!mat.matriz[i].nil?) && (!mat.matriz[i][j].nil?) && ((@matriz[i].nil?) || ((!@matriz[i].nil?) && @matriz[i][j].nil?)))
153
+ aux.matriz[i][j] = - mat.matriz[i][j]
154
+ nElementos += 1
155
+ end
156
+ end
157
+ end
158
+ elsif (mat.class == MatrizDensa)
159
+ for i in 0...@nFil do
160
+ for j in 0...@mCol do
161
+ if ((!@matriz[i].nil?) && (!@matriz[i][j].nil?))
162
+ aux.matriz[i][j] = @matriz[i][j] - mat.matriz[i][j]
163
+ nElementos += 1
164
+ elsif ((@matriz[i].nil?) || ((!@matriz[i].nil?) && @matriz[i][j].nil?))
165
+ aux.matriz[i][j] = - mat.matriz[i][j]
166
+ nElementos += 1
167
+ end
168
+ end
169
+ end
170
+ end
171
+
172
+ if ((@nFil * @mCol) * 0.4 > nElementos)
173
+ aux = to_dispersa(aux)
174
+ end
175
+ else
176
+ aux = "No se pueden sumar"
177
+ end
178
+ aux
179
+ end
180
+
181
+ def *(mat)
182
+ if (@mCol == mat.nFil)
183
+ result = Array.new
184
+ for i in 0...@nFil do
185
+ result[i] = Array.new
186
+ for j in 0...@mCol do
187
+ result[i][j] = 0
188
+ end
189
+ end
190
+ aux = MatrizDensa.new(result)
191
+ nElementos = 0
192
+ if (mat.class == MatrizDispersa)
193
+ for i in 0...@nFil do
194
+ for j in 0...mat.mCol do
195
+ for z in 0...@mCol do
196
+ if ((!@matriz[i].nil?) && (!@matriz[i][z].nil?) && (!mat.matriz[z].nil?) && (!mat.matriz[z][j].nil?))
197
+ aux.matriz[i][j] += @matriz[i][z] * mat.matriz[z][j]
198
+ nElementos += 1
199
+ end
200
+ end
201
+ end
202
+ end
203
+ else
204
+ for i in 0...@nFil do
205
+ for j in 0...mat.mCol do
206
+ for z in 0...@mCol do
207
+ if ((!@matriz[i].nil?) && (!@matriz[i][z].nil?))
208
+ aux.matriz[i][j] += @matriz[i][z] * mat.matriz[z][j]
209
+ nElementos += 1
210
+ end
211
+ end
212
+ end
213
+ end
214
+ end
215
+ if ((@nFil * @mCol) * 0.4 > nElementos)
216
+ aux = to_dispersa(aux)
217
+ end
218
+ else
219
+ aux = "No se pueden multiplicar"
220
+ end
221
+ aux
222
+ end
223
+
224
+ def <=>(mat)
225
+ if (@nFil == mat.nFil && @mCol == mat.mCol)
226
+ comparacion = 0
227
+ for i in 0...@nFil do
228
+ for j in 0...@mCol do
229
+ if ((!@matriz[i].nil?) && (!@matriz[i][j].nil?) && (!mat.matriz[i].nil?) && (!mat.matriz[i][j].nil?))
230
+ comparacion += @matriz[i][j] <=> mat.matriz[i][j]
231
+ elsif ((!@matriz[i].nil?) && (!@matriz[i][j].nil?) && ((mat.matriz[i].nil?) || ((!mat.matriz[i].nil?) && mat.matriz[i][j].nil?)))
232
+ if (@matriz[i][j] > 0)
233
+ comparacion += 1
234
+ elsif (@matriz[i][j] < 0)
235
+ comparacion -= 1
236
+ end
237
+ elsif ((!mat.matriz[i].nil?) && (!mat.matriz[i][j].nil?) && ((@matriz[i].nil?) || ((!@matriz[i].nil?) && @matriz[i][j].nil?)))
238
+ if (mat.matriz[i][j] > 0)
239
+ comparacion -= 1
240
+ elsif (mat.matriz[i][j] < 0)
241
+ comparacion += 1
242
+ end
243
+ end
244
+ end
245
+ end
246
+ end
247
+ if (comparacion > 0)
248
+ comparacion = 1
249
+ elsif (comparacion < 0)
250
+ comparacion = -1
251
+ end
252
+ comparacion
253
+ end
254
+
255
+ def max
256
+ maxi = 0
257
+ for i in 0...@nFil do
258
+ for j in 0...@mCol do
259
+ if ((!@matriz[i].nil?) && (!@matriz[i][j].nil?))
260
+ if (maxi < @matriz[i][j])
261
+ maxi = @matriz[i][j]
262
+ end
263
+ end
264
+ end
265
+ end
266
+ maxi
267
+ end
268
+
269
+ def min
270
+ mini = 0
271
+ for i in 0...@nFil do
272
+ for j in 0...@mCol do
273
+ if ((!@matriz[i].nil?) && (!@matriz[i][j].nil?))
274
+ if (mini > @matriz[i][j])
275
+ mini = @matriz[i][j]
276
+ end
277
+ end
278
+ end
279
+ end
280
+ mini
281
+ end
282
+
283
+ def to_dispersa(matDen)
284
+ result = {}
285
+ for i in 0...matDen.nFil do
286
+ for j in 0...matDen.mCol do
287
+ if (matDen.matriz[i][j] != 0)
288
+ if (result[i].nil?)
289
+ result[i] = {}
290
+ end
291
+ result[i][j] = matDen.matriz[i][j]
292
+ end
293
+ end
294
+ end
295
+ aux = MatrizDispersa.new(matDen.nFil, matDen.mCol, result)
296
+ end
297
+ end
data/lib/racional.rb ADDED
@@ -0,0 +1,118 @@
1
+ #! /usr/local/ruby/bin/ruby
2
+ include Comparable
3
+
4
+ class Fraccion
5
+ attr_reader :num, :den
6
+
7
+ # Constructor de la clase Fraccion
8
+ def initialize (num, den)
9
+ mcd = gdc(num, den)
10
+ @num = num / mcd
11
+ @den = den / mcd
12
+ end
13
+
14
+ # Metodo que calcula el Maximo común divisor
15
+ def gdc(a, b)
16
+ while b != 0
17
+ a, b = b, a % b
18
+ end
19
+ a
20
+ end
21
+
22
+ def coerce(num)
23
+ [self, num]
24
+ end
25
+
26
+ # Metodo que imprime el numero racional en el formato: num/den
27
+ def to_s
28
+ "#{@num}/#{@den}"
29
+ end
30
+
31
+ # Metodo que devuelve el número racional como flotante
32
+ def to_f
33
+ @num/@den
34
+ end
35
+
36
+ # Metodo que calcula el valor absoluto de un numero racional
37
+ def abs
38
+ Fraccion.new(@num.abs, @den.abs)
39
+ end
40
+
41
+ # Metodo que calcula el reciproco de un numero racional
42
+ def reciproco
43
+ Fraccion.new(@den, @num)
44
+ end
45
+
46
+ # Metodo que cambia de signo el numero racional
47
+ def -@
48
+ Fraccion.new(-@num, @den)
49
+ end
50
+
51
+ # Metodo para sumar dos numeros racionales
52
+ def +(fraccion)
53
+ if (fraccion.class == Fraccion)
54
+ num = @num * fraccion.den + fraccion.num * @den
55
+ den = @den * fraccion.den
56
+ Fraccion.new(num, den)
57
+ else
58
+ num = @num + fraccion * @den
59
+ den = @den
60
+ Fraccion.new(num, den)
61
+ end
62
+ end
63
+
64
+ # Metodo para restar dos numeros racionales
65
+ def -(fraccion)
66
+ if (fraccion.class == Fraccion)
67
+ num = @num * fraccion.den - fraccion.num * @den
68
+ den = @den * fraccion.den
69
+ else
70
+ num = @num - fraccion * @den
71
+ den = @den
72
+ end
73
+ Fraccion.new(num, den)
74
+ end
75
+
76
+ # Metodo para multiplicar dos numeros racionales
77
+ def *(fraccion)
78
+ if (fraccion.class == Fraccion)
79
+ num = @num * fraccion.num
80
+ den = @den * fraccion.den
81
+ else
82
+ num = @num * fraccion
83
+ den = @den
84
+ end
85
+ resultado = Fraccion.new(num, den)
86
+ end
87
+
88
+ # Metodo para dividir dos numeros racionales
89
+ def /(fraccion)
90
+ if (fraccion.class == Fraccion)
91
+ num = @num * fraccion.den
92
+ den = @den * fraccion.num
93
+ else
94
+ num = @num
95
+ den = @den * fraccion
96
+ end
97
+ resultado = Fraccion.new(num, den)
98
+ end
99
+
100
+ # Metodo para calcular el resto de dos numeros racionales
101
+ def %(fraccion)
102
+ ((@num * fraccion.den) % (@den * fraccion.num))
103
+ end
104
+
105
+ # Metodo guerra de las galaxias
106
+ def <=>(fraccion)
107
+ if ((@num == fraccion.num) && (@den == fraccion.den))
108
+ return 0
109
+ end
110
+ if ((@num * fraccion.den) < (@den * fraccion.num))
111
+ return -1
112
+ end
113
+ if ((@num * fraccion.den) > (@den * fraccion.num))
114
+ return 1
115
+ end
116
+ end
117
+
118
+ end
@@ -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 'maths_matrix_ull_etsii_lpp_t41/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "maths_matrix_ull_etsii_lpp_t41"
8
+ spec.version = MathsMatrixUllEtsiiLppT41::VERSION
9
+ spec.authors = ["Oliver Valido", "Rodrigo Colombo"]
10
+ spec.email = ["alu0100598148@ull.edu.es", "alu0100630572@ull.edu.es"]
11
+ spec.description = %q{Clases que permiten trabajar con matrices densas y dispersas}
12
+ spec.summary = %q{Conjunto de clases para matrices densas y dispersas}
13
+ spec.homepage = "https://github.com/alu0100630572/prct09"
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
Binary file
@@ -0,0 +1,99 @@
1
+ require 'matriz'
2
+ require 'matrizDispersa'
3
+ require 'matrizDensa'
4
+ require 'racional'
5
+
6
+ describe Matriz do
7
+
8
+ before :each do
9
+ @m1 = MatrizDensa.new([[1,3],[2,0]])
10
+ @m2 = MatrizDensa.new([[2,1],[0,1]])
11
+ @m3 = MatrizDensa.new([[2,0,1],[3,0,0],[5,1,1]])
12
+ @m4 = MatrizDensa.new([[4,1,6],[2,0,1]])
13
+ @m5 = MatrizDensa.new([[Fraccion.new(1, 2), Fraccion.new(3, 5)], [Fraccion.new(3, 4), Fraccion.new(2, 3)]])
14
+ @m6 = MatrizDensa.new([[Fraccion.new(3, 2), Fraccion.new(1, 5)], [Fraccion.new(5, 4), Fraccion.new(1, 3)]])
15
+ @md1 = MatrizDispersa.new(3,3,{0 => {0 => 3, 2 => 7}, 2 => {1 => 1}})
16
+ @md2 = MatrizDispersa.new(3,3,{0 => {1 => 2, 2 => 2}, 1 => {0 => 1}})
17
+ end
18
+
19
+ describe "# Se crean las matrices del tamanio especificado e inicialidas" do
20
+ it "Se establecen correctamente las dimensiones" do
21
+ @m1.mCol.should == 2
22
+ @m1.nFil.should == 2
23
+ @m3.mCol.should == 3
24
+ @m3.nFil.should == 3
25
+ @m5.mCol.should == 2
26
+ @m5.nFil.should == 2
27
+ @md1.nFil.should == 3
28
+ @md1.mCol.should == 3
29
+ @md2.nFil.should == 3
30
+ @md2.mCol.should == 3
31
+ end
32
+ it "Se inicializa correctamente" do
33
+ @m1.to_s.should == "1 3 \n2 0 \n"
34
+ @m2.to_s.should == "2 1 \n0 1 \n"
35
+ @m3.to_s.should == "2 0 1 \n3 0 0 \n5 1 1 \n"
36
+ @m5.to_s.should == "1/2 3/5 \n3/4 2/3 \n"
37
+ @md1.to_s.should == "3 0 7 \n0 0 0 \n0 1 0 \n"
38
+ @md2.to_s.should == "0 2 2 \n1 0 0 \n0 0 0 \n"
39
+ end
40
+ end
41
+
42
+ describe "Operaciones unarias" do
43
+ it "Traspuesta" do
44
+ (@m3.tras.matriz == MatrizDensa.new([[2,3,5],[0,0,1],[1,0,1]]).matriz).should be_true
45
+ (@md1.tras.matriz == MatrizDispersa.new(3,3,{0 => {0 => 3}, 1 => {2 => 1}, 2 => {0 => 7}}).matriz).should be_true
46
+ end
47
+ it "Determinante" do
48
+ (@m1.det.should == -6)
49
+ end
50
+ end
51
+
52
+ describe "Multiplicar por un escalar" do
53
+ it "Multiplicacion" do
54
+ ((@m3.x(2)).matriz.should == MatrizDensa.new([[4,0,2],[6,0,0],[10,2,2]]).matriz)
55
+ ((@m1.x(-3)).matriz.should == MatrizDensa.new([[-3,-9],[-6,0]]).matriz)
56
+ ((@md1.x(2)).matriz.should == MatrizDispersa.new(3,3,{0 => {0 => 6, 2 => 14}, 2 => {1 => 2}}).matriz)
57
+ end
58
+ end
59
+
60
+ describe "Operaciones con dos matrices" do
61
+ it "Se deben poder sumar dos matrices" do
62
+ ((@m1 + @m2).matriz.should == MatrizDensa.new([[3,4],[2,1]]).matriz)
63
+ ((@m5 + @m6).matriz.should == MatrizDensa.new([[Fraccion.new(2, 1), Fraccion.new(4, 5)], [Fraccion.new(2, 1), Fraccion.new(1, 1)]]).matriz)
64
+ ((@md1 + @md2).matriz.should == MatrizDensa.new([[3,2,9],[1,0,0],[0,1,0]]).matriz)
65
+ ((@md1 + @m3).matriz.should == MatrizDensa.new([[5,0,8],[3,0,0],[5,2,1]]).matriz) #Dispersa + Densa
66
+ end
67
+ it "Se deben poder multiplicar dos matrices" do
68
+ ((@m1 * @m2).matriz.should == MatrizDensa.new([[2,4],[4,2]]).matriz)
69
+ ((@m1 * @m4).matriz.should == MatrizDensa.new([[10,1,9],[8,2,12]]).matriz)
70
+ ((@m5 * @m6).matriz.should == MatrizDensa.new([[Fraccion.new(3, 2), Fraccion.new(3, 10)], [Fraccion.new(47, 24), Fraccion.new(67, 180)]]).matriz)
71
+ ((@md1 * @md2).matriz.should == MatrizDispersa.new(3,3,{0 => {1 => 6, 2 => 6}, 2 => {0 => 1}}).matriz)
72
+ ((@md1 * @m3).matriz.should == MatrizDensa.new([[41,7,10],[0,0,0],[3,0,0]]).matriz) #Dispersa * Densa
73
+ end
74
+ it "Se deben poder restar dos matrices" do
75
+ ((@m1 - @m2).matriz.should == MatrizDensa.new([[-1,2],[2,-1]]).matriz)
76
+ ((@md1 - @md2).matriz.should == MatrizDensa.new([[3,-2,5],[-1,0,0],[0,1,0]]).matriz)
77
+ ((@md1 - @m3).matriz.should == MatrizDensa.new([[1,0,6],[-3,0,0],[-5,0,-1]]).matriz) #Dispersa - Densa
78
+ end
79
+ end
80
+
81
+ describe "Comparaciones de matrices" do
82
+ it "Se debe poder comparar dos matrices" do
83
+ (@m1 == @m2).should be_true
84
+ (@md1 > @md2).should be_true
85
+ end
86
+ end
87
+
88
+ describe "Metodo maximo y minimo" do
89
+ it "Se debe encontrar el maximo elemento de la matriz" do
90
+ (@m1.max.should == 3)
91
+ (@md1.max.should == 7)
92
+ end
93
+ it "Se debe poder encontrar el minimo elemento de la matriz" do
94
+ (@m2.min.should == 0)
95
+ (@md2.min.should == 0)
96
+ end
97
+ end
98
+
99
+ end
@@ -0,0 +1,22 @@
1
+ require 'matrizDensa'
2
+ require 'matriz'
3
+ require 'matrizDispersa'
4
+ require 'test/unit'
5
+
6
+ class TestMatriz < Test::Unit::TestCase
7
+
8
+ def test_densa
9
+ @m1 = MatrizDensa.new([[1,2],[2,5]])
10
+ @m2 = MatrizDensa.new([[3,4],[1,3]])
11
+ @m3 = MatrizDensa.new([[4,6],[3,8]])
12
+ assert_equal((@m1+@m2).to_s, @m3.to_s)
13
+ end
14
+
15
+ def test_dispersa
16
+ @md1 = MatrizDispersa.new(3,3,{0 => {0 => 3, 2 => 7}, 2 => {1 => 1}})
17
+ @md2 = MatrizDispersa.new(3,3,{0 => {1 => 2, 2 => 2}, 1 => {0 => 1}})
18
+ @md3 = MatrizDensa.new([[3,2,9],[1,0,0],[0,1,0]])
19
+ assert_equal((@md1+@md2).to_s, @md3.to_s)
20
+ end
21
+
22
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: maths_matrix_ull_etsii_lpp_t41
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Oliver Valido
8
+ - Rodrigo Colombo
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-11-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ~>
19
+ - !ruby/object:Gem::Version
20
+ version: '1.3'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ version: '1.3'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ description: Clases que permiten trabajar con matrices densas y dispersas
43
+ email:
44
+ - alu0100598148@ull.edu.es
45
+ - alu0100630572@ull.edu.es
46
+ executables:
47
+ - maths_matrix_ull_etsii_lpp_t41
48
+ extensions: []
49
+ extra_rdoc_files: []
50
+ files:
51
+ - .gitignore
52
+ - .travis.yml
53
+ - Gemfile
54
+ - Guardfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - bin/maths_matrix_ull_etsii_lpp_t41
59
+ - lib/maths_matrix_ull_etsii_lpp_t41.rb
60
+ - lib/maths_matrix_ull_etsii_lpp_t41/version.rb
61
+ - lib/matriz.rb
62
+ - lib/matrizDensa.rb
63
+ - lib/matrizDispersa.rb
64
+ - lib/racional.rb
65
+ - maths_matrix_ull_etsii_lpp_t41.gemspec
66
+ - spec/.matriz_spec.rb.swp
67
+ - spec/matriz_spec.rb
68
+ - test/tc_matrizDensa.rb
69
+ homepage: https://github.com/alu0100630572/prct09
70
+ licenses:
71
+ - MIT
72
+ metadata: {}
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 2.0.3
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: Conjunto de clases para matrices densas y dispersas
93
+ test_files:
94
+ - spec/.matriz_spec.rb.swp
95
+ - spec/matriz_spec.rb
96
+ - test/tc_matrizDensa.rb
97
+ has_rdoc: