Eval And Exec
Julia can be used from Javascript directly using eval or exec.
eval allows Julia source to be embedded in Javascript as a string
The function eval takes a string and evaluates that string as if it were typed in the Julia REPL. The following computes the sum of the first 100 integers
julia = require('node-julia');
res = julia.eval('sum(Int64[n for n=1:100])');
console.log("sum of first 100 integers: ",res);
The result of the computation is returned as the function result. Alternatively there is a form where the results are returned as variables bound to arguments in a callback function.
julia = require('node-julia');
julia.eval('sum(Int64[n for n=1:100])',function(err,res) {
if(err) console.log("error in computation ",err);
else console.log("sum of first 100 integers: ",res);
});
setTimeout(function(){ console.log('done.'); },1000);
exec allows predefined Julia functions to be invoked by name
The function exec takes a string as the name of a function which may be module qualified, and any number of additional arguments that will be passed to that function.
julia = require('node-julia');
var a = julia.eval('rand(1:20000,1000)');
var median = julia.exec('Base.median',a);
console.log("median of 1000 random integers between 1 and 20000: ",median);
Or Julia source code can be placed in an external file and then included
For example, using the definition of mandel located on Julia Lang is put in a file mandel.jl
function mandel(z)
c = z
maxiter = 80
for n = 1:maxiter
if abs(z) > 2
return n-1
end
z = z^2 + c
end
return maxiter
end
It can be included and then used with the following:
julia = require('node-julia');
julia.exec('include','mandel.jl');
julia.exec('mandel',0.4,function(err,res) {
if(!err) console.log("mandel of 0.4: ",res);
});
setTimeout(function(){ console.log("done."); },1000);
Like above, this can be executed with or without a function callback
julia = require('node-julia');
julia.exec('include','mandel.jl');
console.log("mandel of 0.4: " + julia.exec('mandel',0.4));
Alternate call styles
Both eval and exec support a call style that does not require a function callback.
The style to use is determined by the type of the last argument; if it is a function, then the style is assumed to be callback-style, otherwise results are returned directly. The two styles can be mixed within the same program. The style determines if the call is synchronous or asynchronous.
Multiple value return
Julia functions can return multiple values as a tuple. In the function callback style, these are bound to multiple variables. For example, the Julia function svd performs singular value decomposition on an input matrix, and return 2 matrices u, v and a vector s. The singular value decomposition of a random 6x6 matrix can be obtained using the following:
var julia = require("node-julia");
var a = julia.exec('rand',6,6);
julia.exec('svd',a,function(err,u,s,v) {
if(!err) {
console.log("u matrix: ", u);
console.log("s vector: ", s);
console.log("v matrix: ", v);
}
});
setTimeout(function(){ console.log("done."); },1000);
The alternate style syntax returns multiple values in an array; the following is equivalent:
var julia = require("node-julia");
var a = julia.exec('rand',6,6);
var res = julia.exec('svd',a);
console.log("u matrix: ", res[0]);
console.log("s vector: ", res[1]);
console.log("v matrix: ", res[2]);
Updated less than a minute ago