f2py pass by reference
I have subroutine foo (a) integer a print*, "Hello from Fortran!" print*, "a=",a a=2 end and from python I want to do
a=1 foo(a)
and I want a's value to now be 2. How do I do this? Mathew
On 12/04/11 23:36, Mathew Yeates wrote:
I have subroutine foo (a) integer a print*, "Hello from Fortran!" print*, "a=",a a=2 end
and from python I want to do
a=1 foo(a) and I want a's value to now be 2. How do I do this?
Mathew _______________________________________________ NumPyDiscussion mailing list NumPyDiscussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpydiscussion
Someone can correct me if I'm wrong but I don't think this is possible with integers because they are treated as immuatable types in python. You can, however, do this with numpy arrays with "intent(inplace)". For example, subroutine foo(a) integer::a(2) !f2py intent(inplace)::a write(*,*) "Hello from Fortran!" write(*,*) "a=",a a(1)=2 end subroutine foo a = np.array([1,2]) foo(a) #a is now [2,2]
On Tue, Apr 12, 2011 at 9:06 PM, Mathew Yeates <mat.yeates@gmail.com> wrote:
I have subroutine foo (a) integer a print*, "Hello from Fortran!" print*, "a=",a a=2 end
and from python I want to do
a=1 foo(a)
and I want a's value to now be 2. How do I do this?
With subroutine foo (a) integer a !f2py intent(in, out) a print*, "Hello from Fortran!" print*, "a=",a a=2 end you will have desired effect:
a=1 a = foo(a) print a 2
HTH, Pearu
bizarre I get =================
hello.foo(a) Hello from Fortran! a= 1 2 a 1 hello.foo(a) Hello from Fortran! a= 1 2 print a 1
=================================
i.e. The value of 2 gets printed! This is numpy 1.3.0 Mathew On Tue, Apr 12, 2011 at 11:45 AM, Pearu Peterson <pearu.peterson@gmail.com> wrote:
On Tue, Apr 12, 2011 at 9:06 PM, Mathew Yeates <mat.yeates@gmail.com> wrote:
I have subroutine foo (a) integer a print*, "Hello from Fortran!" print*, "a=",a a=2 end
and from python I want to do
a=1 foo(a)
and I want a's value to now be 2. How do I do this?
With
subroutine foo (a) integer a !f2py intent(in, out) a print*, "Hello from Fortran!" print*, "a=",a a=2 end
you will have desired effect:
a=1 a = foo(a) print a 2
HTH, Pearu
_______________________________________________ NumPyDiscussion mailing list NumPyDiscussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpydiscussion
Note that hello.foo(a) returns the value of Fortran `a` value. This explains the printed 2 value. So, use
a = hello.foo(a)
and not
hello.foo(a)
As Sameer noted in previous mail, passing Python scalar values to Fortran by reference is not possible because Python scalars are immutable. Hence the need to use `a = foo(a)`. HTH, Pearu On Tue, Apr 12, 2011 at 9:52 PM, Mathew Yeates <mat.yeates@gmail.com> wrote:
bizarre I get =================
hello.foo(a) Hello from Fortran! a= 1 2 a 1 hello.foo(a) Hello from Fortran! a= 1 2 print a 1
=================================
i.e. The value of 2 gets printed! This is numpy 1.3.0
Mathew
On Tue, Apr 12, 2011 at 11:45 AM, Pearu Peterson <pearu.peterson@gmail.com> wrote:
On Tue, Apr 12, 2011 at 9:06 PM, Mathew Yeates <mat.yeates@gmail.com>
wrote:
I have subroutine foo (a) integer a print*, "Hello from Fortran!" print*, "a=",a a=2 end
and from python I want to do
a=1 foo(a)
and I want a's value to now be 2. How do I do this?
With
subroutine foo (a) integer a !f2py intent(in, out) a print*, "Hello from Fortran!" print*, "a=",a a=2 end
you will have desired effect:
a=1 a = foo(a) print a 2
HTH, Pearu
_______________________________________________ NumPyDiscussion mailing list NumPyDiscussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpydiscussion
participants (3)

Mathew Yeates

Pearu Peterson

Sameer Grover