diff options
Diffstat (limited to 'numpy/lib')
-rw-r--r-- | numpy/lib/polynomial.py | 24 | ||||
-rw-r--r-- | numpy/lib/tests/test_polynomial.py | 9 |
2 files changed, 26 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) diff --git a/numpy/lib/tests/test_polynomial.py b/numpy/lib/tests/test_polynomial.py index 45e68d248..85fc18933 100644 --- a/numpy/lib/tests/test_polynomial.py +++ b/numpy/lib/tests/test_polynomial.py @@ -14,6 +14,15 @@ poly1d([ 3., 2., 1.]) >>> print q 2 3 x + 2 x + 1 +>>> print poly1d([1.89999+2j, -3j, -5.12345678, 2+1j]) + 3 2 +(1.9 + 2j) x - 3j x - 5.123 x + (2 + 1j) +>>> print poly1d([100e-90, 1.234567e-9j+3, -1234.999e8]) + 2 +1e-88 x + (3 + 1.235e-09j) x - 1.235e+11 +>>> print poly1d([-3, -2, -1]) + 2 +-3 x - 2 x - 1 >>> p(0) 3.0 |