基于MATLAB的LDL分解法

时间:2021-10-01 05:52:11

Method

When $\textbf{A}=\textbf{U}^T\textbf{D}\textbf{U}=\textbf{L}\textbf{D}\textbf{L}^T,$ like Cholesky factorization, we have:
    $$d_1=a_{11}$$
    $$g_{ij}=a_{ij}-\sum_{k=1}^{j-1}g_{ik}l_{jk}, (j=1,\dots,i-1)$$
    $$l_{ij}=\frac{g_{ij}}{d_j},( j=1,\dots,i-1,  i=2,\dots,n)$$
    $$d_I=a_{ii}-\sum_{k=1}^{i-1}g_{ik}l_{ik},( i=2,\dots,n)$$
    Using these three functions to realize LDL factorization.
function [l,d]=IDIT(A) 
    dim=size(A); 
    n=dim(1); 
    for i=1:n 
        for j=1:n            
            if A(i,j)~=A(j,i) 
                error('The input matrix should be a Symteric Matrix!,see IDIT.m') 
            end 
        end 
    end 

    l=eye(n); 
    d=zeros(n); 
    g=zeros(n); 
    d(1,1)=A(1,1); 
    for i=2:n 
        for j=1:i-1 
            %calculate g(i,j) 
            temp=0; 
            for k=1:j-1 
                temp=temp+g(i,k)*l(j,k); 
            end 
             g(i,j)=A(i,j)-temp; 
            %calculate l(i,j) 
            l(i,j)=g(i,j)/d(j,j); 
        end %loop of j 
        %calculate d(i,i) 
        temp=0; 
        for k=1:i-1 
            temp=temp+g(i,k)*l(i,k); 
        end 
        d(i,i)=A(i,i)-temp; 
    end %loop of i 
end