Fortran指向较小维数组的指针 [英] Fortran pointer to smaller dimension array

查看:140
本文介绍了Fortran指向较小维数组的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个指向维数较小的数组的指针。
例如,我有一些数组 arr(1:2,1:10,1:10)



现在我想创建一个指向 arr(1:1,1:10,1:10)的指针,但我想先删除,但我不知道我应该如何命名它看起来像索引,第二个指针(2:2,1:10,1:10)。



我需要它,因为我想将2维(矩阵)的数组发送到函数。



  INTEGER,DIMENSION(1:2,1:10, 1:10),TARGET :: BOUNDRIES 
INTEGER,DIMENSION(:,:),POINTER:LEFT_BOUNDRY

LEFT_BOUNDRY => BOUNDRIES(1,1:10,1:10)
DO i = 1,n
DO j = 1,10
write(*,*)LEFT_BOUNDRY(i,j)
END DO
END DO

有可能吗?

解决方案

当我们在函数或子例程(统称为procedure)中有一个 dummy em> actual 参数。考虑子程序 s

 子程序s(x)
实数x(5,2)
...
结束子程序s

在这种情况下,伪参数 x 是一个显式形状数组,等级2,形状 [5,2]



如果我们想要

pre $ call $ s $($)

其中 y 是一些 real 我们不需要有 y 一个整数数组,它的等级为2,形状为 [5, 2] 。我们只需要 y 至少有10个元素,一个名为存储关联的东西将这10个元素映射到 x 当我们在子程序中。



想象一下,然后

  real y1(10),y2(1,10),y3(29)
call s(y1)
call s(y2)
call s(y3)

这些作品中的每一个(在最后一种情况下,它只是与假人关联的前十个元素
$ b 至关重要的是,它是一种所谓的元素序列,当选择要与关联的元素时, X 。考虑

  real y(5,12,10,10)
call s(y(1,1,1 :2,3:7))

这是十个元素y的数组部分。这些十个元素在子程序 s 中一起变成 x



<总结一下,如果你想将 arr(2,1:10,1:10)(实际上是一个2级数组部分)传递给rank 2参数这是一个不超过100个元素的显式形状数组,一切都很好。


I would like to create a pointer to an array with smaller dimension. For example, I have some array arr(1:2, 1:10, 1:10).

Now I want to create a pointer to arr(1:1, 1:10, 1:10) but I want to delete first I don't know how I should name it by it look like index, and second pointer to (2:2, 1:10, 1:10).

I need it because I would like to send array with 2 dimensions (matrix) to a function.

Here is an indication of what I want to do, with pseudocode.

INTEGER, DIMENSION (1:2, 1:10, 1:10), TARGET :: BOUNDRIES
INTEGER, DIMENSION (:,:), POINTER : LEFT_BOUNDRY

LEFT_BOUNDRY => BOUNDRIES(1,1:10,1:10)
DO i = 1,n
    DO j = 1,10
    write(*,*) LEFT_BOUNDRY(i,j)
    END DO
END DO

Is it possible to do it?

解决方案

When we have a dummy argument in a function or a subroutine (collectively, procedure) we have a corresponding actual argument when we execute that procedure. Consider the subroutine s:

subroutine s(x)
  real x(5,2)
  ...
end subroutine s

The dummy argument x is in this case an explicit shape array, of rank 2, shape [5,2].

If we want to

call s(y)

where y is some real thing we don't need to have y a whole array which is of rank 2 and shape [5,2]. We simply need to have y have at least ten elements and a thing called storage association maps those ten elements to x when we are in the subroutine.

Imagine, then

real y1(10), y2(1,10), y3(29)
call s(y1)
call s(y2)
call s(y3)

Each of these works (in the final case, it's just the first ten elements that become associated with the dummy argument).

Crucially, it's a so-called element sequence that is important when choosing the elements to associate with x. Consider

real y(5,12,10,10)
call s (y(1,1,1:2,3:7))

This is an array section of y of ten elements. Those ten elements together become x in the subroutine s.

To conclude, if you want to pass arr(2,1:10,1:10) (which is actually a rank 2 array section) to a rank 2 argument which is an explicit shape array of no more than 100 elements, everything is fine.

这篇关于Fortran指向较小维数组的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆