Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

How to simplify this Fortran function?

I have the following code that counts the number of consecuitive logicals from the first one. I want to simplify this function. Have been thinking that perhaps I can use recursion somehow but now quite sure. Is that possilble?

Function count_present              & 
  (                                 &
    p1, p2, p3, p4, p5, p6, p7, p8  &
  )                                 &
    Result (n)

Logical, Intent (in) :: p1
Logical, Intent (in), Optional :: p2, p3, p4, p5, p6, p7, p8 

Integer :: n

n = 0

If (Present (p8)) Then

  If      (p8) Then; n = 8
  Else If (p7) Then; n = 7
  Else If (p6) Then; n = 6
  Else If (p5) Then; n = 5
  Else If (p4) Then; n = 4
  Else If (p3) Then; n = 3
  Else If (p2) Then; n = 2
  Else If (p1) Then; n = 1
  End If

Else If (Present (p7)) Then

  If      (p7) Then; n = 7
  Else If (p6) Then; n = 6
  Else If (p5) Then; n = 5
  Else If (p4) Then; n = 4
  Else If (p3) Then; n = 3
  Else If (p2) Then; n = 2
  Else If (p1) Then; n = 1
  End If

Else If (Present (p6)) Then

  If      (p6) Then; n = 6
  Else If (p5) Then; n = 5
  Else If (p4) Then; n = 4
  Else If (p3) Then; n = 3
  Else If (p2) Then; n = 2
  Else If (p1) Then; n = 1
  End If

Else If (Present (p5)) Then

  If      (p5) Then; n = 5
  Else If (p4) Then; n = 4
  Else If (p3) Then; n = 3
  Else If (p2) Then; n = 2
  Else If (p1) Then; n = 1
  End If

Else If (Present (p4)) Then

  If      (p4) Then; n = 4
  Else If (p3) Then; n = 3
  Else If (p2) Then; n = 2
  Else If (p1) Then; n = 1
  End If

Else If (Present (p3)) Then

  If      (p3) Then; n = 3
  Else If (p2) Then; n = 2
  Else If (p1) Then; n = 1
  End If

Else If (Present (p2)) Then

  If      (p2) Then; n = 2
  Else If (p1) Then; n = 1
  End If

Else

  If (p1) n = 1

End If

End Function count_present
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

It is possible to write this using recursion. Note that

count_present(p_1, p_2, ..., p_n, p_{n+1})

returns the value count_present(p_1, p_2, ..., p_n) unless all p_1, ..., p_{n+1} are present and .TRUE.. In this latter case the result is n+1. count_present(p_1) returns 1 if p_1 is .TRUE., 0 otherwise.

recursive function count_present(p1, p2, p3, p4, p5, p6, p7, p8) result (res)
  logical, intent(in) :: p1, p2, p3, p4, p5, p6, p7, p8
  optional p2, p3, p4, p5, p6, p7, p8
  integer res

  if (PRESENT(p8)) then
    res = count_present(p1, p2, p3, p4, p5, p6, p7)
    if (res.eq.7.and.p8) res = res+1
  else if (PRESENT(p7)) then
    res = count_present(p1, p2, p3, p4, p5, p6)
    if (res.eq.6.and.p7) res = res+1
  else if (PRESENT(p6)) then
    res = count_present(p1, p2, p3, p4, p5)
    if (res.eq.5.and.p6) res = res+1
  else if (PRESENT(p5)) then
    res = count_present(p1, p2, p3, p4)
    if (res.eq.4.and.p5) res = res+1
  else if (PRESENT(p4)) then
    res = count_present(p1, p2, p3)
    if (res.eq.3.and.p4) res = res+1
  else if (PRESENT(p3)) then
    res = count_present(p1, p2)
    if (res.eq.2.and.p3) res = res+1
  else if (PRESENT(p2)) then
    res = count_present(p1)
    if (res.eq.1.and.p2) res = res+1
  else
    res = COUNT([p1])
  end if
end function count_present

Is this a good idea? Well, that's another question.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...