您的位置:首页 > 其它

f2py::Fortran 90 模块中的可分配数组

2012-03-21 13:39 344 查看
F2PY has basic support for Fortran 90 module allocatable arrays.

请看示例Fortran 90 文件:

module mod
real, allocatable, dimension(:,:) :: b
contains
subroutine foo
integer k
if (allocated(b)) then
print*, "b=["
do k = 1,size(b,1)
print*, b(k,1:size(b,2))
enddo
print*, "]"
else
print*, "b is not allocated"
endif
end subroutine foo
end module mod


在dos窗口编译一下:

f2py -c -m allocarr allocarr.f90


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

>>> allocarr.mod.foo()
b=[
1.000000       2.000000       3.000000
4.000000       5.000000       6.000000
]
>>> allocarr.mod.b                             # b is Fortran-contiguous
array([[ 1.,  2.,  3.],
[ 4.,  5.,  6.]],'f')
>>> allocarr.mod.b = [[1,2,3],[4,5,6],[7,8,9]] # reallocate/initialize b
>>> allocarr.mod.foo()
b=[
1.000000       2.000000       3.000000
4.000000       5.000000       6.000000
7.000000       8.000000       9.000000
]
>>> allocarr.mod.b = None                      # deallocate array
>>> allocarr.mod.foo()
b is not allocated


更详细的介绍见官方网站:

http://cens.ioc.ee/projects/f2py2e/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: