function [ o2, c2, e2 ] = polynomial_compress ( o, c, e )

%*****************************************************************************80
%
%% polynomial_compress() compresses a polynomial.
%
%  Discussion:
%
%    The function polynomial_sort() should be called first, or else
%    the E1 vector should be in ascending sorted order.
%
%    Thanks to Meritt Reynolds for suggesting changes that ensure that
%    the output of this function has the dimension O2, instead of including
%    a trailing list of suppressed values,
%    28 November 2015.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    28 November 2015
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer O, the "order" of the polynomial.
%
%    real C(O), the coefficients of the polynomial.
%
%    integer E(O), the indices of the exponents of the polynomial.
%
%  Output:
%
%    integer O2, the "order" of the compressed polynomial.
%
%    real C2(O2,1), the coefficients of the compressed polynomial.
%
%    integer E2(O2,1), the indices of the exponents of the 
%    compressed polynomial.
%

%
%  Destroy all row vectors!
%
  c = c(:);
  e = e(:);

  r8_epsilon_sqrt = sqrt ( eps );

  c2 = zeros ( o, 1 );
  e2 = zeros ( o, 1 );
%
%  Add coefficients with the same exponent.
%
  get = 0;
  put = 0;

  while ( get < o )

    get = get + 1;

    if ( 0 == put )

      put = put + 1;
      c2(put) = c(get);
      e2(put) = e(get);

    else

      if ( e2(put) == e(get) )
        c2(put) = c2(put) + c(get);
      else
        put = put + 1;
        c2(put) = c(get);
        e2(put) = e(get);
      end

    end

  end

  o = put;
%
%  Clear out zeros and tiny coefficients.
%
  get = 0;
  put = 0;

  while ( get < o )

    get = get + 1;

    if ( r8_epsilon_sqrt < abs ( c2(get) ) )
      put = put + 1;
      c2(put) = c2(get);
      e2(put) = e2(get);
    end

  end

  o2 = put;
%
%  Reduce arrays to minimal size (some other functions catenate
%  the entire array causing errors as unused coefficients are called
%  into action)
%
  c2 = c2(1:o2);
  e2 = e2(1:o2);

  return
end

