错误编程FORTRAN 90中的Cholesky分解

时间:2014-03-05 20:48:26

标签: algorithm fortran90 gfortran

我正在努力研究关于波浪能设备的论文。由于我是FORTRAN 90的新手,我想提高我的编程技巧。因此,我刚从

中选择了一个例子

http://rosettacode.org/wiki/Cholesky_decomposition

并尝试实施主页中说明的内容。基本上它是编程一个3x3矩阵A的Cholesky分解。我知道已经有一些包为Fortran进行了分解,但我想亲自体验学习如何编程的工作。

编译时没有错误,但结果不匹配。尽管有元素L(3,3),我基本上找出了所有元素。附上,您可以在Fortran 90中找到我从头开始创建的代码:

Program Cholesky_decomp

implicit none
!size of the matrix
INTEGER, PARAMETER :: m=3 !rows
INTEGER, PARAMETER :: n=3 !cols
REAL, DIMENSION(m,n) :: A, L

REAL :: sum1, sum2
INTEGER i,j,k

! Assign values to the matrix
A(1,:)=(/ 25,  15,  -5 /)   
A(2,:)=(/ 15,  18,   0 /)  
A(3,:)=(/ -5,   0,  11 /)

! Initialize values
L(1,1)=sqrt(A(1,1))
L(2,1)=A(2,1)/L(1,1)
L(2,2)=sqrt(A(2,2)-L(2,1)*L(2,1))
L(3,1)=A(3,1)/L(1,1)

sum1=0
sum2=0
do i=1,n
    do k=1,i
        do j=1,k-1
        if (i==k) then
            sum1=sum1+(L(k,j)*L(k,j))
            L(k,k)=sqrt(A(k,k)-sum1)    
        elseif (i > k) then
            sum2=sum2+(L(i,j)*L(k,j))
            L(i,k)=(1/L(k,k))*(A(i,k)-sum2)
        else
            L(i,k)=0
        end if
        end do
    end do
end do

!write output
do i=1,m
    print "(3(1X,F6.1))",L(i,:)
end do

End program Cholesky_decomp

你能告诉我代码中的错误是什么吗?当L(3,3)= 3时,我得到L(3,3)= 0。我完全迷失了,只是为了记录:在Rosetta代码主页上没有fortran的解决方案,所以任何任何提示都会受到赞赏。

非常感谢你。

2 个答案:

答案 0 :(得分:1)

您希望sum1sum2循环的每次迭代都将ik设置为零。

答案 1 :(得分:0)

我终于找到了如何解决更大顺序,4x4矩阵等问题,如上面附带的链接所示。这是最终的代码:

Program Cholesky_decomp
!*************************************************!
!LBH @ ULPGC 06/03/2014
!Compute the Cholesky decomposition for a matrix A
!after the attached 
!http://rosettacode.org/wiki/Cholesky_decomposition
!note that the matrix A is complex since there might
!be values, where the sqrt has complex solutions.
!Here, only the real values are taken into account
!*************************************************! 
implicit none

INTEGER, PARAMETER :: m=3 !rows
INTEGER, PARAMETER :: n=3 !cols
COMPLEX, DIMENSION(m,n) :: A 
REAL, DIMENSION(m,n) :: L
REAL :: sum1, sum2
INTEGER i,j,k

! Assign values to the matrix
A(1,:)=(/ 25,  15,  -5 /)   
A(2,:)=(/ 15,  18,   0 /)  
A(3,:)=(/ -5,   0,  11 /)
!!!!!!!!!!!!another example!!!!!!!
!A(1,:) = (/ 18,  22,   54,   42 /) 
!A(2,:) = (/ 22,  70,   86,   62 /) 
!A(3,:) = (/ 54,  86,  174,  134 /) 
!A(4,:) = (/ 42,  62,  134,  106 /)





! Initialize values
L(1,1)=real(sqrt(A(1,1)))
L(2,1)=A(2,1)/L(1,1)
L(2,2)=real(sqrt(A(2,2)-L(2,1)*L(2,1)))
L(3,1)=A(3,1)/L(1,1)
!for greater order than m,n=3 add initial row value
!for instance if m,n=4 then add the following line
!L(4,1)=A(4,1)/L(1,1)





do i=1,n
    do k=1,i
        sum1=0
        sum2=0
        do j=1,k-1
        if (i==k) then
            sum1=sum1+(L(k,j)*L(k,j))
            L(k,k)=real(sqrt(A(k,k)-sum1))  
        elseif (i > k) then
            sum2=sum2+(L(i,j)*L(k,j))
            L(i,k)=(1/L(k,k))*(A(i,k)-sum2)
        else
            L(i,k)=0
        end if
        end do
    end do
end do

!write output
do i=1,m
    print "(3(1X,F6.1))",L(i,:)
end do

End program Cholesky_decomp

期待听到评论,更好的编程方法,更正和任何反馈。感谢2 francescalus如此迅速地回答!

问候,lbh