f2py::演示Fortran二维数组在python中的使用示例

22 篇文章 5 订阅
15 篇文章 0 订阅
 

f2py中二维数组的示例:

C FILE: ARRAY.F
      SUBROUTINE FOO(A,N,M)
C
C     INCREMENT THE FIRST ROW AND DECREMENT THE FIRST COLUMN OF A
C
      INTEGER N,M,I,J
      REAL*8 A(N,M)
Cf2py intent(in,out,copy) a
Cf2py integer intent(hide),depend(a) :: n=shape(a,0), m=shape(a,1)
      DO J=1,M
         A(1,J) = A(1,J) + 1D0
      ENDDO
      DO I=1,N
         A(I,1) = A(I,1) - 1D0
      ENDDO
      END
C END OF FILE ARRAY.F


在DOS窗口输入

f2py -c -m arr array.f


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

>>> import arr
>>> from Numeric import array
>>> print arr.foo.__doc__
foo - Function signature:
  a = foo(a,[overwrite_a])
Required arguments:
  a : input rank-2 array('d') with bounds (n,m)
Optional arguments:
  overwrite_a := 0 input int
Return objects:
  a : rank-2 array('d') with bounds (n,m)

>>> a=arr.foo([[1,2,3],
...            [4,5,6]])
copied an array using PyArray_CopyFromObject: size=6, elsize=8
>>> print a
[[ 1.  3.  4.]
 [ 3.  5.  6.]]
>>> a.iscontiguous(), arr.has_column_major_storage(a)
(0, 1)
>>> b=arr.foo(a)              # even if a is proper-contiguous
...                           # and has proper type, a copy is made
...                           # forced by intent(copy) attribute
...                           # to preserve its original contents
... 
copied an array using copy_ND_array: size=6, elsize=8
>>> print a
[[ 1.  3.  4.]
 [ 3.  5.  6.]]
>>> print b
[[ 1.  4.  5.]
 [ 2.  5.  6.]]
>>> b=arr.foo(a,overwrite_a=1) # a is passed directly to Fortran
...                            # routine and its contents is discarded
... 
>>> print a
[[ 1.  4.  5.]
 [ 2.  5.  6.]]
>>> print b
[[ 1.  4.  5.]
 [ 2.  5.  6.]]
>>> a is b                       # a and b are acctually the same objects
1
>>> print arr.foo([1,2,3])       # different rank arrays are allowed
copied an array using PyArray_CopyFromObject: size=3, elsize=8
[ 1.  1.  2.]
>>> print arr.foo([[[1],[2],[3]]])
copied an array using PyArray_CopyFromObject: size=3, elsize=8
[ [[ 1.]
  [ 3.]
  [ 4.]]]
>>>
>>> # Creating arrays with column major data storage order:
...
>>> s = arr.as_column_major_storage(array([[1,2,3],[4,5,6]]))
copied an array using copy_ND_array: size=6, elsize=4
>>> arr.has_column_major_storage(s)
1
>>> print s
[[1 2 3]
 [4 5 6]]
>>> s2 = arr.as_column_major_storage(s)
>>> s2 is s    # an array with column major storage order 
               # is returned immediately
1


详细介绍可见下面的网站

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

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Fortran 二维数组插值可以通过使用内置的插值函数来实现。下面是一个简单的示例代码,演示了如何在 Fortran 使用插值函数来插值一个二维数组: ```fortran program interp_2d_array implicit none integer, parameter :: n = 5 integer :: i, j real :: x(n), y(n), f(n,n), xi, yi, fi ! Generate some data do i = 1, n x(i) = real(i) y(i) = real(i) do j = 1, n f(i,j) = real(i*j) end do end do ! Interpolate the data xi = 2.5 yi = 3.5 call interp2d(x, y, f, xi, yi, fi) ! Print the interpolated value write(*,*) fi contains subroutine interp2d(x, y, f, xi, yi, fi) implicit none integer :: n, m, i, j real, dimension(:), intent(in) :: x, y, f real, intent(in) :: xi, yi real, intent(out) :: fi ! Get the dimensions of the input arrays n = size(x) m = size(y) ! Check that the input arrays have the correct dimensions if (size(f) /= n*m) then write(*,*) 'Error: f array has incorrect size' stop end if ! Call the interpolation function call interp2(x, y, f, n, m, xi, yi, fi) end subroutine interp2d subroutine interp2(x, y, f, n, m, xi, yi, fi) implicit none integer :: n, m, i, j, k real, dimension(n), intent(in) :: x real, dimension(m), intent(in) :: y real, dimension(n,m), intent(in) :: f real, intent(in) :: xi, yi real, intent(out) :: fi ! Declare variables real :: dx, dy, t, u, v ! Find the index of the x-coordinate closest to xi i = locate(x, xi) ! Find the index of the y-coordinate closest to yi j = locate(y, yi) ! Check that xi and yi are within the bounds of the input data if (i < 1 .or. i > n-1 .or. j < 1 .or. j > m-1) then write(*,*) 'Error: xi or yi is out of range' stop end if ! Calculate the distances between xi and x(i) and yi and y(j) dx = xi - x(i) dy = yi - y(j) ! Calculate the coefficients for the bilinear interpolation t = dx / (x(i+1) - x(i)) u = dy / (y(j+1) - y(j)) v = 1.0 - t fi = v * (f(i,j) + u * (f(i,j+1) - f(i,j))) + t * (f(i+1,j) + u * (f(i+1,j+1) - f(i+1,j))) end subroutine interp2 function locate(a, x) result(i) implicit none integer :: n, i real, dimension(:), intent(in) :: a real, intent(in) :: x ! Find the index of the value closest to x n = size(a) i = minloc(abs(a - x)) end function locate end program interp_2d_array ``` 在上面的代码,我们首先生成了一个大小为 $5\times5$ 的二维数组,然后调用 `interp2d` 子程序来插值该数组。`interp2d` 子程序首先检查输入数据的维度是否正确,然后调用 `interp2` 函数来执行实际的插值操作。`interp2` 函数使用双线性插值来计算插值值。最后,我们使用 `locate` 函数来找到最接近目标 $x$ 和 $y$ 坐标的数组索引。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值