diff options
author | Stefan van der Walt <stefan@sun.ac.za> | 2007-08-06 12:42:58 +0000 |
---|---|---|
committer | Stefan van der Walt <stefan@sun.ac.za> | 2007-08-06 12:42:58 +0000 |
commit | 5d4824522b29ac83d08d208723cba56ece1d6ea5 (patch) | |
tree | 2f8c9ef596b8321bbb4f65ce5d1807739d9db3ce /numpy/lib/polynomial.py | |
parent | 7a605b4d18a7d5283c5c3eb31b9258150e5755c5 (diff) | |
download | numpy-5d4824522b29ac83d08d208723cba56ece1d6ea5.tar.gz |
Fix string conversion of polynomial when leading coefficients are
zero. Closes ticket #564.
Diffstat (limited to 'numpy/lib/polynomial.py')
-rw-r--r-- | numpy/lib/polynomial.py | 14 |
1 files changed, 9 insertions, 5 deletions
diff --git a/numpy/lib/polynomial.py b/numpy/lib/polynomial.py index 63ffec629..da1908d93 100644 --- a/numpy/lib/polynomial.py +++ b/numpy/lib/polynomial.py @@ -500,11 +500,15 @@ class poly1d(object): return self.order def __str__(self): - N = self.order thestr = "0" var = self.variable - for k in range(len(self.coeffs)): - coefstr ='%.4g' % abs(self.coeffs[k]) + + # Remove leading zeros + coeffs = self.coeffs[NX.logical_or.accumulate(self.coeffs != 0)] + N = len(coeffs)-1 + + for k in range(len(coeffs)): + coefstr ='%.4g' % abs(coeffs[k]) if coefstr[-4:] == '0000': coefstr = coefstr[:-5] power = (N-k) @@ -533,11 +537,11 @@ class poly1d(object): if k > 0: if newstr != '': - if self.coeffs[k] < 0: + if coeffs[k] < 0: thestr = "%s - %s" % (thestr, newstr) else: thestr = "%s + %s" % (thestr, newstr) - elif (k == 0) and (newstr != '') and (self.coeffs[k] < 0): + elif (k == 0) and (newstr != '') and (coeffs[k] < 0): thestr = "-%s" % (newstr,) else: thestr = newstr |