matrix_extensions 0.0.4 → 0.0.5

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: c978a09fe39c80d12e2aeed84d881d3c95c929d1
4
- data.tar.gz: 593d2c52dd09950898a6bb7072c5b755040fe813
3
+ metadata.gz: 78aace705667ce7954ca00df56c97994d7e6e3cb
4
+ data.tar.gz: b0691977b852e9a5ce290d133d1800849d2d6ea7
5
5
  SHA512:
6
- metadata.gz: 2a005c74d77d416fd2eaf8e68bbdc8eccd71bc8c12cf37915e8bdd4987d31603f20c12df1b0920ee00791f9dd884228444f9558732c49238ef4c7dad1adf76e7
7
- data.tar.gz: 5e9afd308ac55a6772524a06020fb4c5a6d9d7e408adf19774d88cb9e3e50b1c71866e949d826b39a20177bae62bf518a5871f6532a08d5b0748e9dbaa679e12
6
+ metadata.gz: 79490c48885fe48b33a642b69cefa64d6b2d12be4b340895391daa3a40b69ec7c040ffdce3d4eb7babdf06cc8cb66da3b7aeaa21dce099fec0c785db2bb12a66
7
+ data.tar.gz: aafc9805adb15f809f2eee55e85449f6c52a4cc03d1bf47a951a6644e9f5eae11633d674d3bc6d0b0a4f8caf27e295257658c39433722a6e5cdd55b1f64a9b6d
data/README.md CHANGED
@@ -47,15 +47,12 @@ 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 ones:**
51
51
 
52
52
  ```ruby
53
53
  require 'matrix_extensions' # if not loaded automatically
54
54
 
55
- Matrix.zeros(2,4)
56
- => Matrix[[0, 0, 0, 0], [0, 0, 0, 0]]
57
-
58
- Matrix.ones(2,2)
55
+ Matrix.one(2,2)
59
56
  => Matrix[[1, 1], [1, 1]]
60
57
  ```
61
58
 
@@ -108,7 +105,7 @@ Removes a set number of trailing rows from a matrix (destructive) and returns th
108
105
  require 'matrix_extensions' # if not loaded automatically
109
106
 
110
107
  Matrix[ [1,2,3], [4,5,6], [7,8,9] ].vpop(2)
111
- => Matrix[[1, 2, 3]]
108
+ => => Matrix[[7, 8, 9], [4, 5, 6]]
112
109
  ```
113
110
 
114
111
  ## Contributing
@@ -4,18 +4,11 @@ require 'matrix'
4
4
  # An extension to the Ruby Matrix class.
5
5
  # @author Michael Imstepf
6
6
  class Matrix
7
- # Matrix prefilled with zeros.
8
- # @param m [Matrix] matrix
9
- # @return [Matrix] matrix
10
- def self.zeros(rows = 1, columns = 1)
11
- Matrix.build(rows, columns) { 0 }
12
- end
13
- self.singleton_class.send(:alias_method, :zeroes, :zeros)
14
-
15
7
  # Matrix prefilled with ones.
16
- # @param m [Matrix] matrix
8
+ # @param rows [Integer] number of rows
9
+ # @param columns [Integer] number of columns
17
10
  # @return [Matrix] matrix
18
- def self.ones(rows = 1, columns = 1)
11
+ def self.one(rows = 1, columns = 1)
19
12
  Matrix.build(rows, columns) { 1 }
20
13
  end
21
14
 
@@ -99,11 +92,39 @@ class Matrix
99
92
  Matrix.Raise ErrDimensionMismatch unless number_of_rows < self.row_count
100
93
 
101
94
  dropped_rows = []
102
- number_of_rows.times { dropped_rows.unshift @rows.pop }
95
+ number_of_rows.times { dropped_rows << @rows.pop }
103
96
 
104
- Matrix.rows(dropped_rows.reverse)
97
+ Matrix.rows(dropped_rows)
105
98
  end
106
99
 
100
+ # Copies rows.
101
+ # @param number_of_copies [Integer] number of times rows should be copied
102
+ # @return [Matrix] matrix
103
+ def vcopy(number_of_copies = 1)
104
+ existing_rows = rows.clone
105
+
106
+ number_of_copies.times do
107
+ existing_rows.each { |row| rows << row }
108
+ end
109
+
110
+ self
111
+ end
112
+
113
+ # Copies columns.
114
+ # @param number_of_copies [Integer] number of times columns should be copied
115
+ # @return [Matrix] matrix
116
+ def hcopy(number_of_copies = 1)
117
+ rows.each do |row|
118
+ existing_row = row.clone
119
+ number_of_copies.times do
120
+ existing_row.each { |v| row << v }
121
+ end
122
+ end
123
+ @column_count += @column_count * number_of_copies
124
+
125
+ self
126
+ end
127
+
107
128
  # Element-wise division.
108
129
  # @param m [Matrix] matrix
109
130
  # @return [Matrix] matrix
@@ -1,3 +1,3 @@
1
1
  module MatrixExtensions
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -116,21 +116,9 @@ describe Matrix do
116
116
  end
117
117
  end
118
118
 
119
- describe '#ones' do
119
+ describe '#one' do
120
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] ]
121
+ expect(Matrix.one(2, 3)). to eq Matrix[ [1,1,1], [1,1,1] ]
134
122
  end
135
123
  end
136
124
 
@@ -250,5 +238,45 @@ describe Matrix do
250
238
  expect(m23e).to eq Matrix[ [1,2,3] ]
251
239
  end
252
240
  end
253
- end
241
+ end
242
+
243
+ describe '#vcopy' do
244
+ context 'when no argument is given' do
245
+ m23f = m23a.clone
246
+
247
+ it 'copies rows one time' do
248
+ expect(m23f.vcopy).to eq Matrix[[1, 2, 3], [4, 5, 6], [1, 2, 3], [4, 5, 6]]
249
+ expect(m23f).to eq Matrix[[1, 2, 3], [4, 5, 6], [1, 2, 3], [4, 5, 6]]
250
+ end
251
+ end
252
+
253
+ context 'when number of copies equals 3' do
254
+ m23g = m23a.clone
255
+
256
+ it 'copies rows 3 times' do
257
+ expect(m23g.vcopy(3)).to eq Matrix[[1, 2, 3], [4, 5, 6], [1, 2, 3], [4, 5, 6], [1, 2, 3], [4, 5, 6], [1, 2, 3], [4, 5, 6]]
258
+ expect(m23g).to eq Matrix[[1, 2, 3], [4, 5, 6], [1, 2, 3], [4, 5, 6], [1, 2, 3], [4, 5, 6], [1, 2, 3], [4, 5, 6]]
259
+ end
260
+ end
261
+ end
262
+
263
+ describe '#hcopy' do
264
+ context 'when no argument is given' do
265
+ m23h = m23a.clone
266
+
267
+ it 'copies columns one time' do
268
+ expect(m23h.hcopy).to eq Matrix[[1, 2, 3, 1, 2, 3], [4, 5, 6, 4, 5, 6]]
269
+ expect(m23h).to eq Matrix[[1, 2, 3, 1, 2, 3], [4, 5, 6, 4, 5, 6]]
270
+ end
271
+ end
272
+
273
+ context 'when number of copies equals 3' do
274
+ m23i = m23a.clone
275
+
276
+ it 'copies columns 3 times' do
277
+ expect(m23i.hcopy(3)).to eq Matrix[[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3], [4, 5, 6, 4, 5, 6, 4, 5, 6, 4, 5, 6]]
278
+ expect(m23i).to eq Matrix[[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3], [4, 5, 6, 4, 5, 6, 4, 5, 6, 4, 5, 6]]
279
+ end
280
+ end
281
+ end
254
282
  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
4
+ version: 0.0.5
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-08-04 00:00:00.000000000 Z
11
+ date: 2014-08-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler