当前位置: 代码迷 >> 综合 >> f2py::演示Fortran中的公用区在Python中的使用
  详细解决方案

f2py::演示Fortran中的公用区在Python中的使用

热度:77   发布时间:2024-01-04 01:59:11.0

先看示例文件:

C FILE: COMMON.FSUBROUTINE FOOINTEGER I,XREAL ACOMMON /DATA/ I,X(4),A(2,3)PRINT*, "I=",IPRINT*, "X=[",X,"]"PRINT*, "A=["PRINT*, "[",A(1,1),",",A(1,2),",",A(1,3),"]"PRINT*, "[",A(2,1),",",A(2,2),",",A(2,3),"]"PRINT*, "]"END
C END OF COMMON.F


 

在DOS窗口中编译一下:

f2py -c -m common common.f


下面演示如何在Python中使用:

>>> import common
>>> print common.data.__doc__
i - 'i'-scalar
x - 'i'-array(4)
a - 'f'-array(2,3)>>> common.data.i = 5
>>> common.data.x[1] = 2 
>>> common.data.a = [[1,2,3],[4,5,6]]
>>> common.foo()I= 5X=[ 0 2 0 0]A=[[  1.,  2.,  3.][  4.,  5.,  6.]]
>>> common.data.a[1] = 45
>>> common.foo()I= 5X=[ 0 2 0 0]A=[[  1.,  2.,  3.][  45.,  45.,  45.]]
>>> common.data.a                 # a is Fortran-contiguous
array([[  1.,   2.,   3.],[ 45.,  45.,  45.]],'f')


更纤细的内容请看:

http://cens.ioc.ee/projects/f2py2e/usersguide/index.html#string-arguments