[ANN] Pyfort 7.0 Extending Numpy with C
![](https://secure.gravatar.com/avatar/56475d2e8acb48b4308f609982f94440.jpg?s=120&d=mm&r=g)
A beta version of Pyfort 7.0 is available at pyfortran.sf.net. The documentation is not yet upgraded to this version. Pyfort 7.0 adds the ability to wrap Fortran-like C coding to extend Numpy. Dramatically illustrating the virtue of open source software, credit for this improvement goes to: Michiel de Hoon Human Genome Center University of Tokyo For example, if you have this C code: double mydot(int n, double* x, double *y) { int i; double d; d = 0.0; for (i=0; i < n; ++i) d += x[i] * y[i]; return d; } Then you can create a Pyfort input file mymod.pyf: function mydot (n, x, y) integer:: n=size(x) doubleprecision x(n), y(n) doubleprecision mydot end Compile mydot.c into a library libmydot.a. Then: pyfort -c cc -i -L. -l mydot mymod.pyf builds and installs the module mymod containing function mydot, which you can use from Python: import Numeric, mymod x=Numeric.array([1.,2.3.]) y=Numeric.array([5., -1., -1.]) print mymod.mydot(x,y) Note that by wrapping mydot in this way, Pyfort takes care of problems like converting input arrays of the wrong type, such as integer; making sure that x and y have the same length; and making sure x and y are contiguous. I added directory testc that contains an example like this and one where an array is output. Mr. de Hoon explained his patch as follows. "I have modified fortran_compiler.py to add gcc as a compiler. This enables pyfort to be used for C code instead of Fortran code only. To use this option, call pyfort with the -cgcc option to specify gcc as the compiler. In order to switch off the default TRANSPOSE and MIRROR options, some small modifications were needed in generator.py also. [Editor's note: both -c gcc and -c cc will work] Before writing this addition to pyfort, I tried to use swig to generate the wrapper for my C code. However, pyfort was easier to use in my case because it is integrated with numpy. I wasn't able to get swig use numpy arrays. In addition, I am writing extension code both in fortran and C, so it is easier having to use only one tool (pyfort) for both. In a sense, it is easier to extend python with C than with fortran because you don't have to worry about transposing the array. I tried to be minimally instrusive on the existing pyfort code to switch off transposing arrays; there may be prettier ways to do this than what I have done. With this modification, I was able to pass one- and two-dimensional numpy arrays from Python to C and back without problems, as well as scalar variables with intent(in) and intent(out). I have also used the modified Pyfort on some Fortran routines to make sure I didn't break something in the fortran part of Pyfort. I haven't done an extensive test of this addition, but I haven't found any problems with it so far. I hope this patch will be useful to other people trying to extend Python/numpy with C routines." Michiel de Hoon Human Genome Center University of Tokyo mdehoon@ims.u-tokyo.ac.jp
![](https://secure.gravatar.com/avatar/2a726b0de1ade0be11fb5bc5a383d71d.jpg?s=120&d=mm&r=g)
Hi, Nummies might be interested how to wrap C codes with f2py. On Wed, 20 Mar 2002, Paul Dubois wrote:
Different from pyfort, f2py needs the following signature file: python module mymod interface function mydot (n, x, y) intent(c) mydot integer intent(c):: n=size(x) doubleprecision x(n), y(n) doubleprecision mydot end end interface end python module
With f2py the above is equivalent to f2py -c mydot.c mymod.pyf This compiles mydot.c and builds the module mymod into the current directory.
Python session with f2py generated mymod:
Regards, Pearu
![](https://secure.gravatar.com/avatar/2a726b0de1ade0be11fb5bc5a383d71d.jpg?s=120&d=mm&r=g)
Hi, Nummies might be interested how to wrap C codes with f2py. On Wed, 20 Mar 2002, Paul Dubois wrote:
Different from pyfort, f2py needs the following signature file: python module mymod interface function mydot (n, x, y) intent(c) mydot integer intent(c):: n=size(x) doubleprecision x(n), y(n) doubleprecision mydot end end interface end python module
With f2py the above is equivalent to f2py -c mydot.c mymod.pyf This compiles mydot.c and builds the module mymod into the current directory.
Python session with f2py generated mymod:
Regards, Pearu
participants (2)
-
Paul Dubois
-
Pearu Peterson