A comprehensive tutorial for NumPy:

Tutorial for NumPy data types:

**What are Complex Numbers?**

Complex numbers consist of two parts:

**Real Part**: The number you’re familiar with, like 3 or -5.**Imaginary Part**: This is a number multiplied by the imaginary unit (usually denoted as`i`

in mathematics, but`j`

in Python).

In essence, a complex number looks like this: `a + bj`

, where `a`

is the real part and `b`

is the imaginary part.

**NumPy’s Complex Data Types**

In NumPy, we predominantly deal with two types of complex numbers:

`complex64`

: Uses 32 bits each for the real and imaginary parts.`complex128`

: Uses 64 bits each for the real and imaginary parts.**This is common.**

**Creating Complex Numbers in NumPy**

If you’ve come across `np.complex`

in older resources or examples, be aware that this has been **deprecated** since NumPy version 1.20.

Instead, you should use the built-in

function, as shown above. If you specifically want the NumPy scalar type, opt for **complex**`np.complex128`

:

```
z1 = complex(3, 2)
z2 = complex(1, 7)
```

```
z1 = np.complex128(3 + 2j)
z2 = np.complex128(1 + 7j)
```

**In this article, we mainly use the latter method, np.complex().**

**Basic Operations with Complex Numbers**

With NumPy, you can perform a variety of operations on complex numbers:

**Addition**

```
z1 = np.complex128(3 + 2j)
z2 = np.complex128(1 + 7j)
result = z1 + z2
print(result) # Output: (4+9j)
```

**Subtraction**

```
z1 = np.complex128(3 + 2j)
z2 = np.complex128(1 + 7j)
result = z1 - z2
print(result) # Output: (2-5j)
```

**Multiplication**

```
z1 = np.complex128(3 + 2j)
z2 = np.complex128(1 + 7j)
result = z1 * z2
print(result) # Output: (-11+23j)
```

**Division**

```
z1 = np.complex128(3 + 2j)
z2 = np.complex128(1 + 7j)
result = z1 / z2
print(result) # Output: (0.3793103448275862-0.3103448275862069j)
```

**Attributes of Complex Numbers**

Complex numbers in NumPy come with handy attributes:

**Real Part**:`print(z1.real) # Output: 3.0`

**Imaginary Part**:`print(z1.imag) # Output: 2.0`

**Conjugate**:`python print(z1.conjugate()) # Output: (3-2j)`

**Magnitude and Phase**

Understanding the magnitude (or absolute value) and phase (or angle) is crucial when dealing with complex numbers:

**Magnitude**:`magnitude = np.abs(z1) print(magnitude) # Output: 3.605551275463989`

**Phase**:`python phase = np.angle(z1) print(phase) # Output: 0.5880026035475675 (in radians)`

A comprehensive tutorial for NumPy:

Tutorial for NumPy data types: