简单读取函数错误
Simple read function error
我想这是我错过的一些愚蠢的事情,但我问了我整个 class 似乎没有人能解决它。制作一个在子例程中调用的简单程序,但我在读取矩阵条目中的 do 循环时遇到问题。
program Householder_Program
use QR_Factorisation
use numeric_kinds
complex(dp), dimension(:,:), allocatable :: A, Q, R, V
integer :: i, j, n, m
print *, 'Enter how many rows in the matrix A'
read *, m
print *, 'Enter how many columns in the matrix A'
read *, n
allocate(A(m,n), Q(m,n), R(n,n), V(n,n))
do i = 1,m
do j = 1,n
Print *, 'Enter row', i, 'and column', j, 'of matrix A'
read *, A(i,j)
end do
end do
call Householder_Triangularization(A,V,R,n,m)
print *, R
end program
它会要求我输入 A(1,1) 但当我输入数字时它不会要求我输入 A(1,2),它会留下一个空行。当我尝试输入第二个数字时,它会出错并说:
Enter row 1 and column 1 of matrix A
1
2
At line 22 of file HouseholderProgram.f90 (unit = 5, file = 'stdin')
Fortran runtime error: Bad repeat count in item 1 of list input
您的变量 A
是复杂类型的(数组)。这意味着当您尝试对元素值进行列表定向输入时,您不能只指定一个数字。所以,在你的情况下,问题不在于程序,而在于输入。
来自 Fortran 2008 标准,10.10.3
When the next effective item is of type complex, the input form consists of a left parenthesis followed by an ordered pair of numeric input fields separated by a comma (if the decimal edit mode is POINT) or semicolon (if the decimal edit mode is COMMA), and followed by a right parenthesis.
那么输入必须是 (1., 12.)
.
您正在尝试读取 复数 数字(A
是复数)!因此,您应该为代码指定复数……因为您只提供一个整数,程序不知道该怎么做。
提供 (1,0)
和 (2,0)
而不是 1
和 2
就可以了。
如果用户输入始终是真实的,并且您想将其读入复杂类型数组,您可以这样做:
Print *, 'Enter row', i, 'and column', j, 'of matrix A'
read *, dummy
A(i,j)=dummy
其中 dummy
声明为 real
。这将使用户无需键入复数所需的括号。 (自动转换为复数)
我想这是我错过的一些愚蠢的事情,但我问了我整个 class 似乎没有人能解决它。制作一个在子例程中调用的简单程序,但我在读取矩阵条目中的 do 循环时遇到问题。
program Householder_Program
use QR_Factorisation
use numeric_kinds
complex(dp), dimension(:,:), allocatable :: A, Q, R, V
integer :: i, j, n, m
print *, 'Enter how many rows in the matrix A'
read *, m
print *, 'Enter how many columns in the matrix A'
read *, n
allocate(A(m,n), Q(m,n), R(n,n), V(n,n))
do i = 1,m
do j = 1,n
Print *, 'Enter row', i, 'and column', j, 'of matrix A'
read *, A(i,j)
end do
end do
call Householder_Triangularization(A,V,R,n,m)
print *, R
end program
它会要求我输入 A(1,1) 但当我输入数字时它不会要求我输入 A(1,2),它会留下一个空行。当我尝试输入第二个数字时,它会出错并说:
Enter row 1 and column 1 of matrix A
1
2
At line 22 of file HouseholderProgram.f90 (unit = 5, file = 'stdin')
Fortran runtime error: Bad repeat count in item 1 of list input
您的变量 A
是复杂类型的(数组)。这意味着当您尝试对元素值进行列表定向输入时,您不能只指定一个数字。所以,在你的情况下,问题不在于程序,而在于输入。
来自 Fortran 2008 标准,10.10.3
When the next effective item is of type complex, the input form consists of a left parenthesis followed by an ordered pair of numeric input fields separated by a comma (if the decimal edit mode is POINT) or semicolon (if the decimal edit mode is COMMA), and followed by a right parenthesis.
那么输入必须是 (1., 12.)
.
您正在尝试读取 复数 数字(A
是复数)!因此,您应该为代码指定复数……因为您只提供一个整数,程序不知道该怎么做。
提供 (1,0)
和 (2,0)
而不是 1
和 2
就可以了。
如果用户输入始终是真实的,并且您想将其读入复杂类型数组,您可以这样做:
Print *, 'Enter row', i, 'and column', j, 'of matrix A'
read *, dummy
A(i,j)=dummy
其中 dummy
声明为 real
。这将使用户无需键入复数所需的括号。 (自动转换为复数)