2

I have been stuck on this for a week now.I am intializing a 2D vector of doubles and passing it to a simple python function which multiplies it by 2 and returns it.

def in_arr_out_arr(arr_input):

    print(arr_input)
    arr_input2 = 2*arr_input

    return (arr_input2)

The print function prints garbage values to the console .

Here is the function Called

PyObject* call_python_function_vector2d_input(PyObject* function_name, vector<vector<double>> args)
{


    import_array()
    PyObject* np_arg = (PyObject*)vector_to_nparray(args);
    
    PyObject* pReturn, * pArgs;

    pArgs = PyTuple_New(1);
    PyTuple_SetItem(pArgs, 0, np_arg);

    pReturn = PyObject_CallObject(function_name, pArgs);
    PyErr_PrintEx(0);
    //cout << pReturn << "\n";

    return pReturn;

}

static PyArrayObject* vector_to_nparray(vector< vector<double> >& vec, int type_num = PyArray_DOUBLE) {

size_t nRows = vec.size();
size_t nCols = vec[0].size();
npy_intp dims[2] = { nRows, nCols };
PyArrayObject* vec_array = (PyArrayObject*)PyArray_SimpleNew(2, dims, type_num);

double* vec_array_pointer = (double*)PyArray_DATA(vec_array);

for (size_t iRow = 0; iRow < vec.size(); ++iRow) 
{
    copy(vec[iRow].begin(), vec[iRow].end(), vec_array_pointer + iRow * nCols);
}

return vec_array;


}


This is the last version i tried by reading from some previous answer on this forum only. It may have something to do with contigous block of memory allocation for np_arg but i dont have that much expertise in pointers

UPDATE

I removed all the functions and simply ran the whole thing in main function.It worked. Can someone tell why is not running in function forms. Also , how to run it using seperate functions.

pppp_prs
  • 106
  • 1
  • 8
  • A two-dimensional array can be represented by a one-dimensional array. Do you have a one-dimensional array version of your code that works? Also, `double** args` is **not** a two-dimensional array. Let's see where `args` comes from, and how it was put together. – PaulMcKenzie Apr 15 '21 at 23:52
  • edited the question for passing 2d vector its still printing garbage values – pppp_prs Apr 16 '21 at 07:50
  • Its working for 1d arrays – pppp_prs Apr 16 '21 at 12:24
  • You shouldn't have edited the code. You could have stayed with `double **args`. I wanted to see how you created the array using `double **args`. If you created the 2D array [like this](https://stackoverflow.com/questions/21943621/how-to-create-a-contiguous-2d-array-in-c/21944048#21944048), then you could have passed `&args[0][0]` to python, just like a 1d array, along with the number of rows and columns. Then python would have seen it as a single dimension, but actually you are sending the entire 2d array. Then you fix the python code (I am no python expert) to simulate the 2d array. – PaulMcKenzie Apr 16 '21 at 20:17
  • What you newly edited code does is worse, in that you are now creating many vectors and not in contiguous memory, causing a very cache-unfriendly scenario. – PaulMcKenzie Apr 16 '21 at 20:20

0 Answers0