[f2py] Re: [SciPy-dev] Python/Fortran multi-dimensional array issues
Pearu Peterson
pearu@cens.ioc.ee
Wed, 16 Jan 2002 21:38:59 +0200 (EET)
This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.
Send mail to mime@docserver.cac.washington.edu for more info.
--683786235-1296099418-1011209939=:10775
Content-Type: TEXT/PLAIN; charset=US-ASCII
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
--683786235-1296099418-1011209939=:10775
Content-Type: TEXT/PLAIN; charset=US-ASCII; name="fun.pyf"
Content-Transfer-Encoding: BASE64
Content-ID: <Pine.LNX.4.21.0201162138590.10775@cens.ioc.ee>
Content-Description:
Content-Disposition: attachment; filename="fun.pyf"
ISVmOTAgLSotIGY5MCAtKi0NCg0KISAgRXhhbXBsZToNCiEgICAgVXNpbmcg
ZjJweSBmb3Igd3JhcHBpbmcgbXVsdGktZGltZW5zaW9uYWwgRm9ydHJhbiBh
bmQgQyBhcnJheXMNCiEgICAgW05FVyBBUFBST0FDSCwgdXNlIGl0IHdpdGgg
ZjJweSBoaWdoZXIgdGhhbiAyLjgueF0NCiEgICRJZDogZnVuLnB5Zix2IDEu
MiAyMDAyLzAxLzE2IDE4OjU3OjMzIHBlYXJ1IEV4cCAkDQoNCiEgVXNhZ2Ug
KHdpdGggZ2NjIGNvbXBpbGVyKToNCiEgICBmMnB5IC1jIGZ1bi5weWYgZm9v
LmYgYmFyLmMNC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aCBib3VuZHMgKG0sbikNCg0K
ISA+Pj4gcHJpbnQgZnVuLmJhcihhKQ0KISBDOm09Miwgbj0zDQohIFJvdyAx
Og0KISBhKGk9MCxqPTApPTENCiEgYShpPTAsaj0xKT0yDQohIGEoaT0wLGo9
Mik9Mw0KISBSb3cgMjoNCiEgYShpPTEsaj0wKT00DQohIGEoaT0xLGo9MSk9
NQ0KISBhKGk9MSxqPTIpPTYNCiEgW1s3Nzc3ICAgIDIgICAgM10NCiEgIFsg
ICA0ICAgIDUgICAgNl1dDQoNCiAgICBlbmQgaW50ZXJmYWNlIA0KZW5kIHB5
dGhvbiBtb2R1bGUgZnVuDQoNCiEgVGhpcyBmaWxlIHdhcyBhdXRvLWdlbmVy
YXRlZCB3aXRoIGYycHkgKHZlcnNpb246Mi45LjE2NikuDQohIFNlZSBodHRw
Oi8vY2Vucy5pb2MuZWUvcHJvamVjdHMvZjJweTJlLw0K
--683786235-1296099418-1011209939=:10775
Content-Type: TEXT/PLAIN; charset=US-ASCII; name="foo.f"
Content-Transfer-Encoding: BASE64
Content-ID: <Pine.LNX.4.21.0201162138591.10775@cens.ioc.ee>
Content-Description:
Content-Disposition: attachment; filename="foo.f"
ICAgICAgc3Vicm91dGluZSBmb28oYSxtLG4pDQogICAgICBpbnRlZ2VyIGEo
bSxuKSwgbSxuLGksag0KICAgICAgcHJpbnQqLCAiRjc3OiINCiAgICAgIHBy
aW50KiwgIm09IixtLCIsIG49IixuDQogICAgICBkbyAxMDAsaT0xLG0NCiAg
ICAgICAgIHByaW50KiwgIlJvdyAiLGksIjoiDQogICAgICAgICBkbyA1MCxq
PTEsbg0KICAgICAgICAgICAgcHJpbnQqLCAiYShpPSIsaSwiLGo9IixqLCIp
ID0gIixhKGksaikNCiA1MCAgICAgIGNvbnRpbnVlDQogMTAwICBjb250aW51
ZQ0KICAgICAgaWYgKG0qbi5ndC4wKSBhKDEsMSkgPSA3Nzc3Nw0KICAgICAg
ZW5kDQogICAgICANCg==
--683786235-1296099418-1011209939=:10775
Content-Type: TEXT/x-csrc; name="bar.c"
Content-Transfer-Encoding: BASE64
Content-ID: <Pine.LNX.4.21.0201162138592.10775@cens.ioc.ee>
Content-Description:
Content-Disposition: attachment; filename="bar.c"
DQojaW5jbHVkZSA8c3RkaW8uaD4NCg0Kdm9pZCBiYXIoaW50ICphLGludCBt
LGludCBuKSB7DQogIGludCBpLGo7DQogIHByaW50ZigiQzoiKTsNCiAgcHJp
bnRmKCJtPSVkLCBuPSVkXG4iLG0sbik7DQogIGZvciAoaT0wO2k8bTsrK2kp
IHsNCiAgICBwcmludGYoIlJvdyAlZDpcbiIsaSsxKTsNCiAgICBmb3IgKGo9
MDtqPG47KytqKQ0KICAgICAgcHJpbnRmKCJhKGk9JWQsaj0lZCk9JWRcbiIs
aSxqLGFbbippK2pdKTsNCiAgfQ0KICBpZiAobSpuKQ0KICAgIGFbMF0gPSA3
Nzc3Ow0KfQ0K
--683786235-1296099418-1011209939=:10775--