jl4rb 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c2e556eacfd629abcbddd8756e0ef040beac465a
4
+ data.tar.gz: 3acd1b2d43241368d81c4a06ae5f9501b379b887
5
+ SHA512:
6
+ metadata.gz: 99e1ab7677c957848d361191557d928b21cbdc46b011cf9ee70248d4a490f9814f1867f2b157a4c60038cbd81e61b0b504cc817a2751a12629639ac69186bc4c
7
+ data.tar.gz: a7f1070f72ec507cf31cf0e0216af0dc5b2cea8c3058025ef4f7f8597cfac4d045f34c22acc33ee73e100a9dfbab1724886edb2d7a1a7f2cad615ba25c808de4
data/Rakefile ADDED
@@ -0,0 +1,113 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rubygems/package_task'
4
+
5
+ PKG_NAME='jl4rb'
6
+ PKG_VERSION='0.0.1'
7
+ PKG_FILES=FileList[
8
+ 'Rakefile','jl4rb.gemspec',
9
+ 'ext/jl4rb/*.c',
10
+ 'ext/jl4rb/extconf.rb',
11
+ 'ext/jl4rb/*.inc',
12
+ 'ext/jl4rb/*.h',
13
+ 'ext/jl4rb/MANIFEST',
14
+ 'lib/**/*.rb',
15
+ 'test/**/*.rb'
16
+ ]
17
+
18
+ spec = Gem::Specification.new do |s|
19
+ s.platform = Gem::Platform::RUBY
20
+ s.summary = "Julia for ruby"
21
+ s.name = PKG_NAME
22
+ s.version = PKG_VERSION
23
+ s.requirements << 'none'
24
+ s.require_paths = ["lib","ext/jl4rb"]
25
+ s.files = PKG_FILES.to_a
26
+ s.extensions = ["ext/jl4rb/extconf.rb"]
27
+ s.licenses = ['MIT', 'GPL-2']
28
+ s.description = <<-EOF
29
+ R is embedded in ruby with some communication support .
30
+ EOF
31
+ s.author = "CQLS"
32
+ s.email= "rdrouilh@gmail.com"
33
+ s.homepage = "http://cqls.upmf-grenoble.fr"
34
+ s.rubyforge_project = nil
35
+ end
36
+
37
+ ## this allows to produce some parameter for task like Gem::PackageTask (without additional argument!)
38
+ opt={};ARGV.select{|e| e=~/\=/ }.each{|e| tmp= e.split("=");opt[tmp[0]]=tmp[1]}
39
+
40
+ ## rake ... pkgdir=<path to provide> to update PKGDIR
41
+ PKGDIR=opt["pkgdir"] || ENV["RUBYGEMS_PKGDIR"] || "pkg"
42
+ ## OLD: gem task!!!
43
+ # desc "Create #{PKG_NAME+'-'+PKG_VERSION+'.gem'}"
44
+ # Gem::PackageTask.new(spec) do |pkg|
45
+ # pkg.package_dir=PKGDIR
46
+ # pkg.need_zip = false
47
+ # pkg.need_tar = false
48
+ # end
49
+
50
+ # it is less verbose than the previous one
51
+ desc "Create #{PKG_NAME+'-'+PKG_VERSION+'.gem'}"
52
+ task :package do |t|
53
+ #Gem::Builder.new(spec_client).build
54
+ unless File.directory? PKGDIR
55
+ require 'fileutils'
56
+ FileUtils.mkdir_p PKGDIR
57
+ end
58
+ Gem::Package.build(spec)
59
+ `mv #{PKG_NAME+'-'+PKG_VERSION+'.gem'} #{PKGDIR}`
60
+ end
61
+
62
+ ## clean task
63
+ desc "Remove #{File.join(PKGDIR,PKG_NAME+'-'+PKG_VERSION+'.gem')}"
64
+ task :clean do |t|
65
+ rm File.join(PKGDIR,PKG_NAME+'-'+PKG_VERSION+'.gem') if File.exists? File.join(PKGDIR,PKG_NAME+'-'+PKG_VERSION+'.gem')
66
+ rm_rf File.join(PKGDIR,PKG_NAME+'-'+PKG_VERSION) if File.exists? File.join(PKGDIR,PKG_NAME+'-'+PKG_VERSION)
67
+ end
68
+
69
+
70
+ ## install task with doc
71
+ desc "Install #{File.join(PKGDIR,PKG_NAME+'-'+PKG_VERSION+'.gem')}"
72
+ task :install_with_doc do |t|
73
+ `gem install #{File.join(PKGDIR,PKG_NAME+'-'+PKG_VERSION+'.gem')} --local`
74
+ end
75
+
76
+ ## quick install task
77
+ desc "Quick install #{File.join(PKGDIR,PKG_NAME+'-'+PKG_VERSION+'.gem')}"
78
+ task :install do |t|
79
+ `gem install #{File.join(PKGDIR,PKG_NAME+'-'+PKG_VERSION+'.gem')} --local --no-rdoc --no-ri`
80
+ rm_rf File.join(PKGDIR,PKG_NAME+'-'+PKG_VERSION) if File.exists? File.join(PKGDIR,PKG_NAME+'-'+PKG_VERSION)
81
+ end
82
+
83
+ desc "Docker install #{File.join(PKGDIR,PKG_NAME+'-'+PKG_VERSION+'.gem')}"
84
+ task :docker => :package do |t|
85
+ `gem install #{File.join(PKGDIR,PKG_NAME+'-'+PKG_VERSION+'.gem')} --local --no-rdoc --no-ri`
86
+ rm_rf File.join(PKGDIR,PKG_NAME+'-'+PKG_VERSION) if File.exists? File.join(PKGDIR,PKG_NAME+'-'+PKG_VERSION)
87
+ end
88
+
89
+
90
+ ## binary task (mainly for Windows binary)
91
+ spec_bin=Gem::Specification.new do |s|
92
+ s.platform = Gem::Platform::CURRENT
93
+ s.summary = "Julia for ruby"
94
+ s.name = PKG_NAME
95
+ s.version = PKG_VERSION
96
+ s.requirements << 'none'
97
+ s.require_paths = ["lib"]
98
+ s.files = Dir['lib/**/*.rb'] + Dir['lib/*.so']
99
+ s.required_ruby_version = '>= 1.8.0'
100
+ s.description = <<-EOF
101
+ R is embedded in ruby with some communication support .
102
+ EOF
103
+ s.author = "CQLS"
104
+ s.email= "rdrouilh@gmail.com"
105
+ s.homepage = "http://cqls.upmf-grenoble.fr"
106
+ s.rubyforge_project = nil
107
+ s.has_rdoc = false
108
+ end
109
+
110
+ task :gem_bin do |t|
111
+ `cp ext/jl4rb/*.so lib`
112
+ Gem::Builder.new(spec_bin).build
113
+ end
File without changes
@@ -0,0 +1,44 @@
1
+ require "mkmf"
2
+ require 'fileutils' #if RUBY_VERSION < "1.9"
3
+
4
+
5
+ $prefix_include,$prefix_lib=[],[]
6
+ if RUBY_PLATFORM =~ /linux/ and File.exists? "/usr/include/"+RUBY_PLATFORM+"/julia"
7
+ $prefix_include << "/usr/include/"+RUBY_PLATFORM+"/julia"
8
+ $prefix_lib << "/usr/lib/"+RUBY_PLATFORM+"/julia"
9
+ else
10
+ #$prefix=ENV["JLAPI_HOME"] || File.join(ENV["HOME"],".jlapi/julia") || ENV["JULIA_HOME"]
11
+ $prefix=ENV["JULIA_DIR"]
12
+ #p $prefix
13
+ [$prefix+"/include/julia",$prefix+"/usr/include",$prefix+"/src",$prefix+"/src/support"].each do |incl|
14
+ $prefix_include << incl if File.exists? incl
15
+ end
16
+
17
+ ([$prefix+"/lib/julia",$prefix+"/usr/lib"]+(RUBY_PLATFORM=~/(?:mingw|msys)/ ? [$prefix+"/bin"] : [])).each do |lib|
18
+ $prefix_lib << lib if File.exists? lib
19
+ end
20
+ end
21
+
22
+ def jl4rb_makefile(incs,libs)
23
+ #$CFLAGS = "-I"+inc+" -I."
24
+ # $CFLAGS = (enable_config("julia-release") ? "-DWITH_JULIA_RELEASE " : "")+ "-I"+inc+" -I."
25
+ # $LDFLAGS = " -Wl,-rpath,"+lib+" -L"+lib if lib
26
+ # $libs = (enable_config("julia-release") ? " -ljulia-release" : " -ljulia-api" )
27
+
28
+ $CFLAGS = incs.map{|inc| "-I"+inc}.join(" ")+" -I."
29
+ $LDFLAGS = " "+libs.map{|lib| "-Wl,-rpath,"+lib+" -L"+lib}.join(" ") if libs
30
+ $libs = " -ljulia"
31
+
32
+
33
+ header = nil
34
+
35
+ rb4r_name="jl4rb"
36
+ $objs = [rb4r_name+".o"]
37
+
38
+ dir_config("R4rb")
39
+ create_makefile(rb4r_name)
40
+ end
41
+
42
+ jl4rb_makefile($prefix_include,$prefix_lib)
43
+
44
+
data/ext/jl4rb/jl4rb.c ADDED
@@ -0,0 +1,558 @@
1
+ /**********************************************************************
2
+
3
+ jl4rb.c
4
+
5
+ **********************************************************************/
6
+
7
+ #include "julia.h"
8
+ #include <stdio.h>
9
+ #include <string.h>
10
+ #include <math.h>
11
+
12
+ //#define WITH_JULIA_RELEASE
13
+
14
+ //#ifdef WITH_JULIA_RELEASE
15
+ //#else
16
+ //#include "julia-api.h"
17
+ //#endif
18
+
19
+ //-| next macros already exist in ruby and are undefined here
20
+ //#undef T_FLOAT
21
+ #undef NORETURN
22
+ #include "ruby.h"
23
+
24
+ #define length(a) jl_array_size(a,0)
25
+
26
+ /************* INIT *********************/
27
+
28
+
29
+ VALUE Julia_init(VALUE obj, VALUE args)
30
+ {
31
+ char **argv,*julia_home_dir;
32
+ int i,argc;
33
+ VALUE tmp;
34
+
35
+ argc=RARRAY_LEN(args) + 1;
36
+ tmp=rb_ary_entry(args,0);
37
+ julia_home_dir=StringValuePtr(tmp);
38
+ //printf("First initialization with julia_home_dir=%s\n",julia_home_dir);
39
+ // printf("copy stacks ");
40
+ // #ifdef COPY_STACKS
41
+ // printf("defined \n");
42
+ // #else
43
+ // printf("undefined \n");
44
+ // #endif
45
+ // printf("JL_SET_STACK_BASE ");
46
+ // #ifdef JL_SET_STACK_BASE
47
+ // printf("defined \n");
48
+ // #else
49
+ // printf("undefined \n");
50
+ // #endif
51
+ //#ifdef WITH_JULIA_RELEASE
52
+ if(strcmp(julia_home_dir,"")==0) {
53
+ jl_init(NULL);
54
+ //JL_SET_STACK_BASE;
55
+ } else {
56
+ jl_init(julia_home_dir);
57
+ //JL_SET_STACK_BASE;
58
+ }
59
+ //#else
60
+ // jlapi_init(julia_home_dir,mode);
61
+ //#endif
62
+
63
+ return Qtrue;
64
+ }
65
+
66
+ //Maybe try to use cpp stuff to get the output inside julia system (ccall,cgen and cgutils)
67
+ //-| TODO: after adding in the jlapi.c jl_is_<C_type> functions replace the strcmp!
68
+ VALUE jl_value_to_VALUE(jl_value_t *res) {
69
+ size_t i=0,k,nd,d;
70
+ VALUE resRb;
71
+ jl_value_t *tmp;
72
+ jl_function_t *call;
73
+
74
+ if(res!=NULL) { //=> get a result
75
+ //printf("typeof=%s\n",jl_typeof_str(res));
76
+ if(strcmp(jl_typeof_str(res),"Int64")==0 || strcmp(jl_typeof_str(res),"Int32")==0)
77
+ //if(jl_is_long(res)) //does not work because of DLLEXPORT
78
+ {
79
+ //printf("elt=%d\n",jl_unbox_long(res));
80
+ return INT2FIX(jl_unbox_long(res));
81
+ }
82
+ else
83
+ if(strcmp(jl_typeof_str(res),"Float64")==0)
84
+ //if(jl_is_float64(res))
85
+ {
86
+ return rb_float_new(jl_unbox_float64(res));
87
+ }
88
+ else
89
+ if(strcmp(jl_typeof_str(res),"Float32")==0)
90
+ //if(jl_is_float64(res))
91
+ {
92
+ return rb_float_new(jl_unbox_float32(res));
93
+ }
94
+ else
95
+ if(strcmp(jl_typeof_str(res),"Bool")==0)
96
+ //if(jl_is_bool(res))
97
+ {
98
+ return (jl_unbox_bool(res) ? Qtrue : Qfalse);
99
+ }
100
+ else
101
+ if(strcmp(jl_typeof_str(res),"DataType")==0)
102
+ {
103
+ return rb_str_new2(jl_typename_str(res));
104
+ }
105
+ else
106
+ if(strcmp(jl_typeof_str(res),"Nothing")==0)
107
+ {
108
+ return Qnil;
109
+ }
110
+ else
111
+ if(strcmp(jl_typeof_str(res),"Complex")==0)
112
+ //if(jl_is_bool(res))
113
+ {
114
+ resRb = rb_eval_string("require('complex');Complex.new(0,0)");
115
+ rb_iv_set(resRb,"@real",jl_value_to_VALUE(jl_get_field(res, "re")));
116
+ rb_iv_set(resRb,"@image",jl_value_to_VALUE(jl_get_field(res, "im")));
117
+ return resRb;
118
+ }
119
+ else
120
+ if(strcmp(jl_typeof_str(res),"Regex")==0)
121
+ //if(jl_is_bool(res))
122
+ {
123
+ // call=(jl_function_t*)jl_get_global(jl_base_module, jl_symbol("show"));
124
+ // printf("ici\n");
125
+ // if (call) tmp=jl_call1(call,res);
126
+ // else printf("call failed!\n");
127
+ // printf("ici\n");
128
+ resRb = jl_value_to_VALUE(jl_get_field(res, "pattern"));
129
+ return resRb;
130
+ }
131
+ else
132
+ if(strcmp(jl_typeof_str(res),"ASCIIString")==0 || strcmp(jl_typeof_str(res),"UTF8String")==0)
133
+ {
134
+ //printf("value=%s\n",jl_bytestring_ptr(res));
135
+ return rb_str_new2(jl_bytestring_ptr(res));
136
+ }
137
+ else
138
+ if(strcmp(jl_typeof_str(res),"Array")==0 )
139
+ //if(jl_is_array(res))
140
+ {
141
+ nd = jl_array_rank(res);
142
+ //printf("array_ndims=%d\n",(int)nd);
143
+ if(nd==1) {//Vector
144
+ d = jl_array_size(res, 0);
145
+ //printf("array_dim[1]=%d\n",(int)d);
146
+ resRb = rb_ary_new2(d);
147
+ for(i=0;i<d;i++) {
148
+ rb_ary_store(resRb,i,jl_value_to_VALUE(jl_arrayref((jl_array_t *)res,i)));
149
+ }
150
+ return resRb;
151
+ }
152
+ //TODO: multidim array ruby equivalent???? Is it necessary
153
+
154
+ }
155
+ else
156
+ if(strcmp(jl_typeof_str(res),"Tuple")==0 )
157
+ //if(jl_is_array(res))
158
+ {
159
+ d=jl_tuple_len(res);
160
+ resRb = rb_ary_new2(d);
161
+ for(i=0;i<d;i++) {
162
+ rb_ary_store(resRb,i,jl_value_to_VALUE(jl_tupleref(res,i)));
163
+ }
164
+ return resRb;
165
+ }
166
+ //printf("unconverted!!!");
167
+ resRb=rb_str_new2("__unconverted(");
168
+ rb_str_cat2(resRb, jl_typeof_str(res));
169
+ rb_str_cat2(resRb, ")__\n");
170
+ //printf("%s\n",jl_bytestring_ptr(jl_eval_string("\"$(ans)\"")));
171
+ // jl_function_t *call=(jl_function_t*)jl_get_global(jl_base_module, jl_symbol("show"));
172
+ // if (call) jl_call1(call,res);
173
+ // else printf("call failed!\n");
174
+
175
+ return resRb;
176
+ }
177
+ //=> No result (command incomplete or syntax error)
178
+ //#ifndef WITH_JULIA_RELEASE
179
+ // jlapi_print_stderr(); //If this happens but this is really not sure!
180
+ //#endif
181
+ //printf("incomplete!!!");
182
+ resRb=rb_str_new2("__incomplete");
183
+ if(jl_exception_occurred()!=NULL) {
184
+ rb_str_cat2(resRb, "(");
185
+ rb_str_cat2(resRb,jl_typeof_str(jl_exception_occurred()));
186
+ jl_value_t* err=jl_get_field(jl_exception_occurred(),"msg");
187
+ if(err!=NULL) printf("%s: %s\n",jl_typeof_str(jl_exception_occurred()),jl_bytestring_ptr(err));
188
+ jl_exception_clear();
189
+ rb_str_cat2(resRb, ")");
190
+ }
191
+ rb_str_cat2(resRb, "__");
192
+ return resRb;
193
+ }
194
+
195
+ /***************** EVAL **********************/
196
+
197
+ VALUE Julia_eval(VALUE obj, VALUE cmd, VALUE print_stdout)
198
+ {
199
+ char *cmdString;
200
+ jl_value_t *res;
201
+ VALUE resRb;
202
+
203
+ cmdString=StringValuePtr(cmd);
204
+ //printf("cmd=%s\n",cmdString);
205
+ //#ifndef WITH_JULIA_RELEASE
206
+ //This flush redirected stdout before printing
207
+ //if(print_stdout!=Qnil) jlapi_get_stdout();
208
+ //#endif
209
+ //jl_gc_disable();
210
+ res=jl_eval_string(cmdString);
211
+ //printf("cmd=%s\n",cmdString);
212
+ if (jl_exception_occurred()) {
213
+ jl_show(jl_stderr_obj(), jl_exception_occurred());
214
+ jl_printf(jl_stderr_stream(), "\n");
215
+ resRb=Qnil;
216
+ } else {
217
+ //JL_GC_PUSH1(&res);
218
+ //printf("cmd=%s\n",cmdString);
219
+ //jl_set_global(jl_base_module, jl_symbol("ans"),res);
220
+ //#ifndef WITH_JULIA_RELEASE
221
+ // if(print_stdout!=Qnil) jlapi_print_stdout();
222
+ //#endif
223
+ resRb=jl_value_to_VALUE(res);
224
+ //JL_GC_POP();
225
+ }
226
+ //jl_gc_enable();
227
+ return resRb;
228
+ }
229
+
230
+ VALUE Julia_exec(VALUE obj, VALUE cmd, VALUE get_stdout)
231
+ {
232
+ char *cmdString,*outString;
233
+ jl_value_t *res;
234
+ VALUE out;
235
+
236
+ cmdString=StringValuePtr(cmd);
237
+ res=jl_eval_string(cmdString);
238
+ jl_set_global(jl_base_module, jl_symbol("ans"),res);
239
+ //if(get_stdout!=Qnil) {
240
+ // outString=jlapi_get_stdout();
241
+ // jl_set_global(jl_base_module, jl_symbol("rbout"),jl_cstr_to_string(outString));
242
+ // return rb_str_new2(outString);
243
+ //} else
244
+ return Qnil;
245
+ }
246
+
247
+
248
+ /// Util
249
+
250
+ int util_isVector(jl_value_t *ans)
251
+ {
252
+ return (strcmp(jl_typeof_str(ans),"Array")==0) && (jl_array_rank(ans)==1);
253
+ }
254
+
255
+ int util_isVariable(VALUE self)
256
+ {
257
+ VALUE tmp;
258
+ tmp=rb_iv_get(self,"@type");
259
+ return strcmp(StringValuePtr(tmp),"var")==0;
260
+ }
261
+
262
+ jl_value_t *util_getVar(VALUE self)
263
+ {
264
+ jl_value_t *ans;
265
+ char *name;
266
+ VALUE tmp;
267
+
268
+ tmp=rb_iv_get(self,"@name");
269
+ name=StringValuePtr(tmp);
270
+ ans=jl_eval_string(name);
271
+ //printf("name=%s,vector=%d\n",name,util_isVector(ans));
272
+ if(!util_isVector(ans)) return NULL;
273
+ return ans;
274
+ }
275
+
276
+ //with argument!! necessarily an expression and not a variable
277
+ jl_value_t *util_getExpr_with_arg(VALUE self)
278
+ {
279
+ jl_value_t *ans;
280
+ VALUE tmp;
281
+ char *cmd;
282
+
283
+ //printf("getVar:%s\n",name);
284
+ tmp=rb_str_dup(rb_iv_get(self,"@arg"));
285
+ tmp=rb_str_cat2(rb_str_dup(rb_iv_get(self,"@name")),StringValuePtr(tmp));
286
+ cmd=StringValuePtr(tmp);
287
+ ans=jl_eval_string(cmd);
288
+ if(ans==NULL) return ans;
289
+ //if(!util_isVector(ans)) return NULL;
290
+ return ans;
291
+ }
292
+
293
+
294
+ VALUE util_jl_value_to_VALUE(jl_value_t *ans)
295
+ {
296
+ return jl_value_to_VALUE(ans);
297
+ }
298
+
299
+
300
+ //-| Transform only a Vector or Atom
301
+ //-| Todo: when not a vector, avoid transform to vector first.
302
+ jl_value_t* util_VALUE_to_jl_value(VALUE arr)
303
+ {
304
+ jl_value_t *ans,*elt;
305
+ VALUE res,class,tmp;
306
+ int i,n=0,vect=1;
307
+
308
+ if(!rb_obj_is_kind_of(arr,rb_cArray)) {
309
+ n=1;
310
+ res = rb_ary_new2(1);
311
+ rb_ary_push(res,arr);
312
+ arr=res;
313
+ vect=0;
314
+ } else {
315
+ n=RARRAY_LEN(arr);
316
+ }
317
+
318
+ class=rb_class_of(rb_ary_entry(arr,0));
319
+ ans=jl_alloc_cell_1d(n);
320
+ if(class==rb_cFloat) {
321
+ //-| This is maybe faster and can be developped in julia-api as jl_vector_float64(n) for example.
322
+ //ans=jl_alloc_array_1d(jl_float64_type,n);
323
+ for(i=0;i<n;i++) {
324
+ elt=jl_box_float64(NUM2DBL(rb_ary_entry(arr,i)));
325
+ jl_arrayset(ans,elt,i);
326
+ }
327
+ } else if(class==rb_cFixnum || class==rb_cBignum) {
328
+ //ans=jl_alloc_array_1d(jl_long_type,n);
329
+ for(i=0;i<n;i++) {
330
+ elt=jl_box_long(NUM2INT(rb_ary_entry(arr,i)));
331
+ jl_arrayset(ans,elt,i);
332
+ }
333
+ } else if(class==rb_cTrueClass || class==rb_cFalseClass) {
334
+ //ans=jl_alloc_array_1d(jl_bool_type,n);
335
+ for(i=0;i<n;i++) {
336
+ elt=jl_box_bool(rb_class_of(rb_ary_entry(arr,i))==rb_cFalseClass ? 0 : 1);
337
+ jl_arrayset(ans,elt,i);
338
+ }
339
+ } else if(class==rb_cString) {
340
+ //ans=jl_alloc_array_1d(jl_utf8_string_type,n);
341
+ for(i=0;i<n;i++) {
342
+ tmp=rb_ary_entry(arr,i);
343
+ elt=jl_cstr_to_string(StringValuePtr(tmp));
344
+ jl_arrayset(ans,elt,i);
345
+ }
346
+ } else ans=NULL;
347
+ if(!vect && ans) ans=jl_arrayref(ans,0);
348
+ return ans;
349
+ }
350
+
351
+ VALUE JuliaVect_initialize(VALUE self, VALUE name)
352
+ {
353
+ rb_iv_set(self,"@name",name);
354
+ rb_iv_set(self,"@type",rb_str_new2("var"));
355
+ rb_iv_set(self,"@arg",rb_str_new2(""));
356
+ return self;
357
+ }
358
+
359
+ VALUE JuliaVect_isValid(VALUE self)
360
+ {
361
+ jl_value_t* ans;
362
+ char *name;
363
+
364
+ ans = util_getVar(self);
365
+
366
+ if(!util_isVector(ans)) {
367
+ VALUE tmp;
368
+ tmp=rb_iv_get(self,"@name");
369
+ name = StringValuePtr(tmp);
370
+ rb_warn("%s is not a R vector !!!",name);
371
+ return Qfalse;
372
+ }
373
+ return Qtrue;
374
+ }
375
+
376
+ VALUE JuliaVect_length(VALUE self)
377
+ {
378
+ jl_value_t* ans;
379
+ char *name;
380
+
381
+ ans = util_getVar(self);
382
+
383
+ if(ans==NULL) {
384
+ //printf("Sortie de length avec nil\n");
385
+ return Qnil;
386
+ }
387
+
388
+ return INT2NUM(length(ans));
389
+ }
390
+
391
+ VALUE JuliaVect_get(VALUE self)
392
+ {
393
+ jl_value_t* ans;
394
+ VALUE res;
395
+ char *name;
396
+ int n,i;
397
+ VALUE res2;
398
+
399
+ ans = util_getVar(self);
400
+
401
+ if(ans==NULL) {
402
+ //printf("Sortie de get avec nil\n");
403
+ return Qnil;
404
+ }
405
+
406
+ res=util_jl_value_to_VALUE(ans);
407
+ if(length(ans)==1) res=rb_ary_entry(res,0);
408
+ return res;
409
+ }
410
+
411
+ VALUE JuliaVect_get_with_arg(VALUE self)
412
+ {
413
+ jl_value_t* ans;
414
+ VALUE res;
415
+ char *name;
416
+ int n,i;
417
+ VALUE res2;
418
+
419
+ ans = util_getExpr_with_arg(self);
420
+
421
+ if(ans==NULL) {
422
+ //printf("Sortie de get avec nil\n");
423
+ return Qnil;
424
+ }
425
+ res=util_jl_value_to_VALUE(ans);
426
+
427
+ //printf("JuliaVect_get_with_arg: length(ans)=%d\n",length(ans));
428
+ if (length(ans)==1) res=rb_ary_entry(res,0);
429
+
430
+ return res;
431
+ }
432
+
433
+
434
+
435
+ // faster than self.to_a[index]
436
+ // VALUE JuliaVect_aref(VALUE self, VALUE index)
437
+ // {
438
+ // jl_value_t* ans;
439
+ // VALUE res;
440
+ // int n,i;
441
+ // i = FIX2INT(index);
442
+
443
+ // ans = util_getVar(self);
444
+ // n=length(ans);
445
+ // printf("i=%d and n=%d\n",i,n);
446
+ // if(i<n) {
447
+ // res=jl_value_to_VALUE(jl_arrayref((jl_array_t *)ans,i));
448
+ // } else {
449
+ // res = Qnil;
450
+ // }
451
+ // return res;
452
+ // }
453
+
454
+ // VALUE JuliaVect_set(VALUE self,VALUE arr)
455
+ // {
456
+ // jl_value_t* ans;
457
+ // char *name;
458
+ // VALUE tmp;
459
+
460
+ // ans=util_VALUE_to_jl_value(arr);
461
+
462
+ // tmp=rb_iv_get(self,"@name");
463
+ // name = StringValuePtr(tmp);
464
+ // jl_set_global(jl_main_module, jl_symbol(name),ans);
465
+
466
+ // return self;
467
+ // }
468
+
469
+ VALUE JuliaVect_set(VALUE self,VALUE arr)
470
+ {
471
+ VALUE tmp;
472
+ char *cmd;
473
+ // defineVar(install(".rubyExport"),util_VALUE2jl_value_t*(arr),R_GlobalEnv);
474
+ // tmp=rb_iv_get(self,"@arg");
475
+ // util_eval1string(rb_str_cat2(rb_str_cat2(rb_str_dup(rb_iv_get(self,"@name")),StringValuePtr(tmp)),"<-.rubyExport"));
476
+ jl_set_global(jl_main_module, jl_symbol("_ruby_export_"),util_VALUE_to_jl_value(arr));
477
+ tmp=rb_str_dup(rb_iv_get(self,"@name"));
478
+ tmp=rb_str_cat2(tmp,"=_ruby_export_");
479
+ cmd=StringValuePtr(tmp);
480
+ // printf("cmd=%s\n",cmd);
481
+ // jl_eval_string("show(_ruby_export_)");
482
+ // printf("cmd->done\n");
483
+ jl_eval_string(cmd);
484
+ return self;
485
+ }
486
+
487
+ VALUE JuliaVect_assign(VALUE obj, VALUE name,VALUE arr)
488
+ {
489
+ jl_value_t* ans;
490
+ char *tmp;
491
+
492
+ ans=util_VALUE_to_jl_value(arr);
493
+
494
+ tmp = StringValuePtr(name);
495
+ jl_set_global(jl_main_module, jl_symbol(tmp),ans);
496
+
497
+ return Qnil;
498
+ }
499
+
500
+ VALUE JuliaVect_set_with_arg(VALUE self,VALUE arr)
501
+ {
502
+ VALUE tmp;
503
+ char *cmd;
504
+ // defineVar(install(".rubyExport"),util_VALUE2jl_value_t*(arr),R_GlobalEnv);
505
+ // tmp=rb_iv_get(self,"@arg");
506
+ // util_eval1string(rb_str_cat2(rb_str_cat2(rb_str_dup(rb_iv_get(self,"@name")),StringValuePtr(tmp)),"<-.rubyExport"));
507
+ jl_set_global(jl_main_module, jl_symbol("_ruby_export_"),util_VALUE_to_jl_value(arr));
508
+ tmp=rb_str_dup(rb_iv_get(self,"@arg"));
509
+ tmp=rb_str_cat2(rb_str_dup(rb_iv_get(self,"@name")),StringValuePtr(tmp));
510
+ tmp=rb_str_cat2(tmp,"=_ruby_export_");
511
+ cmd=StringValuePtr(tmp);
512
+ jl_eval_string(cmd);
513
+ return self;
514
+ }
515
+
516
+ void
517
+ Init_jl4rb()
518
+ {
519
+ VALUE mJulia;
520
+
521
+ mJulia = rb_define_module("Julia");
522
+
523
+ rb_define_module_function(mJulia, "initJL", Julia_init, 1);
524
+
525
+ rb_define_module_function(mJulia, "evalLine", Julia_eval, 2);
526
+
527
+ rb_define_module_function(mJulia, "execLine", Julia_exec, 2);
528
+
529
+ VALUE cJuliaVect;
530
+
531
+ cJuliaVect = rb_define_class_under(mJulia,"Vector",rb_cObject);
532
+
533
+ rb_define_module_function(cJuliaVect, "assign", JuliaVect_assign, 2);
534
+
535
+ rb_define_method(cJuliaVect,"initialize",JuliaVect_initialize,1);
536
+
537
+ rb_define_method(cJuliaVect,"get",JuliaVect_get,0);
538
+ rb_define_alias(cJuliaVect,"to_a","get");
539
+ rb_define_alias(cJuliaVect,"value","get");
540
+
541
+ rb_define_method(cJuliaVect,"set",JuliaVect_set,1);
542
+ rb_define_alias(cJuliaVect,"<","set");
543
+ rb_define_alias(cJuliaVect,"value=","set");
544
+
545
+ //method "arg=" defined in eval.rb!! @arg initialized in method "initialize"
546
+ rb_define_method(cJuliaVect,"get_with_arg",JuliaVect_get_with_arg,0);
547
+ rb_define_alias(cJuliaVect,"value_with_arg","get_with_arg");
548
+ rb_define_method(cJuliaVect,"set_with_arg",JuliaVect_set_with_arg,1);
549
+ rb_define_alias(cJuliaVect,"value_with_arg=","set_with_arg");
550
+
551
+ rb_define_method(cJuliaVect,"valid?",JuliaVect_isValid,0);
552
+ rb_define_method(cJuliaVect,"length",JuliaVect_length,0);
553
+ //rb_define_method(cJuliaVect,"[]",JuliaVect_aref,1);
554
+
555
+ rb_define_attr(cJuliaVect,"name",1,1);
556
+ rb_define_attr(cJuliaVect,"type",1,1);
557
+
558
+ }
data/jl4rb.gemspec ADDED
@@ -0,0 +1,33 @@
1
+ require 'rubygems'
2
+ require 'rubygems/package_task'
3
+
4
+ PKG_NAME='jl4rb'
5
+ PKG_VERSION='0.0.4'
6
+ PKG_FILES=FileList[
7
+ 'Rakefile','jl4rb.gemspec',
8
+ 'ext/jl4rb/*.c',
9
+ 'ext/jl4rb/extconf.rb',
10
+ 'ext/jl4rb/*.h',
11
+ 'ext/jl4rb/MANIFEST',
12
+ 'lib/**/*.rb',
13
+ 'test/**/*.rb'
14
+ ]
15
+
16
+ Gem::Specification.new do |s|
17
+ s.platform = Gem::Platform::RUBY
18
+ s.summary = "Julia for ruby"
19
+ s.name = PKG_NAME
20
+ s.version = PKG_VERSION
21
+ s.requirements << 'none'
22
+ s.require_paths = ["lib","ext/jl4rb"]
23
+ s.files = PKG_FILES.to_a
24
+ s.extensions = ["ext/jl4rb/extconf.rb"]
25
+ s.licenses = ['MIT', 'GPL-2']
26
+ s.description = <<-EOF
27
+ R is embedded in ruby with some communication support .
28
+ EOF
29
+ s.author = "CQLS"
30
+ s.email= "rdrouilh@gmail.com"
31
+ s.homepage = "http://cqls.upmf-grenoble.fr"
32
+ s.rubyforge_project = nil
33
+ end
@@ -0,0 +1,128 @@
1
+ ## Module Julia
2
+
3
+ module Julia
4
+ # Careful!, Julia.exec code, :get => nil does not fetch stdout
5
+ def Julia.eval(s,opts={})
6
+ opts={:print=>true,:show=>nil,:simplify=>true,:init=>true,:safe=>true}.merge(opts)
7
+ #p opts
8
+ Julia.init if opts[:init] #just in case
9
+ res=[]
10
+ input,output="",""
11
+ s=[s] unless s.is_a? Array
12
+ s.each_with_index do |line,i| #line here is a command!
13
+ input << line
14
+ output=evalLine input, opts[:print]
15
+ if output.is_a? String and output[0...12]=="__incomplete"
16
+ if i==s.length - 1 #last!
17
+ res << {:in => input, :out => output} unless input.empty?
18
+ else
19
+ input << "\n"
20
+ end
21
+ else
22
+ res << {:in => input, :out => output}
23
+ input=""
24
+ end
25
+ end
26
+ if opts[:show]
27
+ res.each do |cmd|
28
+ print "in > ";puts cmd[:in]
29
+ begin print "out> ";puts cmd[:out];end unless cmd[:out]=="__incomplete__"
30
+ end
31
+ else
32
+ res=res[0][:out] if res.length==1 and opts[:simplify]
33
+ return res
34
+ end
35
+ end
36
+
37
+ # Careful!, Julia.exec code, :get => nil does not fetch stdout
38
+ def Julia.exec(code,opts={})
39
+ opts={:get=>true}.merge(opts)
40
+ execLine code, opts[:get]
41
+ end
42
+
43
+ def Julia.<(s)
44
+ Julia.exec(s)
45
+ end
46
+
47
+ def Julia.<<(s)
48
+ Julia.eval(s)
49
+ end
50
+
51
+ class Vector
52
+
53
+ def <<(name)
54
+ if name.is_a? Symbol
55
+ @name=name.to_s
56
+ @type="var"
57
+ else
58
+ @name=name
59
+ @type="expr"
60
+ end
61
+ return self
62
+ end
63
+
64
+ def arg=(arg)
65
+ @arg=arg
66
+ end
67
+
68
+ #this method is the same as the previous one but return self! Let us notice that even by adding return self in the previous one
69
+ # I could not manage to execute (rvect.arg="[2]").value_with_arg but fortunately rvect.set_arg("[2]").value_with_arg is working!
70
+ def set_arg(arg)
71
+ @arg=arg
72
+ return self
73
+ end
74
+
75
+ def [](key)
76
+ set_arg("["+(key+1).to_s+"]")
77
+ get_with_arg
78
+ end
79
+
80
+ def []=(key,val)
81
+ set_arg("["+(key+1).to_s+"]")
82
+ set_with_arg(val)
83
+ end
84
+
85
+ def >(arr)
86
+ res=self.get
87
+ #puts "res";p @name;p res
88
+ if res
89
+ #puts "arr.class:";p arr.class
90
+ #puts "res.class";p res.class
91
+ res=[res] unless res.is_a? Array
92
+ arr.replace(res)
93
+ else
94
+ arr.clear
95
+ end
96
+ return self
97
+ end
98
+
99
+ =begin #Done directly inside R4rb.c
100
+ def value_with_arg(arg)
101
+ old_name,old_type=@name.dup,@type.dup
102
+ @name,@type=@name+arg,"expr"
103
+ value
104
+ @name,@type=old_name,old_type
105
+ end
106
+ =end
107
+
108
+ end
109
+
110
+ end
111
+
112
+ module JL
113
+ def JL.<<(s)
114
+ Julia << s
115
+ end
116
+ end
117
+
118
+
119
+
120
+ class String
121
+
122
+ def jl4rb
123
+ Julia << self
124
+ end
125
+
126
+ alias to_jl jl4rb
127
+ end
128
+
@@ -0,0 +1,15 @@
1
+ module Julia
2
+
3
+ def Julia.init(args={})
4
+ unless Julia.alive?
5
+ args={:img=>File.join(ENV["JULIA_DIR"],"lib")}.merge(args) if ENV["JULIA_DIR"]
6
+ args={:img=>ENV["JULIA_IMG_DIR"]}.merge(args) if ENV["JULIA_IMG_DIR"]
7
+ @@initJL=Julia.initJL([args[:img]])
8
+ end
9
+ end
10
+
11
+ def Julia.alive?
12
+ defined? @@initJL
13
+ end
14
+
15
+ end
data/lib/jl4rb.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'jl4rb.bundle' if File.exists? File.join(File.dirname(__FILE__),'jl4rb.bundle')
2
+ require 'jl4rb.so' if File.exists? File.join(File.dirname(__FILE__),'jl4rb.so')
3
+
4
+ # loading ruby files
5
+ require 'jl4rb/jl2rb_init'
6
+ require 'jl4rb/jl2rb_eval'
@@ -0,0 +1,14 @@
1
+ require 'R4rb'
2
+ require 'jl4rb'
3
+ require File.expand_path("~/Github/dyndoc/lib/dyndoc/common/dynArray")
4
+
5
+ Array.initR
6
+ Julia.init
7
+
8
+ a=[] < 'jl:[2*i for i=1:4]'
9
+
10
+ p a
11
+
12
+ a > 'R:b'
13
+
14
+ R4rb << 'print(b)'
@@ -0,0 +1,31 @@
1
+ require 'jl4rb'
2
+
3
+ Julia.init
4
+
5
+ # julia variable
6
+ if true
7
+ aa=Julia::Vector.new "a"
8
+
9
+ aa < [1,4,3]
10
+
11
+ p "a".to_jl
12
+
13
+ Julia << "a=[4,7,3,9]"
14
+
15
+ p aa.value
16
+ end
17
+
18
+ if true
19
+ Julia << "b=Dict()"
20
+
21
+ bb=Julia::Vector.new ""
22
+ bb << 'b["toto"]'
23
+
24
+ bb < [1,4,66]
25
+
26
+ p 'b["toto"]'.to_jl
27
+
28
+ Julia << 'b["toto"]=[4,7,3,99]'
29
+
30
+ p bb.value
31
+ end
@@ -0,0 +1,25 @@
1
+ require 'jl4rb'
2
+
3
+ Julia.init
4
+
5
+ aa=Julia::Vector.new "a"
6
+
7
+ aa < [1,4,3]
8
+
9
+ p "a".to_jl
10
+
11
+ # In ruby
12
+ p aa[1]
13
+
14
+ # In Julia
15
+ aa.set_arg("[2]")
16
+
17
+ p aa.get_with_arg
18
+
19
+ aa.set_with_arg (12)
20
+
21
+ p "a".to_jl
22
+
23
+ aa[1]=33.0
24
+
25
+ p "a".to_jl
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jl4rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - CQLS
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-07-14 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |
14
+ R is embedded in ruby with some communication support .
15
+ email: rdrouilh@gmail.com
16
+ executables: []
17
+ extensions:
18
+ - ext/jl4rb/extconf.rb
19
+ extra_rdoc_files: []
20
+ files:
21
+ - Rakefile
22
+ - jl4rb.gemspec
23
+ - ext/jl4rb/jl4rb.c
24
+ - ext/jl4rb/extconf.rb
25
+ - ext/jl4rb/MANIFEST
26
+ - lib/jl4rb.rb
27
+ - lib/jl4rb/jl2rb_eval.rb
28
+ - lib/jl4rb/jl2rb_init.rb
29
+ - test/testDynArray.rb
30
+ - test/testJLVector.rb
31
+ - test/testWithArg.rb
32
+ homepage: http://cqls.upmf-grenoble.fr
33
+ licenses:
34
+ - MIT
35
+ - GPL-2
36
+ metadata: {}
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ - ext/jl4rb
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements:
53
+ - none
54
+ rubyforge_project:
55
+ rubygems_version: 2.0.14
56
+ signing_key:
57
+ specification_version: 4
58
+ summary: Julia for ruby
59
+ test_files: []