program reshape_test
implicit none
integer, parameter :: N = 2
integer, dimension(N,N) :: A1,A2,A3,A4,A5,A6,A7,A8,A9
character(20) fmt
integer A(3*N,3*N)
A1 = reshape([11,21,12,22],[N,N])
A2 = reshape([13,23,14,24],[N,N])
A3 = reshape([15,25,16,26],[N,N])
A4 = reshape([31,41,32,42],[N,N])
A5 = reshape([33,43,34,44],[N,N])
A6 = reshape([35,45,36,46],[N,N])
A7 = reshape([51,61,52,62],[N,N])
A8 = reshape([53,63,54,64],[N,N])
A9 = reshape([55,65,56,66],[N,N])
write(fmt,'(*(g0))') '(a/',N,'(i2:1x))'
write(*,fmt) 'A1 = ',transpose(A1)
write(*,fmt) 'A2 = ',transpose(A2)
write(*,fmt) 'A3 = ',transpose(A3)
write(*,fmt) 'A4 = ',transpose(A4)
write(*,fmt) 'A5 = ',transpose(A5)
write(*,fmt) 'A6 = ',transpose(A6)
write(*,fmt) 'A7 = ',transpose(A7)
write(*,fmt) 'A8 = ',transpose(A8)
write(*,fmt) 'A9 = ',transpose(A9)
A = reshape([reshape([A1,A4,A7,A2,A5,A8,A3,A6,A9],[N,3,N,3],order=[1,3,2,4])],[3*N,3*N])
write(fmt,'(*(g0))') '(a/',3*N,'(i2:1x))'
write(*,fmt) 'A = ',transpose(A)
end program reshape_test
超出 4774 个字符的限制
program reshape_test
implicit none
integer M,N
integer J,K
type A_type
character(:), allocatable :: A(:,:)
end type A_type
type(A_type), allocatable :: AI(:)
character(:), allocatable :: A(:,:)
character(20) test_string
integer LI
integer LM
integer LN
integer I
integer IM
integer IN
character(40) fmt
integer IJ
integer IK
M = 2
N = 2
J = 3
K = 3
write(test_string,'(i0)') J*K
LI = len_trim(test_string)
write(test_string,'(i0)') M
LM = len_trim(test_string)
write(test_string,'(i0)') N
LN = len_trim(test_string)
write(fmt,'(4(a,i0))') "('A',i0.",LI,",'(',i",LM,",',',i",LN,",')')"
allocate(AI(J*K))
do I = 1, J*K
allocate(character(4+LI+LM+LN)::AI(I)%A(M,N))
do IM = 1,M
do IN = 1,N
write(AI(I)%A(IM,IN),fmt) I,IM,IN
end do
end do
end do
write(*,'(a)') 'Column-major, direct'
write(*,'(a)') 'Input order'
allocate(character(4+LI+LM+LN)::A(J*M,K*N))
write(fmt,'(3(a,i0))') "(",J*K,"('A',i0.",LI,":','))"
write(*,fmt) ((K*(IJ-1)+IK,IJ=1,J),IK=1,K)
A = reshape([reshape([((AI(K*(IJ-1)+IK)%A,IJ=1,J),IK=1,K)],[M,J,N,K],order=[1,3,2,4])],[M*J,N*K])
write(fmt,'(3(a,i0))') '(',N*K,'(a',len(A),':1x))'
write(*,fmt) transpose(A)
deallocate(A)
write(*,'()')
write(*,'(a)') 'Row-major, direct'
write(*,'(a)') 'Input order'
allocate(character(4+LI+LM+LN)::A(J*M,K*N))
write(fmt,'(3(a,i0))') "(",J*K,"('A',i0.",LI,":','))"
write(*,fmt) (I,I=1,J*K)
A = reshape([reshape([(AI(I)%A,I=1,J*K)],[M,J,N,K],order=[1,3,4,2])],[M*J,N*K])
write(fmt,'(3(a,i0))') '(',N*K,'(a',len(A),':1x))'
write(*,fmt) transpose(A)
deallocate(A)
write(*,'()')
write(*,'(a)') 'Column-major, transposed'
write(*,'(a)') 'Input order'
allocate(character(4+LI+LM+LN)::A(J*N,K*M))
write(fmt,'(3(a,i0))') "(",J*K,"('A',i0.",LI,":','))"
write(*,fmt) ((K*(IJ-1)+IK,IJ=1,J),IK=1,K)
A = reshape([reshape([((AI(K*(IJ-1)+IK)%A,IJ=1,J),IK=1,K)],[N,J,M,K],order=[3,1,2,4])],[N*J,M*K])
write(fmt,'(3(a,i0))') '(',M*K,'(a',len(A),':1x))'
write(*,fmt) transpose(A)
deallocate(A)
write(*,'()')
write(*,'(a)') 'Row-major, transposed'
write(*,'(a)') 'Input order'
allocate(character(4+LI+LM+LN)::A(J*M,K*N))
write(fmt,'(3(a,i0))') "(",J*K,"('A',i0.",LI,":','))"
write(*,fmt) (I,I=1,J*K)
A = reshape([reshape([(AI(I)%A,I=1,J*K)],[N,J,M,K],order=[3,1,4,2])],[N*J,M*K])
write(fmt,'(3(a,i0))') '(',M*K,'(a',len(A),':1x))'
write(*,fmt) transpose(A)
deallocate(A)
end program reshape_test
A(1:3,1:3) = A1
。 - Vladimir F Героям слава