function [ l, d ] = r8mat_ldlt_factor ( a )

%*****************************************************************************80
%
%% r8mat_ldlt_factor computes the LDLT factors of a symmetric matrix.
%
%  Discussion:
%
%    The LDLT factorization is a unit lower triangular matrix L,
%    and diagonal matrix D such that:
%
%      A = L * D * L'
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    05 December 2019
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer N, the number of rows and columns of the matrix A.
%
%    real A(N,N), the matrix.
%
%  Output:
%
%    real L(N,N), D(N,N), the factors.
%
  [ m, n ] = size ( a );

  if ( m ~= n )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'r8mat_ldlt_factor - Fatal error!\n' );
    fprintf ( 1, '  The input matrix is not square.\n' );
    error ( 'r8mat_ldlt_factor - Fatal error!' );
  end

  if ( any ( any ( a ~= a' ) ) )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'r8mat_ldlt_factor - Fatal error!\n' );
    fprintf ( 1, '  The input matrix is not symmetric.\n' );
    error ( 'r8mat_ldlt_factor - Fatal error!' );
  end

  l = eye ( n, n );
  d = zeros ( n, 1 );

  for i = 1 : n
    d(i) = a(i,i) - sum ( l(i,1:i-1).^2 .* d(1:i-1,1)' );
    for j = i + 1 : n
      l(j,i) = ( a(j,i) - sum ( l(j,1:i-1) .* l(i,1:i-1) .* d(1:i-1,1)' ) ) / d(i);
    end

  end

  d = diag ( d );

  return
end
