Silverfrost Forums

Welcome to our forums

implied do loop

24 Sep 2007 9:05 #2261

Hi, I think I was wrong using implied-do loops for several years! I used CVF since I started programming in F90. Consider the following example:0

vector1 = (/ ( dot_product( matrix(i,: ),vector2 ), i=1,N ) /)

This example compiled nicely in CVF (which I used for many years), but when I moved to FTN95 it didn't work. Reading more carefully, I could only find implied do loops examples in read, write and declaration statements, but not in assignment statements like the one above. Was I wrong all this years and CVF didn't tell me?

Thanks

pezze

25 Sep 2007 7:59 #2262

This appears to be valid Fortran 90 code.

FTN95 has a go at it but fails to compute matrxi(i, : ). We will take a look at it.

25 Sep 2007 11:56 #2263

Why don't you simply write the following ?

do i=1,n vector1(i) = dot_product ( matrix(i,: ), vector2 ) end do

Having worked through fortran 4 and early attempts at fortran 77 on pc's, I become uncomfortable looking at such complex single line statements, which ( I think ) can be restated without losing any clarity.

I don't think, because the standard allows it, being able to write 'vector1 = (/ ( dot_product( matrix(i,: ),vector2 ), i=1,N ) /)' as a valid fortran statement is progress.

Just my personal opinion,

John

26 Sep 2007 11:37 #2265

I'm with John on this one!

I have always felt inferior by not fully taking on all the syntax improvements of Fortran 90/95. So I decided to see what all this was about and am now completely confused. Here is my test program.

      real*8 vector1(10),matrix(10,3),matrix2(3,10), vector2(3)
      n=10
      do i=1,3
        vector2(i) = i
      enddo
      do j=1,10
        do i=1,3
          matrix(j,i) = i+j**2
          matrix2(i,j) = matrix(j,i)
        enddo
      enddo
 1000 format(3f12.3)
      print 1000,vector2,((matrix(j,i),i=1,3),j=1,10)
! try the syntax both ways for both vector and matrix
! try changing do loop upper bound to 10 and it crashes on the line
! following the do loop - something is carried over!  
      do i=1,9
        print *,i,dot_product( vector2, matrix(i,:)  )
        print *,i,dot_product( matrix(i,:), vector2  )
        print *,i,dot_product( matrix2(:,i), vector2 )
        print *,i,dot_product( vector2, matrix2(:,i) )
       enddo
      print *,'===================='
      print *,( dot_product( vector2, matrix(i,:) ), i=1,1 )
      print *,'===================='
      vector1 = (/ ( dot_product( vector2, matrix(i,:) ), i=1,N ) /)
      print *,'vector1',vector1
      print *,'===================='
      print *,( dot_product( vector2, matrix(i,:) ), i=1,2 )
      print *,'===================='
      vector1 = (/ ( dot_product( vector2, matrix(i,:) ), i=1,N ) /)
      print *,'vector1',vector1
       print *,'===================='
      print *,( dot_product( vector2, matrix(i,:) ), i=1,3 )
      print *,'===================='
      vector1 = (/ ( dot_product( vector2, matrix(i,:) ), i=1,N ) /)
      print *,'vector1',vector1
      print *,'===================='
      print *,( dot_product( vector2, matrix(i,:) ), i=1,4 )
      print *,'===================='
      vector1 = (/ ( dot_product( vector2, matrix(i,:) ), i=1,N ) /)
      print *,'vector1',vector1
       end
Please login to reply.