I have the following minimal Fortran example that I would like to convert using f2py
:
Fortran module utils_test.f90
:
module utils_test
contains
elemental real(8) function prod(a, b)
real(8), intent(in) :: a, b
prod = a*b
end function
end module utils_test
Using f2py:
python3 -m numpy.f2py -c utils_test.f9 -m utils_test
In python, I could use it as follows:
import numpy as np
from utils_test import utils_test
x1 = 5.0
y1 = 3.0
print("Scalar product", utils_test.prod(x1, y1)) # The result is 15.0
x = np.linspace(1, 5, 5) # Numpy array
y = np.linspace(10, 15, 5) # Numpy array
print("Array element-wise product:", utils_test.prod(x, y)) # this outputs 10.0 not an array
The output is:
Scalar product 15.0
Array element-wise product: 10.0
In the second example it outputs only the product of the first elements of x
and y
only.
I could solve that by vectorizing the function utils_test.prod
:
prod = np.vectorize(utils_test.prod)
Is it possible to get the expected result in python without manually vectorizing the function? In other words, is it possible for f2py to automatically vectorize the function provided that the original Fortran function is elemental
?
Any help is appreciated.