mdarray 0.4.0-java

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.
Files changed (44) hide show
  1. data/LICENSE.txt +54 -0
  2. data/LICENSE.txt~ +32 -0
  3. data/README.md +21 -0
  4. data/Rakefile +40 -0
  5. data/lib/env.rb +11 -0
  6. data/lib/mdarray.rb +414 -0
  7. data/lib/mdarray/access.rb +237 -0
  8. data/lib/mdarray/counter.rb +779 -0
  9. data/lib/mdarray/creation.rb +413 -0
  10. data/lib/mdarray/fast_non_numerical.rb +102 -0
  11. data/lib/mdarray/function_creation.rb +100 -0
  12. data/lib/mdarray/function_map.rb +56 -0
  13. data/lib/mdarray/hierarchy.rb +177 -0
  14. data/lib/mdarray/operators.rb +220 -0
  15. data/lib/mdarray/printing.rb +275 -0
  16. data/lib/mdarray/proc_util.rb +159 -0
  17. data/lib/mdarray/ruby_functions.rb +78 -0
  18. data/lib/mdarray/ruby_generic_functions.rb +37 -0
  19. data/lib/mdarray/ruby_math.rb +57 -0
  20. data/lib/mdarray/ruby_numeric_functions.rb +187 -0
  21. data/lib/mdarray/ruby_operators.rb +201 -0
  22. data/lib/mdarray/ruby_stats.rb +149 -0
  23. data/lib/mdarray/slices.rb +185 -0
  24. data/lib/mdarray/statistics.rb +86 -0
  25. data/test/arithmetic_casting.rb +195 -0
  26. data/test/env.rb +50 -0
  27. data/test/test_access.rb +247 -0
  28. data/test/test_boolean.rb +67 -0
  29. data/test/test_comparison.rb +126 -0
  30. data/test/test_complete.rb +69 -0
  31. data/test/test_counter.rb +184 -0
  32. data/test/test_creation.rb +364 -0
  33. data/test/test_error.rb +53 -0
  34. data/test/test_lazy.rb +52 -0
  35. data/test/test_operator.rb +337 -0
  36. data/test/test_printing.rb +66 -0
  37. data/test/test_shape.rb +96 -0
  38. data/test/test_slices.rb +146 -0
  39. data/test/test_speed.rb +311 -0
  40. data/test/test_statistics.rb +45 -0
  41. data/test/test_trigonometry.rb +60 -0
  42. data/vendor/netcdfAll-4.3.16.jar +0 -0
  43. data/version.rb +2 -0
  44. metadata +197 -0
@@ -0,0 +1,413 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ ##########################################################################################
4
+ # Copyright © 2013 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
5
+ # and distribute this software and its documentation, without fee and without a signed
6
+ # licensing agreement, is hereby granted, provided that the above copyright notice, this
7
+ # paragraph and the following two paragraphs appear in all copies, modifications, and
8
+ # distributions.
9
+ #
10
+ # IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
11
+ # INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
12
+ # THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
13
+ # POSSIBILITY OF SUCH DAMAGE.
14
+ #
15
+ # RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16
+ # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
17
+ # SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
18
+ # RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
19
+ # OR MODIFICATIONS.
20
+ ##########################################################################################
21
+
22
+ ##########################################################################################
23
+ #
24
+ ##########################################################################################
25
+
26
+ class MDArray
27
+
28
+ #------------------------------------------------------------------------------------
29
+ # Given two types returns the upcasted one
30
+ #------------------------------------------------------------------------------------
31
+
32
+ def self.upcast(type1, type2)
33
+
34
+ type1_i = MDArray.numerical.index(type1)
35
+ type2_i = MDArray.numerical.index(type2)
36
+ type_i = (type1_i < type2_i)? type2_i : type1_i
37
+ type = MDArray.numerical.at(type_i)
38
+
39
+ end
40
+
41
+ #------------------------------------------------------------------------------------
42
+ #
43
+ #------------------------------------------------------------------------------------
44
+
45
+ def self.binary_operator=(operator)
46
+ @@binary_operator = operator
47
+ end
48
+
49
+ def self.binary_operator
50
+ @@binary_operator
51
+ end
52
+
53
+ def self.unary_operator=(operator)
54
+ @@unary_operator = operator
55
+ end
56
+
57
+ def self.unary_operator
58
+ @@unary_operator
59
+ end
60
+
61
+ def get_binary_op
62
+ (@binary_operator)? @binary_operator : @@binary_operator
63
+ end
64
+
65
+ def get_unary_op
66
+ (@unary_operator)? @unary_operator : @@unary_operator
67
+ end
68
+
69
+ # Factory Methods
70
+
71
+ #------------------------------------------------------------------------------------
72
+ # Builds a new MDArray
73
+ # @param type the type of the new mdarray to build, could be boolean, byte, short,
74
+ # int, long, float, double, string, structure
75
+ # @param shape [Array] the shape of the mdarray as a ruby array
76
+ # @param storage [Array] a ruby array with the initialization data
77
+ #------------------------------------------------------------------------------------
78
+
79
+ def self.build(type, shape, storage = nil)
80
+
81
+ dtype = DataType.valueOf(type.upcase)
82
+ jshape = shape.to_java :int
83
+
84
+ if (storage)
85
+ jstorage = storage.to_java type.downcase.to_sym
86
+ nc_array = Java::UcarMa2.Array.factory(dtype, jshape, jstorage)
87
+ else
88
+ nc_array = Java::UcarMa2.Array.factory(dtype, jshape)
89
+ end
90
+
91
+ klass = Object.const_get("#{type.capitalize}MDArray")
92
+ return klass.new(type, nc_array)
93
+
94
+ end
95
+
96
+ #------------------------------------------------------------------------------------
97
+ # Builds a boolean mdarray
98
+ # @param shape [Array] the shape of the mdarray as a ruby array
99
+ # @param storage [Array] a ruby array with the initialization data
100
+ #------------------------------------------------------------------------------------
101
+
102
+ def self.boolean(shape, storage = nil)
103
+ self.build("boolean", shape, storage)
104
+ end
105
+
106
+ #------------------------------------------------------------------------------------
107
+ # Builds a byte mdarray
108
+ # @param shape [Array] the shape of the mdarray as a ruby array
109
+ # @param storage [Array] a ruby array with the initialization data
110
+ #------------------------------------------------------------------------------------
111
+
112
+ def self.byte(shape, storage = nil)
113
+ self.build("byte", shape, storage)
114
+ end
115
+
116
+ #------------------------------------------------------------------------------------
117
+ # Builds a char mdarray
118
+ # @param shape [Array] the shape of the mdarray as a ruby array
119
+ # @param storage [Array] a ruby array with the initialization data
120
+ #------------------------------------------------------------------------------------
121
+
122
+ def self.char(shape, storage = nil)
123
+ self.build("char", shape, storage)
124
+ end
125
+
126
+ #------------------------------------------------------------------------------------
127
+ # Builds a byte mdarray
128
+ # @param shape [Array] the shape of the mdarray as a ruby array
129
+ # @param storage [Array] a ruby array with the initialization data
130
+ #
131
+ #------------------------------------------------------------------------------------
132
+
133
+ def self.short(shape, storage = nil)
134
+ self.build("short", shape, storage)
135
+ end
136
+
137
+ #------------------------------------------------------------------------------------
138
+ # Builds an int mdarray
139
+ # @param shape [Array] the shape of the mdarray as a ruby array
140
+ # @param storage [Array] a ruby array with the initialization data
141
+ #------------------------------------------------------------------------------------
142
+
143
+ def self.int(shape, storage = nil)
144
+ self.build("int", shape, storage)
145
+ end
146
+
147
+ #------------------------------------------------------------------------------------
148
+ # Builds a long mdarray
149
+ # @param shape [Array] the shape of the mdarray as a ruby array
150
+ # @param storage [Array] a ruby array with the initialization data
151
+ #
152
+ #------------------------------------------------------------------------------------
153
+
154
+ def self.long(shape, storage = nil)
155
+ self.build("long", shape, storage)
156
+ end
157
+
158
+ #------------------------------------------------------------------------------------
159
+ # Builds a float mdarray
160
+ # @param shape [Array] the shape of the mdarray as a ruby array
161
+ # @param storage [Array] a ruby array with the initialization data
162
+ #------------------------------------------------------------------------------------
163
+
164
+ def self.float(shape, storage = nil)
165
+ self.build("float", shape, storage)
166
+ end
167
+
168
+ #------------------------------------------------------------------------------------
169
+ # Builds a double mdarray
170
+ # @param shape [Array] the shape of the mdarray as a ruby array
171
+ # @param storage [Array] a ruby array with the initialization data
172
+ #------------------------------------------------------------------------------------
173
+
174
+ def self.double(shape, storage = nil)
175
+ self.build("double", shape, storage)
176
+ end
177
+
178
+ #------------------------------------------------------------------------------------
179
+ # Builds a string mdarray
180
+ # @param shape [Array] the shape of the mdarray as a ruby array
181
+ # @param storage [Array] a ruby array with the initialization data
182
+ #------------------------------------------------------------------------------------
183
+
184
+ def self.string(shape, storage = nil)
185
+ self.build("string", shape, storage)
186
+ end
187
+
188
+ #------------------------------------------------------------------------------------
189
+ # Builds a structure mdarray
190
+ # @param shape [Array] the shape of the mdarray as a ruby array
191
+ # @param storage [Array] a ruby array with the initialization data
192
+ #------------------------------------------------------------------------------------
193
+
194
+ def self.structure(shape, storage = nil)
195
+ self.build("structure", shape, storage)
196
+ end
197
+
198
+ #------------------------------------------------------------------------------------
199
+ # Construct an array by executing a function over each coordinate.
200
+ # The resulting array therefore has a value ``fn(x, y, z)`` at
201
+ # coordinate ``(x, y, z)``.
202
+ # Parameters
203
+ # ----------
204
+ # @param type : data-type, optional
205
+ # Data-type of the coordinate arrays passed to `fn`
206
+ # @param shape : (N,) tuple of ints
207
+ # Shape of the output array, which also determines the shape of
208
+ # the coordinate arrays passed to `fn`.
209
+ # @param &block: a block to be executed
210
+ # The block is called with N parameters, each of which
211
+ # represents the coordinates of the array varying along a
212
+ # specific axis. For example, if `shape` were ``(2, 2)``, then
213
+ # the parameters would be two arrays, ``[[0, 0], [1, 1]]`` and
214
+ # ``[[0, 1], [0, 1]]``. `fn` must be capable of operating on
215
+ # arrays, and should return a scalar value.
216
+ #------------------------------------------------------------------------------------
217
+
218
+ def self.fromfunction(type, shape, &block)
219
+
220
+ dtype = DataType.valueOf(type.upcase)
221
+ jshape = shape.to_java :int
222
+ arr = self.build(type, shape)
223
+ arr.dim_set(nil, block)
224
+ return arr
225
+
226
+ end
227
+
228
+ #------------------------------------------------------------------------------------
229
+ # Build mdarray and fills it with the given value
230
+ # @param type type of the mdarray
231
+ # @param shape
232
+ # @param value the given value to fill in the mdarray
233
+ #------------------------------------------------------------------------------------
234
+
235
+ def self.init_with(type, shape, value)
236
+
237
+ size = 1
238
+ shape.each do |val|
239
+ size = size * val
240
+ end
241
+
242
+ storage = Array.new(size, value)
243
+ self.build(type, shape, storage)
244
+
245
+ end
246
+
247
+ #------------------------------------------------------------------------------------
248
+ # Return evenly spaced values within a given interval.
249
+ # Values are generated within the half-open interval [start, stop) (in other words,
250
+ # the interval including start but excluding stop). For integer arguments the function
251
+ # is equivalent to the Python built-in range function, but returns an mdarray rather
252
+ # than a list.
253
+ # When using a non-integer step, such as 0.1, the results will often not be
254
+ # consistent. It is better to use linspace for these cases.
255
+ # @param start
256
+ # @param stop
257
+ # @param step
258
+ # @return int mdarray
259
+ #------------------------------------------------------------------------------------
260
+
261
+ def self.arange(*args)
262
+
263
+ case args.size
264
+ when 1
265
+ start = 0
266
+ last = args[0]
267
+ stride = 1
268
+ when 2
269
+ start = args[0]
270
+ last = args[1]
271
+ stride = 1
272
+ when 3
273
+ start = args[0]
274
+ last = args[1]
275
+ stride = args[2]
276
+ else
277
+ raise "Method arange can have at most 3 arguments"
278
+ end
279
+
280
+ arr = Array.new
281
+ (start...last).step(stride) { |val| arr << val }
282
+ self.build("int", [arr.size], arr)
283
+
284
+ end
285
+
286
+ #------------------------------------------------------------------------------------
287
+ # Return evenly spaced values within a given interval.
288
+ # Values are generated within the half-open interval [start, stop) (in other words,
289
+ # the interval including start but excluding stop). For integer arguments the function
290
+ # is equivalent to the Python built-in range function, but returns an mdarray rather
291
+ # than a list.
292
+ # @param type the desired type of the new mdarray
293
+ # @param start
294
+ # @param stop
295
+ # @param step
296
+ # @returns mdarray of the given type
297
+ #------------------------------------------------------------------------------------
298
+
299
+ def self.typed_arange(type, *args)
300
+
301
+ case args.size
302
+ when 1
303
+ start = 0
304
+ last = args[0]
305
+ stride = 1
306
+ when 2
307
+ start = args[0]
308
+ last = args[1]
309
+ stride = 1
310
+ when 3
311
+ start = args[0]
312
+ last = args[1]
313
+ stride = args[2]
314
+ else
315
+ raise "Method arange can have at most 3 arguments"
316
+ end
317
+
318
+ arr = Array.new
319
+ (start...last).step(stride) { |val| arr << val }
320
+ self.build(type, [arr.size], arr)
321
+
322
+ end
323
+
324
+ #------------------------------------------------------------------------------------
325
+ #
326
+ #------------------------------------------------------------------------------------
327
+
328
+ def self.linspace(type, start, stop, number)
329
+
330
+ arr = (start..stop).step((stop-start).to_f/(number-1)).map{|x| x }
331
+ self.build(type, [arr.size], arr)
332
+
333
+ end
334
+
335
+ #------------------------------------------------------------------------------------
336
+ #
337
+ #------------------------------------------------------------------------------------
338
+
339
+ def self.ones(type, shape)
340
+ init_with(type, shape, 1)
341
+ end
342
+
343
+ #------------------------------------------------------------------------------------
344
+ #
345
+ #------------------------------------------------------------------------------------
346
+
347
+ def self.register_function(name, func)
348
+
349
+ if ((list = MDArray.function_map[name]) == nil)
350
+ list = (MDArray.function_map[name] = Array.new)
351
+ end
352
+ list << FunctionMap.new(name, func[0], func[1], func[2], func[3], func[4], func[5])
353
+
354
+ end
355
+
356
+ #------------------------------------------------------------------------------------
357
+ #
358
+ #------------------------------------------------------------------------------------
359
+
360
+ protected
361
+
362
+ #------------------------------------------------------------------------------------
363
+ # Builds a new CoercedMDArray
364
+ #------------------------------------------------------------------------------------
365
+
366
+ def coerced_build(type, nc_array)
367
+
368
+ klass = Object.const_get("#{type.capitalize}MDArray")
369
+ instance = klass.new(type, nc_array)
370
+ instance.coerced = true
371
+ return instance
372
+
373
+ end
374
+
375
+ #------------------------------------------------------------------------------------
376
+ #
377
+ #------------------------------------------------------------------------------------
378
+
379
+ private
380
+
381
+ #------------------------------------------------------------------------------------
382
+ # Get the shape: length of array in each dimension.
383
+ #------------------------------------------------------------------------------------
384
+
385
+ def self.get_shape(jarray)
386
+ jarray.getShape().to_a
387
+ end
388
+
389
+ #------------------------------------------------------------------------------------
390
+ # Get the number of dimensions of the array.
391
+ #------------------------------------------------------------------------------------
392
+
393
+ def self.get_rank(jarray)
394
+ jarray.getRank()
395
+ end
396
+
397
+ #------------------------------------------------------------------------------------
398
+ # Gets the size of the array.
399
+ #------------------------------------------------------------------------------------
400
+
401
+ def self.get_size(jarray)
402
+ jarray.getSize()
403
+ end
404
+
405
+ #------------------------------------------------------------------------------------
406
+ #
407
+ #------------------------------------------------------------------------------------
408
+
409
+ def self.get_element_type(jarray)
410
+ jarray.getElementType()
411
+ end
412
+
413
+ end
@@ -0,0 +1,102 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ ##########################################################################################
4
+ # Copyright © 2013 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
5
+ # and distribute this software and its documentation, without fee and without a signed
6
+ # licensing agreement, is hereby granted, provided that the above copyright notice, this
7
+ # paragraph and the following two paragraphs appear in all copies, modifications, and
8
+ # distributions.
9
+ #
10
+ # IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
11
+ # INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
12
+ # THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
13
+ # POSSIBILITY OF SUCH DAMAGE.
14
+ #
15
+ # RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16
+ # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
17
+ # SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
18
+ # RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
19
+ # OR MODIFICATIONS.
20
+ ##########################################################################################
21
+
22
+ ##########################################################################################
23
+ #
24
+ ##########################################################################################
25
+
26
+ class FastBoolean < BooleanMDArray
27
+
28
+
29
+ #---------------------------------------------------------------------------------------
30
+ #
31
+ #---------------------------------------------------------------------------------------
32
+
33
+ def initialize(storage, section = false)
34
+ super("boolean", storage, section)
35
+ end
36
+
37
+ #---------------------------------------------------------------------------------------
38
+ # Executes a boolean operator. Valid boolean operators are: and, or. Only applicable
39
+ # to boolean operands
40
+ #---------------------------------------------------------------------------------------
41
+
42
+ def exec_boolean_op(dest, other_val, method)
43
+
44
+ proc = Proc.new { |elmt, value| method.call(elmt, value) }
45
+
46
+ if ((other_val.is_a? TrueClass) || (other_val.is_a? FalseClass))
47
+ @helper.send("booleanOperationWithBool", dest.nc_array, @nc_array, other_val, proc)
48
+ elsif (other_val.is_a? MDArray)
49
+ if (compatible(other_val))
50
+ @helper.send("booleanOperationWithArray", dest.nc_array, @nc_array,
51
+ other_val.nc_array, proc)
52
+ else
53
+ raise "Invalid operation - arrays are incompatible"
54
+ end
55
+ else
56
+ raise "Invalid operation with: #{other_val}"
57
+ end
58
+
59
+ return dest
60
+
61
+ end
62
+
63
+ #---------------------------------------------------------------------------------------
64
+ # Executes a boolean operator. Valid boolean operators are: and, or. Only applicable
65
+ # to boolean operands
66
+ #---------------------------------------------------------------------------------------
67
+
68
+ def exec_bin_op(op1, op2, proc, base)
69
+
70
+ args = Array.new
71
+ args << @nc_array
72
+ (op1)? args << op1.nc_array : nil
73
+
74
+ if (op2.is_a? Numeric)
75
+ name = base + "WithNumber"
76
+ args << op2
77
+ elsif (op2.is_a? NumericalMDArray)
78
+ if (compatible(op2))
79
+ name = base + "WithArray"
80
+ args << op2.nc_array
81
+ else
82
+ raise "Invalid operation - arrays are incompatible"
83
+ end
84
+ else
85
+ raise "Invalid operation with: #{op2}"
86
+ =begin
87
+ # *TODO: make it more general using coerce if other_val type is not recognized
88
+ if (arg is not recognized)
89
+ self_equiv, arg_equiv = arg.coerce(self)
90
+ self_equiv * arg_equiv
91
+ end
92
+ =end
93
+ end
94
+
95
+ args << proc
96
+ @helper.send(make_function_name(name), *args)
97
+ return self
98
+
99
+ end
100
+
101
+ end # FastBoolean
102
+