FORTRAN Simplification of Function


 
Thread Tools Search this Thread
Top Forums Programming FORTRAN Simplification of Function
# 1  
Old 12-07-2014
FORTRAN Simplification of Function

I have the following code that counts the number
of consecuitive logicals from the first one.

Any way I can simplify this function?

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

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

   !!$ Out.
   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

# 2  
Old 12-07-2014
Did you consider recursion?
# 3  
Old 12-07-2014
No I didn't. Not sure how to approach it, since
you mentioned it. What did you have in mind?
# 4  
Old 12-07-2014
My FORTRAN is not just a bit rusty, but has entirely corroded away.
Still, from staring at above function, I'd infer that the result is dependent on a) the respective parameter being present and b) the parameter being true.
Not sure what happens if you try to evaluate a non-present parameter or pass it on to another function. Couldn't you call a sub-function eight times to find out which parameter fulfills the condition and then pass back the return value?
# 5  
Old 12-08-2014
If you don't mind to set n many times (which is a very small overhead)
and if I have understood your code, you can try

Code:
if (present (p1) .and. (p1)) n = 1
if (present (p2) .and. (p2)) n = 2
...
if (present (p8) .and. (p8)) n = 8

---------- Post updated at 03:05 PM ---------- Previous update was at 02:55 PM ----------

Alternatively try the following that is without any overhead:
Code:
 If (Present (p8) .and. (p8)) Then
   n = 8
 Else If (Present (p7) .and. (p7)) Then
   n = 7
 ...
 Else If (Present (p2) .and. (p2)) Then
   n = 2
 Else If (p1) Then
   n = 1
 End If


Last edited by MadeInGermany; 12-08-2014 at 03:59 PM.. Reason: no then
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

FORTRAN p-norm

What would be an accurate and efficient computation of the p-norm in Fortran? Integer :: p Real :: sum, pn Do i = 1, Size (a) sum = sum + ((Abs a(i)) ** p) End Do pn = sum ** (1.0/ Real(p)) (0 Replies)
Discussion started by: kristinu
0 Replies

2. Programming

FORTRAN Extends

I am writing some code in fortran that defines certain shapes. Currently I have a cuboid and a prism. Now a parallelepiped is both a cuboid and a prism. Is there a way to code this? Currently I only have Type, Extends (Cuboid) :: Parallelepipied How can I do so that Parallelepipied is... (3 Replies)
Discussion started by: kristinu
3 Replies

3. Shell Programming and Scripting

FORTRAN Compiler

Need assistance in Fortran Compiler I need to install Fortran compiler on Linux distribution . I don't have root access to use rpm or yum to install compiler . Is there any way I can get it ? Are there any sites that has fortran compiler . Any links to download and install . (10 Replies)
Discussion started by: ajayram_arya
10 Replies

4. Shell Programming and Scripting

How to convert FORTRAN subroutine to awk function?

Hi All I am trying to convert some Fortran subroutine to awk function , those who know kindly explain how to define function in awk, and how to call function Fortran subroutine looks somewhat like this SUBROUTINE GDATE (JD, YEAR,MONTH,DAY) C C---COMPUTES THE GREGORIAN CALENDAR DATE... (5 Replies)
Discussion started by: Akshay Hegde
5 Replies

5. Programming

Fortran 77 and gfortran

Hi! I have a program in fortran77. This program was compiled with pgf90, but now, I need compiled it with gfortran. I show a bit of code. program hello PARAMETER(a=100) integer a write(*,*)'value ', a end program hello What's the problem? Thanks (2 Replies)
Discussion started by: kekaes
2 Replies

6. Programming

learning fortran

Can anyone recommend a very good Fortran online course for unix. It should also cover Fortran X11 programming if possible. I understand this learning cannot be rushed but a course that addresses useful topics sooner is best. If it isn't free, that's okay. (1 Reply)
Discussion started by: gav2251
1 Replies

7. UNIX for Dummies Questions & Answers

FORTRAN compiling

Hello, I have a FORTRAN code that I want to compile using F90 compiler. However, when I enter the command: f90 ./code.f -o ./code.out It would give me prompt saying that absoft license has failed, or something. I remember, however, that I could enter some keyword that ends with 'soft' in... (1 Reply)
Discussion started by: Iamthe great
1 Replies

8. Shell Programming and Scripting

Simplification of a shell script

I have the following script: df > df.txt scp df.txt username@192.168.1.2:~ rm df.txt Its purpose is: Send disk space usage report to another machine Because it is now included in a shell script that is invoked by a cron, it makes maintenance harder. I would like it to be on a single line... (5 Replies)
Discussion started by: figaro
5 Replies

9. Linux

Fortran 90 compiler

Hi everybody: Could anybody help me where could I download Fortran 90 complier? Thanks in advance. :) (2 Replies)
Discussion started by: tonet
2 Replies

10. Shell Programming and Scripting

sed simplification...

All, I have an file in the xml format which looks like this <source>Please wait. This may take several minutes...</source> <translation>Please wait. This may take several minutes...</translation> <source>Your computer must be restarted to complete setup.\nClick on 'OK' button to... (4 Replies)
Discussion started by: vino
4 Replies
Login or Register to Ask a Question