I have a C function which takes, among other arguments, 3 arrays of differents types :
void create_signal(double ztab[], ..., int *pdata, ..., char ranging[], ...);
and I want to use this function in a Python module using SWIG.
I have already figured out how to use C functions which take in an array with numpy.i typemaps :
%apply (double* INPLACE_ARRAY1, int DIM1) {(double xtab[], int length)}
for instance,
or even 2 arrays of same type :
apply (double* INPLACE_ARRAY1, int DIM1) {(double xtab[], int length_x), (double ytab[], int length_y)}
but I can't figure out if it is possible to do so if the arrays are of different types (in my case, int
, double
, and char
), or if I should rewrite my own typemap.
Thanks for your help !
Anyway, here goes an example
test.h
#pragma once
void multipleInputs(const float* data, const int nData,
const float* pos, const int nPositions, const int nDim);
test.cpp
#include "test.h"
#include <cstdio>
void multipleInputs(const float* data, const int nData,
const float* pos, const int nPositions, const int nDim)
{
if (nData > 0) {
printf("%d'th element is: %f\n", (nData-1), data[nData-1]);
}
if ((nDim > 0) && (nPositions > 0)){
printf("%d'th dimension of the %d'th element is: %f\n",
(nDim-1), (nPositions-1), data[nData-1]);
}
}
test.i
%module example
%{
#define SWIG_FILE_WITH_INIT
#include "test.h"
%}
%include "numpy.i"
%init {
import_array();
}
%apply (float* IN_ARRAY1, int DIM1) \
{(const float* data, const int nData)}
%apply (float* IN_ARRAY2, int DIM1, int DIM2) \
{(const float* pos, const int nPositions, const int nDim)};
%include "test.h"
setup.py
import numpy
from setuptools import setup, Extension
setup(name="example",
py_modules=["example"],
ext_modules=[Extension("_example",
["test.i","test.cpp"],
include_dirs = [numpy.get_include(), '.'],
swig_opts=['-c++', '-I.'],
)]
)
test.py
import example
example.multipleInputs(np.ones((10),dtype=np.float32),
np.ones((10,10),dtype=np.float32))