Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Complex Integer
#1
Hi All,

I am trying certain things for my project, but I am stuck at few points. Any pointers on the below points will help.

Coming to doubts on Complex datatype:
1. Is there any other way to create a complex integer apart from complex(a,b)?
2. Since the above complex function only accepts a numeric or string value, do we have any other way to create a complex array like a 1-D array?
3. Apart from Numpy, is there any other library which can help in creating N-Dimensional array? Since, I see Numpy only supports complex double and float but not integer type.

Thanks for the help in advance.
Reply
#2
1. Yes, e.g. 3 + 2j. You might want to provide some context if that's not satisfactory.
2. You can use a list comprehension to map whatever you're starting with into complex numbers. Again, please provide more context if that's not sufficient.
3. I'm not familiar with that niche. If you elaborate on what your ultimate goal is, that would help with recommendations though. As far as Numpy not supporting complex integers, why does that matter? Integers are a subset of floats/doubles anyway.
Reply
#3
If you look at the implementation of Python complex data type, you can see that Python does not support
integer complex numbers at all. The underlying C-struct Py_complex is based on double (e.g. floating point) type.

number = 1 + 3j
type(number.real)
Output:
<class 'float'>
Anywhere, you can implement your own integer complex numbers based on integers. But the main question is still actual: If so famous (and used almost everywhere in Python computational world) package as Numpy is lacking of complex integers, are they really needed?
Reply
#4
def complex_round(complex_number):
    real = round(complex_number.real)
    imag = round(complex_number.imag)
    return complex(real, imag)
It would be nice, to round a complex directly, but this is not implemented in the built-in complex.
I guess it's mathematically not right to round a complex number.

But you can make a class and add the __round__ method.
class Complex(complex):
    def __round__(self, digits=0):
        return complex(round(self.real, digits), round(self.imag, digits))
Using this piece of code:

try:
    # trying first the built-in type complex
    round(complex(1.5, 3.5))
except TypeError as e:
    print(e)
c = Complex(42.1337, 1337.42)
print(round(c))
Output:
type complex doesn't define __round__ method (42+1337j)
By the way, I use complex numbers in real world applications.
If you dive into signal processing, you have to handle them, but rounding happens never.
If you have for example a signal, which could be just In-phase or a complex signal,
where the In-phase is the real part and the Quadrature is the imaginary part.
Doing a fft, results again into complex numbers.
To get the magnitude, you could use abs(), which is sqrt(i**2 + q**2), only positive numbers.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020