sparse_matrix 0.0.1 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cefab8ce99739715090611dd4a221806e9e4711f
4
- data.tar.gz: a9a7bf3c925db43e9761f75d39975b838428c3af
3
+ metadata.gz: 2d3bb6f0f82c1dac4b679ee0ecc93f2a45c9f556
4
+ data.tar.gz: fa6fd2a910191e1a4f7ca60989e586b0303e41ce
5
5
  SHA512:
6
- metadata.gz: d040255afe87e2df3197474835d322f126d055fb11e429bbc33b6c25762a28ce6ac91dde6a2f515c62e2a9ef72aeec80122567f9cbec941b166a980f2b8273b3
7
- data.tar.gz: 7649db8cc449535adff9f22e2916794ab88b7f124e110bbedc64dfab40d3b8c3c149c1f3f1db5bc8220b2f056824e76e97461dc30555f4340ac91a921633af43
6
+ metadata.gz: edd4515e564e016b8f0b350cea8a5eef68e52d7c32147db28ccfbce08a3742f0673c893e07966e321528a316ec412001832b5eee5327b45c3ee6d064f542521b
7
+ data.tar.gz: 06093fe0182f748338cdf1cc4d1b647ad15aa1d30a866d7afcaf0c5d5d90fcf73d98d507c3b07c0f7936259f24a0fe15515c209573fce7388fa552907439c41a
data/README.md CHANGED
@@ -1,27 +1,33 @@
1
- # SparseMatrix
1
+ # SparseMatrix [![Build Status](https://travis-ci.org/kevinrobayna/SparseMatrix.png?branch=master)](https://travis-ci.org/kevinrobayna/SparseMatrix)
2
2
 
3
3
  Build a sparce matrix from dense matrix
4
4
 
5
+ ##RubyGems
6
+ https://rubygems.org/gems/sparse_matrix
7
+
5
8
  ## Tree
6
9
 
10
+ .
7
11
  ├── Gemfile
12
+ ├── Gemfile.lock
13
+ ├── Guardfile
14
+ ├── LICENSE.txt
15
+ ├── README.md
16
+ ├── Rakefile
8
17
  ├── input
18
+ │   ├── matrix_a
19
+ │   ├── matrix_b
20
+ │   └── null
9
21
  ├── lib
10
22
  │   ├── sparse_matrix
11
23
  │   │   └── version.rb
12
24
  │   └── sparse_matrix.rb
13
- ├── LICENSE.txt
14
- ├── Rakefile
15
- ├── README.md
25
+ ├── pkg
26
+ │   └── sparse_matrix-0.0.1.gem
16
27
  ├── sparse_matrix.gemspec
17
- ├── spec
18
- │   └── math
19
- │   ├── matrix_spec.rb
20
- │   └── sparsematrix_spec.rb
21
- └── test
22
- └── math
23
- ├── matrix_test.rb
24
- └── sparsematrix_test.rb
28
+ └── spec
29
+    ├── spec_helper.rb
30
+    └── sparsematrix_spec.rb
25
31
 
26
32
  ## Installation
27
33
  Add this line to your application's Gemfile:
data/Rakefile CHANGED
@@ -7,6 +7,5 @@ RSpec::Core::RakeTask.new
7
7
  task :default => :spec
8
8
  desc "Ejecucion de pruebas rspec"
9
9
  task :spec do
10
- sh "rspec --color --format documentation -Ilib -Ispec spec/math/matrix_spec.rb"
11
- sh "rspec --color --format documentation -Ilib -Ispec spec/math/sparsematrix_spec.rb"
10
+ sh "rspec --color --format documentation -Ilib -Ispec spec/sparsematrix_spec.rb"
12
11
  end
data/lib/sparse_matrix.rb CHANGED
@@ -1,81 +1,209 @@
1
1
  require "sparse_matrix/version"
2
2
 
3
- class SparseMatrix
4
- @sparse_mat
5
-
6
- def initialize(b=0)
7
- @sparse_mat=b
8
- end
9
-
10
- def to_s
11
-
12
- end
13
-
14
- end
15
-
16
- class Matrix_T
17
- @mat
18
- @filename
3
+ module SparseMatrix
4
+ class AbstractMatrix
5
+
6
+ def intialize ()
7
+
8
+ end
19
9
 
20
- def initialize(fn="./input/null")
21
- @filename=fn
22
- @mat=0
23
10
  end
24
11
 
25
- attr_accessor :mat
12
+ class SparseMatrix < AbstractMatrix
26
13
 
27
- def read_matrix
28
- text = File.open(@filename).read
29
- a = text.split(/\n\n+/)
30
- a = text.split(/\n/)
31
- @mat = to_m(a)
32
- end
33
14
 
34
- def mapmap(a)
35
- a.map { |r|
36
- r.map { |e|
37
- yield e
38
- }
39
- }
40
15
  end
41
16
 
42
- def to_m(a)
43
- a = a.map { |r| r.split(/\s+/) }
44
- a = mapmap(a) { |x| x.to_f }
45
- end
46
17
 
47
- def to_s()
48
- s="| "
49
- for i in (0... @mat.length)
50
- for j in (0... @mat.length)
51
- if j==0
52
- s += "{ "
18
+ class DenseMatrix < AbstractMatrix
19
+ @mat
20
+ @filename
21
+ def initialize(fn="./input/null")
22
+ @filename=fn
23
+ @mat = 0
24
+ end
25
+
26
+ attr_accessor :mat
27
+
28
+ def read_matrix
29
+ text = File.open(@filename).read
30
+ a = text.split(/\n\n+/)
31
+ a = text.split(/\n/)
32
+ @mat = to_m(a)
33
+ end
34
+
35
+ def mapmap(a)
36
+ a.map { |r|
37
+ r.map { |e|
38
+ yield e
39
+ }
40
+ }
41
+ end
42
+
43
+ def to_m(a)
44
+ a = a.map { |r| r.split(/\s+/) }
45
+ a = mapmap(a) { |x| x.to_f }
46
+ end
47
+
48
+ def to_s()
49
+ s="| "
50
+ for i in (0... @mat.length)
51
+ for j in (0... @mat.length)
52
+ if j==0
53
+ s += "{ "
54
+ end
55
+ s += "#{@mat[i][j]}\t"
56
+ if j == @mat.length-1
57
+ s += " } , "
58
+ end
53
59
  end
54
- s += "#{@mat[i][j]}\t"
55
- if j == @mat.length-1
56
- s += " } , "
60
+ end
61
+ s += "|"
62
+ end
63
+
64
+ def print_matrix()
65
+ printf "| "
66
+ for i in (0... @mat.length)
67
+ for j in (0... @mat.length)
68
+ if j==0
69
+ printf "{ "
70
+ end
71
+ printf "#{@mat[i][j]}\t"
72
+ if j == @mat.length-1
73
+ printf " } ,"
74
+ end
57
75
  end
58
76
  end
77
+ printf "|"
59
78
  end
60
- s += "|"
61
- end
62
-
63
- def print()
64
- printf "| "
65
- for i in (0... @mat.length)
66
- for j in (0... @mat.length)
67
- if j==0
68
- printf "{ "
79
+
80
+ def +(b)
81
+ c = DenseMatrix.new()
82
+ c.read_matrix
83
+ for i in (0...@mat.length)
84
+ for j in(0...@mat.length)
85
+ c.mat[i][j] = self.mat[i][j]+b.mat[i][j]
69
86
  end
70
- printf "#{@mat[i][j]}\t"
71
- if j == @mat.length-1
72
- printf " } , "
87
+ end
88
+ c
89
+ end
90
+
91
+ def *(b)
92
+ c=DenseMatrix.new()
93
+ c.read_matrix
94
+ for i in(0...@mat.length)
95
+ for j in(0...@mat.length)
96
+ c.mat[i][j]=0
97
+ for k in (0...@mat.length)
98
+ c.mat[i][j] += @mat[i][k]*b.mat[k][j]
99
+ end
73
100
  end
74
101
  end
102
+ c
75
103
  end
76
- printf "|"
77
104
  end
78
105
 
79
-
80
-
106
+ class Fraction
107
+ include Comparable
108
+ def initialize (*args)
109
+ if args.size == 2
110
+ c = gcd(args[0],args[1])
111
+ @num_ = (args[0]/c)
112
+ @den_ = (args[1]/c)
113
+ else
114
+ @num_ = args[0]
115
+ @den_ = 1
116
+ end
117
+ end
118
+ attr_accessor :num_,:den_
119
+
120
+ def to_s
121
+ "#{@num_}/#{@den_}"
122
+ end
123
+ def to_f
124
+ @num_.to_f/@den_.to_f
125
+ end
126
+
127
+ def ==(b)
128
+ return @num_.eql?(b.num_) && @den_.eql?(b.den_)
129
+ end
130
+
131
+ def abs
132
+ c = @num_.to_f/@den_.to_f
133
+ return c.abs
134
+ end
135
+
136
+ def reciprocal
137
+ f=Fraction.new
138
+ f.num_=@den_
139
+ f.den_ = @num_
140
+ f
141
+ end
142
+
143
+ def -@
144
+ Fraction.new(-@num_,@den_)
145
+ end
146
+
147
+ def +(b)
148
+ r=Fraction.new
149
+ if (@den_==b.den_)
150
+ r.num_ = @num_ + b.num_
151
+ r.den_ = @den_
152
+ else
153
+ r.num_ = @num_ * b.den_ + b.num_ * @den_
154
+ r.den_ = @den_ * b.den_
155
+ end
156
+ r.num_,r.den_ = minimiza(r.num_,r.den_)
157
+ return r
158
+ end
159
+
160
+ def -(b)
161
+ r =Fraction.new
162
+ if (@den_ == b.den_)
163
+ r.num_=@num_- b.num_
164
+ r.den_=@den_
165
+ else
166
+ r.num_=@num_ * b.den_ - b.num_ * @den_
167
+ r.den_ = @den_ * b.den_
168
+ end
169
+ r.num_,r.den_ = minimiza(r.num_,r.den_)
170
+ r
171
+ end
172
+
173
+ def *(b)
174
+ r =Fraction.new
175
+ r.num_=@num_ * b.num_
176
+ r.den_=@den_ * b.den_
177
+ r.num_,r.den_ = minimiza(r.num_,r.den_)
178
+ return r
179
+ end
180
+
181
+ def /(b)
182
+ r =Fraction.new
183
+ r.num_=@num_ / b.num_
184
+ r.den_=@den_ * b.den_
185
+ r.num_,r.den_ = minimiza(r.num_,r.den_)
186
+ r
187
+ end
188
+
189
+ def <=>(b)
190
+ self.to_f <=> b.to_f
191
+ end
192
+
193
+ def minimiza(x,y)
194
+ d = gcd(x,y)
195
+ x = x/d
196
+ y = y/d
197
+ return x,y
198
+ end
199
+ end
200
+
201
+ def gcd(u, v)
202
+ u, v = u.abs, v.abs
203
+ while v != 0
204
+ u, v = v, u % v
205
+ end
206
+ u
207
+ end
208
+
81
209
  end
@@ -1,3 +1,3 @@
1
1
  module SparseMatrix
2
- VERSION = "0.0.1"
2
+ VERSION = "0.3.1"
3
3
  end
@@ -0,0 +1,136 @@
1
+ require 'spec_helper'
2
+ include SparseMatrix
3
+
4
+ describe SparseMatrix do
5
+
6
+ describe AbstractMatrix do
7
+
8
+ end
9
+
10
+ describe SparseMatrix do
11
+
12
+ end
13
+
14
+
15
+ describe DenseMatrix do
16
+ before :all do
17
+ DMA = DenseMatrix.new("input/matrix_a")
18
+ DMB = DenseMatrix.new("input/matrix_b")
19
+ end
20
+
21
+ it 'existe una clase Matrix' do
22
+ DMA.instance_of?(DenseMatrix) == true
23
+ end
24
+
25
+ it 'No se ha cargado los datos del fichero al objeto Matrix_T' do
26
+ DMA.mat.should eq(0)
27
+ end
28
+
29
+ it 'Se han cargado los datos al objeto A' do
30
+ DMA.read_matrix
31
+ DMA.mat.should be_kind_of(Array)
32
+ end
33
+
34
+ it 'Se han cargado los datos al objeto B' do
35
+ DMB.read_matrix
36
+ DMB.mat.should be_kind_of(Array)
37
+ end
38
+
39
+ it 'Existe metodo imprimir matrix?' do
40
+ DMA.should respond_to("print_matrix")
41
+ end
42
+
43
+ it 'Se muetra la matriz correctamente?' do
44
+ DMA.print_matrix.should eq(nil)
45
+ end
46
+
47
+ it 'Se convierte la matriz correctamente?' do
48
+ DMA.to_s.should eq("| { 1.0\t2.0\t } , { 4.0\t5.0\t } , |")
49
+ end
50
+
51
+ it 'suma de matrices, existe metodo?' do
52
+ DMA.should respond_to("+")
53
+ end
54
+
55
+ it 'suma de matrices, suma correctamente?' do
56
+ (DMA+DMB).to_s.should eq("| { 2.0\t4.0\t } , { 7.0\t9.0\t } , |")
57
+ end
58
+
59
+ it 'multiplicar matrices, existe metodo?' do
60
+ DMA.should respond_to("*")
61
+ end
62
+
63
+ it 'multiplicar matrices, multiplica correctamente?' do
64
+ (DMA*DMB).to_s.should eq("| { 7.0\t10.0\t } , { 19.0\t28.0\t } , |")
65
+ end
66
+ end
67
+
68
+ describe Fraction do
69
+ before :all do
70
+ FA = Fraction.new(1,1)
71
+ FB = Fraction.new(1,4)
72
+ end
73
+
74
+ describe" basicas" do
75
+ it "Existe un numerador" do
76
+ FA.num_.should == 1
77
+ end
78
+ it "Existe un denominador" do
79
+ FA.den_.should == 1
80
+ end
81
+ it "se debe mostrar en consola de la forma a/b" do
82
+ FA.to_s.should == "1/1"
83
+ end
84
+ it "se debe mostrar en consola la fraccion en formato flotante" do
85
+ FA.to_f.should == 1.0
86
+ end
87
+ end
88
+
89
+ describe "Unarias" do
90
+ it "Se debe comparar si dos fracciones son iguales con ==" do
91
+ (FA==FB).should == false
92
+ end
93
+
94
+ it "Se debe calcular el valor absoluto de una fraccion con el meto abs" do
95
+ FA.abs.should == 1
96
+ end
97
+ it "Se debe calcular el reciproco" do
98
+ (FB.reciprocal).to_s.should == "4/1"
99
+ end
100
+ it "Calcular el opuesto de la fraccion con -" do
101
+ (-FA).to_s.should == "-1/1"
102
+ end
103
+ end
104
+
105
+ describe "aritmeticas" do
106
+ it "Se debe sumar dos fracciones con +" do
107
+ (FA+FB).to_s.should == "5/4"
108
+ end
109
+ it "Se debe restar dos fracciones con -" do
110
+ (FA-FB).to_s.should == "3/4"
111
+ end
112
+ it "Se debe multiplicar dos fracciones con *" do
113
+ (FA*FB).to_s.should == "1/4"
114
+ end
115
+ it "Se debe dividir dos fracciones con /" do
116
+ (FA/FB).to_s.should == "1/4"
117
+ end
118
+ end
119
+
120
+ describe "Comparacion" do
121
+ it "Se debe de poder comprobar si una fraccion es menor que otra" do
122
+ (FB<FA).should == true
123
+ end
124
+ it "Se debe de poder comprobar si una fraccion es mayor que otra" do
125
+ (FA>FB).should == true
126
+ end
127
+ it "Se debe de poder comprobar si una fraccion es menor o igual que otra" do
128
+ (FB<=FA).should == true
129
+ end
130
+ it "Se debe de poder comprobar si una fraccion es mayor o igual que otra" do
131
+ (FA>=FB).should == true
132
+ end
133
+ end
134
+ end
135
+
136
+ end
@@ -0,0 +1,8 @@
1
+ $:.unshift File.dirname(__FILE__) + '/../lib'
2
+ require 'sparse_matrix'
3
+
4
+ RSpec.configure do |config|
5
+ config.treat_symbols_as_metadata_keys_with_true_values = true
6
+ config.run_all_when_everything_filtered = true
7
+ config.filter_run :focus
8
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sparse_matrix
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - KevinRobayna
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-12 00:00:00.000000000 Z
11
+ date: 2013-11-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -111,14 +111,11 @@ files:
111
111
  - input/matrix_a
112
112
  - input/matrix_b
113
113
  - input/null
114
- - lib/matrix.rb
115
114
  - lib/sparse_matrix.rb
116
115
  - lib/sparse_matrix/version.rb
117
116
  - sparse_matrix.gemspec
118
- - spec/math/matrix_spec.rb
119
- - spec/math/sparsematrix_spec.rb
120
- - test/math/matrix_test.rb
121
- - test/math/sparsematrix_test.rb
117
+ - spec/sparsematrix_spec.rb
118
+ - spec/spec_helper.rb
122
119
  homepage: https://github.com/kevinrobayna/SparseMatrix
123
120
  licenses:
124
121
  - MIT
@@ -144,7 +141,5 @@ signing_key:
144
141
  specification_version: 4
145
142
  summary: sparse matrix
146
143
  test_files:
147
- - spec/math/matrix_spec.rb
148
- - spec/math/sparsematrix_spec.rb
149
- - test/math/matrix_test.rb
150
- - test/math/sparsematrix_test.rb
144
+ - spec/sparsematrix_spec.rb
145
+ - spec/spec_helper.rb
data/lib/matrix.rb DELETED
@@ -1,66 +0,0 @@
1
- class Matrix_T
2
- @mat
3
- @filename
4
-
5
- def initialize(fn="./input/null")
6
- @filename=fn
7
- @mat=0
8
- end
9
-
10
- attr_accessor :mat
11
-
12
- def read_matrix
13
- text = File.open(@filename).read
14
- a = text.split(/\n\n+/)
15
- a = text.split(/\n/)
16
- @mat = to_m(a)
17
- end
18
-
19
- def mapmap(a)
20
- a.map { |r|
21
- r.map { |e|
22
- yield e
23
- }
24
- }
25
- end
26
-
27
- def to_m(a)
28
- a = a.map { |r| r.split(/\s+/) }
29
- a = mapmap(a) { |x| x.to_f }
30
- end
31
-
32
- def to_s()
33
- s="| "
34
- for i in (0... @mat.length)
35
- for j in (0... @mat.length)
36
- if j==0
37
- s += "{ "
38
- end
39
- s += "#{@mat[i][j]}\t"
40
- if j == @mat.length-1
41
- s += " } , "
42
- end
43
- end
44
- end
45
- s += "|"
46
- end
47
-
48
- def print()
49
- printf "| "
50
- for i in (0... @mat.length)
51
- for j in (0... @mat.length)
52
- if j==0
53
- printf "{ "
54
- end
55
- printf "#{@mat[i][j]}\t"
56
- if j == @mat.length-1
57
- printf " } , "
58
- end
59
- end
60
- end
61
- printf "|"
62
- end
63
-
64
-
65
-
66
- end
@@ -1,56 +0,0 @@
1
- require 'sparse_matrix'
2
-
3
- describe Matrix_T do
4
- before :all do
5
- A = Matrix_T.new("input/matrix_a")
6
- end
7
- describe "Basicas" do
8
- it 'Existe una clase Matrix?' do
9
- A.instance_of?(Matrix_T) == true
10
- end
11
-
12
- it 'Se puede acceder a la matriz?' do
13
- A.mat.should == 0
14
- end
15
- end
16
-
17
- describe "Construyendo Matriz" do
18
- it 'Existe metodo para leer matrices (READ-MATRIX)?' do
19
- A.should respond_to("read_matrix")
20
- end
21
-
22
- it 'Existe sub-metodo para leer matrices (MAPMAP)?' do
23
- A.should respond_to("mapmap")
24
- end
25
-
26
- it 'Existe sub-metodo para leer matrices (TO_M)?' do
27
- A.should respond_to("to_m")
28
- end
29
-
30
- it 'Se han cargado los datos al objeto A' do
31
- A.read_matrix
32
- A.mat.should be_kind_of(Array)
33
- end
34
-
35
- end
36
-
37
- describe "Mostrando Matrices" do
38
- it 'Existe metodo to_s para devolver una matriz?' do
39
- A.should respond_to("to_s")
40
- end
41
-
42
- it 'Existe metodo print para mostrar una matriz?' do
43
- A.should respond_to("print")
44
- end
45
-
46
- it 'El Metodo to_s devuelve correctamente la matriz?' do
47
- A.to_s.should eq("| { 1.0\t2.0\t } , { 4.0\t5.0\t } , |")
48
- end
49
-
50
- it 'El metodo print imprime correctamente la matriz?' do
51
- A.print.should eq(nil)
52
- end
53
- end
54
-
55
-
56
- end
@@ -1,16 +0,0 @@
1
- require 'sparse_matrix.rb'
2
-
3
- describe SparseMatrix do
4
-
5
- before :all do
6
- A = SparseMatrix.new()
7
- end
8
-
9
- describe "Basicas" do
10
-
11
- it 'Existe una clase SparseMatrix?' do
12
- A.instance_of?(SparseMatrix) == true
13
- end
14
-
15
- end
16
- end
File without changes
File without changes