Complex numbers help us work with values that have a real and an imaginary part. They are useful in many science and engineering problems.
Complex number type in NumPy
numpy.complex64 numpy.complex128 # Create a complex number array: np.array([1+2j, 3+4j], dtype=np.complex64)
Use complex64 for single precision complex numbers (real and imaginary parts are 32-bit floats).
Use complex128 for double precision complex numbers (real and imaginary parts are 64-bit floats).
import numpy as np z = np.array([1+2j, 3+4j], dtype=np.complex64) print(z)
z = np.complex128(5 + 6j) print(z)
z = np.array([1, 2, 3]) + 1j * np.array([4, 5, 6]) print(z)
This program creates an array of complex numbers, then finds their magnitudes and angles. Magnitude tells how far the number is from zero, and angle tells the direction.
import numpy as np # Create a complex array z = np.array([2+3j, 4+5j, 6+7j], dtype=np.complex128) # Calculate the magnitude (absolute value) of each complex number magnitudes = np.abs(z) # Calculate the angle (phase) of each complex number angles = np.angle(z) print("Complex numbers:", z) print("Magnitudes:", magnitudes) print("Angles (radians):", angles)
Complex numbers in numpy always have a real and imaginary part stored as floats.
Use np.abs() to get the magnitude and np.angle() to get the angle of complex numbers.
Complex numbers can be used in many math functions like addition, multiplication, and more.
Complex numbers have a real and an imaginary part.
Use numpy's complex64 or complex128 to work with complex numbers.
You can find magnitude and angle using np.abs() and np.angle().
Practice
numpy.complex128 data type represent?Solution
Step 1: Understand the data type name
The namecomplex128indicates a complex number type with 128 bits total.Step 2: Recall complex number structure in numpy
In numpy,complex128stores two 64-bit floats: one for real and one for imaginary parts.Final Answer:
A complex number with double precision for real and imaginary parts -> Option DQuick Check:
complex128 = complex number with 128 bits [OK]
- Confusing complex128 with real number types
- Thinking complex128 stores integers
- Assuming complex128 is a string type
complex64?Solution
Step 1: Check the array elements
The elements must be complex numbers, so they need an imaginary part like1+2j.Step 2: Check the dtype argument
Usingdtype='complex64'correctly sets the array to complex numbers with 64-bit precision.Final Answer:
np.array([1+2j, 3+4j], dtype='complex64') -> Option AQuick Check:
Complex elements + complex64 dtype = correct array [OK]
- Using integer dtype with complex numbers
- Missing imaginary part in elements
- Using invalid dtype strings
import numpy as np z = np.array([3+4j, 1-1j], dtype=np.complex128) magnitudes = np.abs(z) print(magnitudes)
Solution
Step 1: Understand np.abs on complex array
np.abs returns the magnitude (distance from origin) of each complex number.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.Final Answer:
[5. 1.41421356] -> Option AQuick Check:
Magnitude = sqrt(real² + imag²) [OK]
- Confusing magnitude with real or imaginary parts
- Expecting complex output instead of float magnitudes
- Using np.angle instead of np.abs
import numpy as np z = np.array([1+2j, 3+4j], dtype='complex64') angle = np.angle(z, deg=True) print(angle)
Solution
Step 1: Check dtype compatibility
np.angle works with complex arrays of any valid complex dtype including complex64.Step 2: Check np.angle parameters
The parameterdeg=Trueis valid and returns angle in degrees.Final Answer:
No error; code runs fine -> Option BQuick Check:
np.angle supports complex64 and deg=True [OK]
- Assuming complex64 is unsupported by np.angle
- Thinking deg=True is invalid
- Expecting import error for np.angle
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?Solution
Step 1: Calculate magnitudes with np.abs
np.abs(z) returns an array of magnitudes as floats.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.Final Answer:
np.where(np.abs(z) == 0, 'zero', np.abs(z)) -> Option CQuick Check:
Use np.where for conditional replacement in arrays [OK]
- Trying to use .replace on numpy arrays (not supported)
- Converting all to strings unnecessarily
- Using list comprehension without numpy benefits
Ready to prove it?
Pass the test and this pattern moves unknown
