function [L,D]=ldlt6_5(A);
%%   function [L,D]=ldlt6_5(A);
%%
%%   LDL^t factorization Algorithm 6.5 for symmetric matrices if
%%         one exists, as is the case when A is positive definite
%%   INPUT:
%%     A - a symmetric square matrix
%%
%%   OUTPUT:
%%     L - a unit lower tiangular matrix
%%     D - a diagonal matrix
%%         so that A=L*D*L^t
%%     Message that no factorization exists if diagonal would contain zeros

%%Determine the size of the system
n=length(A(:,1));
d=zeros(1,n);
v=zeros(1,n);
L=eye(n);
flag=0;
%%STEP 1
if  flag==0
  for i=1:n;
%%%%%Step 2 and  Step 3 
     if i==1
           d(1)=A(1,1);
           if d(1)==0;
             flag=1;
           end;
         else
           v(1:i-1)=L(i,1:i-1).*d(1:i-1);
           d(i)=A(i,i)-sum((L(i,1:i-1).*v(1:i-1))');
           if d(i)==0;
             flag=1;
           end;
     end   
%%%%Step 4 
     if i<n  
        for j=i+1:n;
           L(j,i)=(A(j,i)-sum((L(j,1:i-1).*v(1:i-1))'))/d(i);
        end;
     end;
   end;
end;

if flag==0;
   D=diag(d);
else
   L='no decomposition exists';
   D=' ';
end;
