mdarray 0.5.0.pre-java → 0.5.3-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. data/README.md +89 -90
  2. data/Rakefile +6 -1
  3. data/lib/colt/cern_double_functions.rb +193 -0
  4. data/lib/colt/cern_float_functions.rb +193 -0
  5. data/lib/colt/cern_int_functions.rb +152 -0
  6. data/lib/colt/cern_long_functions.rb +152 -0
  7. data/lib/colt/colt.rb +103 -1
  8. data/lib/mdarray.rb +71 -23
  9. data/lib/mdarray/access.rb +8 -0
  10. data/lib/mdarray/counter.rb +43 -1
  11. data/lib/mdarray/creation.rb +5 -10
  12. data/lib/mdarray/fast_operators.rb +17 -13
  13. data/lib/mdarray/function_creation.rb +11 -45
  14. data/lib/mdarray/function_map.rb +16 -8
  15. data/lib/mdarray/lazy_mdarray.rb +311 -0
  16. data/lib/mdarray/lazy_operators.rb +166 -0
  17. data/lib/mdarray/operators.rb +38 -9
  18. data/lib/mdarray/proc_util.rb +2 -0
  19. data/lib/mdarray/ruby_boolean_functions.rb +24 -0
  20. data/lib/mdarray/ruby_functions.rb +76 -2
  21. data/lib/mdarray/ruby_generic_functions.rb +12 -4
  22. data/lib/mdarray/ruby_math.rb +180 -2
  23. data/lib/mdarray/ruby_numeric_functions.rb +198 -7
  24. data/target/helper.jar +0 -0
  25. data/test/colt/ColtMethods.xlsx +0 -0
  26. data/test/colt/test_complete.rb +1 -0
  27. data/test/colt/test_math.rb +249 -0
  28. data/test/complete.rb +1 -0
  29. data/test/env.rb +17 -4
  30. data/test/mdarray/arithmetic_casting.rb +3 -0
  31. data/test/mdarray/test_boolean.rb +1 -1
  32. data/test/mdarray/test_complete.rb +1 -0
  33. data/test/mdarray/test_error.rb +13 -13
  34. data/test/mdarray/test_lazy.rb +306 -0
  35. data/test/mdarray/test_operator.rb +1 -1
  36. data/test/mdarray/test_performance.rb +57 -4
  37. data/test/mdarray/test_trigonometry.rb +5 -1
  38. data/vendor/commons-compiler.jar +0 -0
  39. data/vendor/janino.jar +0 -0
  40. data/version.rb +1 -1
  41. metadata +47 -10
  42. data/test/mdarray/test_statistics.rb +0 -80
data/test/complete.rb CHANGED
@@ -23,3 +23,4 @@ require 'env.rb'
23
23
 
24
24
  require 'mdarray/test_complete'
25
25
  require 'colt/test_complete'
26
+ # require 'mdarray/test_lazy'
data/test/env.rb CHANGED
@@ -1,7 +1,17 @@
1
1
  require 'rbconfig'
2
2
 
3
- # Home directory for MDArray
4
- $MDARRAY_HOME_DIR = ".."
3
+ =begin
4
+ ENV.each do |val|
5
+ p val
6
+ end
7
+ =end
8
+
9
+ # Home directory for MDArray.
10
+ if $INSTALL_DIR
11
+ $MDARRAY_HOME_DIR = $INSTALL_DIR
12
+ else
13
+ $MDARRAY_HOME_DIR = ".."
14
+ end
5
15
 
6
16
  # MDArray Test directory
7
17
  $MDARRAY_TEST_DIR = "./mdarray"
@@ -9,11 +19,14 @@ $MDARRAY_TEST_DIR = "./mdarray"
9
19
  # Colt Test directory
10
20
  $COLT_TEST_DIR = "./colt"
11
21
 
22
+ # Need to change this variable before publication
23
+ # $MDARRAY_ENV = 'cygwin'
24
+
12
25
  ##########################################################################################
13
26
  # If we need to test for coverage
14
27
  ##########################################################################################
15
28
 
16
- if ENV['COVERAGE'] == 'true'
29
+ if $COVERAGE == 'true'
17
30
 
18
31
  require 'simplecov'
19
32
 
@@ -29,7 +42,7 @@ end
29
42
  # Prepare environment to work inside Cygwin
30
43
  ##########################################################################################
31
44
 
32
- if ENV['MDARRAY_ENV'] == 'cygwin'
45
+ if $MDARRAY_ENV == 'cygwin'
33
46
 
34
47
  # RbConfig::CONFIG['host_os'] # returns mswin32 on Windows, for example
35
48
  # p Config::CONFIG
@@ -174,12 +174,15 @@ class MDArrayTest < Test::Unit::TestCase
174
174
  # adding an int to a double, should cast to double
175
175
  res = @a + @c
176
176
  assert_equal("21.87 35.34 47.18 58.84 ", res.to_string)
177
+
177
178
  # adding an int array to a (ruby) float/double number, should cast to double
178
179
  res = @a + 10.55
179
180
  assert_equal("30.55 40.55 50.55 60.55 ", res.to_string)
181
+
180
182
  # adding two ints
181
183
  res = @a + @b
182
184
  assert_equal(res.to_string, "20 31 42 53 ")
185
+
183
186
  # unary operation on arrays
184
187
  res = @c.floor
185
188
  assert_equal(res.to_string, "1.0 5.0 7.0 8.0 ")
@@ -45,7 +45,7 @@ class MDArrayTest < Test::Unit::TestCase
45
45
  result = @bool2.and(@bool1)
46
46
  assert_equal("false false true false ", result.to_string)
47
47
 
48
- # Cannot overload and, so using & as an alias to and
48
+ # Cannot overload 'and', so using '&' as an alias to 'and'
49
49
  result = @bool1 & @bool2
50
50
  assert_equal("false false true false ", result.to_string)
51
51
 
@@ -60,5 +60,6 @@ require_relative 'test_counter'
60
60
  require_relative 'test_trigonometry'
61
61
  require_relative 'test_views'
62
62
  require_relative 'test_printing'
63
+ require_relative 'test_lazy'
63
64
  #require_relative 'test_performance'
64
65
  #=end
@@ -2,7 +2,6 @@ require 'rubygems'
2
2
  require "test/unit"
3
3
  require 'shoulda'
4
4
 
5
- require 'env'
6
5
  require 'mdarray'
7
6
 
8
7
  class MDArrayTest < Test::Unit::TestCase
@@ -27,27 +26,28 @@ class MDArrayTest < Test::Unit::TestCase
27
26
  @f = MDArray.fromfunction("double", [4, 5, 6]) do |x, y, z|
28
27
  9.57 * x + y + z
29
28
  end
30
- @bool = MDArray.boolean([4], [true, true, false, false])
29
+ @g = MDArray.byte([1], [60])
30
+ @h = MDArray.byte([1], [13])
31
+ @i = MDArray.double([4], [2.0, 6.0, 5.0, 9.0])
31
32
 
32
- @trig = MDArray.typed_arange("double", 90)
33
- @long = MDArray.typed_arange("long", 5)
34
-
33
+ @bool1 = MDArray.boolean([4], [true, false, true, false])
34
+ @bool2 = MDArray.boolean([4], [false, false, true, true])
35
+
35
36
  end # setup
36
37
 
37
38
  #-------------------------------------------------------------------------------------
38
39
  #
39
40
  #-------------------------------------------------------------------------------------
40
-
41
+
41
42
  should "correct error!" do
42
43
 
43
- arr = MDArray.double("VALE3_short.csv", true)
44
- arr.print
45
-
46
- arr = MDArray.float("VALE3_short.csv", true)
47
- arr.print
44
+ # define inner_product for the whole hierarchy as it defines it for double
45
+ UserFunction.binary_operator("inner_product", "reduce",
46
+ Proc.new { |sum, val1, val2| sum + (val1 * val2) },
47
+ "double", nil, Proc.new { 0 })
48
48
 
49
- arr = MDArray.int("VALE3_short.csv", true)
50
- arr.print
49
+ c = @a.inner_product(@c)
50
+ assert_equal(926.8, c)
51
51
 
52
52
  end
53
53
 
@@ -0,0 +1,306 @@
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
+
29
+ class MDArrayTest < Test::Unit::TestCase
30
+
31
+ context "Lazy operations" do
32
+
33
+ setup do
34
+
35
+ @a = MDArray.int([2, 3], [1, 2, 3, 4, 5, 6])
36
+ @b = MDArray.int([2, 3], [10, 20, 30, 40, 50, 60])
37
+ @c = MDArray.double([2, 3], [100, 200, 300, 400, 500, 600])
38
+ @d = MDArray.init_with("int", [2, 3], 4)
39
+ @e = MDArray.init_with("int", [2, 3], 5)
40
+ @f = MDArray.init_with("int", [2, 3], 6)
41
+ @g = MDArray.init_with("int", [2, 3], 7)
42
+
43
+ @h = MDArray.init_with("int", [3, 3], 7)
44
+ @i = MDArray.init_with("int", [3, 3], 7)
45
+
46
+ @float = MDArray.init_with("float", [2, 3], 10.5)
47
+ @long = MDArray.init_with("long", [2, 3], 10)
48
+ @byte = MDArray.init_with("byte", [2, 3], 10)
49
+
50
+ MDArray.lazy = true
51
+
52
+ end
53
+
54
+
55
+ #-------------------------------------------------------------------------------------
56
+ #
57
+ #-------------------------------------------------------------------------------------
58
+
59
+ should "execute lazy operations" do
60
+
61
+ p "2 a"
62
+ a_2 = (@a + @a)[]
63
+ a_2.print
64
+
65
+ c = 2 * @a
66
+
67
+ lazy_c = @a + @b
68
+ # calculate the value of the lazy array with []
69
+ c = lazy_c[]
70
+ c.print
71
+
72
+ c = (@c + @a)[]
73
+ c = (@a + @c)[]
74
+
75
+ c = (@c + @byte)[]
76
+ c = (@byte + @c)[]
77
+
78
+ lazy_c = (@a * @d - @e) - (@b + @c)
79
+ lazy_c[].print
80
+
81
+ # evaluate the lazy expression with [] on the same line
82
+ d = ((@a * @d - @e) - (@b + @c))[]
83
+ d.print
84
+
85
+ # evaluate lazy expression with [] anywhere in the expression. (@a * @d - @e) is
86
+ # done lazyly then evaluated then operated with a lazy (@b + @c). The final
87
+ # result is lazy
88
+ d = ((@a * @d - @e)[] - (@b + @c))
89
+ d.print
90
+
91
+ MDArray.lazy = false
92
+
93
+ end
94
+
95
+ #-------------------------------------------------------------------------------------
96
+ #
97
+ #-------------------------------------------------------------------------------------
98
+
99
+ should "check that lazy is contagious" do
100
+
101
+ # this is done lazyly as we have "MDArray.lazy = true" defined in the setup
102
+ l_c = @a + @b
103
+
104
+ # now operations are done eagerly
105
+ MDArray.lazy = false
106
+ e_e = @c + @b
107
+ # note that we do not require [] before printing e_e
108
+ e_e.print
109
+
110
+ # now operating a lazy array with an eager array... should be lazy even when
111
+ # MDArray.lazy is false
112
+ l_f = l_c + e_e
113
+ l_f.print
114
+ # request the calculation and print
115
+ l_f[].print
116
+
117
+ MDArray.lazy = false
118
+
119
+ end
120
+
121
+ #-------------------------------------------------------------------------------------
122
+ #
123
+ #-------------------------------------------------------------------------------------
124
+
125
+ should "execute operations eagerly" do
126
+
127
+ MDArray.lazy = false
128
+
129
+ c = @a + @b
130
+ c.print
131
+
132
+ c = (@a * @d - @e) - (@b + @c)
133
+ c.print
134
+
135
+ MDArray.lazy = false
136
+
137
+ end
138
+
139
+ #-------------------------------------------------------------------------------------
140
+ #
141
+ #-------------------------------------------------------------------------------------
142
+
143
+ should "show that lazy is really lazy" do
144
+
145
+ # MDArray lazy operations are really lazy, i.e., there is not checking of any sort
146
+ # when parsing the expression. Validation is only done when values are required.
147
+
148
+ a = MDArray.int([2, 3], [1, 2, 3, 4, 5, 6])
149
+ b = MDArray.double([3, 1], [1, 2, 3])
150
+
151
+
152
+ # arrays a and b are of different shape and cannot work together. Yet, there will
153
+ # be no error while parsing
154
+ l_c = a + b
155
+
156
+ # now we get an error
157
+ assert_raise ( RuntimeError ) { l_c[].print }
158
+
159
+ # now lets add correctly
160
+ p "calculating lazy c"
161
+ l_c = @a + @b
162
+ l_c[].print
163
+
164
+ # now lets change @b
165
+ @b[0, 0] = 1000
166
+ # and calculate again lazy c
167
+ p "calculating lazy c again"
168
+ l_c[].print
169
+
170
+ p "calculating expression"
171
+ @b[1, 0] = 2000
172
+ p1 = (@a * @d - @e)[]
173
+ p2 = (@b + @c)[]
174
+ (p1 - p2)[].print
175
+ p "@b is"
176
+ @b.print
177
+
178
+ # evaluate lazy expression with [] anywhere in the expression. (@a * @d - @e) is
179
+ # done lazyly then evaluated then operated with a lazy (@b + @c). The final
180
+ # result is lazy
181
+ p "calculating lazy d"
182
+ d = ((@a * @d - @e)[] - (@b + @c))
183
+ d[].print
184
+ # lets now change the value of @a
185
+ @a[0, 0] = 1000
186
+ # no change in d... @a has being eagerly calculated
187
+ p "lazy d again after changing @a"
188
+ d[].print
189
+ # lets now change @b
190
+ @b[0, 0] = 1
191
+ @b[0, 1] = 150
192
+ @b[1, 1] = 1000
193
+ p "b is now:"
194
+ @b.print
195
+ # @b is still lazy on d calculation, so changing @b will change the value
196
+ # of d[].
197
+ p "lazy d again after changing @b"
198
+ d[].print
199
+
200
+ p "calculating new expression"
201
+ p3 = (@b + @c)
202
+ (p1 - p3)[].print
203
+
204
+ MDArray.lazy = false
205
+
206
+ end
207
+
208
+ #-------------------------------------------------------------------------------------
209
+ #
210
+ #-------------------------------------------------------------------------------------
211
+
212
+ should "work with Numeric" do
213
+
214
+ l_c = @a + 2
215
+ l_c[].print
216
+
217
+ l_c = 2 + @a
218
+ l_c[].print
219
+
220
+ l_c = 2 - @a
221
+ l_c[].print
222
+
223
+ MDArray.lazy = false
224
+
225
+ end
226
+
227
+ #-------------------------------------------------------------------------------------
228
+ #
229
+ #-------------------------------------------------------------------------------------
230
+ should "work with unary operators" do
231
+
232
+ # MDArray.lazy = false
233
+
234
+ arr = MDArray.linspace("double", 0, 1, 100)
235
+ l_a = arr.sin
236
+ l_a.print
237
+
238
+ b = l_a[]
239
+ b.print
240
+
241
+ ((@a * @d - @e).sin - (@b + @c))[].print
242
+
243
+ sinh = (arr.sinh)[]
244
+
245
+ MDArray.lazy = false
246
+
247
+ end
248
+
249
+ #-------------------------------------------------------------------------------------
250
+ #
251
+ #-------------------------------------------------------------------------------------
252
+
253
+ =begin
254
+ should "convert expressions involving only arrays to RPN" do
255
+
256
+ lazy_c = @a + @b
257
+ assert_equal("int int add ", lazy_c.rpn)
258
+
259
+ lazy_d = @a * @b
260
+ assert_equal("int int mul ", lazy_d.rpn)
261
+
262
+ lazy_e = lazy_c / lazy_d
263
+ assert_equal("int int add int int mul div ", lazy_e.rpn)
264
+
265
+ lazy_c = @a - @b + (@c * @d / (@f + @g))
266
+ assert_equal("int int sub int int mul int int add div add ", lazy_c.rpn)
267
+
268
+ lazy_f = @a - @b + (@c * @d - @f / (@f + @g))
269
+ assert_equal("int int sub int int mul int int int add div sub add ", lazy_f.rpn)
270
+
271
+ end
272
+
273
+ #-------------------------------------------------------------------------------------
274
+ #
275
+ #-------------------------------------------------------------------------------------
276
+
277
+ should "validate operations" do
278
+
279
+ lazy_c = @a + @b
280
+ lazy_c.validate
281
+
282
+ # does not validate as shapes are different
283
+ lazy_e = @h + @a
284
+ assert_raise ( RuntimeError ) { lazy_e.validate }
285
+
286
+ lazy_f = @h + @i - @a + @b
287
+ assert_raise ( RuntimeError ) { lazy_f.validate }
288
+
289
+ lazy_g = @h + @i
290
+
291
+ lazy_k = lazy_g + lazy_c
292
+ assert_raise ( RuntimeError ) { lazy_k.validate }
293
+
294
+ end
295
+
296
+ #-------------------------------------------------------------------------------------
297
+ #
298
+ #-------------------------------------------------------------------------------------
299
+
300
+ should "convert expressions involving numeric to RPN" do
301
+
302
+ end
303
+ =end
304
+ end
305
+
306
+ end
@@ -315,7 +315,7 @@ class MDArrayTest < Test::Unit::TestCase
315
315
  # Possible to create a new binary operator and force the final type of the resulting
316
316
  # MDArray. Bellow, whenever addd is used the resulting array will be of type double
317
317
  func = MDArray.select_function("add")
318
- UserFunction.binary_operator("addd", "default", func, "double", "double")
318
+ UserFunction.binary_operator("addd", "default", func.function, "double", "double")
319
319
 
320
320
  # @a and @b are int arrays, so result should be int, but since addd is used, resulting
321
321
  # array will be double