degree of non-expanded polynomials

Richard B. Kreckel kreckel at thep.physik.uni-mainz.de
Wed Mar 6 15:48:05 CET 2002


Hi,

On Wed, 6 Mar 2002, Alessandro Zaccagnini wrote:
> I have been experimenting with GiNaC, and I found that on page 46,
> section 
> 5.5.2, of the manual (version 1.0.6) that degree() works reliably also 
> on non-expanded polynomials. But the following program seems to prove 
> that it is not the case. 
> 
> #include <iostream>
> #include <ginac/ginac.h>
> 
> using namespace std;
> using namespace GiNaC;
> 
> typedef GiNaC::ex GExpr;
> typedef GiNaC::symbol GSymbol;
> 
> int main() {
>   GSymbol x("x");
> 
>   GExpr h_a = numeric(1,2) * x * (x-1);
>   GExpr h_b = - (x+1) * (x-1);
>   GExpr h_c = numeric(1,2) * x * (x+1);
> 
>   GExpr p = h_a + h_b + h_c;
>   std::cout << "p = " << p << "  degree " << p.degree(x) << std::endl;
>   p = p.expand();
>   std::cout << "p = " << p << "  degree " << p.degree(x) << std::endl;
>   return 0;
> }
> 
> This is the output that I obtained (from version 1.0.5):
> 
> p = 1/2*(1+x)*x+1/2*(-1+x)*x+(-1+x)*(-1-x)  degree 2
> p = 1  degree 0

Thanks for pointing this out.  Our documentation is wrong and will be
changed.  There is no way to cleverly find out the degree() or ldegree()
of unexpanded/uncollected expressions because there might be cancellations
of the highest/lowest terms.  (x-1)^2-x^2 is a simple example, where x^2
is cancelled in the expanded polynomial.  Thus, degree() and ldegree()
only give upper/lower bounds.  This has always been the intent of degree()
and ldegree() -- sorry for the confusion.  I just checked against Maple
and there it is the same.  Their documentation honors this, though:

: The polynomial a must be in collected form in order for degree/ldegree to
: return an accurate result. For example, given (x+1)*(x+2) - x^2, degree
: would not detect the cancellation of the leading term, and would incorrectly
: return a result of 2. Applying the function collect or expand to the
: polynomial before calling degree avoids this problem. 

Regards
    -richy.
-- 
Richard B. Kreckel
<Richard.Kreckel at Uni-Mainz.DE>
<http://wwwthep.physik.uni-mainz.de/~kreckel/>





More information about the GiNaC-list mailing list