Neat way to define a long parameter vector in fortran

前端 未结 1 526
甜味超标
甜味超标 2020-12-20 01:06

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         


        
相关标签:
1条回答
  • 2020-12-20 01:36

    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.

    0 讨论(0)
提交回复
热议问题