在Fortran中编写复杂的矩阵 [英] writing complex matrix in fortran

查看:204
本文介绍了在Fortran中编写复杂的矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Fortran中将复杂(nXn)矩阵写入文件?
例如:

How do one write a complex (nXn) matrix in Fortran to a file? For example:

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

在这个例子中,获取写入文件的2nXn元素,即真实和虚拟。
而不是两个元素Re(a11)Im(a11),我怎样才能把它写成一个元素Re(a11)+ iIm(a11)?

In this example one get 2nXn 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)?

推荐答案

使用内部函数REAL和AIMAG来编写复数的单个实部和虚部:

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 

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

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.

这篇关于在Fortran中编写复杂的矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆