<html><head><style type="text/css"><!-- DIV {margin:0px;} --></style></head><body><div style="font-family:times new roman,new york,times,serif;font-size:12pt"><div><font size="3">Hi,<br><br>I am trying to use Scilab (5.1.1) from a Java (jdk 6 update 13) application. <br>I have been experimenting with some Scilab scripts that use<br>function ("function" and "deff") definitions. <br>When I tried to convert these scripts into Java, I have come<br>across a number of problems. <br><br>Problems using "function"<br>================<br><br>I took the "function percentile" declaration from Professor Gilberto Gurroz's<br>Basic Statistics and Probability using Scilab. I reproduce below the script.</font><br><br><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;">function [p] = percentile(x,r)</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier
New,courier,monaco,monospace,sans-serif;">//This function calculates the 100*r-th percentile</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;">//(0<r<1) for the vector x</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;">xx = gsort(x);</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;">[n m] = size(xx);</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;">if m>n & n == 1 </span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier
New,courier,monaco,monospace,sans-serif;"> n = m;</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;">end </span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;">if r<0 | r>1 then</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;"> disp('Percentile value must be between 0 and 1');</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;">else</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;"> k =
n*r;</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;"> if k-floor(k) ~= 0 </span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;"> p = xx(round(n*r));</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;"> else</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;"> p = (xx(k)+xx(k+1))/2;</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;">
end</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;">end</span><br><br><font size="3">It is used as follows</font><br><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;">Q1 = percentile(x,0.25)</span><br><br><font size="3">I tried to put it in Java code in two different ways. </font><br><br><font size="3">A. In the first attempt, I tried to execute each line using<br>Scilab.Exec. The code follows.</font><br><br> <span style="font-family: Courier New,courier,monaco,monospace,sans-serif;">public void percentile() {</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;"> Scilab.Exec("function [p] = percentile(x,r)");</span><br style="font-family: Courier
New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;">//This function calculates the 100*r-th percentile</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;">//(0<r<1) for the vector x</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;"> Scilab.Exec("xx = gsort(x);");</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;"> Scilab.Exec("[n m] = size(xx);");</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier
New,courier,monaco,monospace,sans-serif;"> Scilab.Exec("if m>n & n == 1");</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;"> Scilab.Exec("n = m;");</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;"> Scilab.Exec("end");</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;"> Scilab.Exec("if r<0 | r>1 then");</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier
New,courier,monaco,monospace,sans-serif;"> Scilab.Exec("disp('Percentile value must be between 0 and 1');");</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;"> Scilab.Exec("else");</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;"> Scilab.Exec("k = n*r;");</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;"> Scilab.Exec(" if k-floor(k) ~= 0");</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier
New,courier,monaco,monospace,sans-serif;"> Scilab.Exec("p = xx(round(n*r)); ");</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;"> Scilab.Exec("else");</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;"> Scilab.Exec("p = (xx(k)+xx(k+1))/2;");</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;"> Scilab.Exec("end");</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier
New,courier,monaco,monospace,sans-serif;"> Scilab.Exec("endfunction");</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;"> }</span><br><br><font size="3">I tried to apply this function as follows.</font><br><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;">SciDouble Q1P = new SciDouble("Q1");</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;"> Scilab.Exec("Q1 = percentile(x,0.25)"); </span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;"> Q1P.Get();</span><br style="font-family: Courier
New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;"> Q1P.disp();</span><br><br><font size="3">I get the following error. </font><br><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;">double Q1=</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;"> </span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;"> 0. </span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;">Error in Java_javasci_Scilab_Exec routine.</span><br><br><font size="3">B. I tried to declare the entire function as a string and pass
the<br>string as argument to Scilab.Exec().</font><br><br><br><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;">public void percentile() {</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;"> 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";</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;"> Scilab.Exec(perc2);</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span
style="font-family: Courier New,courier,monaco,monospace,sans-serif;">}</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><font size="3"><br>I tried it without \n, and later with \n. In both cases, I got the same error as above. <br><br>I also tried Java conversion of a simpler script, with the same result<br></font><br><pre class="programlisting"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;">//inline definition (see function)</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;">function [x,y]=myfct(a,b)</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;">x=a+b</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier
New,courier,monaco,monospace,sans-serif;">y=a-b</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;">endfunction</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;">public void myFunction(){</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;"> Scilab.Exec("function [x,y]=myfct(a,b)");</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;"> Scilab.Exec("x=a+b");</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span
style="font-family: Courier New,courier,monaco,monospace,sans-serif;"> Scilab.Exec("y=a-b");</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;"> Scilab.Exec("endfunction"); </span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;"> }</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;"> public void myFunction2(){</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;"> String fun = "function [x,y]=myfct(a,b) x=a+b y=a-b endfunction";</span><br
style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;"> Scilab.Exec(fun);</span><br style="font-family: Courier New,courier,monaco,monospace,sans-serif;"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;"> }</span><br><br><font size="3"><span style="font-family: times new roman,new york,times,serif;">2. Using deff</span><br style="font-family: times new roman,new york,times,serif;"><span style="font-family: times new roman,new york,times,serif;">=============</span><br style="font-family: times new roman,new york,times,serif;"><br style="font-family: times new roman,new york,times,serif;"><span style="font-family: times new roman,new york,times,serif;">I was able to successfully write and execute the above example using deff</span></font><br><br> <font size="3"><span style="font-family: Courier
New,courier,monaco,monospace,sans-serif;">Scilab.Exec("deff('[x,y]=myfct(a,b)', ['x=a+b'; 'y=a-b'])");</span></font><br><br><font size="3"><span style="font-family: times new roman,new york,times,serif;">"deff" requires that we state the "statements" of the function as matrix of strings. </span><br style="font-family: times new roman,new york,times,serif;"><span style="font-family: times new roman,new york,times,serif;">If you have a String inside the function declaration (of Scilab script), as in percentile, then</span><br style="font-family: times new roman,new york,times,serif;"><span style="font-family: times new roman,new york,times,serif;">it becomes a problem.</span><br style="font-family: times new roman,new york,times,serif;"><br style="font-family: times new roman,new york,times,serif;"><span style="font-family: times new roman,new york,times,serif;">I was not able to get a correct result for percentile using deff and Java. </span><br
style="font-family: times new roman,new york,times,serif;"><span style="font-family: times new roman,new york,times,serif;">Here is my code:</span></font><br><font size="3"><span style="font-family: Courier New,courier,monaco,monospace,sans-serif;">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' ])");</span></font><br></pre><br><font size="3">I would like to end with a general question. Each of the above methods takes some effort. <br>If the function definition is long, then the effort needed to convert into Java code can be<br>substantial. Is there a simple method?<br><br>Thanks,<br>Prabhakar</font><br><br></div></div></body></html>