Adapted from: “Guide to Fortran 2008 Programming” by Walter S. Brainerd (Springer 2015)
Program to demonstrate the findloc function.¶
program find_loc
implicit none
intrinsic :: findloc
real, dimension (3,3) :: X = &
reshape ( &
[ -11, 12, -13, &
21, 22, -23, &
31, -32, -33 ], &
[ 3, 3 ], order = [ 2, 1 ] )
logical, parameter :: T = .true.
write (*,'(a)') "[*] The matrix X"
call write_matrix(X)
write (*,*)
write (*,'(a)') "[*] Begin findloc call..."
write (*,'(a)') "findloc( X > 0, T )"
write (*,'(a, 2i2, a)') "The first element where (i,j) > 0 is: (", &
findloc( X > 0, T ), &
") in column major order." ! = [ 2, 1 ]
contains
subroutine write_matrix(a)
real, dimension(:,:), intent(in) :: a
integer :: i, j
do i = lbound(a,1), ubound(a,1)
write (*,*) (a(i,j), j = lbound(a,2), ubound(a,2))
end do
end subroutine write_matrix
end program find_locThe above program is compiled and run using Fortran Package Manager (fpm):
import os
root_dir = os.getcwd()code_dir = root_dir + "/" + "Fortran_Code/Section_8_8_Findloc_Function"os.chdir(code_dir)build_status = os.system("fpm build 2>/dev/null")exec_status = os.system("fpm run 2>/dev/null")[*] The matrix X
-11.0000000 12.0000000 -13.0000000
21.0000000 22.0000000 -23.0000000
31.0000000 -32.0000000 -33.0000000
[*] Begin findloc call...
findloc( X > 0, T )
The first element where (i,j) > 0 is: ( 2 1) in column major order.