[pypy-commit] pypy numpypy-complex2: Modified represention of complex numbers

jbs noreply at buildbot.pypy.org
Sat Jun 23 13:14:59 CEST 2012


Author: Jasper Schulz <jasper.schulz at student.hpi.uni-potsdam.de>
Branch: numpypy-complex2
Changeset: r55777:c8767c039027
Date: 2012-06-23 13:10 +0200
http://bitbucket.org/pypy/pypy/changeset/c8767c039027/

Log:	Modified represention of complex numbers

	So that ``repr(complex(0,1)) == '1j'`` (and not ``(0+1j)``)

diff --git a/pypy/module/micronumpy/test/test_dtypes.py b/pypy/module/micronumpy/test/test_dtypes.py
--- a/pypy/module/micronumpy/test/test_dtypes.py
+++ b/pypy/module/micronumpy/test/test_dtypes.py
@@ -439,6 +439,16 @@
         assert c.imag == 2
         assert repr(c) == '(1+2j)'
 
+        c = numpy.complex128(complex(0, 1))
+        assert c.real == 0
+        assert c.imag == 1
+        assert repr(c) == '1j'
+
+        c = numpy.complex128(complex(1, 0))
+        assert c.real == 1
+        assert c.imag == 0
+        assert repr(c) == '(1+0j)'
+
     def test_complex_dtype(self):
         import _numpypy as numpy
 
diff --git a/pypy/module/micronumpy/types.py b/pypy/module/micronumpy/types.py
--- a/pypy/module/micronumpy/types.py
+++ b/pypy/module/micronumpy/types.py
@@ -936,9 +936,12 @@
         return w_obj
 
     def str_format(self, box):
+        imag_str = str_format(box.imag) + 'j'
+        if box.real == 0:
+            return imag_str
+        
         real_str = str_format(box.real)
-        imag_str = str_format(box.imag)
-        return ''.join(['(', real_str, '+', imag_str, 'j', ')'])
+        return ''.join(['(', real_str, '+', imag_str, ')'])
 
 
 NonNativeComplex128 = Complex128


More information about the pypy-commit mailing list