content() inconsistencies
Ralf Stephan
ralf at ark.in-berlin.de
Fri Jul 2 17:24:48 CEST 2004
> > cout << (10*x/2+ex(15)/3).content(x) << endl;
>
> But that's 5x+5...?
Oh well... yes. 15x/2+25/3 would be solved correctly by Pari.
> Anyway, content() and primpart() in GiNaC currently only work correctly for
> polynomials in Z[x]. But your Pari results are correct. The content of a
> polynomial in Q[x] should be calculated in a way that the primitive part is
> a polynomial in Z[x] with content 1.
While going through the loop computing the gcd of the coeff numerators,
one would simultaneously collect the lcm of the denominators, numeric
or otherwise, with the end result gcd divided by lcm.
I'm quite satisfied with the following patch. YMMV.
The outcommented code was rather obscure to me.
Thanks,
ralf
--- GiNaC-1.2.1-orig/ginac/normal.cpp Thu Jan 8 16:06:50 2004
+++ GiNaC-1.2.1/ginac/normal.cpp Fri Jul 2 16:51:17 2004
@@ -851,21 +851,25 @@
return _ex0;
// First, try the integer content
- ex c = e.integer_content();
- ex r = e / c;
- ex lcoeff = r.lcoeff(x);
- if (lcoeff.info(info_flags::integer))
- return c;
+// ex c = e.integer_content();
+// ex r = e / c;
+// ex lcoeff = r.lcoeff(x);
+// if (lcoeff.info(info_flags::integer))
+// return c;
// GCD of all coefficients
int deg = e.degree(x);
int ldeg = e.ldegree(x);
if (deg == ldeg)
return e.lcoeff(x) / e.unit(x);
- c = _ex0;
+ ex c = _ex0, l = 1;
for (int i=ldeg; i<=deg; i++)
- c = gcd(e.coeff(x, i), c, NULL, NULL, false);
- return c;
+ {
+ const ex& t = e.coeff(x,i).numer_denom();
+ c = gcd(t[0], c, NULL, NULL, false);
+ l = lcm(t[1], l);
+ }
+ return c/l;
}
More information about the GiNaC-devel
mailing list