Module: Julia

Defined in:
lib/jl4rb/jl2rb_eval.rb,
lib/jl4rb/jl2rb_init.rb,
ext/jl4rb/jl4rb.c

Overview

Module Julia

Defined Under Namespace

Classes: Vector

Class Method Summary collapse

Class Method Details

.<(s) ⇒ Object



43
44
45
# File 'lib/jl4rb/jl2rb_eval.rb', line 43

def Julia.<(s)
  Julia.exec(s)
end

.<<(s) ⇒ Object



47
48
49
# File 'lib/jl4rb/jl2rb_eval.rb', line 47

def Julia.<<(s)
  Julia.eval(s)
end

.alive?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/jl4rb/jl2rb_init.rb', line 9

def Julia.alive?
  defined? @@initJL
end

.eval(s, opts = {}) ⇒ Object

Careful!, Julia.exec code, :get => nil does not fetch stdout



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/jl4rb/jl2rb_eval.rb', line 5

def Julia.eval(s,opts={})
  opts={:print=>true,:show=>nil,:simplify=>true,:init=>true,:safe=>true}.merge(opts)
  #p opts
  Julia.init if opts[:init] #just in case
  res=[]
  input,output="",""
  s=[s] unless s.is_a? Array
  s.each_with_index do |line,i| #line here is a command!
    input << line        
    output=evalLine input, opts[:print]
    if output.is_a? String and output[0...12]=="__incomplete"
      if i==s.length - 1 #last!
        res << {:in => input, :out => output} unless input.empty?
      else
        input << "\n"
      end
    else
      res << {:in => input, :out => output}
      input=""
    end
  end
  if opts[:show]
    res.each do |cmd|
      print "in > ";puts cmd[:in]
      begin print "out> ";puts cmd[:out];end unless  cmd[:out]=="__incomplete__"
    end
  else
    res=res[0][:out] if res.length==1 and opts[:simplify]
    return res
  end
end

.evalLine(cmd, print_stdout) ⇒ Object

*************** EVAL *********************



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'ext/jl4rb/jl4rb.c', line 177

VALUE Julia_eval(VALUE obj, VALUE cmd, VALUE print_stdout)
{
  char *cmdString;
  jl_value_t *res;
  VALUE resRb;

  cmdString=StringValuePtr(cmd);
  //printf("cmd=%s\n",cmdString);
//#ifndef WITH_JULIA_RELEASE
  //This flush redirected stdout before printing
  //if(print_stdout!=Qnil) jlapi_get_stdout();
//#endif
  //jl_gc_disable();
  res=jl_eval_string(cmdString);
  //printf("cmd=%s\n",cmdString);
  if (jl_exception_occurred()) {
            //jl_show(jl_stderr_obj(), jl_exception_occurred());
            jl_call2(jl_get_function(jl_base_module, "show"), jl_stderr_obj(), jl_exception_occurred());
            jl_printf(jl_stderr_stream(), "\n");
            resRb=Qnil;
  } else {
    //JL_GC_PUSH1(&res);
    //printf("cmd=%s\n",cmdString);
  //jl_set_global(jl_base_module, jl_symbol("ans"),res);
//#ifndef WITH_JULIA_RELEASE
//  if(print_stdout!=Qnil) jlapi_print_stdout();
//#endif
  resRb=jl_value_to_VALUE(res);
  //JL_GC_POP();
  }
  //jl_gc_enable();
  return resRb;
}

.exec(code, opts = {}) ⇒ Object

Careful!, Julia.exec code, :get => nil does not fetch stdout



38
39
40
41
# File 'lib/jl4rb/jl2rb_eval.rb', line 38

def Julia.exec(code,opts={})
  opts={:get=>true}.merge(opts)
  execLine code, opts[:get]
end

.execLine(cmd, get_stdout) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'ext/jl4rb/jl4rb.c', line 211

VALUE Julia_exec(VALUE obj, VALUE cmd, VALUE get_stdout)
{
  char *cmdString,*outString;
  jl_value_t *res;
  VALUE out;

  cmdString=StringValuePtr(cmd);
  res=jl_eval_string(cmdString);
  jl_set_global(jl_base_module, jl_symbol("ans"),res);
  //if(get_stdout!=Qnil) {
  //  outString=jlapi_get_stdout();
  //  jl_set_global(jl_base_module, jl_symbol("rbout"),jl_cstr_to_string(outString));
  //  return  rb_str_new2(outString);
  //} else
  return Qnil;
}

.exitObject



13
14
15
# File 'lib/jl4rb/jl2rb_init.rb', line 13

def Julia.exit()
  Julia.exitJL(0)
end

.exitJL(exitcode) ⇒ Object



40
41
42
43
# File 'ext/jl4rb/jl4rb.c', line 40

VALUE Julia_exit(VALUE obj, VALUE exitcode) {
  jl_atexit_hook(exitcode);
  return Qtrue;
}

.initObject



3
4
5
6
7
# File 'lib/jl4rb/jl2rb_init.rb', line 3

def Julia.init
  unless Julia.alive?
    @@initJL=Julia.initJL
  end
end

.initJLObject

*********** INIT ********************



31
32
33
34
35
36
37
38
# File 'ext/jl4rb/jl4rb.c', line 31

VALUE Julia_init(VALUE obj)
{
  //jl_init();
  printf("%s %s\n", jl_get_libdir(), jl_get_default_sysimg_path());
  jl_init(); //_with_image("/Applications/Julia-1.6.app/Contents/Resources/julia/bin", "../lib/sys.dylib");
  printf("ok\n");
  return Qtrue;
}