WHERE nested statement in Fortran

I have Fortran 90 source code with a nested WHERE statement. There is a problem, but it seems difficult to understand what exactly is happening. I would like to convert it to a DO-IF structure for debugging. It’s not clear to me how to translate the enclosed WHERE.

All arrays are the same size.

WHERE (arrayA(:) > 0)
    diff_frac(:) = 1.5 * arrayA(:)
    WHERE (diff_frac(:) > 2)
        arrayC(:) = arrayC(:) + diff_frac(:)
    ENDWHERE
ENDWHERE

My option A:

DO i=1, SIZE(arrayA)
  IF (arrayA(i) > 0) THEN
    diff_frac(i) = 1.5 * arrayA(i)
    DO j=1, SIZE(diff_frac)
      IF (diff_frac(j) > 2) THEN
          arrayC(j) = arrayC(j) + diff_frac(j)
      ENDIF
    ENDDO
  ENDIF
ENDDO

My option B:

DO i=1, SIZE(arrayA)
  IF (arrayA(i) > 0) THEN
    diff_frac(i) = 1.5 * arrayA(i)        
    IF (diff_frac(i) > 2) THEN
        arrayC(i) = arrayC(i) + diff_frac(i)
    ENDIF        
  ENDIF
ENDDO

thank

+4
source share
3 answers

According to the Nested WHERE Constructs stream in comp.lang.fortran (especially Jan's answer), it seems that the first code in the Question translates to the following:

do i = 1, size( arrayA )
    if ( arrayA( i ) > 0 ) then
        diff_frac( i ) = 1.5 * arrayA( i )
    endif
enddo

do i = 1, size( arrayA )
    if ( arrayA( i ) > 0 ) then
        if ( diff_frac( i ) > 2 ) then
            arrayC( i ) = arrayC( i ) + diff_frac( i )
        endif
    endif
enddo

, , (. ). F2008 :

7.2.3 - WHERE (. 161)

7.2.3.2 (. 162)

... 2. WHERE .

... 4. -expr .

... 8. WHERE, where-body, ​​ , m_c.AND. -.

... 10. where-assign-stmt -expr , , , .

/, diff_frac( i ) > 2 arrayA( i ) > 0, IF- ( , A .and. B Fortran ).


, , ... , gfortran5.2, ifort14.0 Oracle fortran 12.4 ( )

integer, dimension(4) :: x, y, z
integer :: i

x = [1,2,3,4]
y = 0 ; z = 0

where ( 2 <= x )
   y = x
   where ( 3.0 / y < 1.001 )  !! possible division by zero
      z = -10
   end where
end where

print *, "x = ", x
print *, "y = ", y
print *, "z = ", z

:

x =            1           2           3           4
y =            0           2           3           4
z =            0           0         -10         -10

gfortran -ffpe-trap=zero
ifort -fpe0
f95 -ftrap=division  (or with -fnonstd)

gfortran ifort , y(i) = 0 , f95 . ( , Cray gfortran/ifort, NAG/PGI/XLF f95.)


, "" WHERE, , ( . 7.2.3.2, 9 ). ,

integer, dimension(4) :: a, b, c

a = [ 1, 2, 3, 4 ]
b = -1 ; c = -1

where ( 3 <= a )
    b = a * 100
    c = sum( b )
endwhere

a =  1 2 3 4
b =  -1 -1 300 400
c =  -1 -1 698 698

, (b) = 698 b, .

+2

WHERE (arrayA(:) > 0)
    diff_frac(:) = 1.5 * arrayA(:)
ENDWHERE

WHERE (diff_frac(:) > 2 .and. arrayA(:) > 0)
    arrayC(:) = arrayC(:) + diff_frac(:)
ENDWHERE

?

, where s, , . , do, .

, where - , , ( ), . do concurrent.

+1

, , . , , where. , .

, where , ( ), "A" "B". , , where.

: , , ? , , , .

, , -, . , , . , , , , . , .

, , , , , .


: ?
where , "A", "B". .

0
source

All Articles