如何用带fortran77的行写格式化的矩阵? [英] How to write the formatted matrix in a lines with fortran77?
问题描述
do i = 1,N
write(*,(F8.3))(c (i,j),j = 1,N)
end do
在形式中
c(1,2)
... ... 最后,写 然后输出就像矩阵,但当然,它没有格式化。 如何在Fortran77中获得正确的输出? 编辑。看来解决方案之一是写 实际上是想写 这个简单案例的一个相当普遍的解决方案就像
do i = 1,N
write(*,*)(c(i,j),j = 1,N)
end do
pre $ do i = 1,N
do j = 1,N
write(*,'(F9.3,A,$)')c(i,j),''
end do
write(*,*)''
end do
N
每行。
程序临时
隐式无
整数,参数:: N = 3
实数,维N,N):: c
integer :: i,j
character(len = 20):: exFmt
c = 1.0
write(exFmt,'((, I0,(F8.3)))')N
do i = 1,N
write(*,exFmt)(c(i,j),j = 1,N)
end do
end program
这会使 注意一些编译器将允许 exFmt
是'(3(F8.3))'
,它指定打印三个浮点数(注意你可能真的需要'( 3(F8.3,))'$
。这是Fortran 2008规范的一部分,因此您可以访问的所有编译器都不提供此功能。请参阅此处获取编译器支持摘要(请参阅无限格式项目,感谢HighPerformanceMark为此)
exFmt
只是
最后一个简单的例子就是使用一个格式表达式,比如'(1000(F8.3) )'
其中1000比你需要的大。
Suppose I have the matrix c(i,j). I want to write it on the screen on oldest Fortran77 language with three signs after comma. I write
do i=1,N
write(*,"(F8.3)") ( c(i,j), j=1,N )
end do
but the output is in the form
c(1,1)
c(1,2)
...
c(1,10) c(2,1)
c(2,2)
...
Finally, I may simply write
do i=1,N
write(*,*) ( c(i,j), j=1,N )
end do
and then the output is like the matrix, but, of course, it is not formatted.
How to get the correct output in Fortran77?
An edit. It seems that one of solutions is to write
do i=1, N
do j=1, N
write(*,'(F9.3,A,$)') c(i,j), ' '
end do
write(*,*) ' '
end do
Your format only specifies a single float but you actually want to write N
per line.
A fairly general solution for this simple case would be something like
program temp
implicit none
integer, parameter :: N=3
real, dimension(N,N) :: c
integer :: i,j
character(len=20) :: exFmt
c = 1.0
write(exFmt,'("(",I0,"(F8.3))")') N
do i=1,N
write(*,exFmt) (c(i,j), j=1,N)
end do
end program
This will make exFmt
be '(3(F8.3))'
, which specifies printing three floats (note you probably really want '(3(F8.3," "))'
to explicitly include some spacing).
Note some compilers will allow for exFmt
to be just '(*(F8.3))'
. This is part of the fortran 2008 specification so may not be provided by all compilers you have access to. See here for a summary of compiler support (see Unlimited format item, thanks to HighPerformanceMark for this)
Finally an easy bodge is to use a format statment like '(1000(F8.3))'
where 1000 is larger than you will ever need.
这篇关于如何用带fortran77的行写格式化的矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!