[SciPy-dev] Python/Fortran multi-dimensional array issues

Pearu Peterson pearu at cens.ioc.ee
Wed Jan 16 13:38:59 CST 2002


Hi!

I have implemented a novel multi-dimensional array support for f2py. The
snapshot is available in

http://cens.ioc.ee/projects/f2py2e/2.x/

Basically, from now on there is no visible difference for arrays in
Python or Fortran. f2py generated modules will deal with this rather
confusing issue (that is due the difference how matrices are stored in
arrays for C and Fortran programming languages) in a very efficient way.

Note that this is quite a big news for f2py as this new approach finally
solves the long lasted dilemma of Fortran and C storage orders and how to
deal with it from Python.

An example of a signature file with comments below illustrates all that
with source files included to the attachment.

Enjoy!
	Pearu

!%f90 -*- f90 -*-

!  Example:
!    Using f2py for wrapping multi-dimensional Fortran and C arrays
!    [NEW APPROACH, use it with f2py higher than 2.9.x]
!    See also files foo.f and bar.c.
!
!  $Id: fun.pyf,v 1.2 2002/01/16 18:57:33 pearu Exp $

! Usage (with gcc compiler):
!   f2py -c fun.pyf foo.f bar.c

python module fun ! in 
    interface  ! in :fun

! >>> from Numeric import *
! >>> import fun
! >>> a=array([[1,2,3],[4,5,6]])

        subroutine foo(a,m,n) ! in :fun:foo.f
          integer dimension(m,n) :: a
          intent(in,out) :: a
          integer optional,check(shape(a,0)==m),depend(a) :: m=shape(a,0)
          integer optional,check(shape(a,1)==n),depend(a) :: n=shape(a,1)
        end subroutine foo

! >>> print fun.foo.__doc__
! foo - Function signature:
!   a = foo(a,[m,n])
! Required arguments:
!   a : input rank-2 array('i') with bounds (m,n)
! Optional arguments:
!   m := shape(a,0) input int
!   n := shape(a,1) input int
! Return objects:
!   a : rank-2 array('i') with bounds (m,n)

! >>> print fun.foo(a)
!  F77:
!  m= 2, n= 3
!  Row  1:
!  a(i= 1,j= 1) =  1
!  a(i= 1,j= 2) =  2
!  a(i= 1,j= 3) =  3
!  Row  2:
!  a(i= 2,j= 1) =  4
!  a(i= 2,j= 2) =  5
!  a(i= 2,j= 3) =  6
! [[77777     2     3]
!  [    4     5     6]]


        subroutine bar(a,m,n)
          intent(c)
          intent(c) bar
          integer dimension(m,n) :: a
          intent(in,out) :: a
          integer optional,check(shape(a,0)==m),depend(a) :: m=shape(a,0)
          integer optional,check(shape(a,1)==n),depend(a) :: n=shape(a,1)
          intent(in) m,n
        end subroutine bar

! >>> print fun.bar.__doc__
! bar - Function signature:
!   a = bar(a,[m,n])
! Required arguments:
!   a : input rank-2 array('i') with bounds (m,n)
! Optional arguments:
!   m := shape(a,0) input int
!   n := shape(a,1) input int
! Return objects:
!   a : rank-2 array('i') with bounds (m,n)

! >>> print fun.bar(a)
! C:m=2, n=3
! Row 1:
! a(i=0,j=0)=1
! a(i=0,j=1)=2
! a(i=0,j=2)=3
! Row 2:
! a(i=1,j=0)=4
! a(i=1,j=1)=5
! a(i=1,j=2)=6
! [[7777    2    3]
!  [   4    5    6]]

    end interface 
end python module fun

-------------- next part --------------
!%f90 -*- f90 -*-

!  Example:
!    Using f2py for wrapping multi-dimensional Fortran and C arrays
!    [NEW APPROACH, use it with f2py higher than 2.8.x]
!  $Id: fun.pyf,v 1.2 2002/01/16 18:57:33 pearu Exp $

! Usage (with gcc compiler):
!   f2py -c fun.pyf foo.f bar.c

python module fun ! in 
    interface  ! in :fun

! >>> from Numeric import *
! >>> import fun
! >>> a=array([[1,2,3],[4,5,6]])

        subroutine foo(a,m,n) ! in :fun:foo.f
          integer dimension(m,n) :: a
          intent(in,out) :: a
          integer optional,check(shape(a,0)==m),depend(a) :: m=shape(a,0)
          integer optional,check(shape(a,1)==n),depend(a) :: n=shape(a,1)
        end subroutine foo

! >>> print fun.foo.__doc__
! foo - Function signature:
!   a = foo(a,[m,n])
! Required arguments:
!   a : input rank-2 array('i') with bounds (m,n)
! Optional arguments:
!   m := shape(a,0) input int
!   n := shape(a,1) input int
! Return objects:
!   a : rank-2 array('i') with bounds (m,n)

! >>> print fun.foo(a)
!  F77:
!  m= 2, n= 3
!  Row  1:
!  a(i= 1,j= 1) =  1
!  a(i= 1,j= 2) =  2
!  a(i= 1,j= 3) =  3
!  Row  2:
!  a(i= 2,j= 1) =  4
!  a(i= 2,j= 2) =  5
!  a(i= 2,j= 3) =  6
! [[77777     2     3]
!  [    4     5     6]]


        subroutine bar(a,m,n)
          intent(c)
          intent(c) bar
          integer dimension(m,n) :: a
          intent(in,out) :: a
          integer optional,check(shape(a,0)==m),depend(a) :: m=shape(a,0)
          integer optional,check(shape(a,1)==n),depend(a) :: n=shape(a,1)
          intent(in) m,n
        end subroutine bar

! >>> print fun.bar.__doc__
! bar - Function signature:
!   a = bar(a,[m,n])
! Required arguments:
!   a : input rank-2 array('i') with bounds (m,n)
! Optional arguments:
!   m := shape(a,0) input int
!   n := shape(a,1) input int
! Return objects:
!   a : rank-2 array('i') with bounds (m,n)

! >>> print fun.bar(a)
! C:m=2, n=3
! Row 1:
! a(i=0,j=0)=1
! a(i=0,j=1)=2
! a(i=0,j=2)=3
! Row 2:
! a(i=1,j=0)=4
! a(i=1,j=1)=5
! a(i=1,j=2)=6
! [[7777    2    3]
!  [   4    5    6]]

    end interface 
end python module fun

! This file was auto-generated with f2py (version:2.9.166).
! See http://cens.ioc.ee/projects/f2py2e/
-------------- next part --------------
      subroutine foo(a,m,n)
      integer a(m,n), m,n,i,j
      print*, "F77:"
      print*, "m=",m,", n=",n
      do 100,i=1,m
         print*, "Row ",i,":"
         do 50,j=1,n
            print*, "a(i=",i,",j=",j,") = ",a(i,j)
 50      continue
 100  continue
      if (m*n.gt.0) a(1,1) = 77777
      end
      
-------------- next part --------------
A non-text attachment was scrubbed...
Name: bar.c
Type: text/x-csrc
Size: 278 bytes
Desc: 
Url : http://projects.scipy.org/pipermail/scipy-dev/attachments/20020116/e8bb0366/attachment.bin 


More information about the Scipy-dev mailing list