Silverfrost Forums

Welcome to our forums

Array constructor issue

22 Jan 2025 11:58 #31840

Paul,

One for you to look at as I think the following code demonstrates a bug with FTN95.

Perhaps I should temper my present enthusiasum for eliminating do loops!

program p
implicit none
integer, parameter :: n = 1000
real*8 :: x(n), random_y(n), mean_y(n)
integer :: i

! Generate random data, a sample count in x, and running mean_y
call random_number(random_y)
x = [(i, i = 1, n)]

!do i = 1, n
!  mean_y(i) = sum(random_y(1:i))/x(i)
!end do

! When the do loop above is replaced by the following 'equivalent code' which
! makes use of an array constructor FTN 9.06 returns the running mean values
! as all zero.
! FTN 9.03 also fails with this code.  
! Checkmate reports Error 112 Reference to undefined variable.
! All variables on the RHS have previously been assigned values.

mean_y = [(sum(random_y(1:i)) / x(i), i = 1, n)] 

do i = 1, n
  print*, i, mean_y(i)
end do

end program p
23 Jan 2025 12:50 #31841

Ken,

I am using Version 9.05 /64 /debug and get an access violation error on the array constructor.

I also tried the following with the same outcome. sum_y = [ ( sum(random_y(1:i) ), i = 1, n) ]

Both work with Gfortran

A DO loop would be much quicker !

I should update to Ver 9.06 and see the result.

23 Jan 2025 11:19 #31845

John,

Thanks for confirming that there is a FTN95 issue with this

Yes, you are correct, for large n the repeated calculation of sum( random_y( 1:i ) ) will be crippling in the array constructor.

This would indeed be much faster:

acc = 0.d0
do i = 1, n, 1
  acc = acc + random_y(i)
  mean_y(i) = acc / x(i)
end do
23 Jan 2025 11:47 #31849

Ken

It looks like this approach has defeated FTN95. I have made a note to investigate.

7 May 2025 10:25 #32102

This failure has now been fixed for the next release of FTN95.

8 May 2025 8:59 #32103

Paul,

Thanks for this and other recent fixes.

Please login to reply.