matrc 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 +15 -0
- data/.gitignore +17 -0
- data/.travis.yml +8 -0
- data/Gemfile +4 -0
- data/Guardfile +11 -0
- data/LICENSE.txt +22 -0
- data/README.md +38 -0
- data/Rakefile +11 -0
- data/bin/matrc +3 -0
- data/lib/matrc.rb +12 -0
- data/lib/matrc/fraction.rb +99 -0
- data/lib/matrc/matriz.rb +106 -0
- data/lib/matrc/matriz_densa.rb +29 -0
- data/lib/matrc/matriz_dispersa.rb +19 -0
- data/lib/matrc/mihash.rb +22 -0
- data/lib/matrc/version.rb +3 -0
- data/matrc.gemspec +27 -0
- data/spec/matrices_spec.rb +207 -0
- data/test/tc_matrices.rb +159 -0
- metadata +138 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
MmZlYTc5M2E5YzQ5MmNiZmZmY2JmMzYyZWI1YmEwNTk4ZmMzYTJkZQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
OWZiNmQ1MTQ1MjNiNzFjMjM0ODEwZTEzYzFhZjA4ZDY1MTBiYThjMw==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
NWM0ZWZjZWY5NGExNjljODU5MGJiNjA5ZDkwNWFhMjYzMTQ4Y2Y1YWEwZDY4
|
10
|
+
ODA0NzljMTY3MGY3MTdkMjIyMzUwMDg2MTlkZTg2YjgyYTdiZmIwYjQ3ODJi
|
11
|
+
MjRiNzhjMWIxZGY2ZTdlNDY4ZTZlMzE5M2U1ODcwYjc5NTAxYTI=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
MmVjMTAwMjE0M2U5NjQzOWZkNDQxZThiZThmNzZhNjJiOTA2NmRiZjVhYmMw
|
14
|
+
NjVkYjA5MmRkNDVlOTJmYzVhNDAxOGIxOWE3OWQwZDhlMDQyNzc3MGMyNTFl
|
15
|
+
MGVhZGJmYjFiMTIyOWI5ZDcwOTFiMjc1Njc4YTRlYjFjMTkxMTk=
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Raquel Alvarez, Carlota Lázaro
|
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,38 @@
|
|
1
|
+
# Matrc
|
2
|
+
|
3
|
+
Uso de Clases que permiten resolver problemas con matrices densas y matrices dispersas.
|
4
|
+
|
5
|
+
|
6
|
+
## Instalación
|
7
|
+
|
8
|
+
Añadir esta línea al Gemfile de tu aplicación.
|
9
|
+
|
10
|
+
gem 'matrc'
|
11
|
+
|
12
|
+
Y luego ejecutar:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
O instalar por su cuenta mediante el siguiente comando:
|
17
|
+
|
18
|
+
$ gem install matrc
|
19
|
+
|
20
|
+
## Uso
|
21
|
+
|
22
|
+
|
23
|
+
Para poder hacer un uso correcto de esta librería en tu código de Ruby, se tendrá que incluir el siguiente fichero:
|
24
|
+
|
25
|
+
require "Practica9"
|
26
|
+
|
27
|
+
Para la colaboracion en la escritura del código hay que instalar las dependencias de la librería ejecutando el comando:
|
28
|
+
|
29
|
+
bundle
|
30
|
+
|
31
|
+
|
32
|
+
## Contribuir
|
33
|
+
|
34
|
+
1. Haz un fork
|
35
|
+
2. Crea tu rama de características (`git checkout -b my-new-feature`)
|
36
|
+
3. Haz un commit de tus cambios (`git commit -am 'Add some feature'`)
|
37
|
+
4. Haz un push a la rama (`git push origin my-new-feature`)
|
38
|
+
5. Crea un Pull Request
|
data/Rakefile
ADDED
data/bin/matrc
ADDED
data/lib/matrc.rb
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
module Matrc
|
2
|
+
|
3
|
+
class Fraction
|
4
|
+
|
5
|
+
attr_accessor :num, :den
|
6
|
+
|
7
|
+
include Comparable
|
8
|
+
|
9
|
+
def initialize(num, den)
|
10
|
+
|
11
|
+
com = gcd(num, den)
|
12
|
+
if (den < 0)
|
13
|
+
@num, @den = ((-1*num)/com), ((-1*den)/com)
|
14
|
+
else
|
15
|
+
@num, @den = num/com, den/com
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def to_string()
|
20
|
+
"#{@num}/#{@den}"
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_float()
|
24
|
+
(@num.to_f/@den.to_f)
|
25
|
+
end
|
26
|
+
|
27
|
+
def +(other)
|
28
|
+
if !(other.is_a? Fraction)
|
29
|
+
other = Fraction.new(other,1)
|
30
|
+
end
|
31
|
+
Fraction.new(@num* other.den + other.num*@den, @den * other.den)
|
32
|
+
end
|
33
|
+
|
34
|
+
def -(other)
|
35
|
+
if !(other.is_a? Fraction)
|
36
|
+
other = Fraction.new(other,1)
|
37
|
+
end
|
38
|
+
Fraction.new(@num* other.den - other.num*@den, @den * other.den)
|
39
|
+
end
|
40
|
+
|
41
|
+
def *(other)
|
42
|
+
if !(other.is_a? Fraction)
|
43
|
+
other = Fraction.new(other,1)
|
44
|
+
end
|
45
|
+
Fraction.new(@num * other.num, @den *other.den)
|
46
|
+
end
|
47
|
+
|
48
|
+
def /(other)
|
49
|
+
if !(other.is_a? Fraction)
|
50
|
+
other = Fraction.new(other,1)
|
51
|
+
end
|
52
|
+
Fraction.new(@num * other.den, @den *other.num)
|
53
|
+
end
|
54
|
+
|
55
|
+
def %(other)
|
56
|
+
if !(other.is_a? Fraction)
|
57
|
+
other = Fraction.new(other,1)
|
58
|
+
end
|
59
|
+
a = (@num*other.den)
|
60
|
+
b = (@den * other.num)
|
61
|
+
c = a%b
|
62
|
+
c
|
63
|
+
end
|
64
|
+
|
65
|
+
def abs()
|
66
|
+
Fraction.new(@num.abs, @den.abs)
|
67
|
+
end
|
68
|
+
|
69
|
+
def -@()
|
70
|
+
Fraction.new(@num * -1, @den)
|
71
|
+
end
|
72
|
+
|
73
|
+
def reciprocal()
|
74
|
+
Fraction.new(@den, @num)
|
75
|
+
end
|
76
|
+
|
77
|
+
def <=>(other)
|
78
|
+
if !(other.is_a? Fraction)
|
79
|
+
other = Fraction.new(other,1)
|
80
|
+
end
|
81
|
+
self.to_float() <=> other.to_float()
|
82
|
+
end
|
83
|
+
|
84
|
+
def coerce(other)
|
85
|
+
[self,Fraction.new(other,1)]
|
86
|
+
end
|
87
|
+
|
88
|
+
|
89
|
+
def gcd(u, v)
|
90
|
+
u, v = u.abs, v.abs
|
91
|
+
while v != 0
|
92
|
+
u, v = v, u % v
|
93
|
+
end
|
94
|
+
u
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
data/lib/matrc/matriz.rb
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
class Matriz
|
2
|
+
|
3
|
+
attr_reader :row, :col
|
4
|
+
|
5
|
+
include Enumerable
|
6
|
+
|
7
|
+
def initialize(row,col)
|
8
|
+
@row,@col = row,col
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_s
|
12
|
+
aux = ""
|
13
|
+
for i in 0...self.row
|
14
|
+
for j in 0...self.col
|
15
|
+
aux << "#{self[i,j]}"
|
16
|
+
end
|
17
|
+
aux << "\n"
|
18
|
+
end
|
19
|
+
aux
|
20
|
+
end
|
21
|
+
|
22
|
+
def *(other)
|
23
|
+
if(self.col == other.row)
|
24
|
+
matres = self.class.new(self.row,self.col)
|
25
|
+
for i in 0...self.row
|
26
|
+
for j in 0...other.col
|
27
|
+
for k in 0...self.col
|
28
|
+
matres[i,j] += self[i,k] * other[k,j]
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
matres
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def +(other)
|
37
|
+
if(self.col == other.col && other.row == self.row)
|
38
|
+
matres = self.class.new(self.row,self.col)
|
39
|
+
for i in 0...self.row
|
40
|
+
for j in 0...self.col
|
41
|
+
matres[i,j] = self[i,j] + other[i,j]
|
42
|
+
end
|
43
|
+
end
|
44
|
+
matres
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def -(other)
|
49
|
+
if(self.col == other.col && other.row == self.row)
|
50
|
+
matres = self.class.new(self.row,self.col)
|
51
|
+
for i in 0...self.row
|
52
|
+
for j in 0...self.col
|
53
|
+
matres[i,j] = self[i,j] - other[i,j]
|
54
|
+
end
|
55
|
+
end
|
56
|
+
matres
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def ==(other)
|
61
|
+
if (self.row != other.row || self.col != other.col)
|
62
|
+
return false
|
63
|
+
end
|
64
|
+
for i in 0...self.row
|
65
|
+
for j in 0...self.col
|
66
|
+
if(self[i, j] != other[i,j])
|
67
|
+
return false
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
return true
|
72
|
+
end
|
73
|
+
|
74
|
+
def each
|
75
|
+
for i in 0...self.row
|
76
|
+
for j in 0...self.col
|
77
|
+
yield self[i, j]
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def max
|
83
|
+
max = -9999
|
84
|
+
for i in 0...@row
|
85
|
+
for j in 0...@col
|
86
|
+
if(self[i,j] > max)
|
87
|
+
max = self[i,j]
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
max
|
92
|
+
end
|
93
|
+
|
94
|
+
def min
|
95
|
+
min = 9999
|
96
|
+
for i in 0...@row
|
97
|
+
for j in 0...@col
|
98
|
+
if(self[i,j] < min)
|
99
|
+
min = self[i,j]
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
min
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require "matrc/matriz.rb"
|
2
|
+
|
3
|
+
module Matrc
|
4
|
+
|
5
|
+
class MatrizDensa < Matriz
|
6
|
+
|
7
|
+
def initialize(row,col)
|
8
|
+
super(row,col)
|
9
|
+
@mtdensa = Array.new(row){Array.new(col, zero)}
|
10
|
+
end
|
11
|
+
|
12
|
+
def [](row,col=nil)
|
13
|
+
if (col.nil?)
|
14
|
+
@mtdensa[row]
|
15
|
+
else
|
16
|
+
@mtdensa[row][col]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def []=(row,col=nil,val)
|
21
|
+
if (col.nil?)
|
22
|
+
@mtdensa[row]=val
|
23
|
+
else
|
24
|
+
@mtdensa[row][col]=val
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Matrc
|
2
|
+
|
3
|
+
class MatrizDispersa < Matriz
|
4
|
+
|
5
|
+
def initialize(row, col)
|
6
|
+
super(row,col)
|
7
|
+
@matrizdisp = Hash.new(zero)
|
8
|
+
end
|
9
|
+
|
10
|
+
def [](row, col)
|
11
|
+
@matrizdisp[MiHash.new(row,col)]
|
12
|
+
end
|
13
|
+
|
14
|
+
def []=(row, col, val)
|
15
|
+
@matrizdisp[MiHash.new(row,col)] = val
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/matrc/mihash.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
class MiHash
|
2
|
+
|
3
|
+
attr_accessor :r, :c
|
4
|
+
|
5
|
+
def initialize(r,c)
|
6
|
+
@r, @c = r, c
|
7
|
+
end
|
8
|
+
|
9
|
+
def eql?(other)
|
10
|
+
(@r == other.r) && (@c == other.c)
|
11
|
+
end
|
12
|
+
|
13
|
+
def hash
|
14
|
+
@r.hash ^ @c.hash
|
15
|
+
end
|
16
|
+
|
17
|
+
def ==(other)
|
18
|
+
self.class === other and
|
19
|
+
other.c == @c and
|
20
|
+
other.r == @r
|
21
|
+
end
|
22
|
+
end
|
data/matrc.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'matrc/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "matrc"
|
8
|
+
spec.version = Matrc::VERSION
|
9
|
+
spec.authors = ["Raquel Alvarez" , "Carlota Lázaro"]
|
10
|
+
spec.email = ["alu0100700435@ull.edu.es", "alu0100698862@ull.edu.es"]
|
11
|
+
spec.description = %q{ Gema que usará la funcion de una matriz para la representacion de matrices densas y dispersas }
|
12
|
+
spec.summary = %q{ Representación de matrices densas y dispersas}
|
13
|
+
spec.homepage = "https://github.com/alu0100700435/Practica9.git"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "guard-rspec"
|
25
|
+
spec.add_development_dependency "guard-bundler"
|
26
|
+
|
27
|
+
end
|
@@ -0,0 +1,207 @@
|
|
1
|
+
require "matrc"
|
2
|
+
|
3
|
+
describe Matrc::MatrizDensa do
|
4
|
+
before :all do
|
5
|
+
|
6
|
+
class MatEntero < Matrc::MatrizDensa
|
7
|
+
def zero
|
8
|
+
0
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class MatFraction < Matrc::MatrizDensa
|
13
|
+
def zero
|
14
|
+
Matrc::Fraction.new(0, 1)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class MatDispEntero < Matrc::MatrizDispersa
|
19
|
+
def zero
|
20
|
+
0
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class MatDispFraction < Matrc::MatrizDispersa
|
25
|
+
def zero
|
26
|
+
Matrc::Fraction.new(0, 1)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "#Matrices de enteros" do
|
32
|
+
before :all do
|
33
|
+
|
34
|
+
@m1 = MatEntero.new(2,2)
|
35
|
+
@m2 = MatEntero.new(2,2)
|
36
|
+
@m3 = MatEntero.new(2,2)
|
37
|
+
@md = MatDispEntero.new(2,2)
|
38
|
+
|
39
|
+
|
40
|
+
@m1[0,0] = 1
|
41
|
+
@m1[0,1] = 2
|
42
|
+
@m1[1,0] = 3
|
43
|
+
@m1[1,1] = 4
|
44
|
+
|
45
|
+
@m2[0,0] = 2
|
46
|
+
@m2[0,1] = 3
|
47
|
+
@m2[1,0] = 4
|
48
|
+
@m2[1,1] = 5
|
49
|
+
|
50
|
+
@md[0,0] = 2
|
51
|
+
@md[1,1] = 5
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
it "Suma de dos matrices" do
|
56
|
+
@m3[0,0] = 3
|
57
|
+
@m3[0,1] = 5
|
58
|
+
@m3[1,0] = 7
|
59
|
+
@m3[1,1] = 9
|
60
|
+
|
61
|
+
(@m1+@m2).should == @m3
|
62
|
+
end
|
63
|
+
|
64
|
+
it "Suma de dos matrices (densa y dispersa)" do
|
65
|
+
@m3[0,0] = 3
|
66
|
+
@m3[0,1] = 2
|
67
|
+
@m3[1,0] = 3
|
68
|
+
@m3[1,1] = 9
|
69
|
+
|
70
|
+
(@m1+@md).should == @m3
|
71
|
+
end
|
72
|
+
|
73
|
+
it "Resta de dos matrices (densa y dispersa)" do
|
74
|
+
@m3[0,0] = -1
|
75
|
+
@m3[0,1] = 2
|
76
|
+
@m3[1,0] = 3
|
77
|
+
@m3[1,1] = -1
|
78
|
+
|
79
|
+
(@m1-@md).should == @m3
|
80
|
+
end
|
81
|
+
|
82
|
+
it "Resta de dos matrices" do
|
83
|
+
@m3[0,0] = -1
|
84
|
+
@m3[0,1] = -1
|
85
|
+
@m3[1,0] = -1
|
86
|
+
@m3[1,1] = -1
|
87
|
+
|
88
|
+
(@m1-@m2).should == @m3
|
89
|
+
end
|
90
|
+
|
91
|
+
it "Maximo de una matriz" do
|
92
|
+
@m1.max.should == 4
|
93
|
+
end
|
94
|
+
it "Minimo de una matriz" do
|
95
|
+
@m2.min.should == 2
|
96
|
+
end
|
97
|
+
|
98
|
+
it "Minimo de una matriz dispersa" do
|
99
|
+
@md.min.should == 0
|
100
|
+
end
|
101
|
+
|
102
|
+
it "Maximo de una matriz dispersa" do
|
103
|
+
@md.max.should == 5
|
104
|
+
end
|
105
|
+
|
106
|
+
|
107
|
+
end
|
108
|
+
|
109
|
+
describe "#Matrices con fracciones" do
|
110
|
+
before :all do
|
111
|
+
|
112
|
+
@mf1 = MatFraction.new(2,2)
|
113
|
+
@mf2 = MatFraction.new(2,2)
|
114
|
+
@mf3 = MatFraction.new(2,2)
|
115
|
+
@mf4 = MatFraction.new(2,2)
|
116
|
+
@mdf = MatDispFraction.new(2,2)
|
117
|
+
@m = MatEntero.new(2,2)
|
118
|
+
|
119
|
+
@mf1[0,0] = Matrc::Fraction.new(1, 4)
|
120
|
+
@mf1[0,1] = Matrc::Fraction.new(1, 4)
|
121
|
+
@mf1[1,0] = Matrc::Fraction.new(1, 4)
|
122
|
+
@mf1[1,1] = Matrc::Fraction.new(1, 4)
|
123
|
+
|
124
|
+
@mf2[0,0] = Matrc::Fraction.new(1, 4)
|
125
|
+
@mf2[0,1] = Matrc::Fraction.new(1, 4)
|
126
|
+
@mf2[1,0] = Matrc::Fraction.new(1, 4)
|
127
|
+
@mf2[1,1] = Matrc::Fraction.new(1, 4)
|
128
|
+
|
129
|
+
@m[0,0] = 3
|
130
|
+
@m[0,1] = 3
|
131
|
+
@m[1,0] = 3
|
132
|
+
@m[1,1] = 3
|
133
|
+
|
134
|
+
@mdf[1,0] = Matrc::Fraction.new(1, 5)
|
135
|
+
@mdf[1,1] = Matrc::Fraction.new(1, 5)
|
136
|
+
|
137
|
+
end
|
138
|
+
|
139
|
+
it "Multiplicacion de dos matrices" do
|
140
|
+
@mf3[0,0] = Matrc::Fraction.new(1, 8)
|
141
|
+
@mf3[0,1] = Matrc::Fraction.new(1, 8)
|
142
|
+
@mf3[1,0] = Matrc::Fraction.new(1, 8)
|
143
|
+
@mf3[1,1] = Matrc::Fraction.new(1, 8)
|
144
|
+
|
145
|
+
(@mf1*@mf2).should == @mf3
|
146
|
+
end
|
147
|
+
|
148
|
+
it "Multiplicacion de dos matrices (densa y dispersa)" do
|
149
|
+
@mf3[0,0] = Matrc::Fraction.new(1,20)
|
150
|
+
@mf3[0,1] = Matrc::Fraction.new(1,20)
|
151
|
+
@mf3[1,0] = Matrc::Fraction.new(1,20)
|
152
|
+
@mf3[1,1] = Matrc::Fraction.new(1,20)
|
153
|
+
|
154
|
+
(@mf1*@mdf).should == @mf3
|
155
|
+
end
|
156
|
+
|
157
|
+
it "Resta de dos matrices (densa y dispersa)" do
|
158
|
+
@mf3[0,0] = Matrc::Fraction.new(1,4)
|
159
|
+
@mf3[0,1] = Matrc::Fraction.new(1,4)
|
160
|
+
@mf3[1,0] = Matrc::Fraction.new(1,20)
|
161
|
+
@mf3[1,1] = Matrc::Fraction.new(1,20)
|
162
|
+
|
163
|
+
(@mf1-@mdf).should == @mf3
|
164
|
+
end
|
165
|
+
|
166
|
+
it "Suma de dos matrices diferentes" do
|
167
|
+
@mf4[0,0] = Matrc::Fraction.new(13, 4)
|
168
|
+
@mf4[0,1] = Matrc::Fraction.new(13, 4)
|
169
|
+
@mf4[1,0] = Matrc::Fraction.new(13, 4)
|
170
|
+
@mf4[1,1] = Matrc::Fraction.new(13, 4)
|
171
|
+
|
172
|
+
(@m+@mf1).should == @mf4
|
173
|
+
end
|
174
|
+
|
175
|
+
it "Multiplicacion de dos matrices diferentes" do
|
176
|
+
@mf4[0,0] = Matrc::Fraction.new(3, 2)
|
177
|
+
@mf4[0,1] = Matrc::Fraction.new(3, 2)
|
178
|
+
@mf4[1,0] = Matrc::Fraction.new(3, 2)
|
179
|
+
@mf4[1,1] = Matrc::Fraction.new(3, 2)
|
180
|
+
|
181
|
+
(@m*@mf1).should == @mf4
|
182
|
+
end
|
183
|
+
|
184
|
+
it "Maximo de una matriz de fracciones dispersas" do
|
185
|
+
@mdf.max.should == 0.2
|
186
|
+
end
|
187
|
+
it "Minimo de una matriz de fracciones dispersas" do
|
188
|
+
@mdf.min.should == 0
|
189
|
+
end
|
190
|
+
|
191
|
+
it "Maximo de una matriz de fracciones" do
|
192
|
+
@mf1.max.should == 0.25
|
193
|
+
end
|
194
|
+
it "Minimo de una matriz de fracciones" do
|
195
|
+
@mf1.min.should == 0.25
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
end
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
|
data/test/tc_matrices.rb
ADDED
@@ -0,0 +1,159 @@
|
|
1
|
+
require "matrc"
|
2
|
+
require "test/unit"
|
3
|
+
|
4
|
+
class MatEntero < Matrc::MatrizDensa
|
5
|
+
def zero
|
6
|
+
0
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class MatFraction < Matrc::MatrizDensa
|
11
|
+
def zero
|
12
|
+
Matrc::Fraction.new(0, 1)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
class TestMatrizDensa < Test::Unit::TestCase
|
18
|
+
|
19
|
+
def setup
|
20
|
+
|
21
|
+
@m1 = MatEntero.new(2, 2)
|
22
|
+
@m2 = MatEntero.new(2, 2)
|
23
|
+
@m3 = MatEntero.new(2, 2)
|
24
|
+
|
25
|
+
@mf1 = MatFraction.new(2,2)
|
26
|
+
@mf2 = MatFraction.new(2,2)
|
27
|
+
@mf3 = MatFraction.new(2,2)
|
28
|
+
|
29
|
+
|
30
|
+
@m1[0, 0] = 1
|
31
|
+
@m1[0, 1] = 2
|
32
|
+
@m1[1, 0] = 3
|
33
|
+
@m1[1, 1] = 4
|
34
|
+
|
35
|
+
@m2[0, 0] = 2
|
36
|
+
@m2[0, 1] = 3
|
37
|
+
@m2[1, 0] = 4
|
38
|
+
@m2[1, 1] = 5
|
39
|
+
|
40
|
+
|
41
|
+
@mf1[0, 0] = Matrc::Fraction.new(1, 4)
|
42
|
+
@mf1[0, 1] = Matrc::Fraction.new(1, 4)
|
43
|
+
@mf1[1, 0] = Matrc::Fraction.new(1, 4)
|
44
|
+
@mf1[1, 1] = Matrc::Fraction.new(1, 4)
|
45
|
+
|
46
|
+
@mf2[0, 0] = Matrc::Fraction.new(1, 4)
|
47
|
+
@mf2[0, 1] = Matrc::Fraction.new(1, 4)
|
48
|
+
@mf2[1, 0] = Matrc::Fraction.new(1, 4)
|
49
|
+
@mf2[1, 1] = Matrc::Fraction.new(1, 4)
|
50
|
+
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
def tear_down
|
55
|
+
#nothing
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_simple
|
59
|
+
@m3[0, 0] = 3
|
60
|
+
@m3[0, 1] = 5
|
61
|
+
@m3[1, 0] = 7
|
62
|
+
@m3[1, 1] = 9
|
63
|
+
assert_equal(@m3, @m1+@m2)
|
64
|
+
|
65
|
+
@m3[0, 0] = -1
|
66
|
+
@m3[0, 1] = -1
|
67
|
+
@m3[1, 0] = -1
|
68
|
+
@m3[1, 1] = -1
|
69
|
+
assert_equal(@m3, @m1-@m2)
|
70
|
+
|
71
|
+
@m3[0, 0] = 10
|
72
|
+
@m3[0, 1] = 13
|
73
|
+
@m3[1, 0] = 22
|
74
|
+
@m3[1, 1] = 29
|
75
|
+
assert_equal(@m3, @m1*@m2)
|
76
|
+
|
77
|
+
@mf3[0, 0] = Matrc::Fraction.new(1, 2)
|
78
|
+
@mf3[0, 1] = Matrc::Fraction.new(1, 2)
|
79
|
+
@mf3[1, 0] = Matrc::Fraction.new(1, 2)
|
80
|
+
@mf3[1, 1] = Matrc::Fraction.new(1, 2)
|
81
|
+
assert_equal(@mf3,@mf1+@mf2)
|
82
|
+
|
83
|
+
@mf3[0, 0] = Matrc::Fraction.new(1, 8)
|
84
|
+
@mf3[0, 1] = Matrc::Fraction.new(1, 8)
|
85
|
+
@mf3[1, 0] = Matrc::Fraction.new(1, 8)
|
86
|
+
@mf3[1, 1] = Matrc::Fraction.new(1, 8)
|
87
|
+
assert_equal(@mf3,@mf1*@mf2)
|
88
|
+
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_failure
|
92
|
+
@mf3[0, 0] = Matrc::Fraction.new(1, 2)
|
93
|
+
@mf3[0, 1] = Matrc::Fraction.new(1, 2)
|
94
|
+
@mf3[1, 0] = Matrc::Fraction.new(1, 2)
|
95
|
+
@mf3[1, 1] = Matrc::Fraction.new(1, 2)
|
96
|
+
assert_not_equal(@mf3,@mf1*@mf2)
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_type
|
100
|
+
assert_kind_of(MatEntero, @m1)
|
101
|
+
assert_kind_of(MatFraction, @mf2)
|
102
|
+
end
|
103
|
+
|
104
|
+
|
105
|
+
end
|
106
|
+
|
107
|
+
class MatEnterod < Matrc::MatrizDispersa
|
108
|
+
def zero
|
109
|
+
0
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
class MatFractiond < Matrc::MatrizDispersa
|
114
|
+
def zero
|
115
|
+
Matrc::Fraction.new(0, 1)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
class TestMatrizDispersa < Test::Unit::TestCase
|
120
|
+
|
121
|
+
def setup
|
122
|
+
|
123
|
+
@md = MatEnterod.new(2,2)
|
124
|
+
@mdf = MatFractiond.new(2,2)
|
125
|
+
@mdf1 = MatFractiond.new(2,2)
|
126
|
+
|
127
|
+
@md[0, 0] = 2
|
128
|
+
@md[0, 1] = 0
|
129
|
+
@md[1, 0] = 0
|
130
|
+
@md[1, 1] = 5
|
131
|
+
|
132
|
+
@mdf[0, 0] = Matrc::Fraction.new(0, 1)
|
133
|
+
@mdf[0, 1] = Matrc::Fraction.new(0, 1)
|
134
|
+
@mdf[1, 0] = Matrc::Fraction.new(0, 1)
|
135
|
+
@mdf[1, 1] = Matrc::Fraction.new(1, 5)
|
136
|
+
|
137
|
+
end
|
138
|
+
|
139
|
+
def test_simple
|
140
|
+
@mdf1[0, 0] = Matrc::Fraction.new(2, 1)
|
141
|
+
@mdf1[0, 1] = Matrc::Fraction.new(0, 1)
|
142
|
+
@mdf1[1, 0] = Matrc::Fraction.new(0, 1)
|
143
|
+
@mdf1[1, 1] = Matrc::Fraction.new(26, 5)
|
144
|
+
assert_equal(@mdf1, @mdf+@md)
|
145
|
+
|
146
|
+
end
|
147
|
+
|
148
|
+
def test_failure
|
149
|
+
@mdf1[0, 0] = Matrc::Fraction.new(1, 2)
|
150
|
+
@mdf1[0, 1] = Matrc::Fraction.new(1, 2)
|
151
|
+
@mdf1[1, 0] = Matrc::Fraction.new(1, 2)
|
152
|
+
@mdf1[1, 1] = Matrc::Fraction.new(1, 2)
|
153
|
+
assert_not_equal(@mdf1, @mdf-@md)
|
154
|
+
end
|
155
|
+
|
156
|
+
|
157
|
+
end
|
158
|
+
|
159
|
+
|
metadata
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: matrc
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Raquel Alvarez
|
8
|
+
- Carlota Lázaro
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-11-21 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
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rspec
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: guard-rspec
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: guard-bundler
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ! '>='
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
description: ! ' Gema que usará la funcion de una matriz para la representacion de
|
85
|
+
matrices densas y dispersas '
|
86
|
+
email:
|
87
|
+
- alu0100700435@ull.edu.es
|
88
|
+
- alu0100698862@ull.edu.es
|
89
|
+
executables:
|
90
|
+
- matrc
|
91
|
+
extensions: []
|
92
|
+
extra_rdoc_files: []
|
93
|
+
files:
|
94
|
+
- .gitignore
|
95
|
+
- .travis.yml
|
96
|
+
- Gemfile
|
97
|
+
- Guardfile
|
98
|
+
- LICENSE.txt
|
99
|
+
- README.md
|
100
|
+
- Rakefile
|
101
|
+
- bin/matrc
|
102
|
+
- lib/matrc.rb
|
103
|
+
- lib/matrc/fraction.rb
|
104
|
+
- lib/matrc/matriz.rb
|
105
|
+
- lib/matrc/matriz_densa.rb
|
106
|
+
- lib/matrc/matriz_dispersa.rb
|
107
|
+
- lib/matrc/mihash.rb
|
108
|
+
- lib/matrc/version.rb
|
109
|
+
- matrc.gemspec
|
110
|
+
- spec/matrices_spec.rb
|
111
|
+
- test/tc_matrices.rb
|
112
|
+
homepage: https://github.com/alu0100700435/Practica9.git
|
113
|
+
licenses:
|
114
|
+
- MIT
|
115
|
+
metadata: {}
|
116
|
+
post_install_message:
|
117
|
+
rdoc_options: []
|
118
|
+
require_paths:
|
119
|
+
- lib
|
120
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ! '>='
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - ! '>='
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '0'
|
130
|
+
requirements: []
|
131
|
+
rubyforge_project:
|
132
|
+
rubygems_version: 2.1.5
|
133
|
+
signing_key:
|
134
|
+
specification_version: 4
|
135
|
+
summary: Representación de matrices densas y dispersas
|
136
|
+
test_files:
|
137
|
+
- spec/matrices_spec.rb
|
138
|
+
- test/tc_matrices.rb
|