微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Fortran 函数重塑的问题

如何解决Fortran 函数重塑的问题

我正在使用代码块和 gnu 编译器来运行 Fortran 代码,但我注意到一些非常奇怪的事情。 一旦我有一个数组 (1,2,3,...,16),如果我想重塑为一个 4x4 矩阵,我使用内置的 reshape 函数,理论上应该使用列主序为数字,所以给 {{ 1}}; 相反,我得到 (1 5 9 13; 2 6 10 14; 3 7 11 15; 4 8 12 16)。 这是一个重大的不一致,我想知道它是否与使用代码块或什么有关。 任何其他遇到此问题和/或对原因和解决方案有想法的人。

解决方法

我无法用 Intel Fortran oneAPI HPC

复制该问题

scr1

和代码供参考

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),dg,wt
    character(len=16) :: fmt
    if(present(w)) then
        wt = w
    else
        wt = 12
    end if
    dg = wt-5
    n = size(A,a,".","))"
    write( *,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),optional :: w
    call show_matrix_r(real(A),w)
end subroutine

end program FortranConsole1

结果

scr2

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。