[SciPy-Dev] f2py: questions on array arguments

Sturla Molden sturla@molden...
Mon Jul 19 09:07:56 CDT 2010

Ramon Crehuet skrev:
> real function trace(m)
> !f2py integer,depend(m) :: n=shape(m,0)
> !f2py real, dimension(n,n), intent(in) :: m
> real, dimension(:,:), intent(in) :: m
> But it does not work. Is there a workaround to avoid passing the
> dimension of the matrix as a fortran argument?
If you know your compiler's "dope vector" (known for most compilers, or 
just ask the vendor), we could implement an ndarray to Fortran 90 array 
converter in C, and pass a pointer to a dope vector struct.

If you don't know this, you will need a Fortran wrapper/proxy function. 
There are several ways that could work:


- Pass in all information about the array (shape and strides), then pass 
on a Fortran pointer or a slice.


- A C utility function could expose the PyArrayObject fields to Fortran.
- The Fortran compiler might support C structures as an extension  (e.g. 
- Fortran 2003 ISO C bindings: C structures are supported.

After the ndarray's meta-information is obtained, from any of those 
three, you can:

- Use non-standard Cray pointers to access the buffer.
- Use Fortran 2003 ISO C bindings to access the buffer: c_f_pointer 
converts a C pointer to a Fortran pointer.

There is actually a Cython-related project called 'fwrap' that works 
somewhat like this.

But if you have a Fortran contiguous array, or can create one, f2py will 
mean much less work.


More information about the SciPy-Dev mailing list