Silverfrost Forums

Welcome to our forums

z%re and z%im where z is an array

25 Apr 2023 9:37 #30223

The following code shows that the recently introduced z%re and z%im for working directly with the REAL and AIMAG parts of complex Z, does not work as expected when Z is an array.

program t
implicit none
complex z(2)
real :: d(2) = [1.0,2.0]
z = 0.0
z(1)%re = d(1)
z(1)%im = d(2)
print*, z     ! Should return (1.0 + j*2.0) and (0.0 + j*0.0)
end program t

FTN95 reports: error 378 - The '%' operator can only be applied to objects which are a derived type, which is not correct in this context.

25 Apr 2023 1:05 #30224

Ken

Thank you for the bug report. This particular usage has now been fixed for the next release of FTN95 but I guess that this issue needs more extensive testing and that other failures will come to light.

26 Apr 2023 3:04 (Edited: 26 Apr 2023 10:19) #30226

Thanks Paul.

Below is a small test program that uses these new features where the complex variables are in an array embedded within a defined type which you might like to test with your current version of FTN95.

program t
implicit none

type cw
  complex, allocatable :: z(:)
end type

type(cw) :: z

allocate(z%z(2))
z%z(1) = ( 1.0, 2.0)
z%z(2) = (-1.0,-2.0)
print*, z%z%re       ! Should return 1, -1
print*, z%z%im       !               2, -2
print*
z%z = 0.0
z%z%re = [ 10.0, 20.0]
z%z%im = [-10.0,-20.0]
print*, z%z%re       ! Should return  10,  20
print*, z%z%im       !               -10, -20
print*

deallocate(z%z)

z%z = [(1.0,2.0),(2.0,1.0)]
print*, z%z%re       ! Should return  1, 2
print*, z%z%im       !                2, 1

end program t

PS This one is interesting as only Ifort can run the code correctly:

program t
implicit none

type cw
  complex, allocatable :: z(:)
end type

type(cw) :: z

z%z%re = [10.0,20.0,30.0]   !Works with Ifort (allocate on assignment fails with gFortran)
print*, z%z%re       ! should return 10, 20, 30
print*, z%z%im       ! Undefined

end program t
26 Apr 2023 6:00 #30227

Thanks Ken.

I will make a note that this needs investigating.

5 May 2023 10:45 #30275

These failures have now been fixed for the next release of FTN95.

At the moment I am assuming that 'allocate on assignment' for z%z%re is not valid Standard Fortran.

Please login to reply.