File size: 1,739 Bytes
c011401
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

\section{Applications}
\label{sec:apps}


\subsection{Example: wrapping C library \texttt{fftw}}
\label{sec:wrapfftw}

Here follows a simple example how to use \fpy to generate a wrapper
for C functions. Let us create a FFT code using the functions in FFTW
library. I'll assume that the library \texttt{fftw} is configured with
\texttt{-{}-enable-shared} option. 

Here is the wrapper for the typical usage of FFTW:
\begin{verbatim}
/* File: wrap_dfftw.c */
#include <dfftw.h>

extern void dfftw_one(fftw_complex *in,fftw_complex *out,int *n) {
  fftw_plan p;
  p = fftw_create_plan(*n,FFTW_FORWARD,FFTW_ESTIMATE);
  fftw_one(p,in,out);
  fftw_destroy_plan(p);
}
\end{verbatim}
and here follows the corresponding siganture file (created manually):
\begin{verbatim}
!%f90
! File: fftw.f90
module fftw
  interface
     subroutine dfftw_one(in,out,n)
       integer n
       complex*16 in(n),out(n)
       intent(out) out
       intent(hide) n
     end subroutine dfftw_one
  end interface
end module fftw
\end{verbatim}

Now let us generate the Python C/API module with \fpy:
\begin{verbatim}
f2py fftw.f90
\end{verbatim}
and compile it
\begin{verbatim}
gcc -shared -I/numeric/include -I`f2py -I` -L/numeric/lib -ldfftw \
    -o fftwmodule.so -DNO_APPEND_FORTRAN fftwmodule.c wrap_dfftw.c
\end{verbatim}

In Python:
\begin{verbatim}
>>> from Numeric import *
>>> from fftw import *
>>> print dfftw_one.__doc__
Function signature:
  out = dfftw_one(in)
Required arguments:
  in : input rank-1 array('D') with bounds (n)
Return objects:
  out : rank-1 array('D') with bounds (n)
>>> print dfftw_one([1,2,3,4])
[ 10.+0.j  -2.+2.j  -2.+0.j  -2.-2.j]
>>> 
\end{verbatim}

%%% Local Variables: 
%%% mode: latex
%%% TeX-master: "f2py2e"
%%% End: