function A = monomial_to_legendre_matrix ( n )

%*****************************************************************************80
%
%% monomial_to_legendre_matrix(): convert monomial to Legendre basis.
%
%  Discussion:
%
%    If PM(x) is a linear combination of monomials
%    with coefficients CM, then PL(x) is a linear combination of
%    Legendre polynomials with coefficients CL = A * CM.
%    
%    Note that we assume the coefficients are ordered such that
%    the constant term is first.
%
%  Example:
%
%    N = 11 (each column must be divided by the underlying factor).
%
%       1     .      1     .      7     .    33    .   715    . 4199
%       .     1      .     3      .    27     .  143     . 3315    .
%       .     .      2     .     20     .   110    .  2600    .16150
%       .     .      .     2      .    28     .  182     . 4760    .
%       .     .      .     .      8     .    72    .  2160    .15504
%       .     .      .     .      .     8     .   88     . 2992    .
%       .     .      .     .      .     .    16    .   832    . 7904
%       .     .      .     .      .     .     .   16     .  960    .
%       .     .      .     .      .     .     .    .   128    . 2176
%       .     .      .     .      .     .     .    .     .  128    .
%       .     .      .     .      .     .     .    .     .    .  256
%
%      /1    /1     /3    /5    /35   /63  /231 /429 /6435/12155/46189  
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    19 February 2024
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer N, the order of the matrix.
%
%  Output:
%
%    real A(N,N), the matrix.
%
  A = zeros ( n, n );

  if ( n <= 0 )
    return
  end

  A(1,1) = 1.0;

  if ( n == 1 )
    return
  end

  A(2,2) = 1.0;

  if ( n == 2 )
    return
  end

  for j = 3 : n
    for i = 1 : n
      if ( i == 1 )

        A(i,j) = (     i     ) * A(i+1,j-1) / ( 2 * i + 1 );

      elseif ( i < n )

        A(i,j) = (     i - 1 ) * A(i-1,j-1) / ( 2 * i - 3 ) ...
               + (     i     ) * A(i+1,j-1) / ( 2 * i + 1 );

      else

        A(i,j) = (     i - 1 ) * A(i-1,j-1) / ( 2 * i - 3 );

      end
    end
  end

  return
end
