I have a numpy array of complex numbers and need to create a new array with rounded real and imaginary parts where the rounding at half is either toward zero or toward infinity.
There are several recommendations on stackoverflow for using the decimal package which allows one to specify different types of rounding. For an array of complex numbers x
the following code worked, but was very slow:
rounded_array = np.array([
float(Decimal(x.real).quantize(0, rounding=ROUND_HALF_DOWN)) + 1j * \
float(Decimal(x.imag).quantize(0, rounding=ROUND_HALF_DOWNs)) for x in arr])
What are some simple but faster alternatives to this? This solution was suggested: How to always round up a XX.5 in numpy However, it applies only to real arrays and is much slower than the solutions suggested below.