rmatrix 0.1.1 → 0.1.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: 1809c8900de92ab3be5076af4ccd7793213f62fc
4
- data.tar.gz: 42c42b421b98c28f96a7e0ee22b51c45f5562a97
3
+ metadata.gz: 2669d0b3df98c92b74abe27833d0a1d11f39bf84
4
+ data.tar.gz: e28379824f051e318780f7599bfe1d640dbb3bf1
5
5
  SHA512:
6
- metadata.gz: 6f0ea097bd582f947b4b19fe7cfc762b31f456a9fd98a6c25ad9b9253ef7c69e19e715e7d193e5cb1e64b30d27350ec467045ac1ef6da08f2cc4c1f20770a226
7
- data.tar.gz: fa8cbee25a028eb34ad995f7bc76690c41590a9a5cdc2601bef1c1d48671ed730f5f2954c39dfe4d0587e43b94f300fd6429ba721f50305addd6d5bd29b5c340
6
+ metadata.gz: adcd594922c431268a3f3734ee9e17055f29b05f3ede14bb1f9df17956bd19ce5f203ab78d5875caf5e1307d8b767fe3d9510545677673331c9d877297ea4471
7
+ data.tar.gz: 07642f99e0eca1b196fdee6889684f9271a3f0ea15d4e5c9edec8bb7e1cfd72a361547887b3b41b169be2d5b58da77d6443f718196be5e7e8b5787462dfe491b
data/lib/rmatrix.rb CHANGED
@@ -1,3 +1,3 @@
1
- require "rmatrix/version"
2
- require "rmatrix/core"
3
- require "rmatrix/shortcuts"
1
+ require_relative "rmatrix/version"
2
+ require_relative "rmatrix/core"
3
+ require_relative "rmatrix/shortcuts"
data/lib/rmatrix/core.rb CHANGED
@@ -1,2 +1,2 @@
1
- require "rmatrix/matrix"
2
- require "rmatrix/vector"
1
+ require_relative "matrix"
2
+ require_relative "vector"
@@ -1,19 +1,22 @@
1
1
  module RMatrix
2
2
  class Matrix
3
3
  require 'narray'
4
+ require_relative 'typecode'
4
5
  include Enumerable
5
6
 
6
7
  attr_accessor :invert_next_operation, :matrix, :narray, :typecode
7
8
 
8
- def initialize(source, narray: nil, typecode: 'float')
9
+ def initialize(source, typecode=Typecode::FLOAT)
9
10
  self.typecode = typecode
10
- if [Symbol, String].include?(source.class)
11
- dimensions = "#{source}".split("x").map(&:to_i)
12
- source = NArray.new(typecode, dimensions.inject(:*)).reshape(dimensions[1], dimensions[0])
13
- end
11
+ self.narray = two_dimensional(source, typecode)
12
+ end
13
+
14
+ def matrix
15
+ @matrix ||= NMatrix.refer(narray)
16
+ end
14
17
 
15
- self.narray = NArray.refer(narray || two_dimensional(source, typecode))
16
- self.matrix = NMatrix.refer(self.narray)
18
+ def self.blank(rows, cols, typecode=Typecode::FLOAT)
19
+ self.new(NArray.new(typecode, rows * cols).reshape(cols, rows), typecode)
17
20
  end
18
21
 
19
22
  def _dump(level)
@@ -22,7 +25,7 @@ module RMatrix
22
25
 
23
26
  def self._load arg
24
27
  typecode, columns, rows, as_str = arg.split(":",4)
25
- Matrix.new(nil, narray: NArray.to_na(as_str.to_s, typecode.to_i).reshape(columns.to_i, rows.to_i))
28
+ Matrix.new(NArray.to_na(as_str.to_s, typecode.to_i).reshape(columns.to_i, rows.to_i), typecode.to_i)
26
29
  end
27
30
 
28
31
  def each(&block)
@@ -40,7 +43,7 @@ module RMatrix
40
43
  yield elm
41
44
  end
42
45
  ).to_type(typecode)
43
- Matrix.new(as_na.reshape(*shape), typecode: typecode)
46
+ Matrix.new(as_na.reshape(*shape), typecode)
44
47
  end
45
48
 
46
49
  def coerce(other)
@@ -61,16 +64,28 @@ module RMatrix
61
64
  end
62
65
 
63
66
  def sum_rows
64
- Matrix.new(sum(0), typecode: typecode)
67
+ Matrix.new(sum(0), typecode)
65
68
  end
66
69
 
67
70
  def sum_columns
68
- Matrix.new(sum(1), typecode: typecode)
71
+ Matrix.new(sum(1), typecode)
69
72
  end
70
73
 
71
74
  def two_dimensional(source, type)
72
- source = [source] if Numeric === source
73
- source = NArray.to_na(source).to_type(type)
75
+ case source
76
+ when NArray
77
+ if NMatrix === source
78
+ @matrix = source
79
+ source = NArray.refer(source)
80
+ end
81
+ when Numeric
82
+ source = NArray.to_na([source])
83
+ else
84
+ source = NArray.to_na(source)
85
+ end
86
+
87
+ source = source.to_type(type) unless type == source.typecode
88
+
74
89
  case source.dim
75
90
  when 1
76
91
  source.reshape(source.length, 1)
@@ -96,7 +111,7 @@ module RMatrix
96
111
  result[i] << cofactor(i, j)
97
112
  end
98
113
  end
99
- return Matrix.new(result, typecode: typecode)
114
+ return Matrix.new(result, typecode)
100
115
  end
101
116
 
102
117
  def determinant
@@ -118,18 +133,18 @@ module RMatrix
118
133
  def *(other)
119
134
  if other.kind_of?(Matrix)
120
135
  raise "Matrix A columns(#{self.columns}) != Matrix B rows(#{other.columns})" if other.rows != self.columns
121
- Matrix.new(self.matrix * other.matrix, typecode: typecode)
136
+ Matrix.new(self.matrix * other.matrix, typecode)
122
137
  else
123
- Matrix.new(apply_scalar(:*, other), typecode: typecode)
138
+ Matrix.new(apply_scalar(:*, other), typecode)
124
139
  end
125
140
  end
126
141
 
127
142
  def mult(other)
128
- Matrix.new(apply_elementwise(:*, other), typecode: typecode)
143
+ Matrix.new(self.narray * other.narray, typecode)
129
144
  end
130
145
 
131
146
  def ==(other)
132
- self.matrix == Matrix[other].matrix
147
+ self.narray == Matrix[other].narray
133
148
  end
134
149
 
135
150
  def to_s
@@ -145,6 +160,8 @@ module RMatrix
145
160
  end
146
161
 
147
162
  def inspect
163
+ return Vector::inspect_vector(self) if [rows, columns].include?(1)
164
+
148
165
  inspect_rows = [10, self.rows].min
149
166
  inspect_columns = [10, self.columns].min
150
167
  more_rows = inspect_rows < self.rows
@@ -161,21 +178,11 @@ module RMatrix
161
178
 
162
179
 
163
180
  def [](*args)
164
- args.all?{|x| Fixnum === x } ? narray[*args.reverse] : Matrix.new(narray[*args.reverse], typecode: typecode)
181
+ args.all?{|x| Fixnum === x } ? narray[*args.reverse] : Matrix.new(narray[*args.reverse], typecode)
165
182
  end
166
183
 
167
184
  def transpose
168
- Matrix.new(self.matrix.transpose, typecode: typecode)
169
- end
170
-
171
- def self.new(*args, &blk)
172
- result = super
173
- if result.class == Matrix && (result.rows == 1 || result.columns == 1)
174
- vect = V[1]
175
- vect.typecode, vect.matrix, vect.narray = result.typecode, result.matrix, result.narray
176
- result = vect
177
- end
178
- result
185
+ Matrix.new(self.matrix.transpose, typecode)
179
186
  end
180
187
 
181
188
  def self.[](*inputs)
@@ -183,7 +190,7 @@ module RMatrix
183
190
  inputs[0]
184
191
  elsif inputs.length == 1 && [String, Symbol].include?(inputs[0].class)
185
192
  if ['byte', 'sint', 'int', 'sfloat', 'float', 'scomplex', 'complex', 'object'].include?(inputs[0])
186
- ->(*source){ Matrix.new(source, typecode: inputs[0]) }
193
+ ->(*source){ Matrix.new(source, inputs[0]) }
187
194
  else
188
195
  Matrix.new(inputs[0])
189
196
  end
@@ -201,7 +208,7 @@ module RMatrix
201
208
 
202
209
  def self.gen_matrix_delegator(name)
203
210
  define_method(name) do |*args, &blk|
204
- Matrix.new(matrix.send(name, *args, &blk), typecode: typecode)
211
+ Matrix.new(matrix.send(name, *args, &blk), typecode)
205
212
  end
206
213
  end
207
214
 
@@ -213,7 +220,7 @@ module RMatrix
213
220
 
214
221
  def self.gen_typeconstructor(name)
215
222
  define_singleton_method(name) do
216
- ->(*source){ Matrix.new(source, typecode: name.to_s) }
223
+ ->(*source){ Matrix.new(source, name.to_s) }
217
224
  end
218
225
  end
219
226
 
@@ -222,25 +229,26 @@ module RMatrix
222
229
  :^ => 'xor',
223
230
  :| => 'or'
224
231
  }
225
- def translate_op(op)
232
+ def self.translate_op(op)
226
233
  OPERATIONS_MAP.fetch(op, op)
227
234
  end
228
235
 
229
236
  [:fill!, :random!, :indgen!].each(&method(:gen_mutator))
230
237
  [:reshape, :sort, :sort_index, :inverse, :lu, :delete_at, :where, :where2, :not, :-@].each(&method(:gen_matrix_delegator))
231
- [:sum, :prod, :mean, :stddev, :rms, :rmsdev, :min, :max, :shape].each(&method(:gen_delegator))
238
+ [:sum, :prod, :mean, :stddev, :rms, :rmsdev, :min, :max, :shape, :to_a].each(&method(:gen_delegator))
232
239
  [:byte, :sint, :int, :sfloat, :float, :scomplex, :complex, :object].each(&method(:gen_typeconstructor))
233
240
 
234
241
  [:+, :/, :-, :**, :&, :^, :|].each do |op|
242
+ op = translate_op(op)
235
243
  define_method(op) do |other|
236
- op = translate_op(op)
237
- result = if other.kind_of?(Matrix)
244
+ result = case other
245
+ when Matrix
238
246
  apply_elementwise(op, other)
239
247
  else
240
248
  apply_scalar(op, other)
241
249
  end
242
250
  Matrix.new(
243
- result, typecode: typecode
251
+ result, typecode
244
252
  )
245
253
  end
246
254
  end
@@ -251,9 +259,10 @@ module RMatrix
251
259
 
252
260
  private
253
261
  def test_inverse
254
- inverse = self.invert_next_operation
255
- self.invert_next_operation = false
256
- return inverse
262
+ if self.invert_next_operation
263
+ self.invert_next_operation = false
264
+ return true
265
+ end
257
266
  end
258
267
 
259
268
  def cofactor(i, j)
@@ -266,17 +275,18 @@ module RMatrix
266
275
  if test_inverse
267
276
  other.narray.send(op, self.narray)
268
277
  else
269
- narray.send(op, Matrix[other].narray)
278
+ narray.send(op, other.narray)
270
279
  end
271
280
  end
272
281
 
273
282
  def apply_scalar(op, other)
274
283
  if test_inverse
275
- Matrix[other].narray.send(op, self.narray)
284
+ other.send(op, self.narray)
276
285
  else
277
- narray.send(op, Matrix[other].narray)
286
+ narray.send(op, other)
278
287
  end
279
288
  end
289
+
280
290
  end
281
291
 
282
292
  class ::NArray; include Enumerable; end
@@ -0,0 +1,14 @@
1
+ module RMatrix
2
+ class Matrix
3
+ module Typecode
4
+ BYTE = 1
5
+ SINT = 2
6
+ INT = 3
7
+ SFLOAT = 4
8
+ FLOAT = 5
9
+ SCOMPLEX = 6
10
+ COMPLEX = 7
11
+ OBJECT = 8
12
+ end
13
+ end
14
+ end
@@ -1,8 +1,13 @@
1
1
  module RMatrix
2
2
  class Vector < RMatrix::Matrix
3
3
 
4
- def initialize(source)
5
- super
4
+ def initialize(source, typecode=Typecode::FLOAT)
5
+ if narray
6
+ self.narray = narray
7
+ self.matrix = NMatrix.refer(matrix)
8
+ else
9
+ super
10
+ end
6
11
  raise "Invalid dimensions #{shape.join(?x).reverse}. Vector must be eiter Nx1 or 1xM" unless [rows, columns].include?(1) && shape.length == 2
7
12
  end
8
13
 
@@ -19,13 +24,17 @@ module RMatrix
19
24
  end
20
25
 
21
26
  def inspect
22
- elms = narray.to_a.flatten
27
+ self.class.inspect_vector(self)
28
+ end
29
+
30
+ def self.inspect_vector(v)
31
+ elms = v.narray.to_a.flatten
23
32
  print = elms.first(10)
24
33
  has_more = elms.length > 10
25
- if rows == 1
26
- "Vector(#{narray.length})\nV[#{print.join(", ") + (has_more ? ',...' : '')}]"
34
+ if v.rows == 1
35
+ "Vector(#{v.narray.length})\nV[#{print.join(", ") + (has_more ? ',...' : '')}]"
27
36
  else
28
- "Vector(#{narray.length})\nV[\n [#{print.join("],\n [") + (has_more ? "\n ..." : '')}]\n]"
37
+ "Vector(#{v.narray.length})\nV[\n [#{print.join("],\n [") + (has_more ? "\n ..." : '')}]\n]"
29
38
  end
30
39
  end
31
40
  end
@@ -1,3 +1,3 @@
1
1
  module Rmatrix
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rmatrix
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wouter Coppieters
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-04-08 00:00:00.000000000 Z
11
+ date: 2016-04-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -116,6 +116,7 @@ files:
116
116
  - lib/rmatrix/core.rb
117
117
  - lib/rmatrix/matrix.rb
118
118
  - lib/rmatrix/shortcuts.rb
119
+ - lib/rmatrix/typecode.rb
119
120
  - lib/rmatrix/vector.rb
120
121
  - lib/rmatrix/version.rb
121
122
  - rmatrix.gemspec