matrix_extensions 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +19 -19
- data/lib/matrix_extensions.rb +17 -20
- data/lib/matrix_extensions/version.rb +1 -1
- data/spec/matrix_extensions_spec.rb +13 -5
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c978a09fe39c80d12e2aeed84d881d3c95c929d1
|
4
|
+
data.tar.gz: 593d2c52dd09950898a6bb7072c5b755040fe813
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2a005c74d77d416fd2eaf8e68bbdc8eccd71bc8c12cf37915e8bdd4987d31603f20c12df1b0920ee00791f9dd884228444f9558732c49238ef4c7dad1adf76e7
|
7
|
+
data.tar.gz: 5e9afd308ac55a6772524a06020fb4c5a6d9d7e408adf19774d88cb9e3e50b1c71866e949d826b39a20177bae62bf518a5871f6532a08d5b0748e9dbaa679e12
|
data/README.md
CHANGED
@@ -20,37 +20,37 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
|
23
|
+
**Element-wise multiplication:**
|
24
24
|
|
25
25
|
```ruby
|
26
|
-
require 'matrix_extensions'
|
26
|
+
require 'matrix_extensions' # if not loaded automatically
|
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
|
-
|
32
|
+
**Element-wise division:**
|
33
33
|
|
34
34
|
```ruby
|
35
|
-
require 'matrix_extensions'
|
35
|
+
require 'matrix_extensions' # if not loaded automatically
|
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
|
-
|
41
|
+
**Element-wise exponentiation:**
|
42
42
|
|
43
43
|
```ruby
|
44
|
-
require 'matrix_extensions'
|
44
|
+
require 'matrix_extensions' # if not loaded automatically
|
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
|
-
|
50
|
+
**Prefilled matrix with zeros or ones:**
|
51
51
|
|
52
52
|
```ruby
|
53
|
-
require 'matrix_extensions'
|
53
|
+
require 'matrix_extensions' # if not loaded automatically
|
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
|
-
|
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'
|
67
|
+
require 'matrix_extensions' # if not loaded automatically
|
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
|
-
|
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'
|
82
|
+
require 'matrix_extensions' # if not loaded automatically
|
83
83
|
|
84
84
|
m1 = Matrix[ [1,2,3], [4,5,6] ]
|
85
85
|
m2 = Matrix[ [2,3,4], [5,6,7] ]
|
@@ -89,23 +89,23 @@ 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
|
-
|
92
|
+
**Removing trailing columns:**
|
93
93
|
|
94
|
-
Removes a set number of trailing columns from a matrix. The argument defaults to 1.
|
94
|
+
Removes a set number of trailing columns from a matrix (destructive) and returns the removed columns. The argument defaults to 1.
|
95
95
|
|
96
96
|
```ruby
|
97
|
-
require 'matrix_extensions'
|
97
|
+
require 'matrix_extensions' # if not loaded automatically
|
98
98
|
|
99
99
|
Matrix[ [1,2,3], [4,5,6] ].hpop(2)
|
100
|
-
=> Matrix[[
|
100
|
+
=> Matrix[[2, 3], [5, 6]]
|
101
101
|
```
|
102
102
|
|
103
|
-
|
103
|
+
**Removing trailing rows:**
|
104
104
|
|
105
|
-
Removes a set number of trailing rows from a matrix. The argument defaults to 1.
|
105
|
+
Removes a set number of trailing rows from a matrix (destructive) and returns the removed rows. The argument defaults to 1.
|
106
106
|
|
107
107
|
```ruby
|
108
|
-
require 'matrix_extensions'
|
108
|
+
require 'matrix_extensions' # if not loaded automatically
|
109
109
|
|
110
110
|
Matrix[ [1,2,3], [4,5,6], [7,8,9] ].vpop(2)
|
111
111
|
=> Matrix[[1, 2, 3]]
|
data/lib/matrix_extensions.rb
CHANGED
@@ -73,39 +73,36 @@ class Matrix
|
|
73
73
|
self.rows(rows)
|
74
74
|
end
|
75
75
|
|
76
|
-
# Removes trailing columns from a Matrix.
|
76
|
+
# Removes trailing columns from a Matrix (destructive).
|
77
77
|
# @param number_of_columns [Integer] number of trailing columns to be removed
|
78
|
-
# @return [Matrix] matrix
|
78
|
+
# @return [Matrix] matrix consisting of of dropped columns
|
79
79
|
# @raise [ErrDimensionMismatch] if Matrix does not have enough columns for operation
|
80
80
|
def hpop(number_of_columns = 1)
|
81
81
|
Matrix.Raise ErrDimensionMismatch unless number_of_columns < self.column_count
|
82
82
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
83
|
+
dropped_columns = []
|
84
|
+
number_of_columns.times do
|
85
|
+
dropped_column = []
|
86
|
+
@rows.each {|r| dropped_column << r.pop}
|
87
|
+
dropped_columns << dropped_column
|
88
88
|
end
|
89
|
-
|
90
|
-
|
89
|
+
@column_count -= number_of_columns
|
90
|
+
|
91
|
+
Matrix.columns(dropped_columns.reverse)
|
91
92
|
end
|
92
93
|
|
93
|
-
# Removes trailing rows from a Matrix.
|
94
|
+
# Removes trailing rows from a Matrix (destructive).
|
94
95
|
# @param number_of_rows [Integer] number of trailing rows to be removed
|
95
|
-
# @return [Matrix] matrix
|
96
|
+
# @return [Matrix] matrix consisting of of dropped rows
|
96
97
|
# @raise [ErrDimensionMismatch] if Matrix does not have enough rows for operation
|
97
98
|
def vpop(number_of_rows = 1)
|
98
99
|
Matrix.Raise ErrDimensionMismatch unless number_of_rows < self.row_count
|
99
100
|
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
end
|
106
|
-
|
107
|
-
Matrix.rows(rows)
|
108
|
-
end
|
101
|
+
dropped_rows = []
|
102
|
+
number_of_rows.times { dropped_rows.unshift @rows.pop }
|
103
|
+
|
104
|
+
Matrix.rows(dropped_rows.reverse)
|
105
|
+
end
|
109
106
|
|
110
107
|
# Element-wise division.
|
111
108
|
# @param m [Matrix] matrix
|
@@ -217,9 +217,14 @@ describe Matrix do
|
|
217
217
|
end
|
218
218
|
|
219
219
|
context 'when number of columns are greater than number of columns to be popped' do
|
220
|
-
|
221
|
-
|
222
|
-
|
220
|
+
m23c = m23a.clone
|
221
|
+
m23d = m23a.clone
|
222
|
+
|
223
|
+
it 'pops matrix destructively and returns matrix of dropped columns' do
|
224
|
+
expect(m23c.hpop).to eq Matrix[ [3], [6] ]
|
225
|
+
expect(m23c).to eq Matrix[ [1,2], [4,5] ]
|
226
|
+
expect(m23d.hpop(2)).to eq Matrix[[2, 3], [5, 6]]
|
227
|
+
expect(m23d).to eq Matrix[ [1], [4] ]
|
223
228
|
end
|
224
229
|
end
|
225
230
|
end
|
@@ -238,8 +243,11 @@ describe Matrix do
|
|
238
243
|
end
|
239
244
|
|
240
245
|
context 'when number of rows are greater than number of rows to be popped' do
|
241
|
-
|
242
|
-
|
246
|
+
m23e = m23a.clone
|
247
|
+
|
248
|
+
it 'pops matrix destructively and returns updated matrix' do
|
249
|
+
expect(m23e.vpop).to eq Matrix[ [4,5,6] ]
|
250
|
+
expect(m23e).to eq Matrix[ [1,2,3] ]
|
243
251
|
end
|
244
252
|
end
|
245
253
|
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.
|
4
|
+
version: 0.0.4
|
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-
|
11
|
+
date: 2014-08-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|