matrix_extensions 0.0.1 → 0.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 88ac601a61d4ccb18628977f97571ed3855545b1
4
- data.tar.gz: 2c6efe2d7d213daebe2890c8ed1f86340703244d
3
+ metadata.gz: 0888299dc382dbaf662109214f80784ce36def63
4
+ data.tar.gz: a9b398b17dafb6bfb75b782e99e23619daf83ec8
5
5
  SHA512:
6
- metadata.gz: 24ff51ffd991091061e34dc8786de49c360fe38208760e5d2f85d5589b95eb3590584f85235086b7a8174665338999078f6529afdd3ba2488ff07efdadf84746
7
- data.tar.gz: 86e2592e5c1ceb6bea3f823d05af766614de133cc6dc04c3b708c557977f3bfc464f68d5d116968e6a0a0f6cc9896118ea0dfa7e3b2ce1961a6398b028fc3fa8
6
+ metadata.gz: f95b5b0497717de92579bf5f51ed34d96470825f6dea7b151f42a75add60e288cfbb95238c501c4d789829c0a06c91ab9cf461733475c20f44ecdb8ba386c757
7
+ data.tar.gz: 3d010969030d91e918d5a63e1607d6925d55faa5cc4c24e6481fc5d434b18d3b207954493fdcf7e59b4556483e3c6308dd35548848667780540ee2f5f5d24910
data/README.md CHANGED
@@ -25,8 +25,8 @@ Element-wise multiplication:
25
25
  ```ruby
26
26
  require 'matrix_extensions' # if not loaded automatically
27
27
 
28
- MatrixExtended[ [1,2,3], [4,5,6] ].element_multiplication MatrixExtended[ [2,3,4], [5,6,7] ]
29
- => MatrixExtended[[2, 6, 12], [20, 30, 42]]
28
+ Matrix[ [1,2,3], [4,5,6] ].element_multiplication Matrix[ [2,3,4], [5,6,7] ]
29
+ => Matrix[[2, 6, 12], [20, 30, 42]]
30
30
  ```
31
31
 
32
32
  Element-wise division:
@@ -34,8 +34,8 @@ Element-wise division:
34
34
  ```ruby
35
35
  require 'matrix_extensions' # if not loaded automatically
36
36
 
37
- MatrixExtended[ [2,4,6], [2.0,10,20] ].element_division MatrixExtended[ [2,2,6], [4,5,10] ]
38
- => MatrixExtended[[1, 2, 1], [0.5, 2, 2]]
37
+ Matrix[ [2,4,6], [2.0,10,20] ].element_division Matrix[ [2,2,6], [4,5,10] ]
38
+ => Matrix[[1, 2, 1], [0.5, 2, 2]]
39
39
  ```
40
40
 
41
41
  Element-wise exponentiation:
@@ -43,8 +43,8 @@ Element-wise exponentiation:
43
43
  ```ruby
44
44
  require 'matrix_extensions' # if not loaded automatically
45
45
 
46
- MatrixExtended[ [2,4], [1,4] ].element_exponentiation MatrixExtended[ [2,4], [1,4]] ]
47
- => MatrixExtended[[4, 256], [1, 256]]
46
+ Matrix[ [2,4], [1,4] ].element_exponentiation Matrix[ [2,4], [1,4]] ]
47
+ => Matrix[[4, 256], [1, 256]]
48
48
  ```
49
49
 
50
50
  Prefilled matrix with zeros or ones:
@@ -52,11 +52,11 @@ Prefilled matrix with zeros or ones:
52
52
  ```ruby
53
53
  require 'matrix_extensions' # if not loaded automatically
54
54
 
55
- MatrixExtended.zeros(2,4)
56
- => MatrixExtended[[0, 0, 0, 0], [0, 0, 0, 0]]
55
+ Matrix.zeros(2,4)
56
+ => Matrix[[0, 0, 0, 0], [0, 0, 0, 0]]
57
57
 
58
- MatrixExtended.ones(2,2)
59
- => MatrixExtended[[1, 1], [1, 1]]
58
+ Matrix.ones(2,2)
59
+ => Matrix[[1, 1], [1, 1]]
60
60
  ```
61
61
 
62
62
  Concatenating matrices and vectors horizontally:
@@ -70,8 +70,8 @@ m1 = Matrix[ [1,2,3], [4,5,6] ]
70
70
  m2 = Matrix[ [2,3,4], [5,6,7] ]
71
71
  v = Vector[ 3,4 ]
72
72
 
73
- MatrixExtended.hconcat(m1, m2, v)
74
- => MatrixExtended[[1, 2, 3, 2, 3, 4, 3], [4, 5, 6, 5, 6, 7, 4]]
73
+ Matrix.hconcat(m1, m2, v)
74
+ => Matrix[[1, 2, 3, 2, 3, 4, 3], [4, 5, 6, 5, 6, 7, 4]]
75
75
  ```
76
76
 
77
77
  Concatenating matrices and vectors vertically:
@@ -85,8 +85,8 @@ m1 = Matrix[ [1,2,3], [4,5,6] ]
85
85
  m2 = Matrix[ [2,3,4], [5,6,7] ]
86
86
  v = Vector[ 3,4,5 ]
87
87
 
88
- MatrixExtended.vconcat(m1, m2, v)
89
- => MatrixExtended[[1, 2, 3], [4, 5, 6], [2, 3, 4], [5, 6, 7], [3, 4, 5]]
88
+ Matrix.vconcat(m1, m2, v)
89
+ => Matrix[[1, 2, 3], [4, 5, 6], [2, 3, 4], [5, 6, 7], [3, 4, 5]]
90
90
  ```
91
91
 
92
92
  ## Contributing
@@ -3,35 +3,25 @@ require 'matrix'
3
3
 
4
4
  # An extension to the Ruby Matrix class.
5
5
  # @author Michael Imstepf
6
- class MatrixExtended < Matrix
7
- # Converts a Matrix to an MatrixExtended.
8
- # @param m [Matrix] matrix
9
- # @return [MatrixExtended] matrix
10
- # @raise [TypeError] if matrices are not of type Matrix
11
- def self.convert_to_matrix_extended(m)
12
- raise TypeError, "#{m.class} is not a Matrix" unless m.is_a?(Matrix)
13
-
14
- self.columns(m.column_vectors)
15
- end
16
-
6
+ class Matrix
17
7
  # Matrix prefilled with zeros.
18
- # @param m [MatrixExtended] matrix
19
- # @return [MatrixExtended] matrix
8
+ # @param m [Matrix] matrix
9
+ # @return [Matrix] matrix
20
10
  def self.zeros(rows = 1, columns = 1)
21
- MatrixExtended.build(rows, columns) { 0 }
11
+ Matrix.build(rows, columns) { 0 }
22
12
  end
23
13
  self.singleton_class.send(:alias_method, :zeroes, :zeros)
24
14
 
25
15
  # Matrix prefilled with ones.
26
- # @param m [MatrixExtended] matrix
27
- # @return [MatrixExtended] matrix
16
+ # @param m [Matrix] matrix
17
+ # @return [Matrix] matrix
28
18
  def self.ones(rows = 1, columns = 1)
29
- MatrixExtended.build(rows, columns) { 1 }
19
+ Matrix.build(rows, columns) { 1 }
30
20
  end
31
21
 
32
22
  # Concatenates two matrices horizontally (resulting in more columns).
33
- # @param *matrices [MatrixExtended] matrices
34
- # @return [MatrixExtended] concatenated matrix
23
+ # @param *matrices [Matrix] matrices
24
+ # @return [Matrix] concatenated matrix
35
25
  # @raise [ErrDimensionMismatch] if dimensions don't match
36
26
  # @raise [TypeError] if matrices are not of type Matrix or Vector
37
27
  def self.hconcat(*matrices)
@@ -57,8 +47,8 @@ class MatrixExtended < Matrix
57
47
  end
58
48
 
59
49
  # Concatenates two matrices vertically (resulting in more rows).
60
- # @param *matrices [MatrixExtended] matrices
61
- # @return [MatrixExtended] concatenated matrix
50
+ # @param *matrices [Matrix] matrices
51
+ # @return [Matrix] concatenated matrix
62
52
  # @raise [ErrDimensionMismatch] if dimensions don't match
63
53
  # @raise [TypeError] if matrices are not of type Matrix or Vector
64
54
  def self.vconcat(*matrices)
@@ -84,8 +74,8 @@ class MatrixExtended < Matrix
84
74
  end
85
75
 
86
76
  # Element-wise division.
87
- # @param m [MatrixExtended] matrix
88
- # @return [MatrixExtended] matrix
77
+ # @param m [Matrix] matrix
78
+ # @return [Matrix] matrix
89
79
  # @raise [ErrDimensionMismatch] if dimensions don't match
90
80
  def element_division(m)
91
81
  case m
@@ -115,8 +105,8 @@ class MatrixExtended < Matrix
115
105
  end
116
106
 
117
107
  # Element-wise multiplication.
118
- # @param m [MatrixExtended] matrix
119
- # @return [MatrixExtended] matrix
108
+ # @param m [Matrix] matrix
109
+ # @return [Matrix] matrix
120
110
  # @raise [ErrDimensionMismatch] if dimensions don't match
121
111
  def element_multiplication(m)
122
112
  case m
@@ -146,8 +136,8 @@ class MatrixExtended < Matrix
146
136
  end
147
137
 
148
138
  # Element-wise exponentiation.
149
- # @param m [MatrixExtended] matrix
150
- # @return [MatrixExtended] matrix
139
+ # @param m [Matrix] matrix
140
+ # @return [Matrix] matrix
151
141
  # @raise [ErrDimensionMismatch] if dimensions don't match
152
142
  def element_exponentiation(m)
153
143
  case m
@@ -185,7 +175,7 @@ class MatrixExtended < Matrix
185
175
  # Convert vector to matrix for arithmetic operations.
186
176
  # @param vector [Vector] vector
187
177
  # @param dimension [Symbol] :row or :column
188
- # @return [MatrixExtended] matrix
178
+ # @return [Matrix] matrix
189
179
  # @raise [TypeError] if vector ist not of type Vector
190
180
  def self.convert_vector_to_matrix(v, dimension)
191
181
  raise TypeError, "#{v.class} is not a Vector" unless v.is_a? Vector
@@ -1,3 +1,3 @@
1
1
  module MatrixExtensions
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,205 @@
1
+ require 'spec_helper'
2
+
3
+ describe Matrix do
4
+ m23a = Matrix[ [1,2,3], [4,5,6] ] # 2 rows, 3 columns
5
+ m23b = Matrix[ [2,3,4], [5,6,7] ] # 2 rows, 3 columns
6
+ m22 = Matrix[ [1,2], [3,4] ] # 2 rows, 2 columns
7
+ m12 = Matrix[ [1,2] ] # 1 row, 2 columns
8
+ m21 = Matrix[ [1], [2] ] # 2 rows, 1 column
9
+ v = Vector[ 1,2 ]
10
+
11
+ describe '#element_division' do
12
+ context 'when dimensions do not match' do
13
+ it 'raises an exception' do
14
+ expect {m23a.element_division m22}.to raise_exception(Matrix::ErrDimensionMismatch)
15
+ end
16
+ end
17
+
18
+ context 'when dimensions match' do
19
+ it 'returns correct Matrix' do
20
+ expect(m23a.element_division m23b).to eq Matrix[ [1/2,2/3,3/4], [4/5,5/6,6/7] ]
21
+ expect(m23b.element_division m23a).to eq Matrix[ [2,3/2,4/3], [5/4,6/5,7/6] ]
22
+ end
23
+ end
24
+
25
+ context 'when one operator is numeric' do
26
+ it 'returns correct Matrix' do
27
+ expect(m23a.element_division 2).to eq Matrix[ [1/2,2/2,3/2], [4/2,5/2,6/2] ]
28
+ end
29
+ end
30
+
31
+ context 'when one operator is a vector' do
32
+ context 'when dimensions do not match' do
33
+ it 'raises an exception' do
34
+ expect {m23a.element_division v}.to raise_exception(Matrix::ErrDimensionMismatch)
35
+ end
36
+ end
37
+
38
+ context 'when dimensions match' do
39
+ it 'returns correct Matrix' do
40
+ expect(m21.element_division v).to eq Matrix[ [1],[1] ]
41
+ expect(m12.element_division v).to eq Matrix[ [1,1] ]
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ describe '#element_multiplication' do
48
+ context 'when dimensions do not match' do
49
+ it 'raises an exception' do
50
+ expect {m23a.element_multiplication m22}.to raise_exception(Matrix::ErrDimensionMismatch)
51
+ end
52
+ end
53
+
54
+ context 'when dimensions match' do
55
+ it 'returns correct Matrix' do
56
+ expect(m23a.element_multiplication m23b).to eq Matrix[ [1*2,2*3,3*4], [4*5,5*6,6*7] ]
57
+ expect(m23b.element_multiplication m23a).to eq Matrix[ [2,3*2,4*3], [5*4,6*5,7*6] ]
58
+ end
59
+ end
60
+
61
+ context 'when one operator is numeric' do
62
+ it 'returns correct Matrix' do
63
+ expect(m23a.element_multiplication 2).to eq Matrix[ [1*2,2*2,3*2], [4*2,5*2,6*2] ]
64
+ end
65
+ end
66
+
67
+ context 'when one operator is a vector' do
68
+ context 'when dimensions do not match' do
69
+ it 'raises an exception' do
70
+ expect {m23a.element_multiplication v}.to raise_exception(Matrix::ErrDimensionMismatch)
71
+ end
72
+ end
73
+
74
+ context 'when dimensions match' do
75
+ it 'returns correct Matrix' do
76
+ expect(m21.element_multiplication v).to eq Matrix[ [1],[4] ]
77
+ expect(m12.element_multiplication v).to eq Matrix[ [1,4] ]
78
+ end
79
+ end
80
+ end
81
+ end
82
+
83
+ describe '#element_exponentiation' do
84
+ context 'when dimensions do not match' do
85
+ it 'raises an exception' do
86
+ expect {m23a.element_exponentiation m22}.to raise_exception(Matrix::ErrDimensionMismatch)
87
+ end
88
+ end
89
+
90
+ context 'when dimensions match' do
91
+ it 'returns correct Matrix' do
92
+ expect(m23a.element_exponentiation m23b).to eq Matrix[ [1,2**3,3**4], [4**5,5**6,6**7] ]
93
+ expect(m23b.element_exponentiation m23a).to eq Matrix[ [2,3**2,4**3], [5**4,6**5,7**6] ]
94
+ end
95
+ end
96
+
97
+ context 'when one operator is numeric' do
98
+ it 'returns correct Matrix' do
99
+ expect(m23a.element_exponentiation 2).to eq Matrix[ [1**2,2**2,3**2], [4**2,5**2,6**2] ]
100
+ end
101
+ end
102
+
103
+ context 'when one operator is a vector' do
104
+ context 'when dimensions do not match' do
105
+ it 'raises an exception' do
106
+ expect {m23a.element_exponentiation v}.to raise_exception(Matrix::ErrDimensionMismatch)
107
+ end
108
+ end
109
+
110
+ context 'when dimensions match' do
111
+ it 'returns correct Matrix' do
112
+ expect(m21.element_exponentiation v).to eq Matrix[ [1],[4] ]
113
+ expect(m12.element_exponentiation v).to eq Matrix[ [1,4] ]
114
+ end
115
+ end
116
+ end
117
+ end
118
+
119
+ describe '#ones' do
120
+ it 'creates matrix with ones' do
121
+ expect(Matrix.ones(2, 3)). to eq Matrix[ [1,1,1], [1,1,1] ]
122
+ end
123
+ end
124
+
125
+ describe '#zeros' do
126
+ it 'creates matrix with zeros' do
127
+ expect(Matrix.zeros(2, 3)). to eq Matrix[ [0,0,0], [0,0,0] ]
128
+ end
129
+ end
130
+
131
+ describe '#zeroes' do
132
+ it 'creates matrix with zeros' do
133
+ expect(Matrix.zeros(2, 3)). to eq Matrix[ [0,0,0], [0,0,0] ]
134
+ end
135
+ end
136
+
137
+ describe '#hconcat' do
138
+ context 'when input is not a matrix or vector' do
139
+ it 'raises an exception' do
140
+ expect {Matrix.hconcat(m23a, m12, 5)}.to raise_exception(TypeError)
141
+ end
142
+ end
143
+
144
+ context 'when row dimensions do not match' do
145
+ it 'raises an exception' do
146
+ expect {Matrix.hconcat(m23a, m12)}.to raise_exception(Matrix::ErrDimensionMismatch)
147
+ end
148
+ end
149
+
150
+ context 'when row dimensions match' do
151
+ it 'concatenates' do
152
+ expect(Matrix.hconcat(m23a, m22, m23b)).to eq Matrix[ [1,2,3,1,2,2,3,4], [4,5,6,3,4,5,6,7] ]
153
+ end
154
+ end
155
+
156
+ context 'when matrix is concatenated with a vector' do
157
+ context 'when dimensions match' do
158
+ it 'concatenates' do
159
+ expect(Matrix.hconcat(m22, v)).to eq Matrix[ [1,2,1], [3,4,2] ]
160
+ expect(Matrix.hconcat(m21, v)).to eq Matrix[ [1,1], [2,2] ]
161
+ end
162
+ end
163
+
164
+ context 'when dimensions do not match' do
165
+ it 'raises an exception' do
166
+ expect {Matrix.hconcat(m12, v)}.to raise_exception(Matrix::ErrDimensionMismatch)
167
+ end
168
+ end
169
+ end
170
+ end
171
+
172
+ describe '#vconcat' do
173
+ context 'when input is not a matrix or vector' do
174
+ it 'raises an exception' do
175
+ expect {Matrix.vconcat(m23a, m12, 5)}.to raise_exception(TypeError)
176
+ end
177
+ end
178
+
179
+ context 'when column dimensions do not match' do
180
+ it 'raises an exception' do
181
+ expect {Matrix.vconcat(m23a, m21)}.to raise_exception(Matrix::ErrDimensionMismatch)
182
+ end
183
+ end
184
+
185
+ context 'when column dimensions match' do
186
+ it 'concatenates' do
187
+ expect(Matrix.vconcat(m23a, m23b)).to eq Matrix[ [1,2,3], [4,5,6], [2,3,4], [5,6,7] ]
188
+ end
189
+ end
190
+
191
+ context 'when matrix is concatenated with a vector' do
192
+ context 'when dimensions match' do
193
+ it 'concatenates' do
194
+ expect(Matrix.vconcat(m12, v)).to eq Matrix[ [1,2], [1,2] ]
195
+ end
196
+ end
197
+
198
+ context 'when dimensions do not match' do
199
+ it 'raises an exception' do
200
+ expect {Matrix.vconcat(m21, v)}.to raise_exception(Matrix::ErrDimensionMismatch)
201
+ end
202
+ end
203
+ end
204
+ end
205
+ end
@@ -0,0 +1,13 @@
1
+ require 'matrix_extensions'
2
+
3
+ RSpec.configure do |config|
4
+ # Run specs in random order to surface order dependencies. If you find an
5
+ # order dependency and want to debug it, you can fix the order by providing
6
+ # the seed, which is printed after each run.
7
+ # --seed 1234
8
+ config.order = 'random'
9
+
10
+ # when a focus tag is present in RSpec, only run tests with focus tag: http://railscasts.com/episodes/285-spork
11
+ config.filter_run :focus
12
+ config.run_all_when_everything_filtered = true
13
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: matrix_extensions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Imstepf
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-29 00:00:00.000000000 Z
11
+ date: 2014-07-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -68,6 +68,8 @@ files:
68
68
  - lib/matrix_extensions.rb
69
69
  - lib/matrix_extensions/version.rb
70
70
  - matrix_extensions.gemspec
71
+ - spec/matrix_extensions_spec.rb
72
+ - spec/spec_helper.rb
71
73
  homepage: https://github.com/michaelimstepf/matrix-extensions
72
74
  licenses:
73
75
  - MIT
@@ -92,4 +94,6 @@ rubygems_version: 2.2.2
92
94
  signing_key:
93
95
  specification_version: 4
94
96
  summary: Extension of the Ruby Matrix class.
95
- test_files: []
97
+ test_files:
98
+ - spec/matrix_extensions_spec.rb
99
+ - spec/spec_helper.rb