简明的符号从其他数组大小继承? [英] Concise notation for inheriting size from other array?

查看:134
本文介绍了简明的符号从其他数组大小继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的code,我有一个子程序,它需要一个5级阵列作为参数,并使用一个局部变量,这是一个4个等级阵列共享前4个指标。

In my code, I have a subroutine that takes a 5th-rank array as argument and uses a local variable, which is a 4-th rank array sharing the first 4 indices.

我试图找到一种更简洁的方式来恩preSS的尺寸申报

I'm trying to find a more concise way to express the size declaration in

subroutine mysub(momentum)
  complex, intent(in) :: momentum(:,:,:,:,:)
  complex :: prefactor( &
      & size(momentum,1), size(momentum,2), size(momentum,4) &
      & size(momentum,5) )
  ...
end subroutine mysub

大小声明的冗长损害可读性,尤其是当变量名是甚至更长的时间比在这里。

The verbosity of the size declaration harms readability, especially when variable names are even longer than here.

如果这是八度/ matlab的我倒是pre分配 prefactor

If this was octave/matlab I'd pre-allocate prefactor by writing

prefactor = zeros(size(momentum)([1 2 4 5]))

请问Fortran 90的支持一些类似的简洁?我知道,它可以用preprocessor宏,如

Does Fortran 90 support something similarly concise? I know that it could be solved using preprocessor macros, such as

#define XSIZE2(array,a,b) SIZE(array,a), SIZE(array,b)
#define XSIZE3(array,a,b,c) SIZE(array,a), SIZE(array,b), SIZE(array,c)
#define XSIZE4(array,a,b,c,d) SIZE(array,a), SIZE(array,b), SIZE(array,c), SIZE(array,d)

但引进这样的定义可能会伤害可读性超过它帮助。

but introducing such definitions would probably harm the readability more than it helps.

推荐答案

Fortran的2008年增加了模具说明符的分配声明。如果你有机会到支持此功能的编译器,你可以尝试

Fortran 2008 added the mold specifier to the allocate statement. If you have access to a compiler that supports this feature, you can try

program main

  implicit none

  integer :: a(2,3,4,5,6)
  integer, allocatable :: b(:,:,:,:)

  print *, shape(a)

  allocate(b, mold=a(:,:,:,:,1))
  print *, shape(b)

end program main

此代码片段,其中英特尔Fortran 2016年,更新1。

This snippet worked with Intel Fortran 2016, Update 1.

这篇关于简明的符号从其他数组大小继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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