저는 프로그래밍을 처음 접했고 이제 Python 스크립트로 Fortran 코드를 다시 작성하려고합니다.
원래 포트란 코드에서는 실수를 다음과 같이 선언합니다.
Begin
c----
data (A(0,i), i=0,17)
& /
& 9.526e5, 1.416e6, 1.210e6, 7.734e5, 4.050e5, 1.851e5,
& 7.944e4, 3.382e4, 1.434e4, 5.821e3, 2.174e3, 7.392e2,
& 2.349e2, 7.361e1, 2.405e1, 8.423e0, 3.120e0, 1.176e0
& /
c----
c---- End
이 A_ul (0, i)를 파이썬 코드로 어떻게 변환 할 수 있습니까? 그리고 계산하기 위해 A (0, i)를 어떻게 다시 쓸 수 있습니까?
B = (p**2)*(c**3)*A_ul(0,i)
p와 c가 상수가되도록
두 가지 선택
옵션 1 : 일반 Python 목록 사용
A_ul = [9.526e5, 1.416e6, 1.210e6, 7.734e5, 4.050e5, 1.851e5,
7.944e4, 3.382e4, 1.434e4, 5.821e3, 2.174e3, 7.392e2,
2.349e2, 7.361e1, 2.405e1, 8.423e0, 3.120e0, 1.176e0]
# some values for p, c (assuming scalars)
p = 2.95
c = 3.41
# generate computation of scalar with array using list comprehension
B = [(p**2)*(c**3)*x for x in A_ul]
print(B)
밖
[328713655.56773156, 488619080.70954007, 417534666.42552507, 266877116.54008356, 139753338.76226252, 63872451.863937765, 27412358.595738605, 11670266.461579552, 4948303.402100851, 2008652.308481803, 750182.1196769351, 255075.72348904808, 81056.93648211227, 25400.60065750653, 8298.932832672626, 2906.524376282808, 1076.6183134278003, 405.80228736894003]
옵션 2 : Python Numpy 배열 사용
Numpy는 Python에서 쉬운 배열 및 행렬 표현식을 허용합니다 (예 : Matlab의 대안).
import numpy as np
# A_ul as Numpy array
A_ul = np.array([9.526e5, 1.416e6, 1.210e6, 7.734e5, 4.050e5, 1.851e5,
7.944e4, 3.382e4, 1.434e4, 5.821e3, 2.174e3, 7.392e2,
2.349e2, 7.361e1, 2.405e1, 8.423e0, 3.120e0, 1.176e0])
# some values for p, c (assuming scalars)
p = 2.95
c = 3.41
# numpy understands multiplying scalars by arrays
B = (p**2)*(c**3)*A_ul
print(B)
밖
[3.28713656e+08 4.88619081e+08 4.17534666e+08 2.66877117e+08
1.39753339e+08 6.38724519e+07 2.74123586e+07 1.16702665e+07
4.94830340e+06 2.00865231e+06 7.50182120e+05 2.55075723e+05
8.10569365e+04 2.54006007e+04 8.29893283e+03 2.90652438e+03
1.07661831e+03 4.05802287e+02]
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다