From: Samuele P. <ped...@bl...> - 2003-06-06 11:45:52
|
At 05:57 06.06.2003 +0000, ikke ekki wrote: >The problem I stated before (see below) is very crucial to our ptoject. > >The arrays are populated through a Java program and cannot be instantiated >in the scripts. So I guess the jarray package does not help me alot. >I want to be able to set the matrix to the PythonInterpreter using the set >method. >The scripts only asume the matrix is set by the interpreter object. > >So my question is: how can I set a Float[][] or float[][] into the >interpreter so the matrix becomes visible for the script? And how can I >get it back with the interpreter get method? > >Thanks for your help This works for me: import org.python.util.PythonInterpreter; import org.python.core.PySystemState; public class TwoD { public static void main(String[] args) { PySystemState.initialize(); PythonInterpreter interp = new PythonInterpreter(); interp.exec("import sys;print sys.version"); Float[][] inputMatrix=new Float[3][3]; interp.set("matrix",inputMatrix); interp.exec("print '*jy* test'"); interp.exec("for y in matrix:\n for z in y:\n print 'OK'"); /* -*- */ float[][] f = {{1.0f,2.0f},{1.0f,1.5f}}; Float[][] bxf = new Float[2][2]; for (int i = 0; i<2; i++) { for (int j = 0; j<2; j++) { bxf[i][j] = new Float(f[i][j]); } } interp.set("f",f); interp.set("bxf",bxf); interp.exec("print '*jy* f',f\nfor r in f:\n for x in r:\n print x"); interp.exec("print '*jy* bxf',bxf\nfor r in bxf:\n for x in r:\n print x"); interp.exec("f[0][1]*=2"); interp.exec("bxf[0][1]*=2"); System.out.println("*java* f[0][1] = "+f[0][1]); System.out.println("*java* bxf[0][1] = "+bxf[0][1]); float[][] x=(float[][])interp.get("f",float[][].class); System.out.println("*java* x f identity"); System.out.println(x==f); System.out.println(x[0]==f[0]); System.out.println(x[1]==f[1]); Float[][] y=(Float[][])interp.get("bxf",Float[][].class); System.out.println("*java* y bxf identity"); System.out.println(y==bxf); System.out.println(y[0]==bxf[0]); System.out.println(y[1]==bxf[1]); } } both with jython 2.1: 2.1 *jy* test OK OK OK OK OK OK OK OK OK *jy* f array([array([1.0, 2.0], float), array([1.0, 1.5], float)], [F) 1.0 2.0 1.0 1.5 *jy* bxf array([array([1.0, 2.0], java.lang.Float), array([1.0, 1.5], java.lang. Float)], [Ljava.lang.Float;) 1.0 2.0 1.0 1.5 *java* f[0][1] = 4.0 *java* bxf[0][1] = 4.0 *java* x f identity true true true *java* y bxf identity true true true and the CVS: 2.2a0 *jy* test OK OK OK OK OK OK OK OK OK *jy* f array([array([1.0, 2.0], float), array([1.0, 1.5], float)], [F) 1.0 2.0 1.0 1.5 *jy* bxf array([array([1.0, 2.0], java.lang.Float), array([1.0, 1.5], java.lang Float)], [Ljava.lang.Float;) 1.0 2.0 1.0 1.5 *java* f[0][1] = 4.0 *java* bxf[0][1] = 4.0 *java* x f identity true true true *java* y bxf identity true true true regards |