scicom 0.2.2-java → 0.2.3-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,111 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
- ##########################################################################################
4
- # @author Rodrigo Botafogo
5
- #
6
- # Copyright © 2013 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
7
- # and distribute this software and its documentation, without fee and without a signed
8
- # licensing agreement, is hereby granted, provided that the above copyright notice, this
9
- # paragraph and the following two paragraphs appear in all copies, modifications, and
10
- # distributions.
11
- #
12
- # IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
13
- # INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
14
- # THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
15
- # POSSIBILITY OF SUCH DAMAGE.
16
- #
17
- # RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18
- # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
19
- # SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
20
- # RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
21
- # OR MODIFICATIONS.
22
- ##########################################################################################
23
-
24
- class List < Vector
25
-
26
- #----------------------------------------------------------------------------------------
27
- #
28
- #----------------------------------------------------------------------------------------
29
-
30
- def length
31
- @sexp.length()
32
- end
33
-
34
- #----------------------------------------------------------------------------------------
35
- # index can be a numeric index or a string index.
36
- #----------------------------------------------------------------------------------------
37
-
38
- def get(index)
39
- RubySexp.build(@sexp.get(index))
40
- end
41
-
42
- #----------------------------------------------------------------------------------------
43
- #
44
- #----------------------------------------------------------------------------------------
45
-
46
- def [](index)
47
- get(index)
48
- end
49
-
50
- #----------------------------------------------------------------------------------------
51
- #
52
- #----------------------------------------------------------------------------------------
53
-
54
- def index_of_name(name)
55
- @sexp.indexOfName(name)
56
- end
57
-
58
- #----------------------------------------------------------------------------------------
59
- # index can be a numeric index or a string index.
60
- #----------------------------------------------------------------------------------------
61
-
62
- def get_element_as_double(index)
63
- @sexp.getElementAsDouble(index)
64
- end
65
-
66
- #----------------------------------------------------------------------------------------
67
- # index can be a numeric index or a string index.
68
- #----------------------------------------------------------------------------------------
69
-
70
- def get_element_as_int(index)
71
- @sexp.getElementAsInt(index)
72
- end
73
-
74
- #----------------------------------------------------------------------------------------
75
- #
76
- #----------------------------------------------------------------------------------------
77
-
78
- def get_element_as_list(name)
79
- RubySexp.build(@sexp.getElementAsList(name))
80
- end
81
-
82
- #----------------------------------------------------------------------------------------
83
- #
84
- #----------------------------------------------------------------------------------------
85
-
86
- def get_element_as_vector(name)
87
- RubySexp.build(@sexp.getElementAsVector(name))
88
- end
89
-
90
- #----------------------------------------------------------------------------------------
91
- # Treats ruby style methods in lists as named items on the list
92
- #----------------------------------------------------------------------------------------
93
-
94
- def method_missing(symbol, *args)
95
-
96
- name = symbol.id2name
97
- name.gsub!(/__/,".")
98
- # super if args.length != 0
99
- if (args.length == 0)
100
- # treat name as a named item of the list
101
- ret = R.eval("#{r}[\"#{name}\"]")[0]
102
- else
103
- raise "Illegal argument for named list item #{name}"
104
- end
105
-
106
- ret
107
-
108
- end
109
-
110
- end
111
-
@@ -1,240 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
- # -*- coding: utf-8 -*-
4
-
5
- ##########################################################################################
6
- # Copyright © 2013 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
7
- # and distribute this software and its documentation, without fee and without a signed
8
- # licensing agreement, is hereby granted, provided that the above copyright notice, this
9
- # paragraph and the following two paragraphs appear in all copies, modifications, and
10
- # distributions.
11
- #
12
- # IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
13
- # INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
14
- # THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
15
- # POSSIBILITY OF SUCH DAMAGE.
16
- #
17
- # RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18
- # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
19
- # SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
20
- # RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
21
- # OR MODIFICATIONS.
22
- ##########################################################################################
23
-
24
- require 'rubygems'
25
- require "test/unit"
26
- require 'shoulda'
27
-
28
- require 'env'
29
- require 'scicom'
30
-
31
- class SciComTest < Test::Unit::TestCase
32
-
33
- context "R environment" do
34
-
35
- #--------------------------------------------------------------------------------------
36
- #
37
- #--------------------------------------------------------------------------------------
38
-
39
- setup do
40
-
41
- # creating new instance of R interpreter
42
- @r1 = Renjin.new
43
-
44
- end
45
-
46
- =begin
47
- #--------------------------------------------------------------------------------------
48
- #
49
- #--------------------------------------------------------------------------------------
50
-
51
- should "create Renjin vectors" do
52
-
53
- arr = MDArray.typed_arange(:double, 12)
54
- arr.reshape!([4, 3])
55
- # arr.print
56
-
57
- vector = R.build_vector(arr)
58
- (0...arr.size).each do |index|
59
- vector.getElementAsDouble(index)
60
- end
61
-
62
- # typed_arange does the same as arange but for arrays of other type
63
- arr = MDArray.typed_arange(:double, 60)
64
- # MDArray is stored in row-major order
65
- arr.reshape!([5, 3, 4])
66
- # arr.print
67
-
68
- vector = R.build_vector(arr)
69
- (0...arr.size).each do |index|
70
- vector.getElementAsDouble(index)
71
- end
72
-
73
- end
74
- =end
75
- #--------------------------------------------------------------------------------------
76
- #
77
- #--------------------------------------------------------------------------------------
78
-
79
- should "send 2D arrays to Renjin" do
80
-
81
- # typed_arange does the same as arange but for arrays of other type
82
- arr = MDArray.typed_arange(:double, 12)
83
- arr.reshape!([4, 3])
84
- # arr.print
85
-
86
- # assign MDArray to R vector. MDArray shape is converted to R shape: two dimensions
87
- # are identical in MDArray and R.
88
- R.vec = arr
89
-
90
- # When accessing a vector with the wrong indexes, return nil
91
- res = R.eval("vec[0]")
92
- assert_equal(nil, res)
93
-
94
- # R.eval("print(vec[1, 1])")
95
- # R.eval("print(vec[1, 2])")
96
-
97
-
98
- # First index in R is 1 and not 0.
99
- # method R.ri converts an MDArray index to a R index (in string format) ready
100
- # to evaluate
101
- arr.each_with_counter do |val, ct|
102
- assert_equal(val, R.eval("vec#{R.ri(ct)}"))
103
- end
104
-
105
- end
106
- #=begin
107
- #--------------------------------------------------------------------------------------
108
- #
109
- #--------------------------------------------------------------------------------------
110
-
111
- should "send 3D arrays to Renjin" do
112
-
113
- # typed_arange does the same as arange but for arrays of other type
114
- arr = MDArray.typed_arange(:double, 60)
115
- # MDArray is stored in row-major order
116
- arr.reshape!([5, 3, 4])
117
- # arr.print
118
-
119
- # shape of R.vec is [3, 4, 5].
120
- R.vec = arr
121
- R.eval("print(dim(vec))")
122
- # R.eval("print(vec)")
123
-
124
- # The data in the array can be accessed both in MDArray as in the R vector.
125
- # To access the same element, indexing has to be properly converted from MDArray
126
- # indexing to R indexing. In general converting from MDArray index to R index
127
- # is done as follows: Let [i1, i2, i3, ... in] be the MDArray index, the
128
- # corresponding R index is [i(n-1)+1, in+1, ..., i3+1, i2+1, i1+1]. As ane example
129
- # arr[3, 0, 1] is the R vector vec[1, 2, 4]
130
- assert_equal(arr[3, 0, 1], R.eval("vec[1, 2, 4]")[0])
131
- # arr[3, 1, 2] is vec[2, 3, 4]
132
- assert_equal(arr[3, 1, 2], R.eval("vec[2, 3, 4]")[0])
133
-
134
- # method R.ri converts an MDArray index to a R index (in string format) ready
135
- # to evaluate
136
- arr.each_with_counter do |val, ct|
137
- assert_equal(arr.get(ct), R.eval("vec#{R.ri(ct)}"))
138
- end
139
- end
140
-
141
- #=begin
142
-
143
- #--------------------------------------------------------------------------------------
144
- #
145
- #--------------------------------------------------------------------------------------
146
-
147
- should "send 4D arrays to Renjin" do
148
-
149
- # typed_arange does the same as arange but for arrays of other type
150
- arr = MDArray.typed_arange(:double, 120)
151
- arr.reshape!([2, 4, 3, 5])
152
- R.vec = arr
153
- arr.each_with_counter do |val, ct|
154
- assert_equal(val, R.eval("vec#{R.ri(ct)}"))
155
- end
156
-
157
- end
158
-
159
- #--------------------------------------------------------------------------------------
160
- #
161
- #--------------------------------------------------------------------------------------
162
-
163
- should "send 5D arrays to Renjin" do
164
-
165
- # typed_arange does the same as arange but for arrays of other type
166
- arr = MDArray.typed_arange(:double, 360)
167
- arr.reshape!([2, 4, 3, 5, 3])
168
- R.vec = arr
169
- arr.each_with_counter do |val, ct|
170
- assert_equal(val, R.eval("vec#{R.ri(ct)}"))
171
- end
172
-
173
- end
174
-
175
- #--------------------------------------------------------------------------------------
176
- #
177
- #--------------------------------------------------------------------------------------
178
-
179
- should "send 6D arrays to Renjin" do
180
-
181
- # typed_arange does the same as arange but for arrays of other type
182
- arr = MDArray.typed_arange(:double, 720)
183
- arr.reshape!([2, 4, 3, 5, 3, 2])
184
- R.vec = arr
185
- arr.each_with_counter do |val, ct|
186
- assert_equal(val, R.eval("vec#{R.ri(ct)}"))
187
- end
188
-
189
- end
190
-
191
- #--------------------------------------------------------------------------------------
192
- #
193
- #--------------------------------------------------------------------------------------
194
-
195
- should "send 7D arrays to Renjin" do
196
-
197
- # typed_arange does the same as arange but for arrays of other type
198
- arr = MDArray.typed_arange(:double, 2160)
199
- arr.reshape!([2, 4, 3, 5, 3, 2, 3])
200
- R.vec = arr
201
- arr.each_with_counter do |val, ct|
202
- assert_equal(val, R.eval("vec#{R.ri(ct)}"))
203
- end
204
-
205
- end
206
-
207
- #--------------------------------------------------------------------------------------
208
- #
209
- #--------------------------------------------------------------------------------------
210
-
211
- should "send larger than 7D arrays to Renjin" do
212
-
213
- # typed_arange does the same as arange but for arrays of other type
214
- arr = MDArray.typed_arange(:double, 8640)
215
- arr.reshape!([2, 4, 3, 5, 3, 2, 3, 4])
216
- R.vec = arr
217
- arr.each_with_counter do |val, ct|
218
- assert_equal(val, R.eval("vec#{R.ri(ct)}"))
219
- end
220
-
221
- end
222
- #=end
223
- =begin
224
-
225
- #--------------------------------------------------------------------------------------
226
- #
227
- #--------------------------------------------------------------------------------------
228
-
229
- should "receive multidimensional arrays from Renjin" do
230
-
231
- # returned value is column major but MDArray is interpreting as row major
232
- mat = R.eval(" mat = matrix(rnorm(20), 4)")
233
- mat.print
234
- R.eval("print(mat)")
235
- end
236
- =end
237
-
238
- end
239
-
240
- end