Thanks for the project. The GitHub page gives instructions for Linux and MacOS, but It works on Windows, too. I downloaded the project to c:\fortran\public_domain\github\gfort2py
,
where I created stats.f90
module stats_mod
implicit none
integer, parameter :: dp = kind(1.0d0)
contains
function range_vec(x) result(xrange)
real(kind=dp), intent(in) :: x(:)
real(kind=dp) :: xrange
if (size(x) > 0) then
xrange = maxval(x) - minval(x)
else
xrange = 0.0_dp
end if
end function range_vec
end module stats_mod
and created a DLL with gfortran -shared -o libfile.dll stats.f90
. Then running the Python code
import numpy as np
import gfort2py as gf
SHARED_LIB_NAME=r"c:\fortran\public_domain\github\gfort2py\libfile.dll"
MOD_FILE_NAME="stats_mod.mod"
x = gf.fFort(SHARED_LIB_NAME,MOD_FILE_NAME)
vec = np.random.normal(size=4)
print("vec =", vec)
res = x.range_vec(vec)
print("argument(s) passed:", res[1])
print("range from Fortran, NumPy =", res[0], np.max(vec) - np.min(vec))
gave
vec = [-0.0860823 1.15058585 -0.08149836 1.07459795]
argument(s) passed: {'x': array([-0.0860823 , 1.15058585, -0.08149836, 1.07459795])}
range from Fortran, NumPy = 1.2366681428133555 1.2366681428133555