Bird
Raised Fist0
NumPydata~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the numpy.complex128 data type represent?
easy
A. A string representing a complex number
B. A real number with 128 bits precision
C. An integer number with 128 bits
D. A complex number with double precision for real and imaginary parts

Solution

  1. Step 1: Understand the data type name

    The name complex128 indicates a complex number type with 128 bits total.
  2. Step 2: Recall complex number structure in numpy

    In numpy, complex128 stores two 64-bit floats: one for real and one for imaginary parts.
  3. Final Answer:

    A complex number with double precision for real and imaginary parts -> Option D
  4. Quick Check:

    complex128 = complex number with 128 bits [OK]
Hint: Look for 'complex' and bit size meaning real+imag parts [OK]
Common Mistakes:
  • Confusing complex128 with real number types
  • Thinking complex128 stores integers
  • Assuming complex128 is a string type
2. Which of the following is the correct way to create a numpy array of complex numbers with type complex64?
easy
A. np.array([1+2j, 3+4j], dtype='complex64')
B. np.array([1+2j, 3+4j], dtype='int64')
C. np.array([1, 2, 3], dtype='complex64')
D. np.array([1+2, 3+4], dtype='complex64')

Solution

  1. Step 1: Check the array elements

    The elements must be complex numbers, so they need an imaginary part like 1+2j.
  2. Step 2: Check the dtype argument

    Using dtype='complex64' correctly sets the array to complex numbers with 64-bit precision.
  3. Final Answer:

    np.array([1+2j, 3+4j], dtype='complex64') -> Option A
  4. Quick Check:

    Complex elements + complex64 dtype = correct array [OK]
Hint: Use complex literals with 'j' and set dtype to complex64 [OK]
Common Mistakes:
  • Using integer dtype with complex numbers
  • Missing imaginary part in elements
  • Using invalid dtype strings
3. What is the output of this code?
import numpy as np
z = np.array([3+4j, 1-1j], dtype=np.complex128)
magnitudes = np.abs(z)
print(magnitudes)
medium
A. [5. 1.41421356]
B. [7.+0.j 0.+0.j]
C. [3.+4.j 1.-1.j]
D. [0.6 0.8]

Solution

  1. Step 1: Understand np.abs on complex array

    np.abs returns the magnitude (distance from origin) of each complex number.
  2. Step 2: Calculate magnitudes manually

    For 3+4j, magnitude = sqrt(3^2 + 4^2) = 5. For 1-1j, magnitude = sqrt(1^2 + (-1)^2) = sqrt(2) ≈ 1.41421356.
  3. Final Answer:

    [5. 1.41421356] -> Option A
  4. Quick Check:

    Magnitude = sqrt(real² + imag²) [OK]
Hint: Magnitude = sqrt(real² + imag²) using np.abs [OK]
Common Mistakes:
  • Confusing magnitude with real or imaginary parts
  • Expecting complex output instead of float magnitudes
  • Using np.angle instead of np.abs
4. The following code throws an error. What is the cause?
import numpy as np
z = np.array([1+2j, 3+4j], dtype='complex64')
angle = np.angle(z, deg=True)
print(angle)
medium
A. np.angle does not support degree output
B. No error; code runs fine
C. Missing import for np.angle function
D. dtype 'complex64' is invalid for np.angle

Solution

  1. Step 1: Check dtype compatibility

    np.angle works with complex arrays of any valid complex dtype including complex64.
  2. Step 2: Check np.angle parameters

    The parameter deg=True is valid and returns angle in degrees.
  3. Final Answer:

    No error; code runs fine -> Option B
  4. Quick Check:

    np.angle supports complex64 and deg=True [OK]
Hint: np.angle supports complex64 and degree output [OK]
Common Mistakes:
  • Assuming complex64 is unsupported by np.angle
  • Thinking deg=True is invalid
  • Expecting import error for np.angle
5. You have a numpy array z = np.array([1+1j, 0+0j, -1-1j], dtype=np.complex128). You want to create a new array that contains the magnitudes but replaces zeros with the string 'zero'. Which code correctly does this?
hard
A. np.array([str(m) if m == 0 else m for m in np.abs(z)])
B. np.abs(z).replace(0, 'zero')
C. np.where(np.abs(z) == 0, 'zero', np.abs(z))
D. np.abs(z).astype(str).replace('0.0', 'zero')

Solution

  1. Step 1: Calculate magnitudes with np.abs

    np.abs(z) returns an array of magnitudes as floats.
  2. Step 2: Replace zeros with 'zero' using np.where

    np.where(condition, value_if_true, value_if_false) replaces elements where magnitude is 0 with string 'zero', else keeps magnitude.
  3. Final Answer:

    np.where(np.abs(z) == 0, 'zero', np.abs(z)) -> Option C
  4. Quick Check:

    Use np.where for conditional replacement in arrays [OK]
Hint: Use np.where(condition, 'zero', values) for replacements [OK]
Common Mistakes:
  • Trying to use .replace on numpy arrays (not supported)
  • Converting all to strings unnecessarily
  • Using list comprehension without numpy benefits
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard