CoCoA:HowTo:Round Coefficients

From ApCoCoAWiki


CoCoA computes polynomials either Q or in Z/(p). In case you have polynomials in Q and you do some computations with them, after some time their coefficients probably look quite ugly (ToDo: Add Example). To get a feeling for the coefficients, it may be quite handy to get a rounded value instead of these loooong fractions. Their is no code inside CoCoA to do this automatically, but you can do it quite easy, using CoCoA's FloatApprox or FloatStr functions.

First version: Converting to string

 P := x^2 + 11/10 y + 1/3 z;
 S := Sum(Flatten([ ['( ',FloatStr(LC(M)),'*',Sprint(LT(M)),') +'] | M In Monomials(P)]));

code by dheldt, 23 Jun 2005 (CEST)

Second version: Only rounding coefficients

 P := x^2 + 11/10 y + 1/3 z;
 S := Sum([ FloatApprox(LC(M),1/10^3)LT(M) | M In Monomials(P)]);

code by dheldt, 23 Jun 2005 (CEST)

Third version: Using DecimalStr()

Define PolyStr(Poly,MultSymb)

 T := Sum(Flatten(
      [[SignStr(LC(M))," " ,DecimalStr(Abs(LC(M))),MultSymb ,Sprint(LT(M)), " "] |
        M In Monomials(Poly)]));
 If (T[1] IsIn "+")=True Then
   T := Sum([T[I] | I In 2..Len(T)]);
 Return T;


Code by Matthias Machnik, 26 April 2008


Output First Version:

( 1.000000000*10^0*x^2) +( 1.100000000*10^0*y) +( 3.333333333*10^(-1)*z) +

Output Second Version:

x^2 + 11/10y + 3333/10000z

Calling Third Version with: PolyStr(P,Ascii(183));

1·x^2 + 1.1·y + 0.333·z