Using Fortran in Python: f2py
1. Usage
- Open the
jupyter notebook
:
jupyter notebook
- The basic information is:
In[1]: import os, sys
In[2]: %config InlineBackend.figure_format = 'retina'
import matplotlib.pyplot as plt
import scipy.fftpack as sf
import scipy.linalg as sl
import numpy as np
- Create the fortran file
euclidian_norm.f90
:
In[3]: %%file euclidian_norm.f90
subroutine euclidian_norm (a, b, c)
real(8), intent(in) :: a, b
real(8), intent(out) :: c
c = sqrt (a*a+b*b)
end subroutine euclidian_norm
- Build extension module with f2py program:
In[4]: !{sys.executable} -m numpy.f2py --quiet -c euclidian_norm.f90 -m vect
A vect.so
file will be obtained.
- Use the extension module in Python:
In[5]: import vect
c = vect.euclidian_norm(3,4)
c
Out[5]: 5.0
- The basic information of
vect.euclidian_norm
is:
In[6]: print(vect.euclidian_norm.__doc__)
c = euclidian_norm(a,b)
Wrapper for ``euclidian_norm``.
Parameters
----------
a : input float
b : input float
Returns
-------
c : float