[SciPy-user] f2py, fortran modules, and dynamic arrays

Josh Lawrence josh.k.lawrence@gmail....
Tue Jan 13 14:23:24 CST 2009


I am wanting to write some code in fortran to perform looping  
calculations on numpy arrays. I would like to accomplish using modules  
in my fortran code. I want to approach the problem in this manner  
because I have numerous arrays that would need to be passed and I  
would prefer not to have a fortran function with 10, 20, or more  
arguments. The following is some code that illustrates what I am  
trying to accomplish.

module foo
   integer :: alen(2), xlen, blen
   complex*16, pointer :: a(:,:), x(:), b(:)
! or complex*16, allocatable :: a(:)
end module foo

subroutine bar
   use foo
   integer :: i, j
   if (associated(a) .and. xlen > 0 .and. alen[0] .eq. alen[1]) then
     blen = xlen
     do i = 0, alen
       b(i) = 0.0
       do j = 0, alen
         b(i) = b(i) + a(i,j) * x(i)
       end do
     end do
   end if

Then, if I compile this as FortranMod, I would like to be able to do  
the following in python:

import FortranMod as formod

formod.foo.alen[0] = 2
formod.foo.alen[1] = 2
formod.foo.a = np.array([[1, 2], [3, 4]])
formod.foo.blen = 2
formod.foo.x = np.array([5, 6])


print formod.foo.blen
print formod.foo.b

Now, I do not care if I need to use pointers or allocatable or  
whatever, but I am curious if this is possible to do this type of  
functionality using fortran modules and f2py.



More information about the SciPy-user mailing list