kwyjibo 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 886c37a69e3ad030b4be88e46b566dc2a10c6805
4
+ data.tar.gz: f6d6fc193642069fc4e8d2f7e108954996c5c4c9
5
+ SHA512:
6
+ metadata.gz: a46b15539dd7e17be72a4154d5206b26d69569693364e9693e8fb73984cd9aeb5427afcceea212ddcaa7a841a0983139f9fc50110abe8798cb0729218507ae61
7
+ data.tar.gz: 31cd1b37c0c727e835b329b21b53382a8bff03a0ad4656a010d69fa7d5c2daada1c24f77172af5662340adb3c5adff1a6d4a8b2fe4a0ad221c33e3ed2076ad72
@@ -0,0 +1,18 @@
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
18
+ *~
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
@@ -0,0 +1,5 @@
1
+ rvm:
2
+ - 1.9.3
3
+ - jruby
4
+
5
+ script: "bundle exec rake"
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in kwyjibo.gemspec
4
+ gemspec
5
+
6
+ gem 'guard'
7
+ gem 'guard-rspec'
8
+ gem 'guard-bundler'
@@ -0,0 +1,38 @@
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 :bundler do
13
+ watch('Gemfile')
14
+ # Uncomment next line if your Gemfile contains the `gemspec' command.
15
+ # watch(/^.+\.gemspec/)
16
+ end
17
+
18
+ guard :rspec do
19
+ watch(%r{^spec/.+_spec\.rb$})
20
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
21
+ watch('spec/spec_helper.rb') { "spec" }
22
+
23
+ # Rails example
24
+ watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
25
+ watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
26
+ 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"] }
27
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
28
+ watch('config/routes.rb') { "spec/routing" }
29
+ watch('app/controllers/application_controller.rb') { "spec/controllers" }
30
+
31
+ # Capybara features specs
32
+ watch(%r{^app/views/(.+)/.*\.(erb|haml|slim)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
33
+
34
+ # Turnip features and steps
35
+ watch(%r{^spec/acceptance/(.+)\.feature$})
36
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
37
+ end
38
+
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 José Alberto
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,44 @@
1
+ # Kwyjibo
2
+
3
+ Creating a Gem to represent Matrix and Sparse Matrix
4
+
5
+ ## Class Inheritance
6
+
7
+ We have a first class called Matriz where all Matrix will be stored and a subclass called Dispersa to represent a Sparse Matrix
8
+
9
+ ## Status
10
+
11
+ [![Build Status](https://travis-ci.org/alu0100537291/kwyjibo.png?branch=master)](https://travis-ci.org/alu0100537291/kwyjibo)
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ gem 'kwyjibo'
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install kwyjibo
26
+
27
+ ## Usage
28
+
29
+ Gem under construction!
30
+
31
+ ## Contributing
32
+
33
+ 1. Fork it
34
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
35
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
36
+ 4. Push to the branch (`git push origin my-new-feature`)
37
+ 5. Create new Pull Request
38
+
39
+ ## Authors
40
+
41
+ | ALUMNO | CORREO|
42
+ | -------- | ------- |
43
+ | José Alberto Pérez Melián | alu0100537291[AT]ull.edu.es |
44
+ | Jéssica Alejandra Ramos Villarreal | alu0100537338[AT]ull.edu.es |
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new
5
+ task :default => :spec
6
+
7
+ desc "Unit test"
8
+ task :test do
9
+ sh "ruby -Ilib test/tc_kwyjibo.rb"
10
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'kwyjibo/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "kwyjibo"
8
+ spec.version = Kwyjibo::VERSION
9
+ spec.authors = ["José Alberto"]
10
+ spec.email = ["japmelian@gmail.com"]
11
+ spec.description = %q{CoAuthor: Jéssica Alejandra Ramos Villarreal}
12
+ spec.summary = %q{LPP - Práctica 9}
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
+ spec.add_development_dependency 'rspec', '~> 2.9'
24
+ end
@@ -0,0 +1,305 @@
1
+ require "kwyjibo/version"
2
+
3
+ module Kwyjibo
4
+ class Matrix
5
+ attr_reader :rows, :cols
6
+
7
+ def initialize(rows,cols)
8
+ @rows, @cols = rows, cols
9
+ end
10
+
11
+ def +(other)
12
+ raise ArgumentError, "Matrix size must be equal" unless @rows == other.rows && @cols == other.cols
13
+ c = DenseMatrix.new(@rows, @cols)
14
+ @rows.times do |i|
15
+ @cols.times do |j|
16
+ if self[i][j] == nil && other[i][j] == nil
17
+ c[i][j] = 0
18
+ elsif self[i][j] == nil && other[i][j] != nil
19
+ c[i][j] = other[i][j]
20
+ elsif self[i][j] != nil && other[i][j] == nil
21
+ c[i][j] = self[i][j]
22
+ else
23
+ c[i][j] = self[i][j] + other[i][j]
24
+ end
25
+ end
26
+ end
27
+ c
28
+ end
29
+
30
+ def -(other)
31
+ raise ArgumentError, "Matrix size must be equal" unless @rows == other.rows && @cols == other.cols
32
+ c = DenseMatrix.new(@rows, @cols)
33
+ @rows.times do |i|
34
+ @cols.times do |j|
35
+ if self[i][j] == nil && other[i][j] == nil
36
+ c[i][j] = 0
37
+ elsif self[i][j] == nil && other[i][j] != nil
38
+ c[i][j] = -other[i][j]
39
+ elsif self[i][j] != nil && other[i][j] == nil
40
+ c[i][j] = self[i][j]
41
+ else
42
+ c[i][j] = self[i][j] - other[i][j]
43
+ end
44
+ end
45
+ end
46
+ c
47
+ end
48
+
49
+ def *(other)
50
+ raise ArgumentError, "Columns and Rows must be equal" unless (@cols == other.rows)
51
+ c = DenseMatrix.new(@rows,other.cols)
52
+ @rows.times do |i|
53
+ other.cols.times do |j|
54
+ ac = 0
55
+ @cols.times do |k|
56
+ ac += self[i][k] * other[k][j] if (self[i][k] != nil && other[k][j] != nil)
57
+ end
58
+ c[i][j] = ac
59
+ end
60
+ end
61
+ c
62
+ end
63
+
64
+ def max
65
+ encontrado = false
66
+ value = 0
67
+ i = -1
68
+
69
+ while encontrado == false
70
+ i += 1
71
+ j = 0
72
+ while j < self.cols
73
+ if self[i][j] != nil and value == 0
74
+ value = self[i][j]
75
+ encontrado = true
76
+ break
77
+ else
78
+ j += 1
79
+ end
80
+ end
81
+ end
82
+
83
+ @rows.times do |i|
84
+ @cols.times do |j|
85
+ if self[i][j] != nil && self[i][j] > value
86
+ value = self[i][j]
87
+ end
88
+ end
89
+ end
90
+ value
91
+ end
92
+
93
+ def min
94
+ encontrado = false
95
+ value = 0
96
+ i = -1
97
+
98
+ while encontrado == false
99
+ i += 1
100
+ j = 0
101
+ while j < self.cols
102
+ if self[i][j] != nil and value == 0
103
+ value = self[i][j]
104
+ encontrado = true
105
+ break
106
+ else
107
+ j += 1
108
+ end
109
+ end
110
+ end
111
+
112
+ @rows.times do |i|
113
+ @cols.times do |j|
114
+ if self[i][j] != nil && self[i][j] < value
115
+ value = self[i][j]
116
+ end
117
+ end
118
+ end
119
+ value
120
+ end
121
+ end
122
+
123
+ class DenseMatrix < Matrix
124
+ attr_reader :data
125
+
126
+ def initialize(rows,cols)
127
+ @data = Array.new(rows) {Array.new(cols)}
128
+ super
129
+ end
130
+
131
+ def [](i)
132
+ @data[i]
133
+ end
134
+
135
+ def []=(i,value)
136
+ @data[i] = value
137
+ end
138
+
139
+ def tras()
140
+ c = DenseMatrix.new(@cols, @rows)
141
+ c.rows.times do |i|
142
+ c.cols.times do |j|
143
+ c[i][j] = self[j][i]
144
+ end
145
+ end
146
+ c
147
+ end
148
+
149
+ def x(value)
150
+ self.rows.times do |i|
151
+ self.cols.times do |j|
152
+ self[i][j] *= 2
153
+ end
154
+ end
155
+ end
156
+ end
157
+
158
+ class SparseVector
159
+ attr_reader :vector
160
+
161
+ def initialize(h = {})
162
+ @vector = Hash.new(0)
163
+ @vector = @vector.merge!(h)
164
+ end
165
+
166
+ def [](i)
167
+ @vector[i]
168
+ end
169
+ end
170
+
171
+ class SparseMatrix < Matrix
172
+ attr_reader :data
173
+
174
+ def initialize(rows,cols, h = {})
175
+ @data = Hash.new({})
176
+ for k in h.keys do
177
+ if h[k].is_a? SparseVector
178
+ @data[k] = h[k]
179
+ else
180
+ @data[k] = SparseVector.new(h[k])
181
+ end
182
+ end
183
+ super(rows,cols)
184
+ end
185
+
186
+ def [](i)
187
+ @data[i]
188
+ end
189
+ end
190
+
191
+ class Fraccion
192
+ include Comparable
193
+
194
+ attr_accessor :num, :denom
195
+
196
+ def initialize(a, b)
197
+ x = mcd(a,b)
198
+ @num = a/x
199
+ @denom = b/x
200
+
201
+ if (@num < 0 && @denom < 0)
202
+ @num = @num * -1
203
+ @denom = @denom * -1
204
+ end
205
+
206
+ if (@denom < 0)
207
+ @denom = @denom * -1
208
+ @num = @num * -1
209
+ end
210
+ end
211
+
212
+ def mcd(u, v)
213
+ u, v = u.abs, v.abs
214
+ while v != 0
215
+ u, v = v, u % v
216
+ end
217
+ u
218
+ end
219
+
220
+ def to_s
221
+ "#{@num}/#{@denom}"
222
+ end
223
+
224
+ def to_f
225
+ @num.to_f/@denom.to_f
226
+ end
227
+
228
+ def +(other)
229
+ if other.instance_of? Fixnum
230
+ c = Fraccion.new(other,1)
231
+ Fraccion.new(@num * c.denom + @denom * c.num, @denom * c.denom)
232
+ else
233
+ Fraccion.new(@num * other.denom + @denom * other.num, @denom * other.denom)
234
+ end
235
+ end
236
+
237
+ def -(other)
238
+ if other.instance_of? Fixnum
239
+ c = Fraccion.new(other,1)
240
+ Fraccion.new(@num * c.denom - @denom * c.num, @denom * c.denom)
241
+ else
242
+ Fraccion.new(@num * other.denom - @denom * other.num, @denom * other.denom)
243
+ end
244
+ end
245
+
246
+ def *(other)
247
+ if other.instance_of? Fixnum
248
+ c = Fraccion.new(other,1)
249
+ Fraccion.new(@num * c.num, @denom * c.denom)
250
+ else
251
+ Fraccion.new(@num * other.num, @denom * other.denom)
252
+ end
253
+ end
254
+
255
+ def /(other)
256
+ if other.instance_of? Fixnum
257
+ c = Fraccion.new(other,1)
258
+ Fraccion.new(@num * c.denom, @denom * c.num)
259
+ else
260
+ Fraccion.new(@num * other.denom, @denom * other.num)
261
+ end
262
+ end
263
+
264
+ def %(other)
265
+ if other.instance_of? Fixnum
266
+ c = Fraccion.new(other,1)
267
+ division = Fraccion.new(@num * c.denom, @denom * c.num)
268
+ else
269
+ division = Fraccion.new(@num * other.denom, @denom * other.num)
270
+ end
271
+ division.num % division.denom
272
+ end
273
+
274
+ def abs
275
+ @num = @num.abs
276
+ @denom = @denom.abs
277
+ end
278
+
279
+ def reciprocal
280
+ x = @num
281
+ @num = @denom
282
+ @denom = x
283
+ end
284
+
285
+ def -@
286
+ if (@num > 0)
287
+ @num = @num * -1
288
+ end
289
+ end
290
+
291
+ def <=>(other)
292
+ return nil unless (other.instance_of? Fraccion) || (other.instance_of? Fixnum)
293
+ if other.instance_of? Fixnum
294
+ c = Fraccion.new(other,1)
295
+ (c.num.to_f/c.denom.to_f) <=> (self.num.to_f/self.denom.to_f)
296
+ else
297
+ (self.num.to_f/self.denom.to_f) <=> (other.num.to_f/other.denom.to_f)
298
+ end
299
+ end
300
+
301
+ def coerce(other)
302
+ [self,other]
303
+ end
304
+ end
305
+ end
@@ -0,0 +1,3 @@
1
+ module Kwyjibo
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,329 @@
1
+ require 'spec_helper'
2
+
3
+ describe Kwyjibo do
4
+ before :each do
5
+ @a = Kwyjibo::DenseMatrix.new(2,2)
6
+ @a[0][0] = Kwyjibo::Fraccion.new(1,1)
7
+ @a[0][1] = Kwyjibo::Fraccion.new(2,1)
8
+ @a[1][0] = Kwyjibo::Fraccion.new(3,1)
9
+ @a[1][1] = 4
10
+
11
+ @b = Kwyjibo::SparseMatrix.new(2,2,0 => { 0 => 1, 1 => 2})
12
+ end
13
+
14
+ describe "\n # Matrix data \n" do
15
+ it "### Una matriz densa tiene su numero de filas almacenadas" do
16
+ @a.rows.should eq(2)
17
+ end
18
+
19
+ it "### Una matriz densa tiene su numero de columnas almacenadas" do
20
+ @a.cols.should eq(2)
21
+ end
22
+
23
+ it "### Una matriz dispersa tiene su numero de filas almacenadas" do
24
+ @b.rows.should eq(2)
25
+ end
26
+
27
+ it "### Una matriz dispersa tiene su numero de columnas almacenadas" do
28
+ @b.cols.should eq(2)
29
+ end
30
+ end
31
+
32
+ describe "\n # Operaciones con matrices densas" do
33
+ describe "\n ## Suma de matrices densas \n" do
34
+ it "### Se pueden sumar matrices del mismo tamano" do
35
+ c = Kwyjibo::DenseMatrix.new(2,2)
36
+
37
+ c[0][0] = 1
38
+ c[0][1] = 2
39
+ c[1][0] = 3
40
+ c[1][1] = 4
41
+
42
+ d = @a + c
43
+
44
+ d[0][0].should eq(2)
45
+ d[0][1].should eq(4)
46
+ d[1][0].should eq(6)
47
+ d[1][1].should eq(8)
48
+ end
49
+
50
+ it "### No se pueden sumar matrices de distinto tamano" do
51
+ c = Kwyjibo::DenseMatrix.new(2,1)
52
+
53
+ c[0][0] = 1
54
+ c[1][0] = 3
55
+
56
+ expect {@a + c}.to raise_error(ArgumentError)
57
+ end
58
+
59
+ it "### El resultado de sumar dos matrices densas debe ser una matriz densa" do
60
+ c = Kwyjibo::DenseMatrix.new(2,2)
61
+
62
+ c[0][0] = 1
63
+ c[0][1] = 2
64
+ c[1][0] = 3
65
+ c[1][1] = 4
66
+
67
+ d = @a + c
68
+
69
+ d.should be_an_instance_of Kwyjibo::DenseMatrix
70
+ end
71
+
72
+ it "### La suma de dos matrices densas debe dar una matriz densa del mismo tamano" do
73
+ c = Kwyjibo::DenseMatrix.new(2,2)
74
+
75
+ c[0][0] = 1
76
+ c[0][1] = 2
77
+ c[1][0] = 3
78
+ c[1][1] = 4
79
+
80
+ d = @a + c
81
+
82
+ d.rows.should eq(2)
83
+ d.cols.should eq(2)
84
+ end
85
+ end
86
+
87
+ describe "\n ## Resta de matrices densas \n" do
88
+ it "### Se pueden restar matrices del mismo tamano" do
89
+ c = Kwyjibo::DenseMatrix.new(2,2)
90
+
91
+ c[0][0] = Kwyjibo::Fraccion.new(1,1)
92
+ c[0][1] = Kwyjibo::Fraccion.new(2,1)
93
+ c[1][0] = Kwyjibo::Fraccion.new(3,1)
94
+ c[1][1] = Kwyjibo::Fraccion.new(4,1)
95
+
96
+ d = @a - c
97
+
98
+ d[0][0].should eq(Kwyjibo::Fraccion.new(0,1))
99
+ d[0][1].should eq(Kwyjibo::Fraccion.new(0,1))
100
+ d[1][0].should eq(Kwyjibo::Fraccion.new(0,1))
101
+ d[1][1].should eq(Kwyjibo::Fraccion.new(0,1))
102
+ end
103
+
104
+ it "### No se pueden restar matrices de distinto tamano" do
105
+ c = Kwyjibo::DenseMatrix.new(2,1)
106
+
107
+ c[0][0] = Kwyjibo::Fraccion.new(1,1)
108
+ c[1][0] = Kwyjibo::Fraccion.new(3,1)
109
+
110
+ expect {@a - c}.to raise_error(ArgumentError)
111
+ end
112
+
113
+ it "### El resultado de restar dos matrices densas debe ser una matriz densa" do
114
+ c = Kwyjibo::DenseMatrix.new(2,2)
115
+
116
+ c[0][0] = 1
117
+ c[0][1] = 2
118
+ c[1][0] = 3
119
+ c[1][1] = 4
120
+
121
+ d = @a - c
122
+
123
+ d.should be_an_instance_of Kwyjibo::DenseMatrix
124
+ end
125
+
126
+ it "### La resta de dos matrices debe dar una matriz del mismo tamano" do
127
+ c = Kwyjibo::DenseMatrix.new(2,2)
128
+
129
+ c[0][0] = 1
130
+ c[0][1] = 2
131
+ c[1][0] = 3
132
+ c[1][1] = 4
133
+
134
+ d = @a - c
135
+
136
+ d.rows.should eq(2)
137
+ d.cols.should eq(2)
138
+ end
139
+ end
140
+
141
+ describe "\n ## Multiplicacion de matrices densas \n" do
142
+ it "### Se pueden multiplicar dos matrices si el numero de columnas de la primera es igual al numero de de la segunda matriz" do
143
+ c = Kwyjibo::DenseMatrix.new(2,5)
144
+
145
+ c[0][0] = Kwyjibo::Fraccion.new(1,1)
146
+ c[0][1] = Kwyjibo::Fraccion.new(2,1)
147
+ c[0][2] = Kwyjibo::Fraccion.new(3,1)
148
+ c[0][3] = Kwyjibo::Fraccion.new(4,1)
149
+ c[0][4] = Kwyjibo::Fraccion.new(5,1)
150
+ c[1][0] = Kwyjibo::Fraccion.new(6,1)
151
+ c[1][1] = Kwyjibo::Fraccion.new(7,1)
152
+ c[1][2] = Kwyjibo::Fraccion.new(8,1)
153
+ c[1][3] = Kwyjibo::Fraccion.new(9,1)
154
+ c[1][4] = Kwyjibo::Fraccion.new(10,1)
155
+
156
+ d = @a * c
157
+
158
+ d[0][0].should eq(Kwyjibo::Fraccion.new(13,1))
159
+ d[0][1].should eq(Kwyjibo::Fraccion.new(16,1))
160
+ d[0][2].should eq(Kwyjibo::Fraccion.new(19,1))
161
+ d[0][3].should eq(Kwyjibo::Fraccion.new(22,1))
162
+ d[0][4].should eq(Kwyjibo::Fraccion.new(25,1))
163
+ d[1][0].should eq(Kwyjibo::Fraccion.new(27,1))
164
+ d[1][1].should eq(Kwyjibo::Fraccion.new(34,1))
165
+ d[1][2].should eq(Kwyjibo::Fraccion.new(41,1))
166
+ d[1][3].should eq(Kwyjibo::Fraccion.new(48,1))
167
+ d[1][4].should eq(Kwyjibo::Fraccion.new(55,1))
168
+ end
169
+
170
+ it "### No se pueden multiplicar dos matrices si el numero de columnas de la primera es distinto al numero de de la segunda matriz" do
171
+ c = Kwyjibo::DenseMatrix.new(1,2)
172
+
173
+ c[0][0] = 1
174
+ c[0][1] = Kwyjibo::Fraccion.new(2,1)
175
+
176
+ expect {@a * c}.to raise_error(ArgumentError)
177
+ end
178
+
179
+ it "### El resultado de multiplicar dos matrices debe ser una matriz con numero de filas igual a las filas de la primera matriz y numero de columnas igual al numero de columnas de la segunda matriz" do
180
+ c = Kwyjibo::DenseMatrix.new(2,5)
181
+
182
+ c[0][0] = Kwyjibo::Fraccion.new(1,1)
183
+ c[0][1] = Kwyjibo::Fraccion.new(2,1)
184
+ c[0][2] = Kwyjibo::Fraccion.new(3,1)
185
+ c[0][3] = Kwyjibo::Fraccion.new(4,1)
186
+ c[0][4] = Kwyjibo::Fraccion.new(5,1)
187
+ c[1][0] = Kwyjibo::Fraccion.new(6,1)
188
+ c[1][1] = Kwyjibo::Fraccion.new(7,1)
189
+ c[1][2] = Kwyjibo::Fraccion.new(8,1)
190
+ c[1][3] = Kwyjibo::Fraccion.new(9,1)
191
+ c[1][4] = Kwyjibo::Fraccion.new(10,1)
192
+
193
+ d = @a * c
194
+
195
+ d.rows.should eq(2)
196
+ d.cols.should eq(5)
197
+ end
198
+ end
199
+
200
+ describe "\n ##Operaciones entre matrices densas y escalares \n" do
201
+ it "### Se puede multiplicar una matriz densa por un escalar" do
202
+ @a.x(2)
203
+
204
+ @a[0][0].should eq(2)
205
+ @a[0][1].should eq(4)
206
+ @a[1][0].should eq(6)
207
+ @a[1][1].should eq(8)
208
+ end
209
+ end
210
+
211
+ describe "\n ## Opuesta de una matriz densa \n" do
212
+ it "### Se puede calcular la opuesta de una matriz densa" do
213
+ c = @a.tras
214
+
215
+ c[0][0].should eq(1)
216
+ c[0][1].should eq(3)
217
+ c[1][0].should eq(2)
218
+ c[1][1].should eq(4)
219
+ end
220
+
221
+ it "### En el calculo de la matriz opuesta de una matriz se intercambian sus filas y columnas" do
222
+ c = Kwyjibo::DenseMatrix.new(2,5)
223
+
224
+ c[0][0] = Kwyjibo::Fraccion.new(1,1)
225
+ c[0][1] = Kwyjibo::Fraccion.new(2,1)
226
+ c[0][2] = Kwyjibo::Fraccion.new(3,1)
227
+ c[0][3] = Kwyjibo::Fraccion.new(4,1)
228
+ c[0][4] = Kwyjibo::Fraccion.new(5,1)
229
+ c[1][0] = Kwyjibo::Fraccion.new(6,1)
230
+ c[1][1] = Kwyjibo::Fraccion.new(7,1)
231
+ c[1][2] = Kwyjibo::Fraccion.new(8,1)
232
+ c[1][3] = Kwyjibo::Fraccion.new(9,1)
233
+ c[1][4] = Kwyjibo::Fraccion.new(10,1)
234
+
235
+ d = c.tras
236
+
237
+ d.rows.should eq(5)
238
+ d.cols.should eq(2)
239
+ end
240
+ end
241
+ end
242
+
243
+ describe "\n # Calculo de valores maximos y minimos en una matriz \n" do
244
+ it "### Se puede calcular el valor maximo de los elementos de una matriz" do
245
+ @a.max.should eq(4)
246
+ end
247
+
248
+ it "### Se puede calcular el valor minimo de los elementos de una matriz" do
249
+ @a.min.should eq(1)
250
+ end
251
+ end
252
+
253
+ describe "\n # Operaciones con matrices dispersas \n" do
254
+ it "### Sumar dos matrices dispersas" do
255
+ q = Kwyjibo::SparseMatrix.new(2,2,0 => { 0 => 1, 1 => 2}, 1 => { 0 => 3, 1 => 4})
256
+ w = Kwyjibo::SparseMatrix.new(2,2,0 => { 0 => 1, 1 => 2}, 1 => { 0 => 3, 1 => 4})
257
+
258
+ c = q + w
259
+
260
+ c[0][0].should eq(2)
261
+ c[0][1].should eq(4)
262
+ c[1][0].should eq(6)
263
+ c[1][1].should eq(8)
264
+ end
265
+
266
+ it "### Sumar una matriz densa y otra dispersa" do
267
+ q = Kwyjibo::SparseMatrix.new(2,2,0 => { 0 => 1, 1 => 2}, 1 => { 0 => 3, 1 => 4})
268
+
269
+ c = @a + q
270
+
271
+ c[0][0].should eq(2)
272
+ c[0][1].should eq(4)
273
+ c[1][0].should eq(6)
274
+ c[1][1].should eq(8)
275
+ end
276
+
277
+ it "### Sumar una matriz dispersa y otra densa" do
278
+ q = Kwyjibo::SparseMatrix.new(2,2,0 => { 0 => 1, 1 => 2}, 1 => { 0 => 3, 1 => 4})
279
+
280
+ c = q + @a
281
+
282
+ c[0][0].should eq(2)
283
+ c[0][1].should eq(4)
284
+ c[1][0].should eq(6)
285
+ c[1][1].should eq(8)
286
+ end
287
+
288
+ it "### Multiplicacion de una matriz dispersa por una densa" do
289
+ q = Kwyjibo::SparseMatrix.new(2,2,0 => { 0 => Kwyjibo::Fraccion.new(5,1), 1 => 6})
290
+
291
+ c = @a * q
292
+
293
+ c[0][0].should eq(5)
294
+ c[0][1].should eq(6)
295
+ c[1][0].should eq(15)
296
+ c[1][1].should eq(Kwyjibo::Fraccion.new(18,1))
297
+ end
298
+
299
+ it "### El resultado de multiplicar dos matrices debe ser una matriz con numero de filas igual a las filas de la primera matriz y numero de columnas igual al numero de columnas de la segunda matriz" do
300
+ q = Kwyjibo::SparseMatrix.new(2,2,0 => { 0 => Kwyjibo::Fraccion.new(5,1), 1 => 6})
301
+
302
+ c = @a * q
303
+
304
+ c.rows.should eq(2)
305
+ c.cols.should eq(2)
306
+ end
307
+ end
308
+
309
+ describe "\n # EXPECTATIVA A CUMPLIR \n" do
310
+ it "Suma de una matriz densa y una matriz dispersa" do
311
+ f = Kwyjibo::DenseMatrix.new(2,2)
312
+ f[0][0] = 2
313
+ f[0][1] = 1
314
+ f[1][0] = -5
315
+ f[1][1] = Kwyjibo::Fraccion.new(-3,2)
316
+
317
+ g = Kwyjibo::SparseMatrix.new(2,2, 1 => {1 => 1})
318
+
319
+ h = f + g
320
+
321
+ h.rows.should eq(2)
322
+ h.cols.should eq(2)
323
+ h[0][0].should eq(2)
324
+ h[0][1].should eq(1)
325
+ h[1][0].should eq(-5)
326
+ h[1][1].should eq(Kwyjibo::Fraccion.new(-1,2))
327
+ end
328
+ end
329
+ end
@@ -0,0 +1,8 @@
1
+ $:.unshift File.dirname(__FILE__) + '/../lib'
2
+ require 'kwyjibo'
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,97 @@
1
+ require 'test/unit'
2
+ require 'kwyjibo'
3
+
4
+ class Test_Kwyjibo < Test::Unit::TestCase
5
+ def setup
6
+ @a = Kwyjibo::DenseMatrix.new(2,2)
7
+ @a[0][0] = Kwyjibo::Fraccion.new(1,1)
8
+ @a[0][1] = Kwyjibo::Fraccion.new(2,1)
9
+ @a[1][0] = Kwyjibo::Fraccion.new(3,1)
10
+ @a[1][1] = 4
11
+
12
+ @b = Kwyjibo::DenseMatrix.new(2,2)
13
+ @b[0][0] = Kwyjibo::Fraccion.new(5,1)
14
+ @b[0][1] = Kwyjibo::Fraccion.new(6,1)
15
+ @b[1][0] = 7
16
+ @b[1][1] = 8
17
+
18
+ @c = Kwyjibo::SparseMatrix.new(2,3,0 => { 0 => Kwyjibo::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(Kwyjibo::Fraccion.new(1,1),@a[0][0])
37
+ assert_equal(Kwyjibo::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(Kwyjibo::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(Kwyjibo::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_traspose
79
+ d = @a.tras
80
+ assert_equal(2,d.cols)
81
+ assert_equal(2,d.rows)
82
+ assert_equal(1,d[0][0])
83
+ assert_equal(3,d[0][1])
84
+ assert_equal(2,d[1][0])
85
+ assert_equal(4,d[1][1])
86
+ end
87
+
88
+ def test_max
89
+ assert_equal(4,@a.max)
90
+ assert_equal(6,@c.max)
91
+ end
92
+
93
+ def test_min
94
+ assert_equal(Kwyjibo::Fraccion.new(1,1),@a.min)
95
+ assert_equal(Kwyjibo::Fraccion.new(0,1),@c.min)
96
+ end
97
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kwyjibo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - José Alberto
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-20 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
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '2.9'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '2.9'
55
+ description: 'CoAuthor: Jéssica Alejandra Ramos Villarreal'
56
+ email:
57
+ - japmelian@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .rspec
64
+ - .travis.yml
65
+ - Gemfile
66
+ - Guardfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - kwyjibo.gemspec
71
+ - lib/kwyjibo.rb
72
+ - lib/kwyjibo/version.rb
73
+ - spec/kwyjibo_spec.rb
74
+ - spec/spec_helper.rb
75
+ - test/tc_kwyjibo.rb
76
+ homepage: ''
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.1.11
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: LPP - Práctica 9
100
+ test_files:
101
+ - spec/kwyjibo_spec.rb
102
+ - spec/spec_helper.rb
103
+ - test/tc_kwyjibo.rb