![](https://secure.gravatar.com/avatar/1d205aaad09be6465d4a5f22bdf8d4a8.jpg?s=120&d=mm&r=g)
Hi all, I am new to Python/Python extension programming. I am writing a c-extension to python which takes a 3-Dimensional array object and returns the 2Dimensional array object. The c function returns the 2-D array and converts into Python object, but the 2D python array object doesnot contain the correct values, it's priting out some irrevelent values. I could successfully return single dimension Python array object but I am not able to return 2D python array object. I am attaching the code. Please look at it and point out the errors. Thank you very much.. ------------------------------------------------------- CMATRIXMUL16.c - This takes the array (the python 3D array) and returns the 2D array ------------------------------------------------------ #include <stdio.h> #include <math.h> float* cmatrixmul16(float *array,float *paddarr, int n,int r,int col,float **store) { int i,j,k; int devices; int pathpoints; int column; int len; float sum; float **a,**b,**c,**d; float *temp; static float **tracematrix; tracematrix = store; pathpoints=r; column = col; devices= n; len = pathpoints*2; temp = (float *)malloc( len* sizeof(float)); sum =0.0; a = (float **)malloc(devices * sizeof(float)); b = (float **)malloc(devices * sizeof(float)); c = (float **)malloc(devices * sizeof(float)); d = (float **)malloc(devices * sizeof(float)); for(i=0;i<devices;i++) { a[i] = (float *)malloc(pathpoints*sizeof(float)); b[i] = (float *)malloc(pathpoints*sizeof(float)); c[i] = (float *)malloc(pathpoints*sizeof(float)); d[i] = (float *)malloc(pathpoints*sizeof(float)); } for(k=0;k<devices;k++){ for(i=0;i<pathpoints;i++) { a[k][i] = (*(array+k*pathpoints*column+i*column+0))*(1.0/sqrt(2.0)); b[k][i] = (*(array+k*pathpoints*column+i*column+1))*(1.0/sqrt(2.0)); c[k][i] = (*(array+k*pathpoints*column+i*column+2))*(1.0/sqrt(2.0)); d[k][i] = (*(array+k*pathpoints*column+i*column+3))*(1.0/sqrt(2.0)); tracematrix[k][i] = a[k][i]*a[k][i]+b[k][i]*b[k][i]; tracematrix[k][pathpoints+i] = c[k][i]*c[k][i]+d[k][i]*d[k][i]; } fprintf(stderr,"tracematrix: %f\n", tracematrix[k][0]); } for(i=0;i<devices;i++) { free(a[i]); free(b[i]); free(c[i]); free(d[i]); } free(a); free(b); free(c); free(d); return (float *)tracematrix; } ------------------------------------------------------ This is the module which calls the function _tests16module.c ------------------------------------------------------- #include <Python.h> #include <arrayobject.h> #include <math.h> static PyObject * Py_arraytest1 (PyObject *, PyObject *); static char _tests17_module_doc[] ="tests11: module documentation"; static char arraytest1__doc__[]= "mytest:function documentation"; /********************************Python symbol table *****************************************/ static PyMethodDef _tests17_methods[] = { {"arraytest1" , (PyCFunction)Py_arraytest1 , METH_VARARGS,arraytest1__doc__ }, {NULL, (PyCFunction)NULL,0,NULL } /* terminates the list of methods */ }; /*********************************End of Symbol table ***************************************/ void init_tests17() { /* We will be using C-functions defined in the array module. So we * need to be sure that the shared library defining these functions * is loaded. */ import_array(); (void) Py_InitModule4( "_tests17", /* module name */ _tests17_methods, /* structure containing python symbol info */ _tests17_module_doc, /* module documentation string */ (PyObject *) NULL, PYTHON_API_VERSION); } /*function to calculate the product of two arrays */ static PyObject * Py_arraytest1(PyObject *self, PyObject *args) { PyArrayObject *array, *paddarr, *product; char *c; int n,r,col,i,j,k; int dimensions[2]; float **store; if (!PyArg_ParseTuple(args, "O!O!", &PyArray_Type, &array,&PyArray_Type,&paddarr)) return NULL; /* The arguments are the 3-D array and the 1-Dpaddarr */ n = (long) array->dimensions[0]; r = (long) array->dimensions[1]; col = (long) array->dimensions[2]; store = (float **)malloc(n*sizeof(float)); for(i=0;i<n;i++) store[i]=(float *)malloc(2*r*sizeof(float)); c= (char*)cmatrixmul16((float *)array->data,(float *)paddarr->data,n,r,col,store); dimensions[0] = n; dimensions[1] = 2*r; product = (PyArrayObject *)PyArray_FromDimsAndData(2, dimensions, PyArray_FLOAT,c); PyArray_Return(product); } __________________________________________________ Do You Yahoo!? Yahoo! Movies - coverage of the 74th Academy Awards� http://movies.yahoo.com/
![](https://secure.gravatar.com/avatar/a53ea657e812241a1162060860f698c4.jpg?s=120&d=mm&r=g)
Amitha P <amitha_linux@yahoo.com> writes:
The variable c in this call must point to a storage area which holds the elements of the matrix, i.e. a one-dimensional float array. What you pass in your code is a list of pointers to the rows of the matrix. Konrad. -- ------------------------------------------------------------------------------- Konrad Hinsen | E-Mail: hinsen@cnrs-orleans.fr Centre de Biophysique Moleculaire (CNRS) | Tel.: +33-2.38.25.56.24 Rue Charles Sadron | Fax: +33-2.38.63.15.17 45071 Orleans Cedex 2 | Deutsch/Esperanto/English/ France | Nederlands/Francais -------------------------------------------------------------------------------
![](https://secure.gravatar.com/avatar/a53ea657e812241a1162060860f698c4.jpg?s=120&d=mm&r=g)
Amitha P <amitha_linux@yahoo.com> writes:
The variable c in this call must point to a storage area which holds the elements of the matrix, i.e. a one-dimensional float array. What you pass in your code is a list of pointers to the rows of the matrix. Konrad. -- ------------------------------------------------------------------------------- Konrad Hinsen | E-Mail: hinsen@cnrs-orleans.fr Centre de Biophysique Moleculaire (CNRS) | Tel.: +33-2.38.25.56.24 Rue Charles Sadron | Fax: +33-2.38.63.15.17 45071 Orleans Cedex 2 | Deutsch/Esperanto/English/ France | Nederlands/Francais -------------------------------------------------------------------------------
participants (2)
-
Amitha P
-
Konrad Hinsen