Re: [f2py] incompatible parameter transfer between python and fortran?

Xavier Barthelemy <[email protected]> Mon, 1 Nov 2010 15:21:22 +1100
Newsgroups gmane.comp.python.f2py.user
Message-ID <[email protected]>
Hi again
They they need to match but the automatic doc generated is always right :)
Keep the order of the doc.the wrapper then does the job.

so you can do:

subroutine optimize(maxi,n0,inarr0,mu,sig,ret,exampleout)
implicit none
 integer,intent(in) :: maxi,n0
real*8, intent(in) ::  mu,sig,inarr0(n0)
real*8, intent(out) :: ret(3)
integer::exampleout
!f2py integer intent(in) :: maxi
!f2py integer intent(in),depend(inarr0) :: n0
!f2py real(8) intent(in) :: mu,sig
!f2py real(8) dimension(n0) intent(in) :: inarr0
!f2py real(8) dimension(3) intent(out) :: ret
!f2py integer intent(out) :: exampleout


in python you'll probably have
ret,exampleout=module.optimize(maxi,n0,inarr0,mu,sig)

I strongly suggest you order your python array fortran style, it's
easier to handle then, and i think a source of less errors or passing
errors
like: inarr0=numpy.empty([n0],dtype='d', order='FORTRAN')

i hope it answer your question
Cheers
Xavier




2010/11/1 Jon <[email protected]>:
> Hi Xavier,
>
> Your information is very useful, I checked it and it worked, thank you!
>
> Following your arguments, I checked the detailed doc for the generated
> module. I seem to reach some very surprising conclusion that the positions
> of the variables given in python when calling the module function do not
> need to match the variables defined in the fortran subroutines?! For
> example, what is defined in fortran as
>
>      subroutine optimize(maxi,n0,inarr0,mu,sig,ret)
>       implicit none
>       integer,intent(in) :: maxi,n0
>       real*8, intent(in) ::  mu,sig,inarr0(n0)
>       real*8, intent(out) :: ret(3)
>
> but the optim.optimize.__doc__ gives
>
> optimize - Function signature:
>   ret = optimize(maxi,inarr0,mu,sig,[n0])
> Required arguments:
>   maxi : input int
>   inarr0 : input rank-1 array('d') with bounds (n0)
>   mu : input float
>   sig : input float
> Optional arguments:
>   n0 := len(inarr0) input int
> Return objects:
>   ret : rank-1 array('d') with bounds (3)
>
> which means I have to leave n0 at the end of the argument list when calling
> optimize from optim module, even n0 is defined on the 2nd place in the
> fortran code. I checked the code and it seems my interpretation works.
>
> Please confirm me of this, and please **comment** on how to return several
> variables of different types from the fortran code.
>
> Thanks again,
>
> Sincerely,
> Jon
>
> On Sun, Oct 31, 2010 at 5:24 PM, Xavier Barthelemy <[email protected]> wrote:
>>
>> Hi Jon
>>
>> I think it's because you are mixing fortran declaration and f2py
>> variable declaration.
>>
>> first you have to tell f2py what you really wants to do:
>>
>>     subroutine hello(n,m)
>>      implicit none
>> !f2py integer, intent(in) :: n
>> !f2py integer, intent(out) :: m
>>      integer n,m
>>      print*,'hello world ',n
>>      m = n + 5
>>      end
>>
>> and then, the intend(out) has automatically the hidden attribute: That
>> means the correct call in python will be
>>
>> import optim
>> n=10
>> m=10
>> m=optim.hello(n)
>>
>> you can confirm this with the autodocumentation
>> import optim
>> print optim.__doc__
>>
>> hope it helps
>> Xavier
>>
>>
>>
>> 2010/11/1 Jon <[email protected]>:
>> > Hello,
>> >
>> > I have the following code, compiled by f2py,
>> >
>> >       subroutine hello(n,m)
>> >       implicit none
>> > !      integer, intent(in) :: n
>> > !      integer, intent(out) :: m
>> >       integer n,m
>> >       print*,'hello world ',n
>> >       m = n + 5
>> >       end
>> >
>> > and called by python code as
>> >
>> > import optim
>> > n=10
>> > m=10
>> > optim.hello(n,m)
>> >
>> > The execution result is: the parameter m is not changed, as if only a
>> > formal
>> > parameter is transferred to hello,
>> >
>> > if I comment out
>> >
>> > integer n,m
>> >
>> > and restore the two commented statements,
>> >
>> >       integer, intent(in) :: n
>> >       integer, intent(out) :: m
>> >
>> > then, python complains
>> >
>> > TypeError: optim.hello() takes exactly 1 argument (2 given)
>> >
>> > It seems that f2py does not recognize the intent(out) parameters in the
>> > fortran code?!
>> >
>> > Any idea towards these issues? Thanks a lot!
>> >
>> > Jon
>> >
>> >
>> > _______________________________________________
>> > f2py-users mailing list
>> > f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]
>> > http://cens.ioc.ee/mailman/listinfo/f2py-users
>> >
>> >
>>
>>
>>
>> --
>>  « Quand le gouvernement viole les droits du peuple, l'insurrection
>> est, pour le peuple et pour chaque portion du peuple, le plus sacré
>> des droits et le plus indispensable des devoirs »
>>
>> Déclaration des droits de l'homme et du citoyen, article 35, 1793
>>
>> _______________________________________________
>> f2py-users mailing list
>> f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]
>> http://cens.ioc.ee/mailman/listinfo/f2py-users
>
>
> _______________________________________________
> f2py-users mailing list
> f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]
> http://cens.ioc.ee/mailman/listinfo/f2py-users
>
>



-- 
 « Quand le gouvernement viole les droits du peuple, l'insurrection
est, pour le peuple et pour chaque portion du peuple, le plus sacré
des droits et le plus indispensable des devoirs »

Déclaration des droits de l'homme et du citoyen, article 35, 1793