可变格式
我写了一个程序来计算平方有限差分矩阵,您可以在其中输入行数(等于列数)->这存储在变量矩阵中。该程序工作正常:
program fin_diff_matrix
implicit none
integer, dimension(:,:), allocatable :: A
integer :: matrix,i,j
print *,'Enter elements:'
read *, matrix
allocate(A(matrix,matrix))
A = 0
A(1,1) = 2
A(1,2) = -1
A(matrix,matrix) = 2
A(matrix,matrix-1) = -1
do j=2,matrix-1
A(j,j-1) = -1
A(j,j) = 2
A(j,j+1) = -1
end do
print *, 'Matrix A: '
write(*,1) A
1 format(6i10)
end program fin_diff_matrix
对于输出,我希望矩阵被格式化为输出,例如,如果用户输入 6 行,输出也应该如下所示:
2 -1 0 0 0 0
-1 2 -1 0 0 0
0 -1 2 -1 0 0
0 0 -1 2 -1 0
0 0 0 -1 2 -1
0 0 0 0 -1 2
格式的输出也应该是可变的,例如,如果用户输入 10,则输出也应格式化为 10 列。互联网上的研究给出了带有尖括号的格式语句的以下解决方案:
1 format(<matrix>i<10)
如果我在 Linux 中使用 gfortran 进行编译,我总是在终端中收到以下错误:
fin_diff_matrix.f95:37.12:
1 format(<matrix>i10)
1
Error: Unexpected element '<' in format string at (1)
fin_diff_matrix.f95:35.11:
write(*,1) A
1
Error: FORMAT label 1 at (1) not defined
这不起作用,我的错误是什么?
I wrote a program to calculate a square finite difference matrix, where you can enter the number of rows (equals the number of columns) -> this is stored in the variable matrix. The program works fine:
program fin_diff_matrix
implicit none
integer, dimension(:,:), allocatable :: A
integer :: matrix,i,j
print *,'Enter elements:'
read *, matrix
allocate(A(matrix,matrix))
A = 0
A(1,1) = 2
A(1,2) = -1
A(matrix,matrix) = 2
A(matrix,matrix-1) = -1
do j=2,matrix-1
A(j,j-1) = -1
A(j,j) = 2
A(j,j+1) = -1
end do
print *, 'Matrix A: '
write(*,1) A
1 format(6i10)
end program fin_diff_matrix
For the output I want that matrix is formatted for the output, e.g. if the user enters 6 rows the output should also look like:
2 -1 0 0 0 0
-1 2 -1 0 0 0
0 -1 2 -1 0 0
0 0 -1 2 -1 0
0 0 0 -1 2 -1
0 0 0 0 -1 2
The output of the format should also be variable, e.g. if the user enters 10, the output should also be formatted in 10 columns. Research on the Internet gave the following solution for the format statement with angle brackets:
1 format(<matrix>i<10)
If I compile with gfortran in Linux I always get the following error in the terminal:
fin_diff_matrix.f95:37.12:
1 format(<matrix>i10)
1
Error: Unexpected element '<' in format string at (1)
fin_diff_matrix.f95:35.11:
write(*,1) A
1
Error: FORMAT label 1 at (1) not defined
What doesn't that work and what is my mistake?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您尝试使用的语法是非标准的,它仅在某些编译器中有效,我不鼓励使用它。
另外,请永远忘记
FORMAT()
语句,它们已经过时了。当您自己从多个部分构建矩阵时,您可以在格式字符串中获取自己的数字
您还可以在每行循环中编写矩阵,然后您可以在中使用任意大的计数或
*
Fortran 2008。只需检查我是否无意中转置了矩阵。
The syntax you are trying to use is non-standard, it works only in some compilers and I discourage using it.
Also, forget the
FORMAT()
statements for good, they are obsolete.You can get your own number inside the format string when you construct it yourself from several parts
You can also write your matrix in a loop per row and then you can use an arbitrarily large count number or a
*
in Fortran 2008.Just check if I did not transpose the matrix inadvertently.