从fortran中为Python获取两个值



是否可以从fortran中获得两个值?例如,我想从矩阵中得到最大分数和这个坐标

# python code 
import numpy as np
matrix = np.array([[1, 10, 3, 4, 9],
[2, 1, 0, 9, 13], 
[3, 5, 10, 18, 3]])
max_score= 0
column_coord = 0
row_coord = 0
for i in range(len(matrix[:,0])):
for j in range(len(matrix[0,:])):
if matrix[i, j] >= max_score:
# getting max_score, column, row coordinate
max_score= matrix[i, j]
column_coord = i
row_coord = j
print(max_score, column_coord, row_coord)

这段代码运行良好,但如果矩阵变大,则需要花费大量时间找到我想要的值
因此,我决定使用f2py进行更快的计算,这就是fortran代码。cc是列长度,rr是行长度。

subroutine findthemax(cc, rr, matrix, max_score, col_coord, row_coord)
integer, intent(in) :: cc, rr 
integer, intent(in) :: matrix(0:cc, 0:rr)
integer, intent(out) :: max_score, col_coord, row_coord
max_score = 0
col_coord = 0
row_coord = 0
do i = 1, cc
do j = 1, rr
if (matrix(i, j).GE.max_score) then
max_score = matrix(i, j)
col_coord = i
row_coord = j
end if        
end do
end do
return
end subroutine

我想得到max_score、col_coord、row_coord,所以我导入了f2py转换的findthemax模块
(名为findthemax.f90(。

import numpy as np
matrix = np.array([[1, 10, 3, 4, 9],
[2, 1, 0, 9, 13], 
[3, 5, 10, 18, 3]])
cc = len(matrix[:,0])
rr = len(matrix[0,:])
max_score, column_coord, row_coord = findthemax.findthemax(cc, rr, matrix)

我不知道为什么这不起作用,那是因为我实际上不知道
如何使用fortran和f2py返回两个以上的值。有人能告诉我如何
从fortran中获取多个值吗?

您可以通过以下方式使用带有多个返回值的fortran子例程。

Fortran代码findthemax.f90

subroutine findthemax(matrix, max_score, row, col)
integer, intent(in)  :: matrix(:,:)
integer, intent(out) :: max_score, row, col
integer :: ind(2)
ind       = maxloc(matrix)
row       = ind(1)
col       = ind(2)
max_score = matrix(row,col)
end subroutine

通过f2py 编译

$ f2py -c findthemax.f90 -m findthemax

将其导入您的python代码

import findthemax
import numpy as np
matrix = np.array([[1, 10,  3,  4,  9],
[2,  1,  0,  9, 13],
[3,  5, 10, 18,  3]])
(max_score, row, col) = findthemax.findthemax(matrix)
print(max_score)        # 18
print(row)              #  3
print(col)              #  4

最大值及其索引的实现速度slow,因为它使用python的循环标准。Numpy有大量快速/优化的内置函数,大多数函数甚至在下面使用fortran代码。

对于您的问题,您应该查看numpy.argmax.

示例代码

import numpy as np
matrix = np.array([[1, 10, 3, 4, 9],
[2, 1, 0, 9, 13], 
[3, 5, 10, 18, 3]])
# get flat index of maximum
flat = np.argmax(matrix)
# flat index to row/col indices
(row, col) = np.unravel_index(flat, matrix.shape)
# max value
mymax = matrix[row,col]

在fortran一侧有

subroutine findthemax(cc, rr, matrix, max_score, col_coord, row_coord)
integer, intent(in) :: cc, rr 
integer, intent(in) :: matrix(0:cc, 0:rr)
integer, intent(out) :: max_score, col_coord, row_coord
max_score = 0
col_coord = 0
row_coord = 0
do i = 1, cc    !Does this need a 0, cc ??
do j = 1, rr   !Does this need a 0, rr ??
if (matrix(i, j).GE.max_score) then
max_score = matrix(i, j)
col_coord = i
row_coord = j
end if        
end do
end do
return
end subroutine

你可能想要这样的东西:

subroutine findthemax(cc, rr, matrix, max_score, col_coord, row_coord)
integer, intent(in) :: cc, rr 
integer, intent(in) :: matrix(cc+1, rr+1)  ! not zero ??
integer, intent(out) :: max_score, col_coord, row_coord
max_score = 0
col_coord = 0
row_coord = 0
Outter_Loop: do j = 1, UBOUND(MATRIX, DIM=2)
Inner_Loop: do i = 1, UBOUND(MATRIX, DIM=1)
if (matrix(i, j) >= max_score) then
max_score = matrix(i, j)
col_coord = i
row_coord = j
end if        
end do Inner_Loop
end do Outter_Loop
return
end subroutine

更优雅的是使用fortran中的内在函数:

subroutine findthemax(cc, rr, matrix, threeple)
IMPLICIT NONE
integer, intent(in)  :: cc, rr 
integer, intent(in)  :: matrix(cc+1,rr+1)  ! not 0:rr, or its it 1:rr+1 ???
integer, intent(OUT)  :: Threeple(3)
integer, DIMENSION(2) :: At_Min
! YOU MAY NEED TRANSPOSE - but just intentionally reverse rows and columns if python is row major??
Threeple(1) = MAXVAL(matrix)
At_Min = MAXLOC(Matrix)
Threeple(2) = At_Min(2) ! Or is it (1)??
Threeple(3) = At_Min(1) ! Or is it (2)??
return
end subroutine findthemax

最新更新