问题描述
我正在尝试Pythonize FORTRAN77代码。有一段代码,我似乎无法把握它的意图。
I'm trying to Pythonize a FORTRAN77 code. There's a block of code that I just can't seem to grasp the intent of.
ZM只是0和1之间的一个标量.Z是一个数字的一维数组用NJ元素在0和1之间。 J,J1和J1M是INTEGER类型。 PDFZ是另一个带有NJ元素的一维数组。
ZM is just a scalar between 0 and 1. Z is a 1D array of numbers between 0 and 1 with NJ elements. J, J1, and J1M are type INTEGER. PDFZ is another 1D array with NJ elements. I'm having trouble mapping out the flow of execution.
DO 18 J=2,NJ
IF(ZM.GT.Z(J)) GOTO 18
J1=J
J1M=J-1
GOTO 20
18 CONTINUE
20 CONTINUE
DO 22 J=1,NJ
PDFZ(J)=0.D0
22 CONTINUE
PDFZ(J1)=(ZM-Z(J1M))/(Z(J1)-Z(J1M))
PDFZ(J1M)=1.D0-PDFZ(J1)
我创建了我认为是Python2.7中的等价物。但我不再那么肯定,我的Python代码捕获了Fortran77代码的行为。
I created what I thought was the equivalent in Python2.7. But I'm not so sure anymore that my python code captures the behavior of the Fortran77 code.
loc = np.where(z < z_mean)[0][0]
pdf_z[loc] = (z_mean - z[loc-1])/(z[loc] - z[loc-1])
pdf_z[loc-1] = 1.0 - pdf_z[loc]
推荐答案
当1977年推出时,我已经编程了大约八年。幸运的是,这个代码是没有深奥或复杂的基础。不是说我可以辨别它的功能。
I had already been programming for about eight years when 1977 rolled in. Fortunately this code is bedrock with nothing abstruse or complicated. Not that I can discern what it does either.
然而,我可以翻译它。这里有一个你可以试验的形式。
However, I can translate it. Here it is in a form in which you can experiment with it.
- Fortran数组是1个相对的;即一维数组的第一个元素是第一个元素。
- 正如你所知道的,Python浮点数是双精度浮点数。
- Fortran DO-循环变量假设从第一个到最后一个值的每一个值,不像Python for-loop变量。
<$>GOTO 18
DO循环结束。循环将继续使用DO循环变量的下一个值,J
。 - 相反,
GOTO 20
定位在循环之外的一行,因此,就像一个Pythonbreak
。
- Fortran arrays were 1-relative; ie, the first element of a 1-D array was number one.
- As you already know, Python floats are doubles.
- Fortran DO-loop variables assume each and every value from the first to the last, unlike Python for-loop variables.
- The
GOTO 18
targets the end of the DO-loop. The loop will continue with the next value of the DO-loop variable,J
. - In contrast,
GOTO 20
targets a line outside of the loop and is, hence, like a Pythonbreak
.
def sample(ZM):
Z = [_/10 for _ in range(0,11)]
NJ = len(Z)
for J in range(1, NJ):
if ZM > Z[J]:
continue
J1 = J
J1M = J - 1
break
PDFZ = NJ * [0]
PDFZ[J1] = (ZM - Z[J1M])/(Z[J1] - Z[J1M])
PDFZ[J1M] = 1 - PDFZ[J1]
print (ZM, PDFZ)
for ZM in [0, .1, .2, .3, ]:
sample(ZM)
这篇关于这个Fortran 77代码的意图是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!