0
0
NumPydata~10 mins

Complex number type in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Complex number type
Create complex number
Store in numpy array
Access real and imaginary parts
Perform operations (add, multiply)
Get magnitude and angle
Use results for analysis or visualization
This flow shows how to create, manipulate, and analyze complex numbers using numpy arrays step-by-step.
Execution Sample
NumPy
import numpy as np
z = np.array([1+2j, 3+4j])
real = z.real
imag = z.imag
mag = np.abs(z)
angle = np.angle(z)
Create a numpy array of complex numbers, then extract real, imaginary parts, magnitude, and angle.
Execution Table
StepCode LineVariableValueExplanation
1z = np.array([1+2j, 3+4j])z[1.+2.j 3.+4.j]Create numpy array with two complex numbers
2real = z.realreal[1. 3.]Extract real parts of each complex number
3imag = z.imagimag[2. 4.]Extract imaginary parts of each complex number
4mag = np.abs(z)mag[2.23606798 5.]Calculate magnitude (distance from origin)
5angle = np.angle(z)angle[1.10714872 0.92729522]Calculate angle (phase) in radians
6End--All variables computed successfully
💡 Execution ends after computing magnitude and angle for all complex numbers.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5Final
zundefined[1.+2.j 3.+4.j][1.+2.j 3.+4.j][1.+2.j 3.+4.j][1.+2.j 3.+4.j][1.+2.j 3.+4.j][1.+2.j 3.+4.j]
realundefinedundefined[1. 3.][1. 3.][1. 3.][1. 3.][1. 3.]
imagundefinedundefinedundefined[2. 4.][2. 4.][2. 4.][2. 4.]
magundefinedundefinedundefinedundefined[2.23606798 5.][2.23606798 5.][2.23606798 5.]
angleundefinedundefinedundefinedundefinedundefined[1.10714872 0.92729522][1.10714872 0.92729522]
Key Moments - 3 Insights
Why does z.real return an array of real parts instead of a single number?
Because z is an array of complex numbers, z.real returns the real part of each element, as shown in execution_table step 2.
What does np.abs(z) compute for complex numbers?
np.abs(z) computes the magnitude (distance from origin) of each complex number, not just the absolute value of real or imaginary parts, as seen in step 4.
Why is the angle returned in radians and not degrees?
np.angle returns the phase angle in radians by default, which is standard in numpy and math libraries, as shown in step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 3. What is the value of 'imag'?
A[1. 3.]
B[2.23606798 5.]
C[2. 4.]
D[1.+2.j 3.+4.j]
💡 Hint
Check the 'imag' variable value in the row for step 3 in the execution_table.
At which step does the variable 'mag' get its value?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look for when np.abs(z) is called in the execution_table.
If we change z to np.array([2+0j, 0+3j]), what would be the magnitude of the first element?
A3.0
B2.0
C0.0
D5.0
💡 Hint
Magnitude is the distance from origin, calculated as sqrt(real^2 + imag^2).
Concept Snapshot
Complex number type in numpy:
- Use np.array([a+bj, ...]) to create complex arrays
- Access real parts with .real, imaginary with .imag
- Use np.abs() for magnitude, np.angle() for phase
- Angles are in radians
- Useful for signal processing, physics, and engineering
Full Transcript
This lesson shows how to work with complex numbers in numpy. First, we create a numpy array with complex numbers. Then, we extract the real and imaginary parts separately. Next, we calculate the magnitude using np.abs, which gives the distance from zero in the complex plane. We also find the angle or phase using np.angle, which returns the angle in radians. These steps help analyze complex data in many fields. The execution table traces each step and variable value. Key moments clarify common confusions about array operations and angle units. The quiz tests understanding of variable values and calculations. The snapshot summarizes the main points for quick review.