我编写了一个程序来计算平方有限差分矩阵,您可以在其中输入行数(等于列数)->这存储在变量矩阵中。该程序运行良好:
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
这不起作用,我的错误是什么?