Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fortran DO loop sequence confusion

  1. Apr 25, 2014 #1
    I am trying to convert a FORTRAN version of a Canonical Correlates program listed in Multivariate Morphology - Blackith and Reyment. I've programed in FORTRAN decades ago and now I have to understand the language to rewrite the program into PERL.

    a) DO 260 I = 1 , M
    b) SX( I ) = SX( I ) = SX( I ) + XX( I )
    c) DO 260 J = 1 , M
    d) SS( I , J ) = SS( I , J ) + XX( I ) * XX( J )
    e) 260 SS( J , I ) = SS( I , J )

    What is the process sequence here? When the program moves into a) the first time line a) moves to line b) is this done with I = 1 (first to M) and then move directly to line c) (J = 1) for processing to d), and then move to line e) and then jump back to a) for a repeat of this process ( I = 2 , M and J = 2 , M)?

    I don't have a FORTRAN compiler to figure this out.
    I've never seen multiple processing events (I = 1 to M) in a loop.
     
  2. jcsd
  3. Apr 25, 2014 #2

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    I think you made some typos in your post. Line b) won't even compile, and likes c) and d) both look wrong (guessing what the code it supposed to be calculating)

    But ignoring those issues, It might be easier to understand how the loops work in more Fortran style:
    Code (Text):

    DO I = 1 , M
       SX( I ) = SX( I ) = SX( I ) + XX( I )
       DO J = 1 , M
          SS( I , J ) = SS( I , J ) + XX( I ) * XX( J )
          SS( J , I ) = SS( I , J )
       END DO
    END DO
     
     
  4. Apr 25, 2014 #3
    AlephZero - thank you for your quick reply. Sorry for the redundancy in line line b).

    So, is this the correct sequence of events for the code in my original post:

    first cycle for I = 1 , 10
    line a) with I = 1
    line b) with I = 1
    line c) with J = 1
    line d) with I = 1 , J = 1
    line e) with J = 1 , I = 1

    second cycle for I = 2 , 10

    line a) with I = 2
    line b) with I = 2
    line c) with J = 2
    line d) with I = 2 , J = 2
    line e) with J = 2 , I = 1

    next cycle for I = 3 , 10

    Am I still confused?
    mike
     
  5. Apr 25, 2014 #4

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    This is like nested loops in any other proramming language.

    first cycle for I = 1
    line a) with I = 1
    line b) with I = 1
    line c) with J = 1
    line d) with I = 1 , J = 1
    line e) with J = 1 , I = 1
    line c) with J = 2
    line d) with I = 1 , J = 2
    line e) with J = 2 , I = 1
    ...
    line c) with J = 10
    line d) with I = 1 , J = 10
    line e) with J = 10 , I = 1

    second cycle for I = 2

    line a) with I = 2
    line b) with I = 2
    line c) with J = 1
    line d) with I = 2 , J = 1
    line e) with J = 1 , I = 2
    ...
    line c) with J = 10
    line d) with I = 2 , J = 10
    line e) with J = 10 , I = 2

    ...

    last cycle for I = 10

    line a) with I = 10
    line b) with I = 10
    line c) with J = 1
    line d) with I = 10 , J = 1
    line e) with J = 1 , I = 10
    ...
    line c) with J = 10
    line d) with I = 10 , J = 10
    line e) with J = 10 , I = 10
     
  6. Apr 25, 2014 #5

    SteamKing

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    Also, in general, you are allowed one '=' in an assignment statement, so for b) it appears the correct code would be:

    SX( I ) = SX( I ) + XX( I )
     
  7. Apr 26, 2014 #6

    Mark44

    Staff: Mentor

    Line c looks OK to me - it's just the older style (Fortran 77?) where DO includes a statement label, in this case line e, where 260 is the label.

    I believe this was a typo on the OP's part.

    I don't remember whether Fortran allows only one '=' in an assignment statement. Other languages based on C (C, C++, C#, Java, JavaScript, and others) can have assignment statements with more than one variable being assigned a value.
     
  8. Apr 26, 2014 #7
    Thanks for the feedback! I didn't create an unanticipated error when I transcript the code (line b). When I put fake data into this code it appears that the correct matrix formed indicates that the first DO loop is done from a) to b) - all M of them. and then moves to the next DO loop to process c) to e). But it still doesn't make sense. There is something to be said to have FORTRAN.
    mike
     
  9. Apr 26, 2014 #8

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    It will compile and run, but if you look at the semantics of lines d) and e), it would make more sense if the loop in line c) ran from I to M, not 1 to M.

    Even without a spec to read, if I was reviewing those lines of code I would put a big question mark in the margin.
     
    Last edited: Apr 26, 2014
  10. Apr 26, 2014 #9

    SteamKing

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    There may be non-standard versions of Fortran which allow a series of assignments on one line with multiple '=' signs, but the standard allows only 'A = B' assignment statements.
     
  11. Apr 27, 2014 #10

    mathman

    User Avatar
    Science Advisor
    Gold Member

    General comment: Presumably SX(I), SS(I,J) have been initialized, XX(I) set previously.
    Line e) is confusing- what exactly do you mean?

    In fact the entire program looks flakey.
    You seem to be setting SX(I) = XX(I) and SS(I,J) = XX(I)*XX(J). There is no cumulative sum for either.
     
  12. Apr 27, 2014 #11
    The confusion I think that I have with this code is the stepwise process. Does the program begin at the a) statement - a DO loop, moving to b) were the work gets done (originally I added an unintentional extra SX( I )) I should have just described b) SX( I ) = SX( I ) + XX( I ). Does the program than wallow in a) - b) doing it M times, then moving to c) to e)?

    or

    Does the program begin at a), move to b) for one processing step, then on to c) beginning another DO loop, went it then moves to d) and e), then jumping back to the start a) for another round with I now equal to 2?

    mike
     
  13. Apr 27, 2014 #12

    Mark44

    Staff: Mentor

    A better example might be helpful.
    Code (Text):

        DO 20 M = 1, 4    ; 1
          DO 10 N = 1, 3  ; 2
             PRINT*, M, N  ; 3
    10  CONTINUE         ; 4
    20 CONTINUE          ; 5
     
    This is an example of a nested DO loop, similar to the one you posted, but easier to understand. The outer DO loop (lines 1 through 5) executes four times, for values of M of 1, 2, 3, and 4.
    Each iteration of the outer DO causes the inner DO loop (lines 2 through 4) to execute three times

    So, when M = 1, the inner loop cycles through values of N of 1, 2, and 3.
    Then, when M = 2, the inner loop again cycles through the three N values.
    When M = 3, the inner loop cycles through the three N values.
    When M = 4, the inner loop cycles through the three N values one more time.

    Each time one of the labeled CONTINUE statements is executed, the associated loop control variable is incremented by 1. When the loop control variable exceeds the limiting number (4 for the outer loop and 3 for the inner loop), the loop is finished.

    All in all, the print statement (line 3) executes 4 * 3 = 12 times. The output looks like this:
    1 1
    1 2
    1 3
    2 1
    2 2
    2 3
    3 1
    3 2
    3 3
    4 1
    4 2
    4 3
    That's 12 pairs of numbers.
     
  14. Apr 28, 2014 #13

    mathman

    User Avatar
    Science Advisor
    Gold Member

    DO loops work as follows:
    For each I, do step b, then do steps c,d,e M times (once for each J), before going to the next I.
     
    Last edited: Apr 28, 2014
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Fortran DO loop sequence confusion
  1. Fortran Help (Do Loop) (Replies: 3)

Loading...