Question: OpenMaple problem: How to pass a matrix from C to a function in Maple?

Dear OpenMaple users,

we try to tackle the following problem (unfortunately with limited success):

We would like to use OpenMaple (Maple 12) to call a maple procedure from C (C++ with only C features used). We have the void-function myfunction which gets two n x d arrays (d<=20, called inputmatrix and outputmatrix), a double parameter myparameter, a MKernelVector kv, and an ALGEB r.
The function myfunction should take the inputmatrix, pass it to the maple procedure myproc, calculate some stuff there and then write everything to the outputmatrix which is then accessible from C for further calculations.

Let's assume for the moment, that we simply want to take the inputmatrix, multiply every element by myparameter and write the result to the outputmatrix. How do we achieve this?

This is how we started; Unfortunately, it does not work...

In the main part of the C code, maple is started using

//start maple
MKernelVector kv;
ALGEB r;
char *arg[1];
arg[0]=argv[0];
kv=initmaple(1,arg);

Here comes the code for the function; Comments tell what we try to achieve.

void myfunction(double inputmatrix[][20],int n,int d,double myparameter,double outputmatrix[][20],MKernelVector kv,ALGEB r){
   
    //setup steps for maple
    r=EvalMapleStatement(kv,(char*)"restart:with(LinearAlgebra):with(stats):with(statevalf):Digits:=15:"); //load packages etc.
    MapleAssign(kv,ToMapleName(kv,(char*)"myparameter",TRUE),ToMapleFloat(kv,myparameter)); //pass myparameter to maple
   
    //setup for the procedure
    ALGEB rproc;
    string proc="myproc:=proc(datamatrix,myparameter) local i,j:"
                            "for i from 1 to n do"
                                "for j from 1 to d do"
                                    "datamatrix[i][j]=myparameter*datamatrix[i][j]:"
                                "od:"
                            "od:"
                            "return datamatrix:"
                            "end proc:";
    rproc=EvalMapleStatement(kv,(char*)proc.c_str()); //pass the procedure to maple
       
    //everything correct so far?
    //now the main problem comes: How do we pass the datamatrix to Maple? We tried to create an RTable...
    RTableSettings rts;
  M_INT bounds[4];
  ALGEB rt;
  INTEGER32 *data;
    RTableGetDefaults(kv,&rts);
  rts.num_dimensions=2;
  rts.subtype=RTABLE_MATRIX;
  rts.data_type=RTABLE_INTEGER32;
  bounds[0]=1;
  bounds[1]=n;
  bounds[2]=1;
  bounds[3]=d;
    rt=RTableCreate(kv,&rts,NULL,bounds);
    //fill the rtable
    data=(INTEGER32*)RTableDataBlock(kv,rt);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=d;j++){
            data[MATRIX_OFFSET_C_RECT(i,j,n,d)]=datamatrix[i-1][j-1]; //is this correct? This is what we found in the documentation, but we are not sure if it's correct.
        }       
    }
    MapleAssign(kv,ToMapleName(kv,(char*)"datamatrix",TRUE),rt);
   
    //assuming Maple now knows the entries of datamatrix, how do we call the procedure myproc with datamatrix and myparameter?
    ALGEB result;
    EvalMapleProc(kv,result,???,???);
   
    //write the results to outputmatrix
    for(int i=0;i<n;i++){
        for(int j=0;j<d;j++){
            stringstream str;
            str<<"outputmatrix["<<i+1<<"]["<<j+1<<"]:";
            r=EvalMapleStatement(kv,(char*) str.str().c_str());
            outputmatrix[i][j]=MapleToFloat32(kv,r);
        }       
    }
   
}

Any help is appreciated!

All the best,

Marius

Please Wait...