mdarray 0.5.4-java → 0.5.5-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -37,329 +37,270 @@ class MDMatrix
37
37
 
38
38
 
39
39
  attr_reader :colt_matrix
40
+ attr_reader :colt_algebra
41
+ attr_reader :colt_property
40
42
  attr_reader :mdarray
41
- attr_reader :rank
43
+ attr_accessor :coerced
42
44
 
43
45
  #------------------------------------------------------------------------------------
44
46
  #
45
47
  #------------------------------------------------------------------------------------
46
-
47
- def initialize(mdarray, colt_matrix)
48
- @mdarray = mdarray
49
- @colt_matrix = colt_matrix
50
- @rank = @mdarray.rank
51
- @algebra = nil
48
+
49
+ def coerce(num)
50
+
51
+ matrix = MDMatrix.from_mdarray(@mdarray)
52
+ matrix.coerced = true
53
+ [matrix, num]
54
+
55
+ end
56
+
57
+ #------------------------------------------------------------------------------------
58
+ #
59
+ #------------------------------------------------------------------------------------
60
+
61
+ def add(other_val)
62
+ if (other_val.is_a? Numeric)
63
+ MDMatrix.from_mdarray(@mdarray + other_val)
64
+ elsif (other_val.is_a? MDMatrix)
65
+ MDMatrix.from_mdarray(@mdarray + other_val.mdarray)
66
+ else
67
+ raise "Cannot add a matrix to the given value"
68
+ end
69
+ end
70
+
71
+ alias :+ :add
72
+
73
+ #------------------------------------------------------------------------------------
74
+ #
75
+ #------------------------------------------------------------------------------------
76
+
77
+ def div(other_val)
78
+
79
+ if (other_val.is_a? Numeric)
80
+ val1, val2 = (@coerced)? [other_val, @mdarray] : [@mdarray, other_val]
81
+ MDMatrix.from_mdarray(val1 / val2)
82
+ elsif (other_val.is_a? MDMatrix)
83
+ begin
84
+ self * other_val.inverse
85
+ rescue Exception => e
86
+ puts e.message
87
+ raise "Dividing by singular matrix is not possible"
88
+ end
89
+ else
90
+ raise "Cannot divide the given value from matrix"
91
+ end
92
+
52
93
  end
53
94
 
95
+ alias :/ :div
96
+
54
97
  #------------------------------------------------------------------------------------
55
98
  #
56
99
  #------------------------------------------------------------------------------------
57
100
 
58
- def self.build(type, shape, storage = nil)
59
- if (shape.size > 3)
60
- raise "Cannot create MDMatrix of size greater than 3"
101
+ def sub(other_val)
102
+ if (other_val.is_a? Numeric)
103
+ val1, val2 = (@coerced)? [other_val, @mdarray] : [@mdarray, other_val]
104
+ MDMatrix.from_mdarray(val1 - val2)
105
+ elsif (other_val.is_a? MDMatrix)
106
+ MDMatrix.from_mdarray(@mdarray - matrix.mdarray)
107
+ else
108
+ raise "Cannot subtract the given value from matrix"
61
109
  end
62
- self.from_mdarray(MDArray.build(type, shape, storage))
63
110
  end
64
111
 
112
+ alias :- :sub
113
+
65
114
  #------------------------------------------------------------------------------------
66
115
  #
67
116
  #------------------------------------------------------------------------------------
68
117
 
69
- def self.double(shape, storage = nil)
70
- self.build("double", shape, storage)
118
+ def each(&block)
119
+ @mdarray.each(&block)
71
120
  end
72
121
 
73
122
  #------------------------------------------------------------------------------------
74
123
  #
75
124
  #------------------------------------------------------------------------------------
76
125
 
77
- def self.float(shape, storage = nil)
78
- self.build("float", shape, storage)
126
+ def each_with_counter(&block)
127
+ @mdarray.each_with_counter(&block)
79
128
  end
80
129
 
81
130
  #------------------------------------------------------------------------------------
82
131
  #
83
132
  #------------------------------------------------------------------------------------
84
133
 
85
- def self.long(shape, storage = nil)
86
- self.build("long", shape, storage)
134
+ def reset_traversal
135
+ @mdarray.reset_traversal
87
136
  end
88
137
 
89
138
  #------------------------------------------------------------------------------------
90
139
  #
91
140
  #------------------------------------------------------------------------------------
92
141
 
93
- def self.int(shape, storage = nil)
94
- self.build("int", shape, storage)
142
+ def next
143
+ @mdarray.next
95
144
  end
96
145
 
97
146
  #------------------------------------------------------------------------------------
98
- # Creates a MDMatrix from an MDArray.
99
- # (int rows, int columns, double[] elements, int rowZero, int columnZero,
100
- # int rowStride, int columnStride, boolean isView)
147
+ # Fills the array with the given value
101
148
  #------------------------------------------------------------------------------------
102
149
 
103
- def self.from_mdarray(mdarray)
150
+ def fill(val, func = nil)
104
151
 
105
- case mdarray.rank
152
+ if (func)
153
+ return MDMatrix.from_colt_matrix(@colt_matrix.assign(val.colt_matrix, func))
154
+ end
106
155
 
107
- when 1
108
- dense1D(mdarray)
109
- when 2
110
- dense2D(mdarray)
111
- when 3
112
- dense3D(mdarray)
156
+ if ((val.is_a? Numeric) || (val.is_a? Proc) || (val.is_a? Class))
157
+ MDMatrix.from_colt_matrix(@colt_matrix.assign(val))
158
+ elsif (val.is_a? MDMatrix)
159
+ MDMatrix.from_colt_matrix(@colt_matrix.assign(val.colt_matrix))
113
160
  else
114
- raise "Cannot create MDMatrix of rank greater than 3"
161
+ raise "Cannot fill a Matrix with the given value"
115
162
  end
116
-
117
163
  end
118
164
 
119
165
  #------------------------------------------------------------------------------------
120
- # Creates a new MDMatrix from a given colt_matrix
121
- #------------------------------------------------------------------------------------
122
-
123
- def self.from_colt_matrix(colt_matrix)
124
-
125
- if (colt_matrix.is_a? DenseDoubleMatrix3D)
126
- mdarray = MDArray.from_jstorage("double",
127
- [colt_matrix.slices, colt_matrix.rows,
128
- colt_matrix.columns], colt_matrix.elements)
129
- return DoubleMDMatrix3D.from_mdarray(mdarray)
130
- elsif (colt_matrix.is_a? DenseFloatMatrix3D)
131
- mdarray = MDArray.from_jstorage("float",
132
- [colt_matrix.slices, colt_matrix.rows,
133
- colt_matrix.columns], colt_matrix.elements)
134
- return FloatMDMatrix3D.from_mdarray(mdarray)
135
- elsif (colt_matrix.is_a? DenseLongMatrix3D)
136
- mdarray = MDArray.from_jstorage("long",
137
- [colt_matrix.slices, colt_matrix.rows,
138
- colt_matrix.columns], colt_matrix.elements)
139
- return LongMDMatrix3D.from_mdarray(mdarray)
140
- elsif (colt_matrix.is_a? DenseIntMatrix3D)
141
- mdarray = MDArray.from_jstorage("int",
142
- [colt_matrix.slices, colt_matrix.rows,
143
- colt_matrix.columns], colt_matrix.elements)
144
- return IntMDMatrix3D.from_mdarray(mdarray)
145
- elsif (colt_matrix.is_a? DenseDoubleMatrix2D)
146
- mdarray = MDArray.from_jstorage("double", [colt_matrix.rows, colt_matrix.columns],
147
- colt_matrix.elements)
148
- return DoubleMDMatrix2D.from_mdarray(mdarray)
149
- elsif (colt_matrix.is_a? DenseFloatMatrix2D)
150
- mdarray = MDArray.from_jstorage("float", [colt_matrix.rows, colt_matrix.columns],
151
- colt_matrix.elements)
152
- return FloatMDMatrix2D.from_mdarray(mdarray)
153
- elsif (colt_matrix.is_a? DenseLongMatrix2D)
154
- mdarray = MDArray.from_jstorage("long", [colt_matrix.rows, colt_matrix.columns],
155
- colt_matrix.elements)
156
- return LongMDMatrix2D.from_mdarray(mdarray)
157
- elsif (colt_matrix.is_a? DenseIntMatrix2D)
158
- mdarray = MDArray.from_jstorage("int", [colt_matrix.rows, colt_matrix.columns],
159
- colt_matrix.elements)
160
- return IntMDMatrix2D.from_mdarray(mdarray)
161
- elsif (colt_matrix.is_a? DenseDoubleMatrix1D)
162
- mdarray = MDArray.from_jstorage("double", [colt_matrix.size], colt_matrix.elements)
163
- return DoubleMDMatrix1D.from_mdarray(mdarray)
164
- elsif (colt_matrix.is_a? DenseFloatMatrix1D)
165
- mdarray = MDArray.from_jstorage("float", [colt_matrix.size], colt_matrix.elements)
166
- return FloatMDMatrix1D.from_mdarray(mdarray)
167
- elsif (colt_matrix.is_a? DenseLongMatrix1D)
168
- mdarray = MDArray.from_jstorage("long", [colt_matrix.size], colt_matrix.elements)
169
- return LongMDMatrix1D.from_mdarray(mdarray)
170
- elsif (colt_matrix.is_a? DenseIntMatrix1D)
171
- mdarray = MDArray.from_jstorage("int", [colt_matrix.size], colt_matrix.elements)
172
- return IntMDMatrix1D.from_mdarray(mdarray)
173
- end
166
+ # Fills the matrix based on a given condition
167
+ #------------------------------------------------------------------------------------
174
168
 
169
+ def fill_cond(cond, val)
170
+ return MDMatrix.from_colt_matrix(@colt_matrix.assign(cond, val))
175
171
  end
176
172
 
177
173
  #------------------------------------------------------------------------------------
178
- #
174
+ # Applies a function to each cell and aggregates the results. Returns a value v such
175
+ # that v==a(size()) where a(i) == aggr( a(i-1), f(get(row,column)) ) and terminators
176
+ # are a(1) == f(get(0,0)), a(0)==Double.NaN.
179
177
  #------------------------------------------------------------------------------------
180
178
 
181
- def normalize!
182
- @colt_matrix.normalize
179
+ def reduce(aggr, func, cond = nil)
180
+ (cond)? @colt_matrix.aggregate(aggr, func, cond) :
181
+ @colt_matrix.aggregate(aggr, func)
183
182
  end
184
183
 
185
184
  #------------------------------------------------------------------------------------
186
- #
185
+ # Reshapes the Matrix.
187
186
  #------------------------------------------------------------------------------------
188
-
189
- def sum
190
- @colt_matrix.zSum
187
+
188
+ def reshape!(shape)
189
+ @mdarray.reshape!(shape)
190
+ @colt_matrix = MDMatrix.from_mdarray(@mdarray).colt_matrix
191
+ self
191
192
  end
192
193
 
193
194
  #------------------------------------------------------------------------------------
194
195
  #
195
196
  #------------------------------------------------------------------------------------
196
197
 
197
- def print
198
+ def set(row, column, val)
199
+ @colt_matrix.set(row, column, val)
200
+ end
198
201
 
199
- case mdarray.type
202
+ alias :[]= :set
200
203
 
201
- when "double"
202
- formatter = DoubleFormatter.new
203
- when "float"
204
- formatter = FloatFormatter.new
205
- when "long"
206
- formatter = LongFormatter.new
207
- when "int"
208
- formatter = IntFormatter.new
204
+ #------------------------------------------------------------------------------------
205
+ #
206
+ #------------------------------------------------------------------------------------
209
207
 
210
- end
208
+ def get(*index)
209
+ @colt_matrix.get(*index)
210
+ end
211
211
 
212
- printf(formatter.toString(@colt_matrix))
212
+ alias :[] :get
213
213
 
214
+ #------------------------------------------------------------------------------------
215
+ # Create a new Array using same backing store as this Array, by flipping the index
216
+ # so that it runs from shape[index]-1 to 0.
217
+ #------------------------------------------------------------------------------------
218
+
219
+ def flip(dim)
220
+ MDMatrix.from_mdarray(@mdarray.flip(dim))
214
221
  end
215
-
222
+
216
223
  #------------------------------------------------------------------------------------
217
224
  #
218
225
  #------------------------------------------------------------------------------------
219
226
 
220
- private
227
+ def normalize!
228
+ @colt_matrix.normalize
229
+ end
221
230
 
222
231
  #------------------------------------------------------------------------------------
223
232
  #
224
233
  #------------------------------------------------------------------------------------
225
234
 
226
- def self.dense1D(mdarray)
227
-
228
- storage = mdarray.nc_array.getStorage()
229
- index = mdarray.nc_array.getIndex()
230
- size = index.size
231
-
232
- klass = index.getClass
233
- field = klass.getDeclaredField("stride0")
234
- field.setAccessible true
235
- stride0 = field.get(index)
236
- # p stride0
237
-
238
- klass = klass.getSuperclass()
239
- field = klass.getDeclaredField("offset")
240
- field.setAccessible true
241
- offset = field.get(index)
242
- # p offset
243
-
244
- case mdarray.type
245
- when "double"
246
- colt_matrix = DenseDoubleMatrix1D.new(size, storage, offset, stride0, false)
247
- DoubleMDMatrix1D.new(mdarray, colt_matrix)
248
- when "float"
249
- colt_matrix = DenseFloatMatrix1D.new(size, storage, offset, stride0, false)
250
- FloatMDMatrix1D.new(mdarray, colt_matrix)
251
- when "long"
252
- colt_matrix = DenseLongMatrix1D.new(size, storage, offset, stride0, false)
253
- LongMDMatrix1D.new(mdarray, colt_matrix)
254
- when "int"
255
- colt_matrix = DenseIntMatrix1D.new(size, storage, offset, stride0, false)
256
- IntMDMatrix1D.new(mdarray, colt_matrix)
257
- end
258
-
235
+ def rank
236
+ @mdarray.rank
259
237
  end
260
238
 
261
239
  #------------------------------------------------------------------------------------
262
240
  #
263
241
  #------------------------------------------------------------------------------------
264
242
 
265
- def self.dense2D(mdarray)
266
-
267
- storage = mdarray.nc_array.getStorage()
268
- index = mdarray.nc_array.getIndex()
269
- shape = index.getShape()
270
-
271
- klass = index.getClass
272
- field = klass.getDeclaredField("stride0")
273
- field.setAccessible true
274
- stride0 = field.get(index)
275
- # p stride0
276
-
277
- field = klass.getDeclaredField("stride1")
278
- field.setAccessible true
279
- stride1 = field.get(index)
280
- # p stride1
281
-
282
- klass = klass.getSuperclass()
283
- field = klass.getDeclaredField("offset")
284
- field.setAccessible true
285
- offset = field.get(index)
286
- # p offset
287
-
288
- case mdarray.type
289
- when "double"
290
- colt_matrix = DenseDoubleMatrix2D.new(shape[0], shape[1], storage, offset, 0,
291
- stride0, stride1, false)
292
- DoubleMDMatrix2D.new(mdarray, colt_matrix)
293
- when "float"
294
- colt_matrix = DenseFloatMatrix2D.new(shape[0], shape[1], storage, offset, 0,
295
- stride0, stride1, false)
296
- FloatMDMatrix2D.new(mdarray, colt_matrix)
297
- when "long"
298
- colt_matrix = DenseLongMatrix2D.new(shape[0], shape[1], storage, offset, 0,
299
- stride0, stride1, false)
300
- LongMDMatrix2D.new(mdarray, colt_matrix)
301
- when "int"
302
- colt_matrix = DenseIntMatrix2D.new(shape[0], shape[1], storage, offset, 0,
303
- stride0, stride1, false)
304
- IntMDMatrix2D.new(mdarray, colt_matrix)
305
- end
306
-
243
+ def shape
244
+ @mdarray.shape
307
245
  end
308
246
 
309
247
  #------------------------------------------------------------------------------------
310
248
  #
311
249
  #------------------------------------------------------------------------------------
312
250
 
313
- def self.dense3D(mdarray)
314
-
315
- storage = mdarray.nc_array.getStorage()
316
- index = mdarray.nc_array.getIndex()
317
- shape = index.getShape()
318
-
319
- klass = index.getClass
320
- field = klass.getDeclaredField("stride0")
321
- field.setAccessible true
322
- stride0 = field.get(index)
323
- # p stride0
251
+ def size
252
+ @mdarray.size
253
+ end
324
254
 
325
- field = klass.getDeclaredField("stride1")
326
- field.setAccessible true
327
- stride1 = field.get(index)
328
- # p stride1
255
+ #------------------------------------------------------------------------------------
256
+ # Makes a view of this array based on the given parameters
257
+ # shape
258
+ # origin
259
+ # size
260
+ # stride
261
+ # range
262
+ # section
263
+ # spec
264
+ #------------------------------------------------------------------------------------
265
+
266
+ def region(*args)
267
+ MDMatrix.from_mdarray(@mdarray.region(*args))
268
+ end
269
+
270
+ #------------------------------------------------------------------------------------
271
+ #
272
+ #------------------------------------------------------------------------------------
273
+
274
+ def sum
275
+ @colt_matrix.zSum
276
+ end
329
277
 
330
- field = klass.getDeclaredField("stride2")
331
- field.setAccessible true
332
- stride2 = field.get(index)
333
- # p stride2
278
+ #------------------------------------------------------------------------------------
279
+ #
280
+ #------------------------------------------------------------------------------------
334
281
 
335
- klass = klass.getSuperclass()
336
- field = klass.getDeclaredField("offset")
337
- field.setAccessible true
338
- offset = field.get(index)
339
- # p offset
282
+ def print
340
283
 
341
284
  case mdarray.type
285
+
342
286
  when "double"
343
- colt_matrix = DenseDoubleMatrix3D.new(shape[0], shape[1], shape[2], storage,
344
- offset, 0, 0, stride0, stride1, stride2, false)
345
- DoubleMDMatrix3D.new(mdarray, colt_matrix)
287
+ formatter = DoubleFormatter.new
346
288
  when "float"
347
- colt_matrix = DenseFloatMatrix3D.new(shape[0], shape[1], shape[2], storage,
348
- offset, 0, 0, stride0, stride1, stride2, false)
349
- FloatMDMatrix3D.new(mdarray, colt_matrix)
289
+ formatter = FloatFormatter.new
350
290
  when "long"
351
- colt_matrix = DenseLongMatrix3D.new(shape[0], shape[1], shape[2], storage,
352
- offset, 0, 0, stride0, stride1, stride2, false)
353
- LongMDMatrix3D.new(mdarray, colt_matrix)
291
+ formatter = LongFormatter.new
354
292
  when "int"
355
- colt_matrix = DenseIntMatrix3D.new(shape[0], shape[1], shape[2], storage,
356
- offset, 0, 0, stride0, stride1, stride2, false)
357
- IntMDMatrix3D.new(mdarray, colt_matrix)
293
+ formatter = IntFormatter.new
294
+
358
295
  end
359
296
 
297
+ printf(formatter.toString(@colt_matrix))
298
+
360
299
  end
361
300
 
362
301
  end # MDMatrix
363
302
 
364
- require_relative 'matrix_hierarchy'
365
- require_relative 'matrix2D_floating_algebra'
303
+ require_relative 'creation'
304
+ require_relative 'hierarchy'
305
+ require_relative 'algebra'
306
+ require_relative 'property'