I'm trying to generate a python wrapper for my C code with swig4. As I do not have any experience I'm having some issues converting a numpy-array to a c array and returning a numpy-array from a c array.
Here is my repository with all my code: https://github.com/FelixWeichselgartner/SimpleDFT
void dft(int *x, float complex *X, int N) {
X = malloc(N * sizeof(float complex));
float complex wExponent = -2 * I * pi / N;
float complex temp;
for (int l = 0; l < N; l++) {
temp = 0 + 0 * I;
for (int k = 0; k < N; k++) {
temp = x[k] * cexp(wExponent * k * l) + temp;
}
X[l] = temp/N;
}
}
This is what my C function looks like. int *x is a pointer to a integer array with the length N. float complex *X will be allocated by the function dft with the length of N.
/* File: dft.i */
%module dft
%{
#define SWIG_FILE_WITH_INIT
#include "dft.h"
%}
%include "numpy.i"
%init %{
import_array();
%}
%apply (int * IN_ARRAY1, int DIM1) {(int *x, int N)}
%apply (float complex * ARGOUT_ARRAY1, int DIM1) {(float complex *X, int N)}
%include "dft.h"
This is what my swig file looks like. I guess the %apply part in the end is false, but I have no idea how to match this.
When I build my project by executing my build.sh file the created wrapper looks like this:
def dft(x, X, N):
return _dft.dft(x, X, N)
Here I can see that swig did not recognize my %apply as I want it to look like this
def dft(x):
# N is len(x)
# do some stuff
return X # X is a numpy.array() with len(X) == len(x)
I want to use it like this:
X = dft(x)
Obviously I get this error by executing:
File "example.py", line 12, in <module>
X = dft(x)
TypeError: dft() missing 2 required positional arguments: 'X' and 'N'
Do I have to write my own typemap or is it possible for me to use numpy.i. And how would a typemap with an IN_ARRAY1 and ARGOUT_ARRAY1 in one function work.
Thanks for your help.