function r = toeplitz_cholesky_upper ( n, a )

%*****************************************************************************80
%
%% toeplitz_cholesky_upper() computes the upper Cholesky factor of a Toeplitz matrix.
%
%  Discussion:
%
%    The Toeplitz matrix must be positive semi-definite.
%
%    After factorization, A = R' * R.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    13 November 2012
%
%  Author:
%
%    Michael Stewart
%
%  Reference:
%
%    Michael Stewart,
%    Cholesky factorization of semi-definite Toeplitz matrices.
%    Linear Algebra and its Applications,
%    Volume 254, pages 497-525, 1997.
%
%  Input:
%
%    integer N, the order of the matrix.
%
%    real A(N,N), the Toeplitz matrix.
%
%  Output:
%
%    real R(N,N), the upper Cholesky factor.
%
  g = zeros ( 2, n );
  g(1,1:n) = a(1,1:n);
  g(2,1) = 0.0;
  g(2,2:n) = a(2:n,1)'; 

  r = zeros ( n, n );
  r(1,1:n) = g(1,1:n);
  g(1,2:n) = g(1,1:n-1);
  g(1,1) = 0.0;
  for i = 2 : n
    rho = - g(2,i) / g(1,i);
    A = [ 1.0, rho; ... 
          rho, 1.0 ];
    g(:,i:n) = ( A * g(:,i:n) ) / sqrt ( ( 1.0 - rho ) * ( 1.0 + rho ) );
    r(i,i:n) = g(1,i:n);
    g(1,i+1:n) = g(1,i:n-1);
    g(1,i) = 0.0;
  end

  return
end

