int main() { constexpr const int DISPLAY_SIZE { 22 }; constexpr const float PI { acos(-1.f) }; constexpr std::array<int,2> LdS {WIDTH, HEIGHT }; constexpr std::array<float,2> ldS {1.f, 1.f }; Grid2d grid2d( LdS, ldS); // sanity check std::cout << "This is the value of pi : " << PI << std::endl; std::cout << "Size of rho on grid2d? Do grid2d.rho.size() : " << grid2d.rho.size() << std::endl; std::cout << "Initially, on grid2d.rho : " << std::endl; for (auto i = 0; i < DISPLAY_SIZE; ++i ) { std::cout << " " << grid2d.rho[i] ; } std::cout << std::endl; std::array<int,2> ix_in { 0, 0 }; std::array<float,2> Xi { 0.f, 0.f }; for (auto j = 0; j < grid2d.Ld[0] ; ++j ) { for (auto i = 0; i < grid2d.Ld[1] ; ++i ) { ix_in[0] = i ; ix_in[1] = j ; Xi = grid2d.gridpt_to_space( ix_in ); grid2d.rho[ grid2d.flatten(i,j) ] = sin( 2.f*PI*Xi[0])*sin(2.f*PI*Xi[1]) ; } } // sanity check std::cout << "grid2d.rho, after initializing with values given by sin(2*pi*x)*sin(2*pi*y) : " << std::endl; for (auto i = 0; i < DISPLAY_SIZE; ++i ) { std::cout << " " << grid2d.rho[(i+WIDTH/4)+ HEIGHT/4*WIDTH] ; } std::cout << std::endl; return 0; }
//grid2d() implementation static PyObject *_pixelize_grid2d(PyObject *self,PyObject *args){ PyObject *x_obj,*y_obj,*s_obj,*map_obj; double map_size; int err; //parse input tuple if(!PyArg_ParseTuple(args,"OOOdO",&x_obj,&y_obj,&s_obj,&map_size,&map_obj)) return NULL; //interpret arrays PyObject *x_array = PyArray_FROM_OTF(x_obj,NPY_DOUBLE,NPY_IN_ARRAY); PyObject *y_array = PyArray_FROM_OTF(y_obj,NPY_DOUBLE,NPY_IN_ARRAY); PyObject *s_array = PyArray_FROM_OTF(s_obj,NPY_DOUBLE,NPY_IN_ARRAY); PyObject *map_array = PyArray_FROM_OTF(map_obj,NPY_DOUBLE,NPY_IN_ARRAY); //check if anything failed if(x_array==NULL || y_array==NULL || s_array==NULL || map_array==NULL){ Py_XDECREF(x_array); Py_XDECREF(y_array); Py_XDECREF(s_array); Py_XDECREF(map_array); return NULL; } //get the number of objects in the catalog and the number of pixels int Nobjects = (int)PyArray_DIM(x_array,0); int Npixel = (int)PyArray_DIM(map_array,0); //get the data pointers double *x = (double *)PyArray_DATA(x_array); double *y = (double *)PyArray_DATA(y_array); double *s = (double *)PyArray_DATA(s_array); double *map = (double *)PyArray_DATA(map_array); //call the C backend for the gridding procedure err = grid2d(x,y,s,map,Nobjects,Npixel,map_size); if(err){ if(err==1) PyErr_SetString(PyExc_MemoryError,"A call to malloc failed"); Py_DECREF(x_array); Py_DECREF(y_array); Py_DECREF(s_array); Py_DECREF(map_array); return NULL; } //cleanup Py_DECREF(x_array); Py_DECREF(y_array); Py_DECREF(s_array); Py_DECREF(map_array); //return None Py_RETURN_NONE; }