Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
482 views
in Technique[技术] by (71.8m points)

variables - Neat way to define a long parameter vector in 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).

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

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:

  • The code becomes difficult to read as a consequence of an enormous amount of lines during the definition of the variable.
  • I cannot define constants as a function of other constants.

So,

  • 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?

Thank you for your time.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...