1

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]])
```

@Joel Cornett Not sure that I understand your proposal. It does not matter for me whether this will be

`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
3

```
>>> 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]]
```

If I try to print matrix [[1.j,1.],[0.,1.j] I get [[1j, (1+0j)], [0, 1j]]. And this won't work with negative elements. However, it is a good idea to use smartness of print function itself - Misha 2012-04-04 06:07

I had just realized that this has problems with negatives, I believe that this solution works for what you need - Nolen Royalty 2012-04-04 06:09

reprfunction - Joel Cornett 2012-04-04 05:58