Silverfrost Forums

Welcome to our forums

Can TRANSPOSE be permitted in an initialisation expression?

3 Apr 2023 11:24 #30148

I do not recall if this has been discussed before.

The TRANSPOSE intrinsic function is not permitted in an initialisation expression in FTN95 (while RESHAPE is permitted).

gFortran and iFort do allow the use of TRANSPOSE in this situation.

It would help, when receiving code written in these alternative compilers, if FTN95 also allowed the use of TRANSPOSE in an initialisation.

For example for:

program p
implicit none
integer row, col
integer, parameter :: n = 3
real :: a(n,n) = transpose(reshape([1,2,3,4,5,6,7,8,9],shape(a)))  ! Not permitted in FTN95
open(unit=10,file='temp.txt',status='unknown') ; rewind 10
do row = 1, n
  write(10,*), (a(row,col), col=1,n)
end do
rewind 10 ; close(unit=10,status='keep')
end program p

both of the alternative compilers returns for the above code: 1.000000 2.000000 3.000000
4.000000 5.000000 6.000000
7.000000 8.000000 9.000000

Not urgent, the source code I received has been modified to be compatible with FTN95, but something to perhaps consider - time permitting?

4 Apr 2023 1:04 #30154

Ken

Thank you for the feedback.

I have added this to the wish list.

18 Apr 2023 10:17 #30198

This has now been implemented for the next release of FTN95.

10 Jul 2023 10:01 #30432

Paul,

The following code runs as expected, now that the TRANSPOSE intrinsic is allowed in an initialisation expression.

program p
implicit none
integer row, col
integer, parameter :: n = 3
real :: a(n,n) = transpose(reshape([1,2,3,4,5,6,7,8,9],shape(a))) 
do row = 1, n
  write(*,*) (a(row,col), col=1,n)
end do
end program p

However, if the real array a is given the parameter attribute, as in:

program p
implicit none
integer row, col
integer, parameter :: n = 3
real, parameter :: a(n,n) = transpose(reshape([1,2,3,4,5,6,7,8,9],shape(a))) 
do row = 1, n
  write(*,*) (a(row,col), col=1,n)
end do
end program p

The complier says:

,shape(a))) do row = 1, n write(,) (a(row,col), col=1,n) end do end program p

However, if the real array a is given the parameter attribute, as in:

program p
implicit none
integer row, col
integer, parameter :: n = 3
real, parameter :: a(n,n) = transpose(reshape([1,2,3,4,5,6,7,8,9],shape(a))) 
do row = 1, n
  write(*,*) (a(row,col), col=1,n)
end do
end program p

The complier says: [quote:3bbf7ede05]FreeFormat7.F95(6) : error 898 - Constant found on left hand side of an assignment where a variable was expected

Simplifying the code further, so there is no DO LOOP or implied do loop i.e. just a simple print*, as in the following, the same error is still reported.

program p
implicit none
integer row, col
integer, parameter :: n = 3
real, parameter :: a(n,n) = transpose(reshape([1,2,3,4,5,6,7,8,9],shape(a))) 
print*, a
end program p

If a has the parameter attribute, but transpose is not used (only reshape), there is no issue attempting to print the array a i.e.:

program p
implicit none
integer row, col
integer, parameter :: n = 3
real, parameter :: a(n,n) = (reshape([1,2,3,4,5,6,7,8,9],shape(a))) 
do row = 1, n
  write(*,*) (a(row,col), col=1,n)
end do
end program p

Very strange! Perhaps I am trying to do too much at compile time, the code which identified this issue was more complex than these reduced examples.

One for you to look at when you have the opportunity. Apologies for adding to your to-do list once again. Perphaps Dan is correct when he says I use too many tricks!

11 Jul 2023 6:21 #30433

Ken

Thanks for the feedback. I have made a note of this.

1 Nov 2023 11:49 #30683

The failure of TRANSPOSE with PARAMETER has now been fixed for a future release of FTN95.

6 Nov 2023 11:02 #30706

Paul, thanks for this fix.

Please login to reply.