fortran - Fortran 函数重塑的问题
问题描述
我正在使用代码块和 gnu 编译器来运行 Fortran 代码,但我注意到一些非常奇怪的东西。一旦我有一个数组(1,2,3,...,16)
,如果我想重塑为一个 4x4 矩阵,我会使用内置reshape
函数,理论上它应该对数字使用列优先顺序,因此给出(1 5 9 13; 2 6 10 14; 3 7 11 15; 4 8 12 16)
; 相反,我得到(1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16)
. 这是一个主要的不一致,我想知道它是否与使用代码块或什么有关。任何其他有此问题和/或有关原因和解决方案的想法的人。
解决方案
我无法复制这个问题Intel Fortran oneAPI HPC
和参考代码
program FortranConsole1
use, intrinsic :: iso_fortran_env
implicit none
interface show
procedure show_matrix_i, show_matrix_r, show_matrix_d
end interface
integer :: row(16), matrix(4,4)
real(real64) :: A(4,4)
row = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
matrix = reshape( row, [4, 4])
call show(matrix)
A = dble(matrix)
A = sqrt( matmul( transpose(A), A) )
call show(A, 8)
call show(A, 12)
call show(A, 16)
contains
subroutine show_matrix_i(A, w)
! Display the matrix 'A' in columns
! A : the array of integers
! w : the column width. Default = 5
integer, intent(in) :: A(:,:)
integer, intent(in), optional :: w
integer :: i,j,n,m, wt
character(len=16) :: fmt
if(present(w)) then
wt = w
else
wt = 5
end if
n = size(A,1)
m = size(A,2)
write( fmt, "(a,g0,a)") "(*(g",wt,".0))"
write( * , fmt ) ( (A(i,j),j=1,m), new_line("A"), i=1,n )
end subroutine
subroutine show_matrix_r(A, w)
! Display the matrix 'A' in columns
! A : the array of real numbers
! w : the column width. deafult = 12
! s : sig. figures w-5 (calculated)
real(real32), intent(in) :: A(:,:)
integer, intent(in), optional :: w
integer :: i,j,n,m,dg,wt
character(len=16) :: fmt
if(present(w)) then
wt = w
else
wt = 12
end if
dg = wt-5
n = size(A,1)
m = size(A,2)
write( fmt, "(a,g0,a,g0,a)") "(*(g",wt,".",dg,"))"
write( * , fmt ) ( (A(i,j),j=1,m), new_line("A"), i=1,n )
end subroutine
subroutine show_matrix_d(A,w)
! Display the matrix 'A' in columns
! A : the array of dble numbers
! w : the column width. default = 12
! Converts 'A' into single precision and calls `show_matrix_r`
real(real64), intent(in) :: A(:,:)
integer, intent(in), optional :: w
call show_matrix_r(real(A),w)
end subroutine
end program FortranConsole1
结果
推荐阅读
- python - 亚马逊登录和访问订单
- reactjs - 使用 react js 发布项目
- git - Git更新被拒绝,因为尽管在克隆后做了它,但分支的尖端落后了?
- azure - 有没有办法将发布管道数据发送到 devops 中的 GIT 存储库?
- haskell - 在haskell中获取列表的长度
- powershell - Powershell - 时间是否在时间范围内?
- python - 如何迭代 defaultdict(list) 并在 python 中生成 json 输出?
- java - 如何投影快速变化的数据库的所有数据库列?
- javascript - Javascript Fetch 不适用于 Edge 上的 URLsearchparams
- javascript - 删除按钮按下反应的输入数据