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,37 @@
1
+
2
+ ##########################################################################################
3
+ #
4
+ ##########################################################################################
5
+
6
+ module GenericFunctions
7
+ extend FunctionCreation
8
+ extend RubyFunctions
9
+
10
+ @null = Proc.new { }
11
+
12
+ #---------------------------------------------------------------------------------------
13
+ #
14
+ #---------------------------------------------------------------------------------------
15
+
16
+ make_unary_op("dim_set", :set_block,
17
+ ["ruby_dim_set", "RubyFunctions", @null, "*", "*", "void"])
18
+
19
+ #---------------------------------------------------------------------------------------
20
+ #
21
+ #---------------------------------------------------------------------------------------
22
+
23
+ make_binary_op("fill", :fill,
24
+ ["ruby_fill", "RubyFunctions", @null, "*", "*", "void"])
25
+
26
+
27
+ end # GenericFunctions
28
+
29
+ ##########################################################################################
30
+ #
31
+ ##########################################################################################
32
+
33
+ class MDArray
34
+
35
+ include GenericFunctions
36
+
37
+ end # NumericalMDArray
@@ -0,0 +1,57 @@
1
+ ##########################################################################################
2
+ #
3
+ ##########################################################################################
4
+
5
+ module RubyMath
6
+ extend FunctionCreation
7
+ extend RubyFunctions
8
+
9
+ @acos = Proc.new { |val| Math.acos(val) }
10
+ @acosh = Proc.new { |val| Math.acosh(val) }
11
+ @asin = Proc.new { |val| Math.asin(val) }
12
+ @asinh = Proc.new { |val| Math.asinh(val) }
13
+ @atan = Proc.new { |val| Math.atan(val) }
14
+ @atan2 = Proc.new { |val| Math.atan2(val) }
15
+ @atanh = Proc.new { |val| Math.atanh(val) }
16
+ @cbrt = Proc.new { |val| Math.cbrt(val) }
17
+ @cos = Proc.new { |val| Math.cos(val) }
18
+ @cosh = Proc.new { |val| Math.cosh(val) }
19
+ @erf = Proc.new { |val| Math.erf(val) }
20
+ @erfc = Proc.new { |val| Math.erfc(val) }
21
+ @exp = Proc.new { |val| Math.exp(val) }
22
+ @gamma = Proc.new { |val| Math.gamma(val) }
23
+ @hypot = Proc.new { |val| Math.hypotn(val) }
24
+ @ldexp = Proc.new { |val| Math.ldexp(val) }
25
+ @log = Proc.new { |val| Math.log(val) }
26
+ @log10 = Proc.new { |val| Math.log10(val) }
27
+ @log2 = Proc.new { |val| Math.log2(val) }
28
+ @sin = Proc.new { |val| Math.sin(val) }
29
+ @sinh = Proc.new { |val| Math.sinh(val) }
30
+ @sqrt = Proc.new { |val| Math.sqrt(val) }
31
+ @tan = Proc.new { |val| Math.tan(val) }
32
+ @tanh = Proc.new { |val| Math.tanh(val) }
33
+ @neg = Proc.new { |val| -1 * val }
34
+
35
+ @unary_methods = [:acos, :acosh, :asin, :asinh, :atan, :atan2,
36
+ :atanh, :cbrt, :cos, :erf, :exp, :gamma, :hypot, :ldexp,
37
+ :log, :log10, :log2, :sin, :sinh, :sqrt, :tan, :tanh, :neg]
38
+
39
+ @unary_methods.each do |method|
40
+ make_unary_operators(method.to_s,
41
+ ruby_unary_function("#{method.to_s}_ruby",
42
+ instance_variable_get("@#{method.to_s}")))
43
+ end
44
+
45
+ alias :-@ :neg
46
+
47
+ end # RubyMath
48
+
49
+ ##########################################################################################
50
+ #
51
+ ##########################################################################################
52
+
53
+ class NumericalMDArray
54
+
55
+ include RubyMath
56
+
57
+ end # NumericalMDArray
@@ -0,0 +1,187 @@
1
+
2
+ ##########################################################################################
3
+ #
4
+ ##########################################################################################
5
+
6
+ module NumericFunctions
7
+ include_package "ucar.ma2.MAMath"
8
+
9
+ extend FunctionCreation
10
+ extend RubyFunctions
11
+
12
+ @add = Proc.new { |val1, val2| val1 + val2 }
13
+ @sub = Proc.new { |val1, val2| val1 - val2 }
14
+ @mul = Proc.new { |val1, val2| val1 * val2 }
15
+ @div = Proc.new { |val1, val2| val1 / val2 }
16
+ @power = Proc.new { |val1, val2| val1 ** val2 }
17
+ @abs = Proc.new { |val| val.abs }
18
+ @ceil = Proc.new { |val| val.ceil }
19
+ @floor = Proc.new { |val| val.floor }
20
+ @truncate = Proc.new { |val| val.truncate }
21
+ @is_zero = Proc.new { |val| val.zero }
22
+ @square = Proc.new { |val| val ** 2 }
23
+ @cube = Proc.new { |val| val ** 3 }
24
+ @fourth = Proc.new { |val| val ** 4 }
25
+
26
+ @min = Proc.new { |val1, val2| val1 < val2 ? val1 : val2 }
27
+ @max = Proc.new { |val1, val2| val1 > val2 ? val1 : val2 }
28
+
29
+ #---------------------------------------------------------------------------------------
30
+ #
31
+ #---------------------------------------------------------------------------------------
32
+
33
+ def zero?(val)
34
+ @is_zero.call(val)
35
+ end
36
+
37
+ #---------------------------------------------------------------------------------------
38
+ #
39
+ #---------------------------------------------------------------------------------------
40
+
41
+ def fast_add(other_val)
42
+ arr = Java::UcarMa2::MAMath.add(@nc_array, other_val.nc_array)
43
+ end
44
+
45
+ #---------------------------------------------------------------------------------------
46
+ #
47
+ #---------------------------------------------------------------------------------------
48
+
49
+
50
+ @binary_methods = [:add, :sub, :mul, :div, :power, :min, :max]
51
+
52
+ @unary_methods = [:abs, :ceil, :floor, :truncate, :is_zero, :square,
53
+ :cube, :fourth]
54
+
55
+ #---------------------------------------------------------------------------------------
56
+ #
57
+ #---------------------------------------------------------------------------------------
58
+
59
+ @binary_methods.each do |method|
60
+ make_binary_operators(method.to_s,
61
+ ruby_binary_function("#{method.to_s}_ruby",
62
+ instance_variable_get("@#{method.to_s}")))
63
+ end
64
+
65
+ @unary_methods.each do |method|
66
+ make_unary_operators(method.to_s,
67
+ ruby_unary_function("#{method.to_s}_ruby",
68
+ instance_variable_get("@#{method.to_s}")))
69
+ end
70
+
71
+ #======================================================================================
72
+ # Arithmetic
73
+ #======================================================================================
74
+
75
+ alias :+ :add
76
+ alias :- :sub
77
+ alias :* :mul
78
+ alias :/ :div
79
+ alias :** :power
80
+ alias :addAssign :add!
81
+ alias :subAssign :sub!
82
+ alias :mulAssign :mul!
83
+ alias :powAssign :power!
84
+
85
+ end # NumericFunctions
86
+
87
+ ##########################################################################################
88
+ #
89
+ ##########################################################################################
90
+
91
+ module ComparisonOperators
92
+ extend FunctionCreation
93
+ extend RubyFunctions
94
+
95
+ @ge = Proc.new { |val1, val2| val1 >= val2 ? true : false}
96
+ @gt = Proc.new { |val1, val2| val1 > val2 ? true : false}
97
+ @le = Proc.new { |val1, val2| val1 <= val2 ? true : false}
98
+ @lt = Proc.new { |val1, val2| val1 < val2 ? true : false}
99
+ @eq = Proc.new { |val1, val2| val1 == val2 ? true : false}
100
+
101
+ @binary_methods = [:ge, :gt, :le, :lt, :eq]
102
+
103
+ @binary_methods.each do |method|
104
+ make_comparison_op(method.to_s,
105
+ ruby_binary_function("#{method.to_s}_ruby",
106
+ instance_variable_get("@#{method.to_s}")))
107
+ end
108
+
109
+ alias :>= :ge
110
+ alias :> :gt
111
+ alias :<= :le
112
+ alias :< :lt
113
+ alias :== :eq
114
+
115
+ end # ComparisonOperators
116
+
117
+ ##########################################################################################
118
+ #
119
+ ##########################################################################################
120
+
121
+ module BitwiseOperators
122
+ extend FunctionCreation
123
+ extend RubyFunctions
124
+
125
+ @binary_and = Proc.new { |val1, val2| val1 & val2 }
126
+ @binary_or = Proc.new { |val1, val2| val1 | val2 }
127
+ @binary_xor = Proc.new { |val1, val2| val1 ^ val2 }
128
+ @binary_left_shift = Proc.new { |val1, val2| val1 << val2 }
129
+ @binary_right_shift = Proc.new { |val1, val2| val1 >> val2 }
130
+
131
+ @binary_ones_complement = Proc.new { |val| ~val }
132
+
133
+ #---------------------------------------------------------------------------------------
134
+ #
135
+ #---------------------------------------------------------------------------------------
136
+
137
+
138
+ @binary_methods = [:binary_and, :binary_or, :binary_xor, :binary_left_shift,
139
+ :binary_right_shift]
140
+
141
+ @unary_methods = [:binary_ones_complement]
142
+
143
+ #---------------------------------------------------------------------------------------
144
+ #
145
+ #---------------------------------------------------------------------------------------
146
+
147
+ @binary_methods.each do |method|
148
+ make_binary_operators(method.to_s,
149
+ ruby_binary_function("#{method.to_s}_ruby",
150
+ instance_variable_get("@#{method.to_s}")))
151
+ end
152
+
153
+ @unary_methods.each do |method|
154
+ make_unary_operators(method.to_s,
155
+ ruby_unary_function("#{method.to_s}_ruby",
156
+ instance_variable_get("@#{method.to_s}")))
157
+ end
158
+
159
+ alias :& :binary_and
160
+ alias :| :binary_or
161
+ alias :^ :binary_xor
162
+ alias :~ :binary_ones_complement
163
+ alias :<< :binary_left_shift
164
+ alias :>> :binary_right_shift
165
+
166
+ end # BitwiseOperators
167
+
168
+ ##########################################################################################
169
+ #
170
+ ##########################################################################################
171
+
172
+ class NumericalMDArray
173
+
174
+ include NumericFunctions
175
+ include ComparisonOperators
176
+
177
+ end # NumericalMDArray
178
+
179
+ ##########################################################################################
180
+ #
181
+ ##########################################################################################
182
+
183
+ class LongMDArray
184
+
185
+ include BitwiseOperators
186
+
187
+ end # LongMDArray
@@ -0,0 +1,201 @@
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 RubyBinaryOperator < BinaryOperator
27
+
28
+ #---------------------------------------------------------------------------------------
29
+ #
30
+ #---------------------------------------------------------------------------------------
31
+
32
+ def initialize(name, exec_type, force_type = nil, pre_condition = nil,
33
+ post_condition = nil)
34
+ super(name, 2, exec_type, force_type, pre_condition, post_condition)
35
+ end
36
+
37
+ #---------------------------------------------------------------------------------------
38
+ #
39
+ #---------------------------------------------------------------------------------------
40
+
41
+ def default(*args)
42
+
43
+ get_args(*args) do |op1_iterator, op2_iterator, shape, *other_args|
44
+ result = MDArray.build(@type, shape)
45
+ res_iterator = result.get_iterator_fast
46
+ if (@coerced)
47
+ while (res_iterator.has_next?)
48
+ res_iterator.set_next(@do_func.call(op2_iterator.get_next,
49
+ op1_iterator.get_next))
50
+ end
51
+ else
52
+ while (res_iterator.has_next?)
53
+ res_iterator.set_next(@do_func.call(op1_iterator.get_next,
54
+ op2_iterator.get_next))
55
+ end
56
+ end
57
+ return result
58
+ end
59
+
60
+ end
61
+
62
+ #---------------------------------------------------------------------------------------
63
+ #
64
+ #---------------------------------------------------------------------------------------
65
+
66
+ def fill(*args)
67
+
68
+ get_args(*args) do |op1_iterator, op2_iterator, shape, *other_args|
69
+ while (op1_iterator.has_next?)
70
+ op1_iterator.set_next(op2_iterator.get_next)
71
+ end
72
+ return self
73
+ end
74
+
75
+ end
76
+
77
+ #---------------------------------------------------------------------------------------
78
+ #
79
+ #---------------------------------------------------------------------------------------
80
+
81
+ def in_place(*args)
82
+
83
+ get_args(*args) do |op1_iterator, op2_iterator, shape, *other_args|
84
+ while (op1_iterator.has_next?)
85
+ op1_iterator.set_current(@do_func.call(op1_iterator.get_next,
86
+ op2_iterator.get_next))
87
+ end
88
+ return self
89
+ end
90
+
91
+ end
92
+
93
+ #---------------------------------------------------------------------------------------
94
+ #
95
+ #---------------------------------------------------------------------------------------
96
+
97
+ def reduce(*args)
98
+
99
+ get_args(*args) do |op1_iterator, op2_iterator, shape, *other_args|
100
+ result = @pre_condition_result
101
+ while (op1_iterator.has_next?)
102
+ result = @do_func.call(result, op1_iterator.get_next, op2_iterator.get_next)
103
+ end
104
+ return result
105
+ end
106
+
107
+ end
108
+
109
+ end # RubyBinaryOperator
110
+
111
+ ##########################################################################################
112
+ #
113
+ ##########################################################################################
114
+
115
+ class RubyUnaryOperator < UnaryOperator
116
+
117
+ #---------------------------------------------------------------------------------------
118
+ #
119
+ #---------------------------------------------------------------------------------------
120
+
121
+ def initialize(name, exec_type, force_type = nil, pre_condition = nil,
122
+ post_condition = nil)
123
+ super(name, 1, exec_type, force_type, pre_condition, post_condition)
124
+ end
125
+
126
+ #---------------------------------------------------------------------------------------
127
+ #
128
+ #---------------------------------------------------------------------------------------
129
+
130
+ def default(*args)
131
+
132
+ get_args(*args) do |op_iterator, shape, *other_args|
133
+ result = MDArray.build(@type, shape)
134
+ res_iterator = result.get_iterator_fast
135
+ while (res_iterator.has_next?)
136
+ res_iterator.set_next(@do_func.call(op_iterator.get_next))
137
+ end
138
+ return result
139
+ end
140
+
141
+ end
142
+
143
+ #---------------------------------------------------------------------------------------
144
+ #
145
+ #---------------------------------------------------------------------------------------
146
+
147
+ def set_block(*args)
148
+
149
+ get_args(*args) do |op_iterator, shape, *other_args|
150
+ block = other_args[0]
151
+ while (op_iterator.has_next?)
152
+ op_iterator.next
153
+ if (shape.size < 8)
154
+ op_iterator.set_current(block.call(*op_iterator.get_current_counter))
155
+ else
156
+ op_iterator.set_current(block.call(op_iterator.get_current_counter))
157
+ end
158
+ end if block
159
+ end
160
+
161
+ end
162
+
163
+ #---------------------------------------------------------------------------------------
164
+ #
165
+ #---------------------------------------------------------------------------------------
166
+
167
+ def in_place(*args)
168
+
169
+ get_args(*args) do |op_iterator, *other_args|
170
+ while (op_iterator.has_next?)
171
+ op_iterator.set_current(@do_func.call(op_iterator.get_next))
172
+ end
173
+ return self
174
+ end
175
+
176
+ end
177
+
178
+ #---------------------------------------------------------------------------------------
179
+ #
180
+ #---------------------------------------------------------------------------------------
181
+
182
+ def reduce(*args)
183
+
184
+ get_args(*args) do |op_iterator, shape, *other_args|
185
+ result = @pre_condition_result
186
+ while (op_iterator.has_next?)
187
+ result = @do_func.call(result, op_iterator.get_next)
188
+ end
189
+ return result
190
+ end
191
+
192
+ end
193
+
194
+ end # RubyUnaryOperator
195
+
196
+ ##########################################################################################
197
+ #
198
+ ##########################################################################################
199
+
200
+ MDArray.binary_operator = RubyBinaryOperator
201
+ MDArray.unary_operator = RubyUnaryOperator