Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Look at add c/c++ to numpy example #2

Open
xichaoqiang opened this issue Sep 23, 2020 · 2 comments
Open

Look at add c/c++ to numpy example #2

xichaoqiang opened this issue Sep 23, 2020 · 2 comments

Comments

@xichaoqiang
Copy link

Numpy is very common used in python to array computation.Is there any way to convert emxarry in C or coder array in C++ by numpy.i offered by numpy?

@rlivings39
Copy link
Member

https://numpy.org/doc/stable/reference/swig.html Could be interesting

@xichaoqiang
Copy link
Author

xichaoqiang commented Sep 25, 2020

https://numpy.org/doc/stable/reference/swig.html Could be interesting

Dear,sir Thanks for you reply.

I know swig in these few days.And I try to warp emxArray_real_T to numpy array.

Here I want get y= timestwo(x) in python,where x and y are numpy array.

The code shown as below is my code in timestwo.i file, it can't work.

Can you help me to fix it?

/* numpy array as input convert to emxArray_real_T */
%typemap(in)
    (emxArray_real_T*)
    (PyArrayObject* array=NULL, int is_new_object=0)
{
    array = (PyArrayObject*) obj_to_array_contiguous_allow_conversion($input, NPY_DOUBLE, &is_new_object);
    if (!array) SWIG_fail;
    int numDimensions;
	numDimensions=array_numdims(array)	;
	int*size= (int*)malloc(numDimensions * sizeof(int));
	int n_elems=1;
	for (int i=0;i<numDimensions;i++)
	{
	size[i]=array_size(array, i);
	n_elems*=size[i];	
	}
    $1 = emxCreateND_real_T(numDimensions, size);
    memcpy($1->data, array_data(array), n_elems);
}

/*emxArray_real_T out convert to numpyas */
%typemap(out) 
(emxArray_real_T*)
{
    $result = NULL;
    if ($1)
    {	
            int numDimensions= $1->numDimensions;
            npy_intp dims =  $1->size ;
            PyObject* res_array = PyArray_SimpleNewFromData(numDimensions, dims, NPY_DOUBLE,$1->data);
            $result = SWIG_Python_AppendOutput($result, res_array);
            emxDestroyArray_real_T($1);
    }

    if($result == NULL)
    {
        PyErr_SetString(PyExc_RuntimeError, "Result not yet implemented");
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants