matrix_extensions 0.0.2 → 0.0.3

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: 0888299dc382dbaf662109214f80784ce36def63
4
- data.tar.gz: a9b398b17dafb6bfb75b782e99e23619daf83ec8
3
+ metadata.gz: fc5568b2a7d1ef2cafc04aeca1dad9d13679045c
4
+ data.tar.gz: 52ceb8debb31147b857df272143afdd09a94045d
5
5
  SHA512:
6
- metadata.gz: f95b5b0497717de92579bf5f51ed34d96470825f6dea7b151f42a75add60e288cfbb95238c501c4d789829c0a06c91ab9cf461733475c20f44ecdb8ba386c757
7
- data.tar.gz: 3d010969030d91e918d5a63e1607d6925d55faa5cc4c24e6481fc5d434b18d3b207954493fdcf7e59b4556483e3c6308dd35548848667780540ee2f5f5d24910
6
+ metadata.gz: edd9ecefc3945eb9ce267852bacbc47f19f0c11cc3f003975ef90ec576ad2915ff5a65d1d58bd9d2ac318761b0291b2bb7fcc9c0d3b87035b86daf2a1690c8dc
7
+ data.tar.gz: 9f03600db372d97aea3ea336ca58a58c54021d62519e5e7e8077c47874f6c9c73080dd3240c2a264bc98dc3b1cc4d74f3d4a5bdbbb7b58b3a7ae5d8276132b26
data/README.md CHANGED
@@ -20,37 +20,37 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- Element-wise multiplication:
23
+ *Element-wise multiplication:*
24
24
 
25
25
  ```ruby
26
- require 'matrix_extensions' # if not loaded automatically
26
+ require 'matrix_extensions'
27
27
 
28
28
  Matrix[ [1,2,3], [4,5,6] ].element_multiplication Matrix[ [2,3,4], [5,6,7] ]
29
29
  => Matrix[[2, 6, 12], [20, 30, 42]]
30
30
  ```
31
31
 
32
- Element-wise division:
32
+ *Element-wise division:*
33
33
 
34
34
  ```ruby
35
- require 'matrix_extensions' # if not loaded automatically
35
+ require 'matrix_extensions'
36
36
 
37
37
  Matrix[ [2,4,6], [2.0,10,20] ].element_division Matrix[ [2,2,6], [4,5,10] ]
38
38
  => Matrix[[1, 2, 1], [0.5, 2, 2]]
39
39
  ```
40
40
 
41
- Element-wise exponentiation:
41
+ *Element-wise exponentiation:*
42
42
 
43
43
  ```ruby
44
- require 'matrix_extensions' # if not loaded automatically
44
+ require 'matrix_extensions'
45
45
 
46
46
  Matrix[ [2,4], [1,4] ].element_exponentiation Matrix[ [2,4], [1,4]] ]
47
47
  => Matrix[[4, 256], [1, 256]]
48
48
  ```
49
49
 
50
- Prefilled matrix with zeros or ones:
50
+ *Prefilled matrix with zeros or ones:*
51
51
 
52
52
  ```ruby
53
- require 'matrix_extensions' # if not loaded automatically
53
+ require 'matrix_extensions'
54
54
 
55
55
  Matrix.zeros(2,4)
56
56
  => Matrix[[0, 0, 0, 0], [0, 0, 0, 0]]
@@ -59,12 +59,12 @@ Matrix.ones(2,2)
59
59
  => Matrix[[1, 1], [1, 1]]
60
60
  ```
61
61
 
62
- Concatenating matrices and vectors horizontally:
62
+ *Concatenating matrices and vectors horizontally:*
63
63
 
64
64
  This iterates through a list of matrices and vectors and appends columns of each list one after another. The row number of all matrices and vectors must be equal. The number of arguments passed in can be freely chosen.
65
65
 
66
66
  ```ruby
67
- require 'matrix_extensions' # if not loaded automatically
67
+ require 'matrix_extensions'
68
68
 
69
69
  m1 = Matrix[ [1,2,3], [4,5,6] ]
70
70
  m2 = Matrix[ [2,3,4], [5,6,7] ]
@@ -74,12 +74,12 @@ Matrix.hconcat(m1, m2, v)
74
74
  => Matrix[[1, 2, 3, 2, 3, 4, 3], [4, 5, 6, 5, 6, 7, 4]]
75
75
  ```
76
76
 
77
- Concatenating matrices and vectors vertically:
77
+ *Concatenating matrices and vectors vertically:*
78
78
 
79
79
  This iterates through a list of matrices and vectors and appends rows of each list one after another. The column number of all matrices and vectors must be equal. The number of arguments passed in can be freely chosen.
80
80
 
81
81
  ```ruby
82
- require 'matrix_extensions' # if not loaded automatically
82
+ require 'matrix_extensions'
83
83
 
84
84
  m1 = Matrix[ [1,2,3], [4,5,6] ]
85
85
  m2 = Matrix[ [2,3,4], [5,6,7] ]
@@ -89,6 +89,28 @@ Matrix.vconcat(m1, m2, v)
89
89
  => Matrix[[1, 2, 3], [4, 5, 6], [2, 3, 4], [5, 6, 7], [3, 4, 5]]
90
90
  ```
91
91
 
92
+ *Removing trailing columns:*
93
+
94
+ Removes a set number of trailing columns from a matrix. The argument defaults to 1.
95
+
96
+ ```ruby
97
+ require 'matrix_extensions'
98
+
99
+ Matrix[ [1,2,3], [4,5,6] ].hpop(2)
100
+ => Matrix[[1], [4]]
101
+ ```
102
+
103
+ *Removing trailing rows:*
104
+
105
+ Removes a set number of trailing rows from a matrix. The argument defaults to 1.
106
+
107
+ ```ruby
108
+ require 'matrix_extensions'
109
+
110
+ Matrix[ [1,2,3], [4,5,6], [7,8,9] ].vpop(2)
111
+ => Matrix[[1, 2, 3]]
112
+ ```
113
+
92
114
  ## Contributing
93
115
 
94
116
  1. Fork it ( https://github.com/[my-github-username]/matrix_extensions/fork )
@@ -1,3 +1,3 @@
1
1
  module MatrixExtensions
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -71,6 +71,40 @@ class Matrix
71
71
 
72
72
  # create new matrix
73
73
  self.rows(rows)
74
+ end
75
+
76
+ # Removes trailing columns from a Matrix.
77
+ # @param number_of_columns [Integer] number of trailing columns to be removed
78
+ # @return [Matrix] matrix
79
+ # @raise [ErrDimensionMismatch] if Matrix does not have enough columns for operation
80
+ def hpop(number_of_columns = 1)
81
+ Matrix.Raise ErrDimensionMismatch unless number_of_columns < self.column_count
82
+
83
+ columns = []
84
+ last_column_to_be_included = self.column_count - number_of_columns
85
+ self.column_vectors.each_with_index do |column, index|
86
+ break if index + 1 > last_column_to_be_included
87
+ columns << column.to_a
88
+ end
89
+
90
+ Matrix.columns(columns)
91
+ end
92
+
93
+ # Removes trailing rows from a Matrix.
94
+ # @param number_of_rows [Integer] number of trailing rows to be removed
95
+ # @return [Matrix] matrix
96
+ # @raise [ErrDimensionMismatch] if Matrix does not have enough rows for operation
97
+ def vpop(number_of_rows = 1)
98
+ Matrix.Raise ErrDimensionMismatch unless number_of_rows < self.row_count
99
+
100
+ rows = []
101
+ last_row_to_be_included = self.row_count - number_of_rows
102
+ self.row_vectors.each_with_index do |row, index|
103
+ break if index + 1 > last_row_to_be_included
104
+ rows << row.to_a
105
+ end
106
+
107
+ Matrix.rows(rows)
74
108
  end
75
109
 
76
110
  # Element-wise division.
@@ -21,4 +21,5 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency "bundler", "~> 1.6"
22
22
  spec.add_development_dependency "rake"
23
23
  spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "pry"
24
25
  end
@@ -201,5 +201,46 @@ describe Matrix do
201
201
  end
202
202
  end
203
203
  end
204
- end
204
+ end
205
+
206
+ describe '#hpop' do
207
+ context 'when number of columns equal number of columns to be popped' do
208
+ it 'raises an exception' do
209
+ expect {m23a.hpop(3)}.to raise_exception(Matrix::ErrDimensionMismatch)
210
+ end
211
+ end
212
+
213
+ context 'when number of columns are smaller than number of columns to be popped' do
214
+ it 'raises an exception' do
215
+ expect {m23a.hpop(4)}.to raise_exception(Matrix::ErrDimensionMismatch)
216
+ end
217
+ end
218
+
219
+ context 'when number of columns are greater than number of columns to be popped' do
220
+ it 'returns popped matrix' do
221
+ expect(m23a.hpop).to eq Matrix[ [1,2], [4,5] ]
222
+ expect(m23a.hpop(2)).to eq Matrix[ [1], [4] ]
223
+ end
224
+ end
225
+ end
226
+
227
+ describe '#vpop' do
228
+ context 'when number of rows equal number of rows to be popped' do
229
+ it 'raises an exception' do
230
+ expect {m23a.vpop(2)}.to raise_exception(Matrix::ErrDimensionMismatch)
231
+ end
232
+ end
233
+
234
+ context 'when number of rows are smaller than number of rows to be popped' do
235
+ it 'raises an exception' do
236
+ expect {m23a.vpop(3)}.to raise_exception(Matrix::ErrDimensionMismatch)
237
+ end
238
+ end
239
+
240
+ context 'when number of rows are greater than number of rows to be popped' do
241
+ it 'returns popped matrix' do
242
+ expect(m23a.vpop).to eq Matrix[ [1,2,3] ]
243
+ end
244
+ end
245
+ end
205
246
  end
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'matrix_extensions'
2
+ require 'pry'
2
3
 
3
4
  RSpec.configure do |config|
4
5
  # Run specs in random order to surface order dependencies. If you find an
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.2
4
+ version: 0.0.3
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-30 00:00:00.000000000 Z
11
+ date: 2014-07-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  description: Element-wise matrix operations, concatenation and pre-filled matrices
56
70
  with single values.
57
71
  email: