mdarray 0.4.0-java

Sign up to get free protection for your applications and to get access to all the features.
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
data/test/env.rb ADDED
@@ -0,0 +1,50 @@
1
+ require 'rbconfig'
2
+
3
+ # Config::CONFIG['host_os'] # returns mswin32 on Windows, for example
4
+ # p Config::CONFIG
5
+
6
+ # Fix environment variable for MultiCell
7
+ # Prepare environment to work inside Cygwin
8
+
9
+ # Return the cygpath of a path
10
+ def cygpath(path)
11
+ `cygpath -p -m #{path}`.tr("\n", "")
12
+ end
13
+
14
+ # Add path to load path
15
+ def mklib(path, home_path = true)
16
+
17
+ if (home_path)
18
+ lib = path + "/lib"
19
+ else
20
+ lib = path
21
+ end
22
+
23
+ $LOAD_PATH << `cygpath -p -m #{lib}`.tr("\n", "")
24
+
25
+ end
26
+
27
+ # Home directory for MDArray
28
+ $MDARRAY_HOME_DIR = "/home/zxb3/Desenv/MDArray"
29
+ mklib($MDARRAY_HOME_DIR)
30
+
31
+ # Home directory for MultiCell
32
+ $MC_HOME_DIR = "/home/zxb3/Desenv/MultiCell"
33
+ mklib($MC_HOME_DIR)
34
+
35
+ # Home directory for NcInterface
36
+ $NCI_HOME_DIR = "/home/zxb3/Desenv/NcInterface"
37
+ mklib($NCI_HOME_DIR)
38
+
39
+ # Home directory for BM&F Bovespa files
40
+ $BMF_BOVESPA_DIR = "/home/zxb3/Investimentos/SeriesHistoricas/BMF_BOVESPA/"
41
+
42
+ # Home directory for results
43
+ $RESULT_HOME_DIR = $MC_HOME_DIR + "/results/"
44
+
45
+ =begin
46
+ # Build Jruby classpath from environment classpath
47
+ ENV['WCLASSPATH'].split(';').each do |path|
48
+ $CLASSPATH << cygpath(path)
49
+ end
50
+ =end
@@ -0,0 +1,247 @@
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 for educational, research, and
6
+ # not-for-profit purposes, without fee and without a signed licensing agreement, is hereby
7
+ # granted, provided that the above copyright notice, this paragraph and the following two
8
+ # paragraphs appear in all copies, modifications, and distributions. Contact Rodrigo
9
+ # Botafogo - rodrigo.a.botafogo@gmail.com for commercial licensing opportunities.
10
+ #
11
+ # IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
12
+ # INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
13
+ # THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
14
+ # POSSIBILITY OF SUCH DAMAGE.
15
+ #
16
+ # RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17
+ # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
18
+ # SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
19
+ # RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
20
+ # OR MODIFICATIONS.
21
+ ##########################################################################################
22
+
23
+ require 'rubygems'
24
+ require "test/unit"
25
+ require 'shoulda'
26
+
27
+ require 'mdarray'
28
+
29
+ class MDArrayTest < Test::Unit::TestCase
30
+
31
+ context "Access Tests" do
32
+
33
+ setup do
34
+
35
+ # create a byte array filled with 0's
36
+ @a = MDArray.byte([2, 3, 4])
37
+
38
+ # create double array
39
+ @b = MDArray.double([2, 3, 4])
40
+
41
+ end
42
+
43
+ #-------------------------------------------------------------------------------------
44
+ #
45
+ #-------------------------------------------------------------------------------------
46
+
47
+ should "allow data assignment and retrieval" do
48
+
49
+ assert_equal(0, @a[1, 2, 3])
50
+ # assign values to a using [] operator
51
+ @a[0, 0, 0] = 10
52
+ @a[0, 1, 2] = 10
53
+ @a[0, 1, 3] = 10
54
+ assert_equal(10, @a[0, 0, 0])
55
+ assert_equal(10, @a[0, 1, 2])
56
+ assert_equal(10, @a[0, 1, 3])
57
+ assert_equal(0, @a[0, 0, 1])
58
+
59
+ # assigning a float value to a byte array type will truncate the data
60
+ @a[0, 0, 0] = 1.5
61
+ assert_equal(1, @a[0, 0, 0])
62
+
63
+ assert_equal(0.0, @b[1, 1, 1])
64
+
65
+ # assign values to b using set method
66
+ @b.set([1, 1, 1], 5.25)
67
+ assert_equal(5.25, @b.get([1, 1, 1]))
68
+
69
+ end
70
+
71
+ #-------------------------------------------------------------------------------------
72
+ #
73
+ #-------------------------------------------------------------------------------------
74
+
75
+ should "generate RuntimeError and RangeError when counter is wrong" do
76
+
77
+ # When counter error in [], we get a RuntimeError.
78
+ # RuntimeError... dimension error on b
79
+ assert_raise ( RuntimeError ) { @b[0, 0, 0, 0] = 11.34 }
80
+ # RuntimeError... shape of first dimension too big
81
+ assert_raise ( RuntimeError ) { @b[5, 1, 1] = 7.32 }
82
+ # RangeError... shape of second dimension too big
83
+ assert_raise ( RuntimeError ) { @b[1, 3, 1] = 7.32 }
84
+
85
+ # When counter error is in get, we get a RangeError. Method get is faster than
86
+ # method [] as get does not do any counter validation nor convertion. [] does
87
+ # counter validation and convertion allowing for the use of negative counter
88
+ # and range.
89
+ assert_raise ( RangeError ) { @b.set([0, 0, 0, 0], 11.34) }
90
+ assert_raise ( RangeError ) { @b.set([5, 1, 1], 7.32) }
91
+ assert_raise ( RangeError ) { @b.set([1, 3, 1], 7.32) }
92
+
93
+ end
94
+
95
+ #-------------------------------------------------------------------------------------
96
+ # It is possible to use negative indexing by using methods set and get. Indexing with
97
+ # Unlike NumPy, [] does not allow negative indices. Accessing an array with [] is
98
+ # faster than using get as [] does not require convertion of negative indices to
99
+ # positive ones.
100
+ #-------------------------------------------------------------------------------------
101
+
102
+ should "allow working with negative indices" do
103
+
104
+ @b[1, -1, -1] = 4.20
105
+ assert_equal(4.20, @b[1, 2, 3])
106
+ @b[-1, -1, -1] = 10.35
107
+
108
+ # using negative indices with set raises an exeption...
109
+ assert_raise ( RangeError ) { @b.set([-1, -1, -1], 11.40) }
110
+ # but works with []
111
+ assert_equal(10.35, @b[-1, -1, -1])
112
+ assert_equal(@b[-1, -1, -1], 10.35)
113
+
114
+ # using negative indices with get raises an exeption
115
+ assert_raise ( RangeError ) { @b.get([-1, -1, -1]) }
116
+ assert_equal(10.35, @b[1, 2, 3])
117
+
118
+ # RuntimeError... shape of first dimension too big (on the negative side)
119
+ assert_raise ( RangeError ) { @b.set([-5, 1, 1], 7.32) }
120
+ # RuntimeError... shape of second dimension too big (on the negative side)
121
+ assert_raise ( RangeError ) { @b.set([1, -4, 1], 7.32) }
122
+
123
+ end
124
+
125
+ #-------------------------------------------------------------------------------------
126
+ #
127
+ #-------------------------------------------------------------------------------------
128
+
129
+ should "output data in string format" do
130
+
131
+ a = MDArray.arange(10)
132
+ assert_equal("0 1 2 3 4 5 6 7 8 9 ", a.to_string)
133
+
134
+ end
135
+
136
+ #-------------------------------------------------------------------------------------
137
+ #
138
+ #-------------------------------------------------------------------------------------
139
+
140
+ should "allow array traversal in cannonical order" do
141
+
142
+ a = MDArray.arange(10)
143
+
144
+ # set the value at the current index position. Raise an exception as there
145
+ # is no currently defined position. Note that a[] refers to the current counter
146
+ # position
147
+ assert_raise ( RuntimeError ) { a[] = 18.25 }
148
+
149
+ # gets the value at the current index position. Raises an exception as
150
+ # current position not defined
151
+ assert_raise ( RuntimeError) { a[] }
152
+
153
+ # reset_traversal initializes the traversal, but leaves the counter in an invalid
154
+ # position
155
+ a.reset_traversal
156
+ # move to the first position
157
+ a.next
158
+ assert_equal(0, a[])
159
+ # sets the value at the current position. No need to give the counter.
160
+ a[] = 10
161
+ assert_equal(10, a[])
162
+ # move to the next position
163
+ a.next
164
+ assert_equal(1, a[])
165
+ # get_next returns the next position and moves the counter. Method get_next and
166
+ # set_next are "fast" methods in the sense that they do not require any counter
167
+ # correction.
168
+ assert_equal(2, a.get_next)
169
+ assert_equal(2, a[])
170
+ assert_equal(3, a.get_next)
171
+ assert_equal(4, a.get_next)
172
+
173
+ end
174
+
175
+ #-------------------------------------------------------------------------------------
176
+ #
177
+ #-------------------------------------------------------------------------------------
178
+
179
+ should "allow array traversal using each like methods" do
180
+
181
+ a = MDArray.arange(10)
182
+
183
+ counter = 0
184
+
185
+ a.each do |elmt|
186
+ assert_equal(counter, elmt)
187
+ counter += 1
188
+ end
189
+
190
+ a.reset_traversal
191
+ a.next
192
+ assert_equal(0, a[])
193
+
194
+ # continues the each from where the counter stoped previously
195
+ counter = 1
196
+ a.each_cont do |elmt|
197
+ assert_equal(counter, elmt)
198
+ counter += 1
199
+ end
200
+
201
+ # each_with_counter returns the elmt and it's index
202
+ a.reshape!([5, 2])
203
+ counter = 0
204
+ a.each_with_index do |elmt, index|
205
+ assert_equal(counter, elmt)
206
+ counter += 1
207
+ end
208
+
209
+ # Collect returns a one dimensional ruby array
210
+ b = a.collect { |val| val * 2}
211
+
212
+ end
213
+
214
+ #-------------------------------------------------------------------------------------
215
+ #
216
+ #-------------------------------------------------------------------------------------
217
+ =begin
218
+ should "traverse the array over given axes" do
219
+
220
+ @b.each_over_axes([0]) do |elmt|
221
+ p elmt
222
+ end
223
+
224
+ end
225
+ =end
226
+ #-------------------------------------------------------------------------------------
227
+ #
228
+ #-------------------------------------------------------------------------------------
229
+
230
+ should "have the right precision for the type" do
231
+
232
+ b = MDArray.float([2, 3], [0, 1, 2, 3, 4, 5])
233
+ assert_equal(1.0, b[0, 1])
234
+
235
+ # float should work with 1 decimal precision
236
+ b[0, 1] = 1.5
237
+ assert_equal(1.5, b[0, 1])
238
+
239
+ c = MDArray.build("double", [2, 3], [0, 1, 2, 3, 4, 5])
240
+ c[0, 0] = 1.51
241
+ assert_equal(1.51, c[0, 0])
242
+
243
+ end
244
+
245
+ end
246
+
247
+ end
@@ -0,0 +1,67 @@
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
+
31
+ @bool1 = MDArray.boolean([4], [true, false, true, false])
32
+ @bool2 = MDArray.boolean([4], [false, false, true, true])
33
+
34
+ end # setup
35
+
36
+ #-------------------------------------------------------------------------------------
37
+ #
38
+ #-------------------------------------------------------------------------------------
39
+
40
+ should "work with boolean arrays and operators" do
41
+
42
+ # elementwise boolean operations supported
43
+ result = @bool1.and(@bool2)
44
+ assert_equal("false false true false ", result.to_string)
45
+
46
+ result = @bool2.and(@bool1)
47
+ assert_equal("false false true false ", result.to_string)
48
+
49
+ # Cannot overload and, so using & as an alias to and
50
+ result = @bool1 & @bool2
51
+ assert_equal("false false true false ", result.to_string)
52
+
53
+ result = @bool2.or(@bool1)
54
+ assert_equal("true false true true ", result.to_string)
55
+
56
+ # Cannot overload or, so using | as an alias to or
57
+ result = @bool2 | @bool1
58
+ assert_equal("true false true true ", result.to_string)
59
+
60
+ #result = !@bool1
61
+ #assert_equal("false true false true ", result.to_string)
62
+
63
+ end
64
+
65
+ end
66
+
67
+ end
@@ -0,0 +1,126 @@
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 "Comparison 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, 40.18, 84.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.boolean([4], [true, true, true, false])
51
+
52
+ end # setup
53
+
54
+ #-------------------------------------------------------------------------------------
55
+ #
56
+ #-------------------------------------------------------------------------------------
57
+
58
+ should "compare elementwise" do
59
+
60
+ result = @a > 30
61
+ assert_equal("false false true true ", result.to_string)
62
+
63
+ result = @a > @b
64
+ assert_equal("true true true true ", result.to_string)
65
+
66
+ result = @a > @c
67
+ assert_equal("true true false false ", result.to_string)
68
+
69
+ result = @c < @a
70
+ assert_equal("true true false false ", result.to_string)
71
+
72
+ result = @c == @c
73
+ assert_equal("true true true true ", result.to_string)
74
+
75
+ result = @a.ge(30)
76
+ assert_equal(result[0], false)
77
+ assert_equal(result[1], true)
78
+
79
+ # also works with logical operators
80
+ c = @a > 20.5
81
+ assert_equal("false true true true ", c.to_string)
82
+
83
+ # TODO: assert_equal
84
+ result = @a.le(30)
85
+
86
+ # TODO: assert_equal
87
+ result = @e.le(15)
88
+
89
+ # TODO: assert_equal
90
+ result = @e.le(@f)
91
+
92
+ # TODO: assert_equal
93
+ result = @a >= 30.5
94
+
95
+ # TODO: assert_equal
96
+ result = @a <= @a
97
+
98
+ # TODO: assert_equal
99
+ result = @e < @f
100
+
101
+ # TODO: assert_equal
102
+ result = @f > @e
103
+
104
+ assert_raise ( NoMethodError ) { result = @g < @g }
105
+
106
+ end
107
+
108
+ #-------------------------------------------------------------------------------------
109
+ #
110
+ #-------------------------------------------------------------------------------------
111
+
112
+ should "allow coercion on comparison" do
113
+
114
+ result = 30.001 > @a
115
+ assert_equal("true true false false ", result.to_string)
116
+
117
+ c = 25 > @a
118
+ assert_equal("true false false false ", c.to_string)
119
+
120
+ end
121
+
122
+ end
123
+
124
+ end
125
+
126
+