function c = monomial_symmetrize ( d, b, c )

%*****************************************************************************80
%
%% monomial_symmetrize() symmetrizes the coefficients of equivalent monomials.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    06 November 2022
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer d: the dimension.
%
%    integer b: the base.
%
%    real c(b^d): the coefficients.
%
%  Output:
%
%    real c2(b^d): the symmetrized coefficients.
%
  n = b^d;
%
%  Use a sorted copy Z of vector A as a key.
%
  c2 = zeros ( 1, n );
  z = [];
  more = false;
  j = 0;

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Data gathered to representatives and averaged:\n' );
  fprintf ( 1, '\n' );

  while ( true )
%
%  Compute the next Z value.
%
    [ z, more ] = vector_representative_next ( d, b, z, more );

    if ( ~ more )
      break
    end
%
%  Generate all vectors A whose sorted value is Z.
%  Gather those C values into one vector C2.
%
    j = j + 1;
    mult = 0;
    a = z;
    more2 = false;
    while ( true )
      mult = mult + 1;
      i = vector_rank ( d, b, a );
      c2(j) = c2(j) + c(i);
      [ a, more2 ] = vector_equivalent_next ( a );
      if ( ~ more2 )
        break;
      end
    end
%
%  Average C2 by number of contributors.
%
    c2(j) = c2(j) / mult;

    fprintf ( 1, '  #%2d  mult=%d  C2=%g, [ %d, %d, %d ]\n', j, mult, c2(j), z );
%
%  Scatter averaged value back to contributors.
%
    a = z;
    more2 = false;
    while ( true )
      i = vector_rank ( d, b, a );
      c(i) = c2(j);
      [ a, more2 ] = vector_equivalent_next ( a );
      if ( ~ more2 )
        break;
      end
    end

  end

  return
end

