/* string representation of polynomial with coefficients
   in c and indeterminate x.

   Uses bob:h.string.string
*/

#ifndef _string_string
#include bob:string.string
#endif

#ifndef _string_polystr
#define _string_polystr 1
#endif

poly2str(c,x)
{
 local n,i,prev,s,a;
 n = sizeof(c);
 prev = FALSE;
 s = "";
 for ( i = 0; i < n; i++)
   if (a = c[i])
   {
    s += coeff(a,prev,i)+monom(x,i);
    prev = TRUE;
   }
 return (prev)?s:"0";
}

coeff(a,prev,deg)
{
 local s;
 s = (prev && a>0)?"+":"";
 switch (a)
 {
  case 1:
    if (deg == 0) s += "1";
    break;
  case -1:
    s += (deg == 0)?"-1":"-";
    break;
  default:
    s += string(a,10);
    break;
 }
 return s;
}
 
monom(x,deg)
{
 local s;
 switch (deg)
 {
  case 0:
    s = "";
    break;
  case 1:
    s = x;
    break;
  default:
    s = x+"^"+string(deg,10);
    break;
 }
 return s;
}
