如何简化这个Fortran函数? [英] How to simplify this Fortran function?

查看:209
本文介绍了如何简化这个Fortran函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的代码来计算第一个逻辑的连续数b $ b逻辑。我想简化这个功能。有
一直在想,也许我可以使用递归,但现在完全相当
。这是否是possilble?

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


推荐答案

递归。请注意,

It is possible to write this using recursion. Note that

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

返回值 count_present(p_1,p_2,...,p_n) p_1 ,..., p_ {n + 1} 并且 .TRUE。。在后一种情况下,结果是 n + 1 count_present(p_1)如果 p_1 为<$,则返回 1 c $ c> .TRUE。, 0 否则。

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.

这篇关于如何简化这个Fortran函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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