Created
April 25, 2016 12:30
-
-
Save mverleg/71dbbf755ed1cf96589236a69bc64027 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!! | |
!! f2py -c -m search search.f90 | |
!! | |
subroutine find_first(needle, haystack, haystack_length, index) | |
!! | |
!! Find the first index of `needle` in `haystack`. | |
!! | |
implicit none | |
integer, intent(in) :: needle | |
integer, intent(in) :: haystack_length | |
integer, intent(in), dimension(haystack_length) :: haystack | |
!f2py intent(inplace) haystack | |
integer, intent(out) :: index | |
integer :: k | |
index = -1 | |
do k = 1, haystack_length | |
if (haystack(k)==needle) then | |
index = k - 1 | |
exit | |
endif | |
enddo | |
end |
@mverleg Yes, that's what is confusing me. I'm calling the function via search.find_first(needle, array) but nowhere am I giving the length as a parameter. Is this handled internally in the python-fortran interface?
@hech92 Yeah Python knows this information, just Fortran doesn't, so Python can fill it in automatically before calling Fortran. And because it's always the same logic, it can be in the wrapper code so you don't have to do it.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@hech92 It means that you should leave it empty in Python, and then in Fortran it will have as value the number of elements in the array "haystack". In Fortran the number of elements needs to be passed separately, it's not part of the array (or lets say it's complicated).