我可以创建使用指针用Fortran子阵列? [英] Can I create sub arrays in Fortran using pointers?

查看:107
本文介绍了我可以创建使用指针用Fortran子阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够有一个大的主数组,并参考使用子阵列的不同部分。目前我使用偏移指数要做到这一点,但它可以变得很复杂做这种方式。

I want to be able to have a large master array and refer to different parts of it using sub arrays. At the moment I'm using offset indices to do this, but it can get very complicated doing it this way.

我可以有尺寸的主阵列(9),和尺寸的3个子阵列(3),使得sub_array1指向master_array的前3个元素,sub_array2指向下一个3元素和sub_array3点到最后一个3要素?

Can I have a master array of dimension(9), and 3 sub arrays of dimension(3) such that sub_array1 points to the first 3 elements of the master_array, sub_array2 points to the next 3 elements and sub_array3 points to the last 3 elements?

例如,数组会像这样定义的:

For example, the arrays would be defined like so:

integer, dimension(9) :: master_array
integer, dimension(3) :: sub_array1, sub_array2, sub_array3

在阵列之间的关系是:

The relationship between the arrays would be:

sub_array1(1) -> master_array(1)
sub_array1(2) -> master_array(2)
sub_array1(3) -> master_array(3)

sub_array2(1) -> master_array(4)
sub_array2(2) -> master_array(5)
sub_array2(3) -> master_array(6)

sub_array3(1) -> master_array(7) 
sub_array3(2) -> master_array(8)
sub_array3(3) -> master_array(9)

此外,是有可能有混合数据类型,让我有一个整数较大的主数组?

Furthermore, is it possible to have mixed datatypes so that I have one sub array of reals within a larger master array of integers?

在此先感谢您的帮助。

推荐答案

是的,你可以使用完全使用指针指向数组的次区域。

Yes, you can use absolutely use pointers to point to subregions of an array. This can be very handy in a lot of situations, such as for stencil calculations for PDEs:

program pointerviews
    real, dimension(10), target :: alldata
    real, dimension(:), pointer :: left
    real, dimension(:), pointer :: centre
    real, dimension(:), pointer :: right

    alldata = (/ (i, i=1,10) /)

    left  => alldata(1:8)
    right => alldata(3:10)
    centre=> alldata(2:9)


    print *, alldata
    print *, left
    print *, centre
    print *, right
    print *, (left - 2*centre + right)
end program pointerviews

在FORTRAN数组指针不仅仅是一个地址越多,它们包含数组的大小,间距和类型信息,太。所以,你可以做更疯狂这样的事情(如果你已经习惯了C指针),并包括大步:

Array pointers in FORTRAN are more than just an address, they contain array size, stride, and type information, too. So you can do even crazier things like this (if you're used to C pointers) and include strides:

program pointerviews2
    real, dimension(10), target :: alldata
    real, dimension(:), pointer :: left
    real, dimension(:), pointer :: centre
    real, dimension(:), pointer :: right

    alldata = (/ (i, i=1,10) /)

    left  => alldata(1:8:2)
    right => alldata(3:10:2)
    centre=> alldata(2:9:2)

    print *, alldata
    print *, left
    print *, centre
    print *, right

    print *, 'Changing alldata(4) = 9999'
    alldata(4) = 9999.

    print *, alldata
    print *, left
    print *, centre
    print *, right

end program pointerviews2

您的不能的,但是,有一个数组这是一个不同的类型比数据的其余部分,根据定义的区域。你将不得不使用派生类型,或者多态,如果你想它包含多种类型的数据的变量。

You cannot, however, have regions of an array which are of a different type than the rest of the data, by definition. You'll have to use derived types, or polymorphism, if you want a variable which contains data of several types.

这篇关于我可以创建使用指针用Fortran子阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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