scicom 0.2.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 (56) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +674 -0
  3. data/README.md +66 -0
  4. data/README.md~ +290 -0
  5. data/Rakefile +51 -0
  6. data/config.rb +163 -0
  7. data/doc/PypeR.pdf +0 -0
  8. data/doc/Stat 133 Class Notes (Phil Spector).pdf +29905 -45
  9. data/doc/The R interface.docx +0 -0
  10. data/lib/JRubyR/as_mdarray.rb +60 -0
  11. data/lib/JRubyR/attributes.rb +74 -0
  12. data/lib/JRubyR/dataframe.rb +35 -0
  13. data/lib/JRubyR/environment.rb +60 -0
  14. data/lib/JRubyR/function.rb +61 -0
  15. data/lib/JRubyR/index.rb +278 -0
  16. data/lib/JRubyR/list.rb +56 -0
  17. data/lib/JRubyR/list_orig.rb +111 -0
  18. data/lib/JRubyR/logical_value.rb +56 -0
  19. data/lib/JRubyR/rbsexp.rb +386 -0
  20. data/lib/JRubyR/renjin.rb +431 -0
  21. data/lib/JRubyR/ruby_classes.rb +58 -0
  22. data/lib/JRubyR/sequence.rb +56 -0
  23. data/lib/JRubyR/vector.rb +493 -0
  24. data/lib/env.rb +12 -0
  25. data/lib/rinruby.rb +795 -0
  26. data/lib/scicom.rb +29 -0
  27. data/target/helper.jar +0 -0
  28. data/test/baseball.csv +1 -0
  29. data/test/env.rb +7 -0
  30. data/test/test_R_interface.rb +165 -0
  31. data/test/test_array.rb +191 -0
  32. data/test/test_attributes.rb +261 -0
  33. data/test/test_basic.rb +156 -0
  34. data/test/test_column-major.rb +114 -0
  35. data/test/test_complete.rb +49 -0
  36. data/test/test_creation.rb +299 -0
  37. data/test/test_dataframe.rb +248 -0
  38. data/test/test_distribution.rb +320 -0
  39. data/test/test_double_assign.rb +240 -0
  40. data/test/test_double_receive.rb +106 -0
  41. data/test/test_environment.rb +57 -0
  42. data/test/test_factor.rb +285 -0
  43. data/test/test_functions.rb +67 -0
  44. data/test/test_linear_model.rb +64 -0
  45. data/test/test_list.rb +220 -0
  46. data/test/test_matrix.rb +205 -0
  47. data/test/test_mdarray.rb +258 -0
  48. data/test/test_operators.rb +227 -0
  49. data/test/test_sequence.rb +63 -0
  50. data/test/test_subsetting.rb +67 -0
  51. data/test/test_tmp.rb +67 -0
  52. data/test/test_vector.rb +227 -0
  53. data/vendor/Renjin.pdf +0 -0
  54. data/vendor/renjin-script-engine-0.7.0-RC7-SNAPSHOT-jar-with-dependencies.jar +0 -0
  55. data/version.rb +2 -0
  56. metadata +196 -0
@@ -0,0 +1,258 @@
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 'env'
27
+ require 'scicom'
28
+
29
+
30
+ class SciComTest < Test::Unit::TestCase
31
+
32
+ context "R environment" do
33
+
34
+ #--------------------------------------------------------------------------------------
35
+ #
36
+ #--------------------------------------------------------------------------------------
37
+
38
+ setup do
39
+
40
+ end
41
+
42
+
43
+ #--------------------------------------------------------------------------------------
44
+ #
45
+ #--------------------------------------------------------------------------------------
46
+
47
+ should "convert a vector to an MDArray" do
48
+
49
+ # Method 'get' converts a Vector to an MDArray
50
+ vec = i3.get
51
+ assert_equal(true, vec.is_a?(MDArray))
52
+ assert_equal("int", vec.type)
53
+
54
+ # For consistancy with R notation one can also call as__mdarray to convert a
55
+ # vector to an MDArray
56
+ vec2 = i3.as__mdarray
57
+ assert_equal(true, vec2.is_a?(MDArray))
58
+ assert_equal("int", vec2.type)
59
+
60
+ # Now 'vec' is an MDArray and its elements can be accessed through indexing, but
61
+ # this time the first index is 0, and the element is an actual number
62
+ assert_equal(10, vec[0])
63
+
64
+ # Convert vector to an MDArray
65
+ array = vec2.get
66
+
67
+ # Use array as any other MDArray...
68
+ array.each do |elmt|
69
+ p elmt
70
+ end
71
+
72
+ # ... although there is no need to convert a vector to an MDArray to call each:
73
+ # the each method is also defined for vectors
74
+ vec1.each do |elmt|
75
+ p elmt
76
+ end
77
+
78
+ end
79
+
80
+ =begin
81
+
82
+ #--------------------------------------------------------------------------------------
83
+ #
84
+ #--------------------------------------------------------------------------------------
85
+
86
+ should "integrate MDArray with R vector" do
87
+
88
+ # typed_arange does the same as arange but for arrays of other type
89
+ arr = MDArray.typed_arange(:double, 60)
90
+ # MDArray is stored in row-major order
91
+ arr.reshape!([5, 3, 4])
92
+ # arr.print
93
+
94
+ R.eval <<EOF
95
+ print(#{arr.r});
96
+ vec = #{arr.r};
97
+ print(vec);
98
+ print(vec[1, 1, 1]);
99
+
100
+ EOF
101
+
102
+ end
103
+ =end
104
+
105
+ #--------------------------------------------------------------------------------------
106
+ #
107
+ #--------------------------------------------------------------------------------------
108
+
109
+ should "receive 1 Dimensional MDArrays in R" do
110
+
111
+ vec = MDArray.double([6], [1, 3, 5, 7, 11, 13])
112
+ # vec is just a normal MDArray that can be changed at anytime
113
+ vec[0] = 2
114
+
115
+ R.prime = vec
116
+
117
+ # now vec is immutable, since it is now in Renjin and Renjin requires an immutable
118
+ # vector
119
+ assert_raise ( RuntimeError ) { vec[0] = 1 }
120
+
121
+ vec2 = R.eval("print(prime)")
122
+ vec2.print
123
+
124
+ assert_raise ( RuntimeError ) { vec2[1] = 7 }
125
+
126
+ end
127
+
128
+ #--------------------------------------------------------------------------------------
129
+ #
130
+ #--------------------------------------------------------------------------------------
131
+
132
+ should "cast MDArray numeric value to different types" do
133
+
134
+ i1 = R.d(10.2387)
135
+
136
+ # double cannot be converted to boolean
137
+ assert_raise ( RuntimeError ) { i1.get_as(:boolean) }
138
+
139
+ # method .get_as returns the current element of MDArray, which, in this case, is the
140
+ # first element
141
+ assert_equal(10, i1.get_as(:byte))
142
+ assert_equal(10, i1.get_as(:char))
143
+ assert_equal(10, i1.get_as(:short))
144
+ assert_equal(10, i1.get_as(:int))
145
+ assert_equal(10, i1.get_as(:long))
146
+ assert_equal(10.238699913024902, i1.get_as(:float))
147
+ assert_equal(10.2387, i1.get_as(:double))
148
+ assert_equal("10.2387", i1.get_as(:string))
149
+
150
+ end
151
+
152
+ #--------------------------------------------------------------------------------------
153
+ #
154
+ #--------------------------------------------------------------------------------------
155
+
156
+ should "assign MDArray to R transparently" do
157
+
158
+ # MDArray instances created in Ruby namespace can also be access in the R
159
+ # namespace with the r method:
160
+ array = MDArray.typed_arange(:double, 18)
161
+ R.eval("print(#{array.r})")
162
+
163
+ end
164
+
165
+ #--------------------------------------------------------------------------------------
166
+ #
167
+ #--------------------------------------------------------------------------------------
168
+
169
+ should "create MDArrays from R methods" do
170
+
171
+ # R.c creates a MDArray. In general it will be a double MDArray
172
+ res = R.c(2, 3, 4, 5)
173
+ assert_equal("double", res.type)
174
+ assert_equal(4, res.size)
175
+
176
+ # to create an int MDArray with R.c, all elements need to be integer. To create an
177
+ # int we need R.i method
178
+ res = R.c(R.i(2), R.i(3), R.i(4))
179
+ assert_equal("int", res.type)
180
+ assert_equal(3, res.size)
181
+
182
+ # using == method from MDArray. It returns an boolean MDArray
183
+ res = (R.c(2, 3, 4) == R.c(2, 3, 4))
184
+ assert_equal("boolean", res.type)
185
+
186
+ # array multiplication
187
+ (R.c(2, 3, 4) * R.c(5, 6, 7)).pp
188
+
189
+ # A sequence also becomes an MDArray
190
+ res = R.seq(10, 40)
191
+ res.print
192
+
193
+ res = R.rep(R.c(1, 2, 3), 3)
194
+ res.print
195
+
196
+ end
197
+
198
+ #======================================================================================
199
+ #
200
+ #======================================================================================
201
+
202
+ should "be able to create a double vector in R" do
203
+
204
+ vec = R.c(1, 2, 3, 4)
205
+
206
+
207
+ assert_equal("1.0 2.0 3.0 4.0 ", vec.to_string)
208
+
209
+ # All methods on MDArray can be called normally
210
+ vec.reset_statistics
211
+ assert_equal(2.5, vec.mean)
212
+ assert_equal(1.118033988749895, vec.standard_deviation)
213
+ assert_equal("11.0 12.0 13.0 14.0 ", (vec + 10).to_string)
214
+
215
+ end
216
+
217
+ #======================================================================================
218
+ # Need to check how MDArray works exactly with NaN
219
+ #======================================================================================
220
+
221
+ should "work with NA and NaN in double vectors" do
222
+
223
+ vec2 = R.c(1, NA, 3, 4)
224
+ vec2.print
225
+
226
+ vec3 = R.c(1, NaN, 3, 4)
227
+ vec3.print
228
+
229
+ end
230
+
231
+
232
+ #--------------------------------------------------------------------------------------
233
+ #
234
+ #--------------------------------------------------------------------------------------
235
+
236
+ should "use MDArray in R" do
237
+
238
+ # create a double MDArray
239
+ vec = MDArray.typed_arange(:double, 12)
240
+ vec.print
241
+
242
+ # assign the MDArray to an R vector "my.vec"
243
+ R.my__vec = vec
244
+
245
+ # print R's "my.vec"
246
+ R.eval("print(my.vec)")
247
+
248
+ # use the .r method in MDArray's to get the MDArray value in R
249
+ R.eval("print(#{vec.r})")
250
+
251
+ # Passing an MDArray without assigning it in a R vector is also possible
252
+ R.eval("print(#{MDArray.typed_arange(:double, 5).r})")
253
+
254
+ end
255
+
256
+ end
257
+
258
+ end
@@ -0,0 +1,227 @@
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 'env'
27
+ require 'scicom'
28
+
29
+ class SciComTest < Test::Unit::TestCase
30
+
31
+ context "R Vectors" do
32
+
33
+ #--------------------------------------------------------------------------------------
34
+ #
35
+ #--------------------------------------------------------------------------------------
36
+
37
+ setup do
38
+
39
+ end
40
+
41
+ #--------------------------------------------------------------------------------------
42
+ #
43
+ #--------------------------------------------------------------------------------------
44
+
45
+ should "do basic vector arithmetic" do
46
+
47
+ vec1 = R.c(1, 2.5, 4.5)
48
+ vec2 = R.c(3, 4, 5)
49
+
50
+ # unary minus
51
+ res = -vec1
52
+ assert_equal(true, (R.all(R.c(-1, -2.5, -4.5) == res)).gt)
53
+ assert_equal(false, (R.all(R.c(1, -2.5, -4.5) == res)).gt)
54
+
55
+ # unary plus
56
+ res = +vec1
57
+ assert_equal(false, (R.all(R.c(-1, -2.5, -4.5) == res)).gt)
58
+ assert_equal(true, (R.all(R.c(1, 2.5, 4.5) == res)).gt)
59
+
60
+ # addition
61
+ res = vec1 + vec2
62
+ assert_equal(true, (R.all(R.c(4, 6.5, 9.5) == res)).gt)
63
+ assert_equal(false, (R.all(R.c(4, 6.5, 9) == res)).gt)
64
+
65
+ # subtraction
66
+ res = vec1 - vec2
67
+ assert_equal(true, (R.all(R.c(-2, -1.5, -0.5) == res)).gt)
68
+
69
+ # multiplication
70
+ res = vec1 * vec2
71
+ assert_equal(true, (R.all(R.c(3, 10, 22.5) == res)).gt)
72
+
73
+ # division
74
+ res = vec1 / vec2
75
+ assert_equal(true, (R.all(R.c(0.333333333333333333333333, 0.625, 0.9) == res)).gt)
76
+
77
+ # modulus (x mod y)
78
+ res = vec1 % vec2
79
+ assert_equal(true, (R.all(R.c(1, 2.5, 4.5) == res)).gt)
80
+
81
+ # modulus (x mod y)
82
+ res = vec2 % vec1
83
+ assert_equal(true, (R.all(R.c(0, 1.5, 0.5) == res)).gt)
84
+
85
+ # integer division
86
+ res = vec1.int_div(vec2)
87
+ assert_equal(true, (R.all(R.c(0, 0, 0) == res)).gt)
88
+
89
+ # integer division
90
+ res = vec2.int_div(vec1)
91
+ assert_equal(true, (R.all(R.c(3, 1, 1) == res)).gt)
92
+
93
+ # exponetiation
94
+ res = vec1 ** vec2
95
+ assert_equal(true, (R.all(R.c(1, 39.0625, 1845.28125) == res)).gt)
96
+
97
+ end
98
+
99
+ #--------------------------------------------------------------------------------------
100
+ #
101
+ #--------------------------------------------------------------------------------------
102
+
103
+ should "do vector comparison" do
104
+
105
+ vec1 = R.c(1, 2.5, 4.5)
106
+ vec2 = R.c(3, 4, 5)
107
+ vec3 = R.c(1, 3, 4.5)
108
+
109
+ # R.all checks to see if every element of the vector is TRUE. R.any checks to see if at
110
+ # least one element of the vector is TRUE.
111
+
112
+ res = vec1 < vec2
113
+ assert_equal(true, R.all(res).gt)
114
+
115
+ res = vec1 < vec3
116
+ assert_equal(false, R.all(res).gt)
117
+
118
+ res = vec1 <= vec3
119
+ assert_equal(true, R.all(res).gt)
120
+
121
+ res = vec1 > vec2
122
+ assert_equal(false, R.all(res).gt)
123
+
124
+ res = vec1 > vec3
125
+ assert_equal(false, R.all(res).gt)
126
+
127
+ res = vec2 > vec3
128
+ assert_equal(true, R.all(res).gt)
129
+
130
+ res = vec3 >= vec1
131
+ assert_equal(true, R.all(res).gt)
132
+
133
+ res = vec3 > vec1
134
+ assert_equal(false, R.all(res).gt)
135
+ assert_equal(true, R.any(res).gt)
136
+
137
+ res = vec2 != vec1
138
+ assert_equal(true, R.all(res).gt)
139
+
140
+ # comparison is done element by element. R.all is true only if all elements on the
141
+ # vector are true. Is this case, there are elements that are equal
142
+ res = vec3 != vec1
143
+ assert_equal(false, R.all(res).gt)
144
+
145
+ end
146
+
147
+ #--------------------------------------------------------------------------------------
148
+ #
149
+ #--------------------------------------------------------------------------------------
150
+
151
+ should "do logical vector operations" do
152
+
153
+ vec1 = R.c(TRUE, TRUE, FALSE, FALSE)
154
+ vec2 = R.c(TRUE, FALSE, TRUE, FALSE)
155
+
156
+ # not
157
+ res = !vec1
158
+ assert_equal(true, (R.all(R.c(FALSE, FALSE, TRUE, TRUE) == res)).gt)
159
+
160
+ # and
161
+ res = vec1 & vec2
162
+ assert_equal(true, (R.all(R.c(TRUE, FALSE, FALSE, FALSE) == res)).gt)
163
+ assert_equal(false, (R.all(R.c(FALSE, FALSE, FALSE, FALSE) == res)).gt)
164
+
165
+ # or
166
+ res = vec1 | vec2
167
+ assert_equal(false, (R.all(R.c(TRUE, FALSE, FALSE, FALSE) == res)).gt)
168
+ assert_equal(true, (R.all(R.c(TRUE, TRUE, TRUE, FALSE) == res)).gt)
169
+
170
+ # only compares the first element of the vectors. Equivalent to R's &&
171
+ res = vec1.l_and(vec2)
172
+ assert_equal(true, res.gt)
173
+
174
+ end
175
+
176
+ #--------------------------------------------------------------------------------------
177
+ #
178
+ #--------------------------------------------------------------------------------------
179
+
180
+ should "do arithmetic with scalars" do
181
+
182
+ vec1 = R.c(1, 2.5, 4.5)
183
+ vec2 = R.c(3, 4, 5)
184
+
185
+ res = vec1 + 2
186
+ assert_equal(true, (R.all(R.c(3, 4.5, 6.5) == res)).gt)
187
+
188
+ res = vec1 - 2
189
+ res = vec1 * 2
190
+ res = vec1 / 2
191
+ res = vec1 % 2
192
+ res = vec1 ** 2
193
+ res = vec1 | 2
194
+ res = vec1 & 2
195
+ res = vec1 > 2
196
+ res = vec1 >= 2
197
+ res = vec1 < 2.5
198
+ res = vec1 <= 2.5
199
+
200
+ end
201
+
202
+ #--------------------------------------------------------------------------------------
203
+ #
204
+ #--------------------------------------------------------------------------------------
205
+
206
+ should "coerce scalar to vector when needed" do
207
+
208
+ vec1 = R.c(1, 2.5, 4.5)
209
+ vec2 = R.c(3, 4, 5)
210
+
211
+ res = 2 * vec2
212
+ res.pp
213
+
214
+ res = 2 - vec2
215
+ res.pp
216
+
217
+ res = 2 > vec2
218
+ res.pp
219
+
220
+ res = 2 <= vec1
221
+ res.pp
222
+
223
+ end
224
+
225
+ end
226
+
227
+ end