summaryrefslogtreecommitdiff
path: root/numpy/lib/polynomial.py
diff options
context:
space:
mode:
authorPauli Virtanen <pav@iki.fi>2009-04-24 21:17:10 +0000
committerPauli Virtanen <pav@iki.fi>2009-04-24 21:17:10 +0000
commit0d5e8c8e97e3075b1ccb2c257c998b63fad80903 (patch)
tree6e88c8b3e2436db28129bc8aa84d7920e2b3bf1d /numpy/lib/polynomial.py
parent1a613e1c460d2e7756724b7e5c638c95519ee498 (diff)
downloadnumpy-0d5e8c8e97e3075b1ccb2c257c998b63fad80903.tar.gz
Fix formatting of complex coefficients in poly1d
Diffstat (limited to 'numpy/lib/polynomial.py')
-rw-r--r--numpy/lib/polynomial.py24
1 files changed, 17 insertions, 7 deletions
diff --git a/numpy/lib/polynomial.py b/numpy/lib/polynomial.py
index fee498757..6a2b5f18a 100644
--- a/numpy/lib/polynomial.py
+++ b/numpy/lib/polynomial.py
@@ -15,6 +15,7 @@ from numpy.lib.getlimits import finfo
from numpy.lib.twodim_base import diag, vander
from numpy.lib.shape_base import hstack, atleast_1d
from numpy.lib.function_base import trim_zeros, sort_complex
+from numpy.lib.type_check import iscomplex, real, imag
from numpy.linalg import eigvals, lstsq
class RankWarning(UserWarning):
@@ -891,10 +892,21 @@ class poly1d(object):
coeffs = self.coeffs[NX.logical_or.accumulate(self.coeffs != 0)]
N = len(coeffs)-1
+ def fmt_float(q):
+ s = '%.4g' % q
+ if s.endswith('.0000'):
+ s = s[:-5]
+ return s
+
for k in range(len(coeffs)):
- coefstr ='%.4g' % abs(coeffs[k])
- if coefstr[-4:] == '0000':
- coefstr = coefstr[:-5]
+ if not iscomplex(coeffs[k]):
+ coefstr = fmt_float(real(coeffs[k]))
+ elif real(coeffs[k]) == 0:
+ coefstr = '%sj' % fmt_float(imag(coeffs[k]))
+ else:
+ coefstr = '(%s + %sj)' % (fmt_float(real(coeffs[k])),
+ fmt_float(imag(coeffs[k])))
+
power = (N-k)
if power == 0:
if coefstr != '0':
@@ -921,12 +933,10 @@ class poly1d(object):
if k > 0:
if newstr != '':
- if coeffs[k] < 0:
- thestr = "%s - %s" % (thestr, newstr)
+ if newstr.startswith('-'):
+ thestr = "%s - %s" % (thestr, newstr[1:])
else:
thestr = "%s + %s" % (thestr, newstr)
- elif (k == 0) and (newstr != '') and (coeffs[k] < 0):
- thestr = "-%s" % (newstr,)
else:
thestr = newstr
return _raise_power(thestr)