It is quite common situation when complex floating point matrices are really filled with either real or imaginary numbers. Sometimes they are even integer (but intermediate numbers are not. And anyway there is no complex integer type). Probably there is a smart solution, without formatting everything ''by hand''?
Example: with (as close as possible to what is needed) combination
numpy.around(numpy.real_if_close(numpy.array([[1.0j,1.0],[0.0,1.0j]],complex)))
I get
array([[ 0.+1.j, -1.+0.j],
[ 0.+0.j, 0.+1.j]])
Desired output is
array([[ 1j, -1 ],
[ 0 , 1j]])
repr
function or just an outside function. The problem is tricky suppression of zeroes during this print. The logic behind it is obvious, but quite wordy - Misha 2012-04-04 06:13
>>> import numpy
>>> a = numpy.around(numpy.real_if_close(numpy.array([[1.0j,0.0],[0.0,1.0j]],complex)))
>>> a
array([[ 0.+1.j, 0.+0.j],
[ 0.+0.j, 0.+1.j]])
>>> [[c if c.imag else c.real for c in b] for b in a]
[[1j, 0.0], [0.0, 1j]]
I am not sure of a built in way to do it.
Edit: the suggestion to subclass numpy's array is a good one, that might be the easiest way. Otherwise you can cast just grab everything from the array and put it in a list, but that doesn't print 0 like you want it(it prints 0j)
>>> [[c for c in b] for b in a]
[[1j, 0j], [0j, 1j]]