Using functions in Java
Sattiraju Prabhakar
maitreyakara at yahoo.com
Sat May 30 10:15:36 CEST 2009
Hi,
I am trying to use Scilab (5.1.1) from a Java (jdk 6 update 13) application.
I have been experimenting with some Scilab scripts that use
function ("function" and "deff") definitions.
When I tried to convert these scripts into Java, I have come
across a number of problems.
Problems using "function"
================
I took the "function percentile" declaration from Professor Gilberto Gurroz's
Basic Statistics and Probability using Scilab. I reproduce below the script.
function [p] = percentile(x,r)
//This function calculates the 100*r-th percentile
//(0<r<1) for the vector x
xx = gsort(x);
[n m] = size(xx);
if m>n & n == 1
n = m;
end
if r<0 | r>1 then
disp('Percentile value must be between 0 and 1');
else
k = n*r;
if k-floor(k) ~= 0
p = xx(round(n*r));
else
p = (xx(k)+xx(k+1))/2;
end
end
It is used as follows
Q1 = percentile(x,0.25)
I tried to put it in Java code in two different ways.
A. In the first attempt, I tried to execute each line using
Scilab.Exec. The code follows.
public void percentile() {
Scilab.Exec("function [p] = percentile(x,r)");
//This function calculates the 100*r-th percentile
//(0<r<1) for the vector x
Scilab.Exec("xx = gsort(x);");
Scilab.Exec("[n m] = size(xx);");
Scilab.Exec("if m>n & n == 1");
Scilab.Exec("n = m;");
Scilab.Exec("end");
Scilab.Exec("if r<0 | r>1 then");
Scilab.Exec("disp('Percentile value must be between 0 and 1');");
Scilab.Exec("else");
Scilab.Exec("k = n*r;");
Scilab.Exec(" if k-floor(k) ~= 0");
Scilab.Exec("p = xx(round(n*r)); ");
Scilab.Exec("else");
Scilab.Exec("p = (xx(k)+xx(k+1))/2;");
Scilab.Exec("end");
Scilab.Exec("endfunction");
}
I tried to apply this function as follows.
SciDouble Q1P = new SciDouble("Q1");
Scilab.Exec("Q1 = percentile(x,0.25)");
Q1P.Get();
Q1P.disp();
I get the following error.
double Q1=
0.
Error in Java_javasci_Scilab_Exec routine.
B. I tried to declare the entire function as a string and pass the
string as argument to Scilab.Exec().
public void percentile() {
String perc2 = "function [p] = percentile(x,r) \n xx = gsort(x); \n [n m] = size(xx); \n if m>n & n == 1 \n n = m; \n end \n if r<0 | r>1 then \n disp('Percentile value must be between 0 and 1'); \n else \n k = n*r; \n if k-floor(k) ~= 0 \n p = xx(round(n*r)); \n else \n p = (xx(k)+xx(k+1))/2; \n end \n endfunction";
Scilab.Exec(perc2);
}
I tried it without \n, and later with \n. In both cases, I got the same error as above.
I also tried Java conversion of a simpler script, with the same result
//inline definition (see function)
function [x,y]=myfct(a,b)
x=a+b
y=a-b
endfunction
public void myFunction(){
Scilab.Exec("function [x,y]=myfct(a,b)");
Scilab.Exec("x=a+b");
Scilab.Exec("y=a-b");
Scilab.Exec("endfunction");
}
public void myFunction2(){
String fun = "function [x,y]=myfct(a,b) x=a+b y=a-b endfunction";
Scilab.Exec(fun);
}
2. Using deff
=============
I was able to successfully write and execute the above example using deff
Scilab.Exec("deff('[x,y]=myfct(a,b)', ['x=a+b'; 'y=a-b'])");
"deff" requires that we state the "statements" of the function as matrix of strings.
If you have a String inside the function declaration (of Scilab script), as in percentile, then
it becomes a problem.
I was not able to get a correct result for percentile using deff and Java.
Here is my code:
Scilab.Exec("deff('[p]=percentile(x,r)', ['xx = gsort(x);'; '[n m] = size(xx);'; ' if m>n & n == 1 n = m;'; 'end'; 'if r<0 | r>1 then disp('Percentile value must be between 0 and 1');' ; 'else k = n*r;'; 'if k-floor(k) ~= 0 p = xx(round(n*r));'; 'else p = (xx(k)+xx(k+1))/2;'; 'end' ])");
I would like to end with a general question. Each of the above methods takes some effort.
If the function definition is long, then the effort needed to convert into Java code can be
substantial. Is there a simple method?
Thanks,
Prabhakar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.scilab.org/pipermail/users/attachments/20090530/339d9f3c/attachment.htm>
More information about the users
mailing list