f2py comparison Re: [Numpy-discussion] [ANN] Pyfort 7.0 Extending Numpy with C

Pearu Peterson pearu at cens.ioc.ee
Sun Mar 24 09:59:04 CST 2002


Hi,

Nummies might be interested how to wrap C codes with f2py.

On Wed, 20 Mar 2002, Paul Dubois wrote:

> 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

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

> 
> 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,

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.

>  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)

Python session with f2py generated mymod:

>>> import mymod
>>> print mymod.mydot([1,2,3],[1,2,4.])
17.0


Regards,
	Pearu





More information about the Numpy-discussion mailing list