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,53 @@
1
+ require 'rubygems'
2
+ require "test/unit"
3
+ require 'shoulda'
4
+
5
+ require 'env'
6
+ require 'mdarray'
7
+
8
+ class MDArrayTest < Test::Unit::TestCase
9
+
10
+ context "Arithmetic Tests" do
11
+
12
+ setup do
13
+
14
+ # create a = [20 30 40 50]
15
+ @a = MDArray.arange(20, 60, 10)
16
+ # create b = [0 1 2 3]
17
+ @b = MDArray.arange(4)
18
+ # create c = [1.87 5.34 7.18 8.84]
19
+ @c = MDArray.double([4], [1.87, 5.34, 7.18, 8.84])
20
+ # create d = [[1 2] [3 4]]
21
+ @d = MDArray.int([2, 2], [1, 2, 3, 4])
22
+ # creates an array from a function (actually a block). The name fromfunction
23
+ # is preserved to maintain API compatibility with NumPy (is it necessary?)
24
+ @e = MDArray.fromfunction("double", [4, 5, 6]) do |x, y, z|
25
+ 3.21 * (x + y + z)
26
+ end
27
+ @f = MDArray.fromfunction("double", [4, 5, 6]) do |x, y, z|
28
+ 9.57 * x + y + z
29
+ end
30
+ @bool = MDArray.boolean([4], [true, true, false, false])
31
+
32
+ @trig = MDArray.typed_arange("double", 90)
33
+ @long = MDArray.typed_arange("long", 5)
34
+
35
+ end # setup
36
+
37
+ #-------------------------------------------------------------------------------------
38
+ #
39
+ #-------------------------------------------------------------------------------------
40
+
41
+ should "correct error!" do
42
+
43
+ # creates an array from a function (actually a block). The name fromfunction
44
+ # is preserved to maintain API compatibility with NumPy (is it necessary?)
45
+ arr = MDArray.fromfunction("double", [5, 5, 5, 5, 5, 5, 5, 5]) do |x|
46
+ x.inject(:+)
47
+ end
48
+
49
+ end
50
+
51
+ end
52
+
53
+ end
data/test/test_lazy.rb ADDED
@@ -0,0 +1,52 @@
1
+ require 'rubygems'
2
+ require "test/unit"
3
+ require 'shoulda'
4
+
5
+ require 'env'
6
+ require 'mdarray'
7
+
8
+ class MDArrayTest < Test::Unit::TestCase
9
+
10
+ context "Arithmetic Tests" do
11
+
12
+ setup do
13
+
14
+ # create a = [20 30 40 50]
15
+ @a = MDArray.arange(20, 60, 10)
16
+ # create b = [0 1 2 3]
17
+ @b = MDArray.arange(4)
18
+ # create c = [1.87 5.34 7.18 8.84]
19
+ @c = MDArray.double([4], [1.87, 5.34, 7.18, 8.84])
20
+ # create d = [[1 2] [3 4]]
21
+ @d = MDArray.int([2, 2], [1, 2, 3, 4])
22
+ # creates an array from a function (actually a block). The name fromfunction
23
+ # is preserved to maintain API compatibility with NumPy (is it necessary?)
24
+ @e = MDArray.fromfunction("double", [4, 5, 6]) do |x, y, z|
25
+ 3.21 * (x + y + z)
26
+ end
27
+ @f = MDArray.fromfunction("double", [4, 5, 6]) do |x, y, z|
28
+ 9.57 * x + y + z
29
+ end
30
+ @g = MDArray.byte([1], [60])
31
+ @h = MDArray.byte([1], [13])
32
+ @i = MDArray.double([4], [2.0, 6.0, 5.0, 9.0])
33
+
34
+ end # setup
35
+
36
+ #-------------------------------------------------------------------------------------
37
+ #
38
+ #-------------------------------------------------------------------------------------
39
+
40
+ should "do basic lazy operations" do
41
+
42
+ op = @a.add_(@b)
43
+ p op
44
+ p op.type
45
+ op.exec(3)
46
+
47
+ end
48
+
49
+ end
50
+
51
+ end
52
+
@@ -0,0 +1,337 @@
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
+ require 'rubygems'
23
+ require "test/unit"
24
+ require 'shoulda'
25
+
26
+ require 'mdarray'
27
+
28
+ class MDArrayTest < Test::Unit::TestCase
29
+
30
+ context "Operators Tests" do
31
+
32
+ setup do
33
+
34
+ # create a = [20 30 40 50]
35
+ @a = MDArray.arange(20, 60, 10)
36
+ # create b = [0 1 2 3]
37
+ @b = MDArray.arange(4)
38
+ # create c = [1.87 5.34 7.18 8.84]
39
+ @c = MDArray.double([4], [1.87, 5.34, 7.18, 8.84])
40
+ # create d = [[1 2] [3 4]]
41
+ @d = MDArray.int([2, 2], [1, 2, 3, 4])
42
+ # creates an array from a function (actually a block). The name fromfunction
43
+ # is preserved to maintain API compatibility with NumPy (is it necessary?)
44
+ @e = MDArray.fromfunction("double", [4, 5, 6]) do |x, y, z|
45
+ 3.21 * (x + y + z)
46
+ end
47
+ @f = MDArray.fromfunction("double", [4, 5, 6]) do |x, y, z|
48
+ 9.57 * x + y + z
49
+ end
50
+ @g = MDArray.byte([1], [60])
51
+ @h = MDArray.byte([1], [13])
52
+ @i = MDArray.double([4], [2.0, 6.0, 5.0, 9.0])
53
+
54
+ end # setup
55
+
56
+ #-------------------------------------------------------------------------------------
57
+ #
58
+ #-------------------------------------------------------------------------------------
59
+
60
+ should "allow setting values" do
61
+
62
+ a = MDArray.int([5, 3, 5])
63
+ a.fill(10)
64
+
65
+ end
66
+
67
+ #-------------------------------------------------------------------------------------
68
+ #
69
+ #-------------------------------------------------------------------------------------
70
+
71
+ should "do basic array operations" do
72
+
73
+ # c = [20 31 42 53]
74
+ # chage the way operators work by changing the binary_operator parameter.
75
+ # @a.binary_operator = BinaryOperator
76
+ c = @a + @b
77
+ assert_equal(20, c[0])
78
+
79
+ # c = [20 29 38 47]
80
+ c = @a - @b
81
+ assert_equal(38, c[2])
82
+
83
+ # c = [0 30 80 100]
84
+ c = @a * @b
85
+ assert_equal(150, c[3])
86
+
87
+ # c = [0 0 0 0]
88
+ c = @b / @a
89
+ assert_equal(0, c[1])
90
+
91
+ # c = [1 30 1600 125000]
92
+ c = @a ** @b
93
+ assert_equal(1600, c[2])
94
+ # although a and d have the same number of elements we cannot do arithmetic
95
+ # with them.
96
+ assert_raise ( RuntimeError ) { c = @a + @d }
97
+
98
+ # arrays a and b still have the same values as before
99
+ assert_equal(20, @a[0])
100
+ assert_equal(30, @a[1])
101
+ assert_equal(2, @b[2])
102
+ assert_equal(3, @b[3])
103
+
104
+ # adding two double arrays
105
+ result = @e + @f
106
+ assert_equal(result[0, 0, 0], @e[0, 0, 0] + @f[0, 0, 0])
107
+
108
+ # request that result to be of type "byte" is respected
109
+ c = @a.add(@b, "byte")
110
+ assert_equal("byte", c.type)
111
+
112
+ # normal int type for result
113
+ c = @a.add 10
114
+ assert_equal("int", c.type)
115
+
116
+ # upcast will be enforced. result is double
117
+ c = @a.add @c
118
+ assert_equal("double", c.type)
119
+
120
+ end
121
+
122
+ #-------------------------------------------------------------------------------------
123
+ # Array operations are done elementwise
124
+ #-------------------------------------------------------------------------------------
125
+
126
+ should "do basic operations with numbers" do
127
+
128
+ # elementwise operation
129
+ c = @a + 20
130
+ assert_equal("40 50 60 70 ", c.to_string)
131
+
132
+ # Power test
133
+ c = @c ** 2.5
134
+ assert_equal("4.781938829669405 65.89510321368653 138.13734690718798 232.3435723800906 ",
135
+ c.to_string)
136
+
137
+ end
138
+
139
+ #-------------------------------------------------------------------------------------
140
+ #
141
+ #-------------------------------------------------------------------------------------
142
+
143
+ should "do in-place operations" do
144
+
145
+ # a = [20 30 40 50]
146
+ # a ^ 2 = [400, 900, 1600, 2500]
147
+ @a.mul! @a
148
+ assert_equal(1600, @a[2])
149
+
150
+ @a.add! 5
151
+ assert_equal("405 905 1605 2505 ", @a.to_string)
152
+
153
+ # do in place operations
154
+ # a = a + b = [405 906 1607 2508]
155
+ @a.add! @b
156
+ assert_equal(906, @a[1])
157
+ assert_equal(2508, @a[3])
158
+
159
+ end
160
+
161
+ #-------------------------------------------------------------------------------------
162
+ #
163
+ #-------------------------------------------------------------------------------------
164
+
165
+ should "work with expressions" do
166
+
167
+ result = @a + 2 * @b - @c
168
+ assert_equal("18.13 26.66 36.82 47.16 ", result.to_string)
169
+ result = (@a + 2) * @b - @c
170
+ assert_equal("-1.87 26.66 76.82 147.16 ", result.to_string)
171
+ result = (@a + 2) * (@b - @c)
172
+ assert_equal("-41.14 -138.88 -217.56 -303.68 ", result.to_string)
173
+
174
+ end
175
+
176
+ #-------------------------------------------------------------------------------------
177
+ #
178
+ #-------------------------------------------------------------------------------------
179
+
180
+ should "do bitwise operations" do
181
+
182
+ result = @g & @h
183
+ assert_equal("12 ", result.to_string)
184
+ result = @g | @h
185
+ assert_equal("61 ", result.to_string)
186
+ result = @g ^ @h
187
+ assert_equal("49 ", result.to_string)
188
+ # result = @g.binary_ones_complement(@h)
189
+ # assert_equal("-60 ", result.to_string)
190
+ result = @g << 2
191
+ assert_equal("240 ", result.to_string)
192
+ result = @g >> 2
193
+ assert_equal("15 ", result.to_string)
194
+
195
+ result = @g & 13
196
+ # although we can coerce arithmetic operations we cannot coerce bitwise operations
197
+ assert_raise ( TypeError ) { result = 13 & @g }
198
+
199
+ end
200
+
201
+ #-------------------------------------------------------------------------------------
202
+ #
203
+ #-------------------------------------------------------------------------------------
204
+
205
+ should "coerce arithmetic operations" do
206
+
207
+ # resulting array c is upcasted to double. Note that the operation is commutative
208
+ c = 20.5 + @a
209
+ assert_equal("40.5 50.5 60.5 70.5 ", c.to_string)
210
+ assert_equal(c.type, "double")
211
+
212
+ # Can also operate with the number first. Still doing elementwise operation
213
+ # @c = MDArray.double([4], [1.87, 5.34, 7.18, 8.84])
214
+ c = 10 / @c
215
+ assert_equal("5.347593582887701 1.8726591760299627 1.392757660167131 1.1312217194570136 ",
216
+ c.to_string)
217
+
218
+ # Works with subtraction
219
+ c = 0.5 - @a
220
+ assert_equal("-19.5 -29.5 -39.5 -49.5 ", c.to_string)
221
+
222
+ # *TODO: 2.5 ** @a is not working... seems to be a bug, since everything else works
223
+ # fine. Open a case after trying to make a simpler example.
224
+ # c = 2.5 ** @a
225
+
226
+ end
227
+
228
+ #-------------------------------------------------------------------------------------
229
+ #
230
+ #-------------------------------------------------------------------------------------
231
+
232
+ should "work with unary operators" do
233
+
234
+ result = @c.ceil
235
+ assert_equal(result.to_string, "2.0 6.0 8.0 9.0 ")
236
+ result = result / 4.254
237
+ assert_equal(result.to_string, "0.47014574518100616 1.4104372355430186 1.8805829807240246 2.1156558533145278 ")
238
+ result.floor!
239
+ assert_equal("0.0 1.0 1.0 2.0 ", result.to_string)
240
+ @c.ceil!
241
+ assert_equal("2.0 6.0 8.0 9.0 ", @c.to_string)
242
+
243
+ end
244
+
245
+ #------------------------------------------------------------------------------------
246
+ #
247
+ #------------------------------------------------------------------------------------
248
+
249
+ should "allow users operator's creation" do
250
+
251
+
252
+ # creating a binary_op as it takes two MDArrays as arguments. The method "total_sum"
253
+ # will make a cumulative sum of all sums of array1[i] + array2[i]. To achieve this
254
+ # we state that the result is "reduce" (just a number) and that the operation
255
+ # perfomed is the proc given that sums (sum, val1, val2). The initial value of sum
256
+ # is given by the second proc Proc.new { 0 }. This binary operator is defined
257
+ # for "int" MDArrays and all "lower" classes, i.e., short, byte.
258
+ UserFunction.binary_operator("total_sum", "reduce",
259
+ Proc.new { |sum, val1, val2| sum + val1 + val2 },
260
+ "int", nil, Proc.new { 0 })
261
+ c = @a.total_sum(@b)
262
+ assert_equal(146, c)
263
+
264
+ c = @a.total_sum(@c)
265
+ assert_equal(163.23000000000002, c)
266
+
267
+ c = @g.total_sum(@h)
268
+ assert_equal(73, c)
269
+
270
+ # total_sum is not defined for double MDArray
271
+ assert_raise ( NoMethodError ) { c = @c.total_sum(@b) }
272
+
273
+
274
+ # define inner_product for the whole hierarchy as it defines it for double
275
+ UserFunction.binary_operator("inner_product", "reduce",
276
+ Proc.new { |sum, val1, val2| sum + (val1 * val2) },
277
+ "double", nil, Proc.new { 0 })
278
+
279
+ c = @a.inner_product(@b)
280
+ assert_equal(260, c)
281
+
282
+ c = @a.inner_product(@c)
283
+ assert_equal(926.8, c)
284
+
285
+ c = @e.inner_product(@f)
286
+ assert_equal(50079.530999999995, c)
287
+
288
+ # same as the inner product but multiplies all values. Note that in this case
289
+ # we need to initialize prod with value 1, which is done with the second Proc
290
+ UserFunction.binary_operator("proc_proc", "reduce",
291
+ Proc.new { |prod, val1, val2| prod * (val1 * val2) },
292
+ "double", nil, Proc.new { 1 })
293
+ c = @a.proc_proc(@b)
294
+ assert_equal(0, c)
295
+
296
+ c = @a.proc_proc(@c)
297
+ assert_equal(760_572_850.7_520_001, c)
298
+
299
+
300
+ # Possible to create a new binary operator and force the final type of the resulting
301
+ # MDArray. Bellow, whenever addd is used the resulting array will be of type double
302
+ func = MDArray.select_function("add")
303
+ UserFunction.binary_operator("addd", "default", func, "double", "double")
304
+
305
+ # @a and @b are int arrays, so result should be int, but since addd is used, resulting
306
+ # array will be double
307
+ c = @a.addd(@b)
308
+ assert_equal("double", c.type)
309
+ c.print
310
+
311
+ # request that resulting array of type int is ignored when using addd
312
+ c = @a.addd(@b, "int")
313
+ assert_equal("double", c.type)
314
+
315
+ =begin
316
+
317
+ MDArray.make_binary_operators("perc_inc",
318
+ Proc.new { |val1, val2| (val2 - val1) / val1 })
319
+
320
+ result = @c.perc_inc @i
321
+ assert_equal("0.06951871657754005 0.12359550561797755 -0.3036211699164345 0.018099547511312233 ", result.to_string)
322
+
323
+ # do it in place
324
+ # "0.06951871514320374 0.1235954761505127 -0.3036211431026459 0.018099529668688774 "
325
+
326
+ # @c.binary_operator = FastBinaryOperator
327
+ # @c.binary_operator = BinaryOperator
328
+
329
+ @c.perc_inc! @i
330
+ assert_equal("0.06951871657754005 0.12359550561797755 -0.3036211699164345 0.018099547511312233 ", @c.to_string)
331
+ =end
332
+
333
+ end
334
+
335
+ end
336
+
337
+ end
@@ -0,0 +1,66 @@
1
+ require 'rubygems'
2
+ require "test/unit"
3
+ require 'shoulda'
4
+
5
+ require 'env'
6
+ require 'mdarray'
7
+
8
+ class MDArrayTest < Test::Unit::TestCase
9
+
10
+ context "Printing Tests" do
11
+
12
+ setup do
13
+
14
+ # create a = [20 30 40 50]
15
+ @a = MDArray.arange(20, 60, 10)
16
+ # create b = [0 1 2 3]
17
+ @b = MDArray.arange(4)
18
+ # create c = [1.87 5.34 7.18 8.84]
19
+ @c = MDArray.double([4], [1.87, 5.34, 7.18, 8.84])
20
+ # create d = [[1 2] [3 4]]
21
+ @d = MDArray.int([2, 2], [1, 2, 3, 4])
22
+ # creates an array from a function (actually a block). The name fromfunction
23
+ # is preserved to maintain API compatibility with NumPy (is it necessary?)
24
+ @e = MDArray.fromfunction("double", [4, 5, 6]) do |x, y, z|
25
+ 3.21 * (x + y + z)
26
+ end
27
+ @f = MDArray.fromfunction("double", [2, 2, 2, 2]) do |x, y, z, w|
28
+ 9.57 * x + y + z + w
29
+ end
30
+ @g = MDArray.fromfunction("double", [3, 2, 3, 2, 3]) do |x, y, z, w, k|
31
+ 9.57 * x + y + z + w + k
32
+ end
33
+
34
+ end # setup
35
+
36
+ #-------------------------------------------------------------------------------------
37
+ #
38
+ #-------------------------------------------------------------------------------------
39
+
40
+
41
+ should "apply a method over axes" do
42
+ @e.apply_over_axes([0, 2])
43
+ # @e.section([0, 0, 0], [1, 5, 1])
44
+ # @e.print
45
+ end
46
+
47
+ #=begin
48
+ should "print 1D array" do
49
+ @a.print
50
+ end
51
+ #=end
52
+
53
+ should "print nD array" do
54
+ @d.print
55
+ @e.print
56
+ @f.print
57
+ @g.print
58
+ end
59
+
60
+ should "print output to csv" do
61
+ @e.to_csv
62
+ end
63
+
64
+ end
65
+
66
+ end