在fortran中定义长参数向量的简洁方法 [英] Neat way to define a long parameter vector in fortran

查看:391
本文介绍了在fortran中定义长参数向量的简洁方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我现在有这个问题.我要在向量中整理一组(大量)参数.

Well, I've this problem now. I've a (huge) set of parameters I'd like to organize in a vector.

当然,我可以做类似的事情:

Of course, I can do something like:

real, dimension(64)          :: CONST

CONST(1) = 2.4
CONST(2) = 1.4
...
CONST(n) = CONST(1)*CONST(14)**CONST(7)
...
CONST(64) = ABS(CONST(18))

(请注意,某些常量与其他常量相关).

(Note that some of the constants are related to other constants).

但是在那种情况下,我不会在变量中具有parameter属性.

But in that case, I wouldn't have the parameter attribute in the variable, wich I'd like to have.

我可以考虑的另一种选择是使用属性parameter,在这种情况下,我必须在变量定义期间将值分配给向量.像这样:

The other option I can think about is using the attribute parameter, in which case I've to assign the value to the vector during the definition of the variable. Something like:

real, parameter, dimension(64) :: CONST =[2.4 , 1.4       , &
                                                ...       , &
                                                1.5412356 , &
                                                ...       , &
                                                342.5]

但是,有一些巨大的对手:

But, with some huge counterparts:

  • 在定义变量期间,由于大量行而导致代码难以阅读.
  • 我不能将常量定义为其他常量的函数.

所以

  • 在Fortran中,有一种巧妙的方法(如第一个代码中一样)来定义具有属性parameter的长向量?
  • (我想不是)(一旦我没有定义),有一种方法可以更改变量的属性?
  • There is a neat way (as in the first code) to define long vector with the attribute parameter in Fortran?
  • (As I suppose it is not) there is a way to change the attributes of the variable once I'd defined it values?

谢谢您的时间.

推荐答案

参数的问题仅在于您无法定义要依赖于其自身的常量数组.但是您可以定义基本数量,然后定义包括派生数量在内的整个数组,如下所示:

The problem with the parameter is only that you can't define the constant array to depend on itself. But you could define the fundamental quantities, and then the whole array including the derived quantities, as so:

program foo
implicit none

real, dimension(2), parameter :: basic = [2.4, 1.4]
real, dimension(4), parameter :: all = [basic(1), basic(2),           &   
                                        basic(1)*basic(2)**basic(1),  &
                                        abs(basic(1))]

print *, basic
print *, all

end program foo

实际上,如果您想那样做,您不妨命名基本数量:

and in fact if you want to go that way, you might as well name the fundamental quantities:

program foo
implicit none

real, parameter :: height = 2.4, bodymass = 1.4
real, dimension(4), parameter :: all = [height, bodymass,     &   
                                        height*bodymass**2,  &
                                        abs(height)]

print *, height, bodymass
print *, all

end program foo

我无法想象身高就是你需要绝对值的那种东西,但是你明白我的意思了.

I can't imagine height is the sort of thing you need to take the absolute value of, but you see my point.

这篇关于在fortran中定义长参数向量的简洁方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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