On Fri, Oct 30, 2015 at 11:15 PM, laurentes <laurentelsha...@gmail.com> wrote:
> Using Swig, I don't manage to (properly) create the Python Binding for the > following C-like function: > > void add_array(double* input_array1, double* input_array2, double* > output_array, int length); > > where the three arrays have all the same length. > do you have to use SWIG? this would be really easy in Cython.... cdef cdef extern from "your_header.h": void add_array(double* input_array1, double* input_array2, double* output_array, int length) def py_add_array( np.ndarray[double, ndim=1] arr1, np.ndarray[double, ndim=1] arr2): cdef int length if arr1.shape != arr2.shape: raise ValueError("Arrays must be the same size") length = arr1.shape[0] cdef np.ndarray[double, ndim=1] out_arr = np.empty((length), dtype=np.float64) add_array(&arr1[0], &arr2[0], &out_arr[0], length) return out_arr Untested and from memory -- but you get the idea. -CHB > > > > This is similar to this thread > > < > http://numpy-discussion.10968.n7.nabble.com/Numpy-SWIG-td25709.html#a25710 > > > > , which has never been fully addressed online. > > > > From Python, I would like to be able to call: > > > add_array(input_array1, input_array2) > > which would return me a newly allocated NumPy array (output_array) with the > result. > > In my Swig file, I've first used the wrapper function trick described here > < > http://web.mit.edu/6.863/spring2011/packages/numpy_src/doc/swig/doc/numpy_swig.html#a-common-example > > > , that is: > > %apply (double* IN_ARRAY1, int DIM1) {(double* input_array1, int length1), > (double* input_array2, int length2)}; > %apply (double* ARGOUT_ARRAY1, int DIM1) {(double* output_array, int > length3)}; > > %rename (add_array) my_add_array; > %exception my_add_array { > $action > if (PyErr_Occurred()) SWIG_fail; > } > %inline %{ > void my_add_array(double* input_array1, int length1, double* input_array2, > int length2, double* output_array, int length3) { > if (length1 != length2 || length1 != length3) { > PyErr_Format(PyExc_ValueError, > "Arrays of lengths (%d,%d,%d) given", > length1, length2, length3); > } > else { > add_array(input_array1, input_array2, output_array, length1); > } > } > %} > > This allows me to call the function from Python using > add_array(input_array1, input_array2, length). But the third argument of > this function is useless and this function does not look 'Pythonic'. > > Could someone help me to modify my Swig file, such that only the first two > arguments are required for the Python API? > > Thanks a lot, > Laurent > > > > -- > View this message in context: > http://numpy-discussion.10968.n7.nabble.com/NumPy-Swig-Return-NumPy-array-with-same-size-as-input-array-no-additional-length-argument-tp41601.html > Sent from the Numpy-discussion mailing list archive at Nabble.com. > _______________________________________________ > NumPy-Discussion mailing list > NumPy-Discussion@scipy.org > https://mail.scipy.org/mailman/listinfo/numpy-discussion > -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception chris.bar...@noaa.gov
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org https://mail.scipy.org/mailman/listinfo/numpy-discussion