[Numpy-svn] r3949 - in trunk/numpy/lib: . tests
numpy-svn at scipy.org
numpy-svn at scipy.org
Mon Aug 6 08:43:16 EDT 2007
Author: stefan
Date: 2007-08-06 07:42:58 -0500 (Mon, 06 Aug 2007)
New Revision: 3949
Modified:
trunk/numpy/lib/polynomial.py
trunk/numpy/lib/tests/test_polynomial.py
Log:
Fix string conversion of polynomial when leading coefficients are
zero. Closes ticket #564.
Modified: trunk/numpy/lib/polynomial.py
===================================================================
--- trunk/numpy/lib/polynomial.py 2007-08-06 09:51:47 UTC (rev 3948)
+++ trunk/numpy/lib/polynomial.py 2007-08-06 12:42:58 UTC (rev 3949)
@@ -500,11 +500,15 @@
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 @@
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
Modified: trunk/numpy/lib/tests/test_polynomial.py
===================================================================
--- trunk/numpy/lib/tests/test_polynomial.py 2007-08-06 09:51:47 UTC (rev 3948)
+++ trunk/numpy/lib/tests/test_polynomial.py 2007-08-06 12:42:58 UTC (rev 3949)
@@ -82,5 +82,17 @@
def check_roots(self):
assert_array_equal(N.roots([1,0,0]), [0,0])
+ def check_str_leading_zeros(self):
+ p = N.poly1d([4,3,2,1])
+ p[3] = 0
+ assert_equal(str(p),
+ " 2\n"
+ "3 x + 2 x + 1")
+
+ p = N.poly1d([1,2])
+ p[0] = 0
+ p[1] = 0
+ assert_equal(str(p), " \n0")
+
if __name__ == "__main__":
NumpyTest().run()
More information about the Numpy-svn
mailing list