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.
- data/LICENSE.txt +35 -16
- data/README.md +180 -234
- data/lib/colt/matrix/algebra.rb +466 -0
- data/lib/colt/matrix/colt_matrix.rb +161 -220
- data/lib/colt/matrix/creation.rb +374 -0
- data/lib/colt/matrix/{matrix_hierarchy.rb → hierarchy.rb} +78 -99
- data/lib/colt/matrix/property.rb +363 -0
- data/lib/mdarray/views.rb +46 -1
- data/test/colt/matrix/test_complete.rb +29 -0
- data/test/colt/{test_matrix.rb → matrix/test_creation.rb} +26 -3
- data/test/colt/{test_double_matrix2d.rb → matrix/test_matrix1d_floatingalgebra.rb} +86 -84
- data/test/colt/matrix/test_matrix2d_fixpointalgebra.rb +531 -0
- data/test/colt/matrix/test_matrix2d_floatingalgebra.rb +693 -0
- data/test/colt/matrix/test_operations.rb +120 -0
- data/test/colt/matrix/test_properties.rb +649 -0
- data/test/colt/test_complete.rb +1 -1
- data/test/complete.rb +12 -7
- data/test/mdarray/test_views.rb +55 -0
- data/vendor/{incanter.jar → mdarray.jar} +0 -0
- data/version.rb +1 -1
- metadata +22 -16
- data/lib/colt/matrix/matrix2D_floating_algebra.rb +0 -325
- data/test/colt/test_float_matrix2d.rb +0 -171
- data/vendor/commons-compiler.jar +0 -0
- data/vendor/janino.jar +0 -0
@@ -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
|
-
|
43
|
+
attr_accessor :coerced
|
42
44
|
|
43
45
|
#------------------------------------------------------------------------------------
|
44
46
|
#
|
45
47
|
#------------------------------------------------------------------------------------
|
46
|
-
|
47
|
-
def
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
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
|
59
|
-
if (
|
60
|
-
|
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
|
70
|
-
|
118
|
+
def each(&block)
|
119
|
+
@mdarray.each(&block)
|
71
120
|
end
|
72
121
|
|
73
122
|
#------------------------------------------------------------------------------------
|
74
123
|
#
|
75
124
|
#------------------------------------------------------------------------------------
|
76
125
|
|
77
|
-
def
|
78
|
-
|
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
|
86
|
-
|
134
|
+
def reset_traversal
|
135
|
+
@mdarray.reset_traversal
|
87
136
|
end
|
88
137
|
|
89
138
|
#------------------------------------------------------------------------------------
|
90
139
|
#
|
91
140
|
#------------------------------------------------------------------------------------
|
92
141
|
|
93
|
-
def
|
94
|
-
|
142
|
+
def next
|
143
|
+
@mdarray.next
|
95
144
|
end
|
96
145
|
|
97
146
|
#------------------------------------------------------------------------------------
|
98
|
-
#
|
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
|
150
|
+
def fill(val, func = nil)
|
104
151
|
|
105
|
-
|
152
|
+
if (func)
|
153
|
+
return MDMatrix.from_colt_matrix(@colt_matrix.assign(val.colt_matrix, func))
|
154
|
+
end
|
106
155
|
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
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
|
161
|
+
raise "Cannot fill a Matrix with the given value"
|
115
162
|
end
|
116
|
-
|
117
163
|
end
|
118
164
|
|
119
165
|
#------------------------------------------------------------------------------------
|
120
|
-
#
|
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
|
182
|
-
@colt_matrix.
|
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
|
190
|
-
@
|
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
|
198
|
+
def set(row, column, val)
|
199
|
+
@colt_matrix.set(row, column, val)
|
200
|
+
end
|
198
201
|
|
199
|
-
|
202
|
+
alias :[]= :set
|
200
203
|
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
formatter = FloatFormatter.new
|
205
|
-
when "long"
|
206
|
-
formatter = LongFormatter.new
|
207
|
-
when "int"
|
208
|
-
formatter = IntFormatter.new
|
204
|
+
#------------------------------------------------------------------------------------
|
205
|
+
#
|
206
|
+
#------------------------------------------------------------------------------------
|
209
207
|
|
210
|
-
|
208
|
+
def get(*index)
|
209
|
+
@colt_matrix.get(*index)
|
210
|
+
end
|
211
211
|
|
212
|
-
|
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
|
-
|
227
|
+
def normalize!
|
228
|
+
@colt_matrix.normalize
|
229
|
+
end
|
221
230
|
|
222
231
|
#------------------------------------------------------------------------------------
|
223
232
|
#
|
224
233
|
#------------------------------------------------------------------------------------
|
225
234
|
|
226
|
-
def
|
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
|
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
|
314
|
-
|
315
|
-
|
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
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
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
|
-
|
331
|
-
|
332
|
-
|
333
|
-
# p stride2
|
278
|
+
#------------------------------------------------------------------------------------
|
279
|
+
#
|
280
|
+
#------------------------------------------------------------------------------------
|
334
281
|
|
335
|
-
|
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
|
-
|
344
|
-
offset, 0, 0, stride0, stride1, stride2, false)
|
345
|
-
DoubleMDMatrix3D.new(mdarray, colt_matrix)
|
287
|
+
formatter = DoubleFormatter.new
|
346
288
|
when "float"
|
347
|
-
|
348
|
-
offset, 0, 0, stride0, stride1, stride2, false)
|
349
|
-
FloatMDMatrix3D.new(mdarray, colt_matrix)
|
289
|
+
formatter = FloatFormatter.new
|
350
290
|
when "long"
|
351
|
-
|
352
|
-
offset, 0, 0, stride0, stride1, stride2, false)
|
353
|
-
LongMDMatrix3D.new(mdarray, colt_matrix)
|
291
|
+
formatter = LongFormatter.new
|
354
292
|
when "int"
|
355
|
-
|
356
|
-
|
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 '
|
365
|
-
require_relative '
|
303
|
+
require_relative 'creation'
|
304
|
+
require_relative 'hierarchy'
|
305
|
+
require_relative 'algebra'
|
306
|
+
require_relative 'property'
|