Friday, December 19, 2014

Using 'Home-Made' Fortran Binary as Python module

Python is easy to use, but it's slow, especially for loop computation. So I compute it using fortran like this

        subroutine subs(a, b, n)
        double precision a(n)
        double precision b(n)
        integer n
cf2py   intent(in) :: a
cf2py   intent(out) :: b
cf2py   intent(hide) :: n
!        b(1) = a(1)
        do 100 i=2, n
                b(i) = a(i)-1
100     continue

save it as and do the following command
$ f2py -c aravir.f -m aravir

To use the module on the python I use the code below
import numpy as np
import aravir as ar

a = np.linspace(0,1,100)

b = ar.subs(a)

print a
print b