%load_ext f2pymagic
import numpy as np
%%f2py -f
SUBROUTINE FOO(A,B)
REAL*8 A, B
Cf2py intent(in) a
Cf2py intent(inout) b
PRINT*, " A=",A," B=",B
PRINT*, "INCREMENT A AND B"
A = A + 1D0
B = B + 1D0
PRINT*, "NEW A=",A," B=",B
END
Compilation went OK...
foo is ready for use
print(foo.__doc__)
foo(a,b) Wrapper for ``foo``. Parameters ---------- a : input float b : in/output rank-0 array(float,'d')
a=np.array(2) # these are integer rank-0 arrays
b=np.array(3)
print(a,b)
foo(a,b)
print(a, b)
(array(2), array(3)) (array(2), array(4))
%%f2py
SUBROUTINE FIB(A,N)
C
C CALCULATE FIRST N FIBONACCI NUMBERS
C
INTEGER N
REAL*8 A(N)
Cf2py intent(in) n
Cf2py intent(out) a
Cf2py depend(n) a
DO I=1,N
IF (I.EQ.1) THEN
A(I) = 0.0D0
ELSEIF (I.EQ.2) THEN
A(I) = 1.0D0
ELSE
A(I) = A(I-1) + A(I-2)
ENDIF
ENDDO
END
Compilation went OK...
fib is ready for use
print(fib.__doc__)
a = fib(n) Wrapper for ``fib``. Parameters ---------- n : input int Returns ------- a : rank-1 array('d') with bounds (n)
a = fib(10)
print(a)
[ 0. 1. 1. 2. 3. 5. 8. 13. 21. 34.]
%%f2py
real function r(m,t)
integer m
real t
r = 0.1 * t * (m ** 2 + 14 * m + 46)
if (r .LT. 0) r =0.0
return
end
Compilation went OK...
r is ready for use
r(1, 10.0)
61.0
%%f2py
subroutine dbl(x)
real, intent(inout) :: x
x = 2.0e0 * x
end subroutine
Compilation went OK...
dbl is ready for use
myvar=np.array((10.0,))
dbl(myvar)
print(myvar)
[ 20.]
%%f2py
module laplace
implicit none
contains
subroutine for_update1(u, dx2, dy2, nx, ny)
real(8), intent(inout) :: u(nx,ny)
real(8), intent(in) :: dx2, dy2
integer, intent(in) :: nx, ny
integer :: i, j
do j = 2, ny-1
do i = 2, nx-1
u(i, j) = ((u(i+1, j) + u(i-1, j)) * dy2 + &
(u(i, j+1) + u(i, j-1)) * dx2) / (2*(dx2+dy2))
end do
end do
end subroutine
subroutine for_update2(u, dx2, dy2, nx, ny)
real(8), intent(inout) :: u(nx,ny)
real(8), intent(in) :: dx2, dy2
integer, intent(in) :: nx, ny
u(2:nx-1,2:ny-1) = ((u(3:,2:ny-1)+u(:ny-2,2:ny-1))*dy2 + &
(u(2:nx-1,3:) + u(2:nx-1,:ny-2))*dx2) / (2*(dx2+dy2))
end subroutine
end module laplace
Compilation went OK...
laplace is ready for use
print(laplace.__doc__)
for_update1(u,dx2,dy2,[nx,ny]) Wrapper for ``for_update1``. Parameters ---------- u : in/output rank-2 array('d') with bounds (nx,ny) dx2 : input float dy2 : input float Other Parameters ---------------- nx : input int, optional Default: shape(u,0) ny : input int, optional Default: shape(u,1) for_update2(u,dx2,dy2,[nx,ny]) Wrapper for ``for_update2``. Parameters ---------- u : in/output rank-2 array('d') with bounds (nx,ny) dx2 : input float dy2 : input float Other Parameters ---------------- nx : input int, optional Default: shape(u,0) ny : input int, optional Default: shape(u,1)