[英]QR decomposition Fortran error
I have a problem with QR decomposition method. 我对QR分解方法有疑问。 I use dgeqrf subroutine for decomposition but there is no error in the compiler but it gives a problem after that.
我使用dgeqrf子例程进行分解,但编译器中没有错误,但此后出现了问题。 I haven't found where is the mistake.
我还没发现错误在哪里。 Another question is, A=Q*R=> if A matrix has zero, Can decomposition be zero or lose the rank.
另一个问题是,如果A矩阵为零,则A = Q * R =>可以分解为零或失去秩。
program decomposition
!CONTAINS
!subroutine Qrdecomposition(A_mat, R)
real,dimension(2,2) :: A_mat !real,dimension(2,2),intent(inout)
:: A_mat
real,dimension(2,2) :: R !real,dimension(2,2),intent(out)
:: R
real,dimension(2,2) :: A
integer :: M,N,LDA,LWORK,INFO
real,allocatable, dimension(:,:) :: TAU
real,allocatable, dimension(:,:) :: WORK
external dgeqrf
M=2
N=2
LDA=2
LWORK=2
INFO=0
A_mat(1,1)=4
A_mat(1,2)=1
A_mat(2,1)=3
A_mat(2,2)=1
A=A_mat
call dgeqrf(M,N,A,TAU,WORK,LWORK,INFO)
R=A
print *,R,WORK,LWORK
!end subroutine Qrdecomposition
end program decomposition
I see three mistakes in your code: 我在您的代码中看到三个错误:
1) You forgot the LDA
argument to dgeqrf
, 1)您忘记了
dgeqrf
的LDA
参数,
2) TAU
and WORK
must be explicitly allocated, 2)必须明确分配
TAU
和WORK
,
3) All arrays should be declared with double precision to be consistent with dgeqrf
interface: 3)所有数组都应以双精度声明以与
dgeqrf
接口保持一致:
program decomposition
!CONTAINS
!subroutine Qrdecomposition(A_mat, R)
! Note: using '8' for the kind parameter is not the best style but I'm doing it here for brevity.
real(8),dimension(2,2) :: A_mat !real,dimension(2,2),intent(inout)
real(8),dimension(2,2) :: R !real,dimension(2,2),intent(out)
real(8),dimension(2,2) :: A
integer :: M,N,LDA,LWORK,INFO
real(8),allocatable, dimension(:,:) :: TAU
real(8),allocatable, dimension(:,:) :: WORK
external dgeqrf
M=2
N=2
LDA=2
LWORK=2
INFO=0
A_mat(1,1)=4
A_mat(1,2)=1
A_mat(2,1)=3
A_mat(2,2)=1
A=A_mat
allocate(tau(M,N), work(M,N))
call dgeqrf(M,N,A,LDA,TAU,WORK,LWORK,INFO)
R=A
print *,R,WORK,LWORK
!end subroutine Qrdecomposition
end program decomposition
In certain situations, Fortran does perform automatic allocation of arrays, but it should generally not be counted on and it is not the case here. 在某些情况下,Fortran确实会执行数组的自动分配,但是通常不应该依靠它,在这里情况并非如此。
EDIT Point 3 was pointed out by roygvib, see below. roygvib指出了编辑点3,请参见下文。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.