用 Fortran 将复数矩阵写入文件

发布于 2024-12-24 18:54:49 字数 240 浏览 3 评论 0原文

如何将 Fortran 中的复数 (n×n) 矩阵写入文件? 例如:

DO I=1,N
       write(14,'(100g15.5)') ( M(i,j), j=1,n )
ENDDO  

在此示例中,将 2n×n 个元素写入文件,即实数和虚数。 除了两个元素 Re(a11) Im(a11),我如何将其写为一个元素 Re(a11)+iIm(a11)?

How does one write a complex (n×n) matrix in Fortran to a file?
For example:

DO I=1,N
       write(14,'(100g15.5)') ( M(i,j), j=1,n )
ENDDO  

In this example one gets 2n×n elements written to the file i.e. the real and imaginary.
Instead of two element, Re(a11) Im(a11), How can I write it as one element Re(a11)+iIm(a11)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴意少年 2024-12-31 18:54:49

使用内部函数 REAL 和 AIMAG 写入复数的各个实部和虚部:

CHARACTER(LEN=3),DIMENSION(n,n) :: imag_unit = '+i*'

WHERE(AIMAG(M)<0.)imag_unit = '-i*'

DO I=1,N
  write(14,'(100(g15.5,a,g15.5,2x))') ( REAL(M(i,j)),imag_unit(i,j),&
                                        ABS(AIMAG(M(i,j))), j=1,n )
ENDDO 

说明:此代码定义一个字符串矩阵,当虚部为正时,其值为“+i”,当虚部为正时,值为“-i”消极的。因为负虚部在格式('-i')中被考虑,所以我们取虚部的绝对值。相应地编辑格式描述符,以便用于读取输出文件的程序能够读取它。

Use intrinsic functions REAL and AIMAG to write individual real and imaginary components of a complex number:

CHARACTER(LEN=3),DIMENSION(n,n) :: imag_unit = '+i*'

WHERE(AIMAG(M)<0.)imag_unit = '-i*'

DO I=1,N
  write(14,'(100(g15.5,a,g15.5,2x))') ( REAL(M(i,j)),imag_unit(i,j),&
                                        ABS(AIMAG(M(i,j))), j=1,n )
ENDDO 

Explanation: This code defines a matrix of character strings that have value '+i' when imaginary part is positive, and '-i' where imaginary part is negative. Because the negative imaginary part is accounted for in the formatting ('-i'), we take absolute value of the imaginary part. Edit the format descriptor accordingly so that the program you use to read the output file will be able to read it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文