[Numpy-discussion] [SciPy-User] Saving Complex Numbers

Pauli Virtanen pav@iki...
Fri Jul 16 03:46:33 CDT 2010


Thu, 15 Jul 2010 20:00:09 -0400, David Warde-Farley wrote:

> (CCing NumPy-discussion where this really belongs)
> 
> On 2010-07-08, at 1:34 PM, cfrazer@uci.edu wrote:
> 
>> Need Complex numbers in the saved file.
> 
> Ack, this has come up several times according to list archives and no
> one's been able to provide a real answer.
> 
> It seems that there is nearly no formatting support for complex numbers
> in Python. for a single value, "{0.real:.18e}{0.imag:+.18e}".format(val)
> will get the job done, but because of the way numpy.savetxt creates its
> format string this isn't a trivial fix.
> 
> Anyone else have ideas on how complex number format strings can be
> elegantly incorporated in savetxt?

The bug here is that

	>>> z = np.complex64(1+2j)
	>>> '%.18e' % z
	'1.000000000000000000e+00'
	>>> float(np.complex64(1+2j))
	1.0

This should raise an exception. I guess the culprit is at 
scalarmathmodule.c.src:1023.

It should at least be changed to raise a ComplexWarning, which we now 
anyway do in casting.

-- 
Pauli Virtanen



More information about the NumPy-Discussion mailing list