rmatrix 0.1.0 → 0.1.1

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: e572268a9fa4fd04c2ac166d7141581ca677edd0
4
- data.tar.gz: 41f62ceec7f2c54bc3b13899e479dbf8f433a5ca
3
+ metadata.gz: 1809c8900de92ab3be5076af4ccd7793213f62fc
4
+ data.tar.gz: 42c42b421b98c28f96a7e0ee22b51c45f5562a97
5
5
  SHA512:
6
- metadata.gz: a75e6d8d05a41a7c830e6d50853c26c33233e0463d55a8afc3cf170be5bbaf2d3d090ec95929465eb930be8fcc4f3e3418666db70c122705904f21c7faed4e8a
7
- data.tar.gz: 40cbe6898a74869c74a411357eec77f5ea7aa2ce3c857d674cc884115123d11157317ae276999e9f053cf205c143b1ae791d2b37295cb4cd790526dfa9b368de
6
+ metadata.gz: 6f0ea097bd582f947b4b19fe7cfc762b31f456a9fd98a6c25ad9b9253ef7c69e19e715e7d193e5cb1e64b30d27350ec467045ac1ef6da08f2cc4c1f20770a226
7
+ data.tar.gz: fa8cbee25a028eb34ad995f7bc76690c41590a9a5cdc2601bef1c1d48671ed730f5f2954c39dfe4d0587e43b94f300fd6429ba721f50305addd6d5bd29b5c340
@@ -5,9 +5,8 @@ module RMatrix
5
5
 
6
6
  attr_accessor :invert_next_operation, :matrix, :narray, :typecode
7
7
 
8
- def initialize(source, narray: nil)
9
- self.typecode = 'float'
10
-
8
+ def initialize(source, narray: nil, typecode: 'float')
9
+ self.typecode = typecode
11
10
  if [Symbol, String].include?(source.class)
12
11
  dimensions = "#{source}".split("x").map(&:to_i)
13
12
  source = NArray.new(typecode, dimensions.inject(:*)).reshape(dimensions[1], dimensions[0])
@@ -41,7 +40,7 @@ module RMatrix
41
40
  yield elm
42
41
  end
43
42
  ).to_type(typecode)
44
- Matrix.new(as_na.reshape(*shape))
43
+ Matrix.new(as_na.reshape(*shape), typecode: typecode)
45
44
  end
46
45
 
47
46
  def coerce(other)
@@ -62,11 +61,11 @@ module RMatrix
62
61
  end
63
62
 
64
63
  def sum_rows
65
- Matrix.new(sum(0))
64
+ Matrix.new(sum(0), typecode: typecode)
66
65
  end
67
66
 
68
67
  def sum_columns
69
- Matrix.new(sum(1))
68
+ Matrix.new(sum(1), typecode: typecode)
70
69
  end
71
70
 
72
71
  def two_dimensional(source, type)
@@ -97,7 +96,7 @@ module RMatrix
97
96
  result[i] << cofactor(i, j)
98
97
  end
99
98
  end
100
- return Matrix.new(result)
99
+ return Matrix.new(result, typecode: typecode)
101
100
  end
102
101
 
103
102
  def determinant
@@ -119,14 +118,14 @@ module RMatrix
119
118
  def *(other)
120
119
  if other.kind_of?(Matrix)
121
120
  raise "Matrix A columns(#{self.columns}) != Matrix B rows(#{other.columns})" if other.rows != self.columns
122
- Matrix.new(self.matrix * other.matrix)
121
+ Matrix.new(self.matrix * other.matrix, typecode: typecode)
123
122
  else
124
- Matrix.new(apply_scalar(:*, other))
123
+ Matrix.new(apply_scalar(:*, other), typecode: typecode)
125
124
  end
126
125
  end
127
126
 
128
127
  def mult(other)
129
- apply_elementwise(:*, other)
128
+ Matrix.new(apply_elementwise(:*, other), typecode: typecode)
130
129
  end
131
130
 
132
131
  def ==(other)
@@ -162,18 +161,18 @@ module RMatrix
162
161
 
163
162
 
164
163
  def [](*args)
165
- args.any?{|x| Range === x} ? Matrix.new(narray[*args.reverse]) : narray[*args.reverse]
164
+ args.all?{|x| Fixnum === x } ? narray[*args.reverse] : Matrix.new(narray[*args.reverse], typecode: typecode)
166
165
  end
167
166
 
168
167
  def transpose
169
- Matrix.new(self.matrix.transpose)
168
+ Matrix.new(self.matrix.transpose, typecode: typecode)
170
169
  end
171
170
 
172
171
  def self.new(*args, &blk)
173
172
  result = super
174
173
  if result.class == Matrix && (result.rows == 1 || result.columns == 1)
175
174
  vect = V[1]
176
- vect.matrix, vect.narray = result.matrix, result.narray
175
+ vect.typecode, vect.matrix, vect.narray = result.typecode, result.matrix, result.narray
177
176
  result = vect
178
177
  end
179
178
  result
@@ -183,7 +182,11 @@ module RMatrix
183
182
  if inputs.length == 1 && Matrix === inputs[0]
184
183
  inputs[0]
185
184
  elsif inputs.length == 1 && [String, Symbol].include?(inputs[0].class)
186
- Matrix.new(inputs[0])
185
+ if ['byte', 'sint', 'int', 'sfloat', 'float', 'scomplex', 'complex', 'object'].include?(inputs[0])
186
+ ->(*source){ Matrix.new(source, typecode: inputs[0]) }
187
+ else
188
+ Matrix.new(inputs[0])
189
+ end
187
190
  else
188
191
  Matrix.new(inputs)
189
192
  end
@@ -198,7 +201,7 @@ module RMatrix
198
201
 
199
202
  def self.gen_matrix_delegator(name)
200
203
  define_method(name) do |*args, &blk|
201
- Matrix.new(matrix.send(name, *args, &blk))
204
+ Matrix.new(matrix.send(name, *args, &blk), typecode: typecode)
202
205
  end
203
206
  end
204
207
 
@@ -208,6 +211,12 @@ module RMatrix
208
211
  end
209
212
  end
210
213
 
214
+ def self.gen_typeconstructor(name)
215
+ define_singleton_method(name) do
216
+ ->(*source){ Matrix.new(source, typecode: name.to_s) }
217
+ end
218
+ end
219
+
211
220
  OPERATIONS_MAP = {
212
221
  :& => 'and',
213
222
  :^ => 'xor',
@@ -218,17 +227,20 @@ module RMatrix
218
227
  end
219
228
 
220
229
  [:fill!, :random!, :indgen!].each(&method(:gen_mutator))
221
- [:reshape, :sort, :sort_index, :inverse, :lu, :delete_at, :where, :where2, :not].each(&method(:gen_matrix_delegator))
230
+ [:reshape, :sort, :sort_index, :inverse, :lu, :delete_at, :where, :where2, :not, :-@].each(&method(:gen_matrix_delegator))
222
231
  [:sum, :prod, :mean, :stddev, :rms, :rmsdev, :min, :max, :shape].each(&method(:gen_delegator))
232
+ [:byte, :sint, :int, :sfloat, :float, :scomplex, :complex, :object].each(&method(:gen_typeconstructor))
233
+
223
234
  [:+, :/, :-, :**, :&, :^, :|].each do |op|
224
235
  define_method(op) do |other|
225
236
  op = translate_op(op)
237
+ result = if other.kind_of?(Matrix)
238
+ apply_elementwise(op, other)
239
+ else
240
+ apply_scalar(op, other)
241
+ end
226
242
  Matrix.new(
227
- if other.kind_of?(Matrix)
228
- apply_elementwise(op, other)
229
- else
230
- apply_scalar(op, other)
231
- end
243
+ result, typecode: typecode
232
244
  )
233
245
  end
234
246
  end
@@ -254,7 +266,7 @@ module RMatrix
254
266
  if test_inverse
255
267
  other.narray.send(op, self.narray)
256
268
  else
257
- narray.send(op, other.narray)
269
+ narray.send(op, Matrix[other].narray)
258
270
  end
259
271
  end
260
272
 
@@ -7,7 +7,15 @@ module RMatrix
7
7
  end
8
8
 
9
9
  def self.[](*inputs)
10
- Vector.new(inputs)
10
+ if inputs.length == 1 && [String, Symbol].include?(inputs[0].class)
11
+ if ['byte', 'sint', 'int', 'sfloat', 'float', 'scomplex', 'complex', 'object'].include?(inputs[0].to_s)
12
+ ->(*source){ Matrix.new(source, typecode: inputs[0].to_s) }
13
+ else
14
+ Vector.new(inputs[0])
15
+ end
16
+ else
17
+ Vector.new(inputs)
18
+ end
11
19
  end
12
20
 
13
21
  def inspect
@@ -1,3 +1,3 @@
1
1
  module Rmatrix
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
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.0
4
+ version: 0.1.1
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-06 00:00:00.000000000 Z
11
+ date: 2016-04-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler