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,156 @@
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
+ should "get info from the workspace" do
47
+
48
+ # get default R options
49
+ opts = R.options
50
+
51
+ p "Some R options:"
52
+ print("\n")
53
+ # access the options through their names
54
+ p "timeout is: #{opts.timeout.gz}"
55
+ p "na.action: #{opts.na__action.gz}"
56
+ p "prompt: #{opts.prompt.gz}"
57
+ p "help.search: #{opts.help__search__types.gz}"
58
+ p "show error message: #{opts.show__error__messages.gz}"
59
+ print("\n")
60
+
61
+ end
62
+
63
+ #--------------------------------------------------------------------------------------
64
+ #
65
+ #--------------------------------------------------------------------------------------
66
+
67
+ should "work with missing numbers" do
68
+
69
+ # Since every value is a vector in R, .gt returns the 0th index of the vector as
70
+ # a truth value
71
+ assert_equal(false, R.is__na(10).gt)
72
+ assert_equal(true, R.is__na(NA).gt)
73
+
74
+ # this will result in error. In R is.na(NaN) is true and in Renjin it's false
75
+ # R.eval("print(is.na(NaN))")
76
+ # assert_equal(true, R.is__na(NaN).gt)
77
+
78
+ # R.is__na and R.na? are both valid and do the same thing
79
+ assert_equal(false, R.na?(10).gt)
80
+ assert_equal(false, R.na?(10.35).gt)
81
+ assert_equal(false, R.na?(R.eval("10L")).gt)
82
+ assert_equal(false, R.na?(R.eval("10.456")).gt)
83
+
84
+ # Use nil in Ruby when needing a NULL in R
85
+ p "R prints Warning message when is.na is applied to a value and not a vector: "
86
+ #assert_equal(0, R.length(R.na?(nil).gt))
87
+
88
+ p "checking if NULL is na"
89
+ R.eval("is.na(NULL)").pp
90
+
91
+ # Check NA property on a vector
92
+ vec = R.is__na(R.c(10.35, 10.0, 56, NA))
93
+
94
+ # remember that in the Renjin::Vector class the first element is index 1
95
+ assert_equal(false, vec[1].gt)
96
+ assert_equal(true, vec[4].gt)
97
+
98
+ # gt also works with an index. Remember .gt converts Renjin::Vector to MDArray and
99
+ # MDArray's first element is idexed by 0.
100
+ assert_equal(false, vec.gt(0))
101
+ assert_equal(false, vec.gt(1))
102
+ assert_equal(false, vec.gt(2))
103
+ assert_equal(true, vec.gt(3))
104
+
105
+ # Check NaN properties
106
+ assert_equal(true, R.is__nan(NaN).gt)
107
+ assert_equal(true, R.nan?(NaN).gt)
108
+ # Those are NaN
109
+ assert_equal(false, R.nan?(NA).gt)
110
+
111
+ # The result of is.nan(NULL) is logical(0). If we try to access a 0 length vector
112
+ # in SciCon a RuntimeError is raised
113
+ assert_raise ( RuntimeError ) { R.nan?(nil).gt }
114
+ assert_raise ( RuntimeError ) { R.nan?(R.eval("NULL")).gt }
115
+
116
+ end
117
+
118
+ #--------------------------------------------------------------------------------------
119
+ #
120
+ #--------------------------------------------------------------------------------------
121
+
122
+ should "work with infinites" do
123
+
124
+ # Infinite number
125
+ p Inf.gz
126
+
127
+ # Negative infinite number, equivalent to "-Inf" in R
128
+ p MInf.gz
129
+
130
+ assert_equal(Inf, Inf)
131
+ assert_equal(false, R.finite?(Inf).gt)
132
+ assert_equal(false, R.finite?(MInf).gt)
133
+
134
+ # Check if the number if finite
135
+ assert_equal(true, R.finite?(10).gt)
136
+ assert_equal(true, R.finite?(10.35).gt)
137
+
138
+ # chekc numbers to see if they are finite
139
+ # assert_equal(false, R.finite?(R.NaN_double))
140
+
141
+ assert_equal(false, R.finite?(NA).gt)
142
+ # assert_equal(false, R.finite?(R.NaN_double))
143
+
144
+ # Check a vector for the finite? property
145
+ R.finite?(R.c(2, 3, 4))
146
+ R.finite?(R.eval("10.456"))
147
+
148
+ # Int_NA is finite; however R.NA_double is not finite. Is this correct? Should
149
+ # check with the Renjin team.
150
+ assert_equal(false, R.finite?(NA).gt)
151
+
152
+ end
153
+
154
+ end
155
+
156
+ end
@@ -0,0 +1,114 @@
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 environment" do
32
+
33
+ #--------------------------------------------------------------------------------------
34
+ #
35
+ #--------------------------------------------------------------------------------------
36
+
37
+ setup do
38
+
39
+ # creating two distinct instances of SciCom
40
+ @r1 = R.new
41
+ @r2 = R.new
42
+
43
+ end
44
+
45
+
46
+ #--------------------------------------------------------------------------------------
47
+ # We should be able to create MDArray with different layouts such as row-major,
48
+ # column-major, or R layout.
49
+ #--------------------------------------------------------------------------------------
50
+
51
+ should "work with colum-major indexes" do
52
+
53
+ col2 = MDArray.double([2, 3, 4],
54
+ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
55
+ 17, 18, 19, 20, 21, 22, 23],
56
+ :column)
57
+ col2.print
58
+
59
+ assert_equal(0, col2[0, 0, 0])
60
+ assert_equal(3, col2[0, 0, 1])
61
+ assert_equal(6, col2[0, 0, 2])
62
+ assert_equal(9, col2[0, 0, 3])
63
+
64
+ # slice the col array. take a slice of c on the first dimension (0) and taking only
65
+ # the first (0) index,we should get the following array:
66
+ # [[0.00 3.00 6.00 9.00]
67
+ # [1.00 4.00 7.00 10.00]
68
+ # [2.00 5.00 8.00 11.00]]
69
+ slice = col2.slice(0, 0)
70
+ assert_equal(0, slice[0, 0])
71
+ assert_equal(3, slice[0, 1])
72
+ assert_equal(6, slice[0, 2])
73
+ assert_equal(9, slice[0, 3])
74
+
75
+ end
76
+
77
+ =begin
78
+
79
+ #--------------------------------------------------------------------------------------
80
+ # We should be able to create MDArray with different layouts such as row-major,
81
+ # column-major.
82
+ #--------------------------------------------------------------------------------------
83
+
84
+ should "work with R layout" do
85
+
86
+ ret = @r1.eval("vec=c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23)")
87
+ @r1.eval("dim(vec) = c(2, 3, 4)")
88
+
89
+ p "returned array is"
90
+ ret.print
91
+
92
+ @r1.eval("print(vec[1, 1, 1])")
93
+ @r1.eval("print(vec[1, 1, 2])")
94
+ @r1.eval("print(vec[1, 1, 3])")
95
+ @r1.eval("print(vec[1, 1, 4])")
96
+
97
+ end
98
+
99
+
100
+ #--------------------------------------------------------------------------------------
101
+ #
102
+ #--------------------------------------------------------------------------------------
103
+
104
+ should "raise when layout is unknown" do
105
+
106
+ assert_raise ( RuntimeError ) {
107
+ MDArray.double([2, 2, 3], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], :unk)
108
+ }
109
+
110
+ end
111
+ =end
112
+ end
113
+
114
+ end
@@ -0,0 +1,49 @@
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
+ require_relative 'test_R_interface'
30
+ require_relative 'test_creation'
31
+ require_relative 'test_basic'
32
+ require_relative 'test_vector'
33
+ require_relative 'test_operators'
34
+ require_relative 'test_list'
35
+ require_relative 'test_attributes'
36
+ require_relative 'test_factor'
37
+ require_relative 'test_array'
38
+ require_relative 'test_matrix'
39
+ require_relative 'test_dataframe'
40
+ require_relative 'test_linear_model'
41
+
42
+ =begin
43
+ require_relative 'test_subsetting'
44
+ require_relative 'test_double_receive'
45
+ require_relative 'test_double_assign'
46
+ require_relative 'test_functions'
47
+ require_relative 'test_user_function'
48
+ require_relative 'test_column-major'
49
+ =end
@@ -0,0 +1,299 @@
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 environment" do
32
+
33
+ #--------------------------------------------------------------------------------------
34
+ #
35
+ #--------------------------------------------------------------------------------------
36
+
37
+ setup do
38
+
39
+ end
40
+
41
+ #--------------------------------------------------------------------------------------
42
+ #
43
+ #--------------------------------------------------------------------------------------
44
+
45
+ should "create integer variable" do
46
+
47
+ # In R, every number is a vector. R Vector's are converted to Ruby Renjin::Vector
48
+ # (a new class defined by SciCom).
49
+ # An int can be created by calling 'eval'...
50
+ i1 = R.eval("10L")
51
+
52
+ # ... or it can be created by calling the 'i' method.
53
+ i2 = R.i(10)
54
+
55
+ # Method .r can be used in a Ruby vector to make it available in the R namespace
56
+ R.eval("print(#{i2.r})")
57
+
58
+ # both i1 and i2 are vectors. To print a vector we use method .pp
59
+ i1.pp
60
+ i2.pp
61
+
62
+ # Integer nuberic Vectors are created with method .i
63
+ # the returned value is a Renjin::Vector
64
+ my_int = R.i(10)
65
+ assert_equal(10, my_int.gz)
66
+ # method typeof returns the type of this vector
67
+ assert_equal("integer", my_int.typeof.gz)
68
+
69
+ end
70
+
71
+ #--------------------------------------------------------------------------------------
72
+ #
73
+ #--------------------------------------------------------------------------------------
74
+
75
+ should "create double variable" do
76
+
77
+ R.eval("i1 = 10.2387")
78
+ # the returned value is a Renjin::Vector
79
+ i1 = R.i1
80
+ assert_equal(10.2387, i1.gz)
81
+
82
+ # assign to an R variable the Vector returned previously. The original variable
83
+ # is still valid
84
+ R.assign("i2", i1)
85
+ assert_equal(10.2387, R.i2.gz)
86
+ assert_equal(10.2387, R.i1.gz)
87
+
88
+ # type of i2 is a double
89
+ assert_equal("double", R.eval("typeof(i2)").gz)
90
+ # same call can be done easier. Remember, i2 is defined only in the R namespace.
91
+ assert_equal("double", R.typeof(R.i2).gz)
92
+
93
+ # create a double without calling eval. Method .d creates a double vector with
94
+ # one element. Variable i2 is now defined in the Ruby namespace
95
+ i2 = R.d(345.7789)
96
+ assert_equal("double", i2.typeof.gz)
97
+ assert_equal(345.7789, i2.gz)
98
+
99
+ # Creating a double vector is done with R.d. From now on we will use preferably
100
+ # Ruby integration and not use 'eval'. 'eval' will be used sometimes just to show
101
+ # that it works and that a user that prefers to use SciCom with a standard R
102
+ # loook and feel can still do it.
103
+ dbl = R.d(10.25)
104
+ # Prints the vector (R notation)
105
+
106
+ end
107
+
108
+ #--------------------------------------------------------------------------------------
109
+ #
110
+ #--------------------------------------------------------------------------------------
111
+
112
+ should "acess variable content with .gz and .gt" do
113
+
114
+ # In order to access the value of a variable for it to be used in Ruby, method .gz is
115
+ # used. Later will see method get and why the name for the method as .gz.
116
+ dbl = R.d(10.25)
117
+ assert_equal(10.25, dbl.gz)
118
+
119
+ # When looking at a logical vector, method .gt gets the truth value of the vector
120
+ var = R.c(TRUE)
121
+ assert_equal(true, var.gt)
122
+
123
+ end
124
+
125
+ #--------------------------------------------------------------------------------------
126
+ #
127
+ #--------------------------------------------------------------------------------------
128
+
129
+ should "create complex variable" do
130
+
131
+ # complex vector: created by calling R.complex. Real and imaginary parts are obtained
132
+ # by calling R.Re and R.Im functions on the variable.
133
+
134
+ comp_var = R.complex(real: 2, imaginary: 1)
135
+ assert_equal(2, R.Re(comp_var).gz)
136
+ assert_equal(1, R.Im(comp_var).gz)
137
+
138
+ p "complex"
139
+
140
+ comp = R.as__complex(-1)
141
+ assert_equal(-1, R.Re(comp).gz)
142
+ assert_equal(0, R.Im(comp).gz)
143
+ assert_equal(true, R.is__complex(comp).gt)
144
+
145
+ # The (x, y) representation of numbers is easier to understand at first, but a
146
+ # polar coordinates representation is often more practical. You can get the
147
+ # relevant components of this representation by finding the modulus and complex
148
+ # argument of a complex number. In R, you would use Mod and Arg:
149
+
150
+ z = R.complex(real: 0, imaginary: 1)
151
+
152
+ R.Mod(z).pp
153
+ # [1] 1
154
+
155
+ R.Arg(z).pp
156
+ # [1] 1.570796
157
+
158
+ # Finally, you’ll want to be able to take the complex conjugate of a complex
159
+ # number; to do that in R, you can use Conj:
160
+ R.Conj(z).pp
161
+ # [1] 0-1i
162
+
163
+ # Obtain components of a complex number in polar coordinates
164
+ comp = R.complex(imaginary: 1, real: 0)
165
+ mod = R.Mod(comp)
166
+ arg = R.Arg(comp)
167
+ assert_equal(true, ((R.pi/R.d(2)).gz == arg.gz))
168
+ assert_equal(false, ((R.pi/R.d(2)).gz == mod.gz))
169
+
170
+ # To get the complex square root, you need to cast your negative number as a
171
+ # complex number using as__complex before applying sqrt:
172
+ R.as__complex(-1).pp
173
+ # Renjin not implemented yet
174
+ # R.sqrt(R.as__complex(-1)).pp
175
+ R.sqrt(-1).pp
176
+
177
+ end
178
+
179
+ #--------------------------------------------------------------------------------------
180
+ #
181
+ #--------------------------------------------------------------------------------------
182
+
183
+ should "create string variable" do
184
+
185
+ R.str1 = "hello there;"
186
+
187
+ # method get, gets the current element
188
+ assert_equal("hello there;", R.str1.gz)
189
+
190
+ R.str2 = "This is another string"
191
+ assert_equal("This is another string", R.str2.gz)
192
+
193
+ end
194
+
195
+ #--------------------------------------------------------------------------------------
196
+ #
197
+ #--------------------------------------------------------------------------------------
198
+
199
+ should "create vectors with more than one element (R.c)" do
200
+
201
+ # To create vectors in R, one uses the 'c' function. Use R.c to created vectors
202
+ # in SciCom
203
+
204
+ vec = R.c(1, 2, 3, 4, 5)
205
+ assert_equal(true, (R.d(1).eq vec[1]).gt)
206
+ assert_equal(true, (R.d(2).eq vec[2]).gt)
207
+
208
+ # In R, indexing a vector with zero returns Vector numeric(0)
209
+ oops = vec[0]
210
+ oops.pp
211
+
212
+ # Calling get on numeric(0) raises an exception
213
+ assert_raise ( RuntimeError ) { oops.get }
214
+
215
+ # Accessing a value outside of the defined vector bound returns a vector with
216
+ # one element, the NA (Not Available)
217
+ na = vec[10]
218
+ assert_equal(NA, na)
219
+
220
+ # Converting to Ruby will return NaN (Not a Number)
221
+ assert_equal(NaN, na.gz)
222
+
223
+ # Method get can be used with an index, to get a given element of a vector
224
+ assert_equal(1, vec.get(0))
225
+ assert_equal(4, vec.get(3))
226
+
227
+ # method R.c also works with strings
228
+ str = R.c("this is the first string", "this is the second string")
229
+ str.pp
230
+
231
+ end
232
+
233
+ #--------------------------------------------------------------------------------------
234
+ #
235
+ #--------------------------------------------------------------------------------------
236
+
237
+ should "accept (from..to) operators" do
238
+
239
+ # Vectors can be created using (from..to) or (from...to) operators in place where
240
+ # R uses ':' notation. (1..4) is a range that goes from 1 to 4 and (1...4) is a
241
+ # range that goes from 1 to 3.
242
+ vec1 = R.c((1..4))
243
+ vec1.pp
244
+
245
+ # Method length return the number of elements in a vector
246
+ assert_equal(4, vec1.length)
247
+ # The same can be obtained by calling length in R, but remember that R always returns
248
+ # a vector.
249
+ R.length(vec1).pp
250
+
251
+ # to use assert_equal, we need to get the vector's content
252
+ assert_equal(4, R.length(vec1).gz)
253
+
254
+ # We can also acess a Ruby vector in R by calling the .r method in this vector
255
+ R.eval("print(#{vec1.r})")
256
+
257
+ vec2 = R.c((1...4))
258
+ vec2.pp
259
+
260
+ # vec2 has only three elements
261
+ assert_equal(3, vec2.length)
262
+
263
+ end
264
+
265
+ #--------------------------------------------------------------------------------------
266
+ #
267
+ #--------------------------------------------------------------------------------------
268
+
269
+ should "create vectors with method seq" do
270
+
271
+ # create a sequence (from, to)
272
+ seq1 = R.seq(2, 10)
273
+ seq1.pp
274
+
275
+ # R options can also be used, although the notation is a little different. In
276
+ # R we would call seq(2, 10, by = 3). In SciCom the same call is R.seq(2, 10, by: 3)
277
+ seq2 = R.seq(2, 10, by: 3)
278
+ seq2.print
279
+
280
+ # giving the number of required elements in the sequence
281
+ seq3 = R.seq(2, 100, length: 12)
282
+ seq3.pp
283
+
284
+ end
285
+
286
+ #--------------------------------------------------------------------------------------
287
+ #
288
+ #--------------------------------------------------------------------------------------
289
+
290
+ should "create vectors with method rep" do
291
+
292
+ vec = R.rep(R.c(1, 2, 3), 3)
293
+ vec.pp
294
+
295
+ end
296
+
297
+ end
298
+
299
+ end