复数的 fortran 格式说明符 [英] fortran format specifier for complex number

查看:30
本文介绍了复数的 fortran 格式说明符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在 fortran 中为复数指定格式说明符吗?我有一个简单的程序.

Can I specify a format specifier for a complex number in fortran? I have a simple program.

program complx1
implicit none
complex :: var1
var1 = (10,20)
write (*,*) var1
write (*,'(F0.0)') var1
write (*,'(F0.0,A,F0.0)') real(var1), ' + i ' , aimag(var1)
end program complx1

输出:

 (  10.0000000    ,  20.0000000    )
10.
20.
10. + i 20.

我想使用带有格式说明符的 a+bi 内置格式,而不是手动(程序的倒数第二行).显然 F0.0 不起作用.有什么想法吗?

I wanted to use inbuilt format for a+bi with some format specifier, instead of one did manually (second last line of program). Obviously F0.0 did not work. Any ideas?

我不认为这是帖子的重复:用fortran编写复杂矩阵,它说要使用 REALAIMAG 函数.我已经使用了这些功能,想知道是否有可以完成这项工作的内置格式.

I don't think this is a duplicate of post: writing complex matrix in fortran, which says to use REAL and AIMAG functions. I already used those functions and wondering whether there is an inbuilt format that can do the work.

推荐答案

@francescalus' 现有的且大部分令人满意的答案的附录.格式字符串,例如

An addendum to @francescalus' existing, and mostly satisfactory, answer. A format string such as

fmt = '(F0.0,SP,F0.0,"i")'

应该导致在实部和虚部之间以正确符号显示复数;无需摆弄字符串即可获得加号.

should result in a complex number being displayed with the correct sign between real and imaginary parts; no need to fiddle around with strings to get a plus sign in there.

这篇关于复数的 fortran 格式说明符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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