Pass simple numpy array to C via SWIG
By : bhakti satam
Date : March 29 2020, 07:55 AM
|
How to create a wrapper for a Matrix class to Numpy with SWIG?
By : Luram Archanjo
Date : March 29 2020, 07:55 AM
To fix this issue Based on the info you provided, typemaps would work. But my experience as an intermittent SWIG user (typically a couple weeks where I use it a lot then hiatus till next project/phase) is that few people have the time to grok that feature. In your case I believe SWIG typemaps are more of a convenience than a requirement so I would use one of two approaches: code :
old_some_func = some_function
def some_function(numpy_array):
tempMat = Matrix()
# convert numpy_array to SWIG'd Matrix class
old_some_func(tempMat)
|
Accessing a int16 array using numpy.i and swig
By : user3630770
Date : March 29 2020, 07:55 AM
it fixes the issue In this instance numpy.i did not work. The main problem was numpy.i does not support (out) typemaps which in this case was precisely what I needed. Instead using the command -debug-tmsearch when compiling the swig rtserver.i file. Using the output from the command I was able to determine the defined swig type for RadarParm::lag and what it should be. Here is the code that works. code :
%typemap(out) int16*[2] {
int i;
$result = PyList_New(2);
for (i = 0; i < 2; i++) {
PyObject *o = PyInt_FromLong($1[i]);
PyList_SetItem($result,i,o);
}
}
%apply int16*{lag[2]};
|
Passing numpy string array to C using Swig
By : bin_ast
Date : March 29 2020, 07:55 AM
This might help you So it is doable, but you need to convert the numpy object array to a list of python strings with a.tolist(). Then you can pass it to the C code with the following tutorial code as a char ** http://www.swig.org/Doc1.3/Python.html#Python_nn59 code :
// This tells SWIG to treat char ** as a special case
%typemap(in) char ** {
/* Check if is a list */
if (PyList_Check($input)) {
int size = PyList_Size($input);
Py_ssize_t i = 0;
$1 = (char **) malloc((size+1)*sizeof(char *));
for (i = 0; i < size; i++) {
PyObject *o = PyList_GetItem($input,i);
if (PyUnicode_Check(o))
$1[i] = PyUnicode_AsUTF8(PyList_GetItem($input,i));
else {
//PyErr_SetString(PyExc_TypeError,"list must contain strings");
PyErr_Format(PyExc_TypeError, "list must contain strings. %d/%d element was not string.", i, size);
free($1);
return NULL;
}
}
$1[i] = 0;
} else {
PyErr_SetString(PyExc_TypeError,"not a list");
return NULL;
}
}
// This cleans up the char ** array we malloc'd before the function call
%typemap(freearg) char ** {
free((char *) $1);
}
|
C++ Array to Numpy with SWIG Problem with TypeError
By : Manjunath Nayak
Date : March 29 2020, 07:55 AM
wish of those help Your typemaps and your declaration of the array_add function are not valid. NumPy arrays always have a size and this has to be communicated to C++. There is quite extensive documentation for the usage of NumPy with SWIG. Two other things: code :
int add(int x, int y) { return x + y; }
int sub(int x, int y) { return x - y; }
#include <algorithm>
#include <iostream>
class Example {
public:
void array_add(int *a, int len_a, int *b, int len_b, int *c, int len_c) {
int const max = std::max(len_a, std::max(len_b, len_c));
for (int i = 0; i < max; ++i) {
c[i] = a[i] + b[i];
}
}
};
%module example
%{
#define SWIG_FILE_WITH_INIT
#include "example.h"
%}
%include "numpy.i"
%init %{
import_array();
%}
%apply (int *IN_ARRAY1, int DIM1) { (int *a, int len_a), (int *b, int len_b) };
%apply (int *INPLACE_ARRAY1, int DIM1) { (int *c, int len_c) };
%include "example.h"
import example
import numpy as np
E = example.Example()
a = np.array([1,1], dtype=np.int32)
b = np.array([1,1], dtype=np.int32)
c = np.array([1,1], dtype=np.int32)
E.array_add(a,b,c)
print(c)
$ swig -python -c++ example.i
$ clang++ -Wall -Wextra -Wpedantic -I/usr/include/python2.7 -fPIC -shared example_wrap.cxx -o _example.so -lpython2.7
$ python test.py
[2 2]
|