In this article, we will learn about how to write python program to swap two complex numbers with examples.
Getting Started
The task is to swap two complex numbers using python programming. For example,
Let’s say a = 65.34 + 3.2j and b = 1.32 + 2.1j, then output should be a = 1.32 + 2.1j and b = 65.34 + 3.2j.
There are multiple ways to achieve above tasks.
- Using a Temporary Variable
- Using Arithmetic Operations
- Using Tuple Packing and Unpacking
- Using map Function
1. Using a Temporary Variable
Using third variable, we can write python program to swap two complex numbers as shown below –
def swap_complex_numbers(a, b): temp_real = a.real # store the real part of a in temp_real. temp_imag = a.imag # store the imaginary part of a in temp_imag. a = b.real # store the real part of b in a. a += 1j * b.imag # store the imaginary part of b in a. b = temp_real + 1j * temp_imag # store the real and imaginary parts of a in b. return a, b # Example usage a = 5.4 + 2.3j b = 10.6 + 7.1j print("Before swap: a =", a, "b =", b) a, b = swap_complex_numbers(a, b) print("After swap: a =", a, "b =", b)
Here,
- There are two complex variables a and b which we want to swap or interchange.
- temp_real and temp_imag are two temporary variables which are being used to store value of a temporarily.
Output:
Before swap: a = (5.4+2.3j) b = (10.6+7.1j) After swap: a = (10.6+7.1j) b = (5.4+2.3j)
2. Using Arithmetic Operations
In previous example, we treated complex number as two parts. Now, what if we want to treat it as single unit?
We can do so and use subtraction – and addition i.e. + operations to swap two numbers.
Pseudo code –
- Define two complex variables a and b.
- Then, find sum of a and b and store sum in a.
- Then subtract b from a and store the difference in b.
- Then, subtract b from a and store in a.
- Now, value of a and b are swapped value.
Program –
def swap_complex_numbers(a, b): a = a + b # Take the sum of a and b and store it in a. b = a - b # Subtract b from the updated a and store in b. a = a - b # Subtract the new b from the updated a and store in a. return a, b # Example usage num1 = 5.04 + 2.3j num2 = 6.01 + 4.5j print("Before swap: num1 =", num1, "num2 =", num2) num1, num2 = swap_complex_numbers(num1, num2) print("After swap: num1 =", num1, "num2 =", num2)
Output:
Before swap: num1 = (5.04+2.3j) num2 = (6.01+4.5j) After swap: num1 = (6.01+4.5j) num2 = (5.04+2.3j)
3. Using Tuple Packing and Unpacking
We can also use techniques of tuple packing and unpacking to write python program to swap two complex numbers with examples as shown below –
This is simplest way to interchange two complex numbers in python.
def swap_complex_numbers(a, b): return b, a # Example usage num1 = 5.32 + 2.1j num2 = 6.43 + 3.7j print("Before swap: num1 =", num1, "num2 =", num2) num1, num2 = swap_complex_numbers(num1, num2) print("After swap: num1 =", num1, "num2 =", num2)
In above code, we are just interchanging the position and return the values.
Output
Before swap: num1 = (5.32+2.1j) num2 = (6.43+3.7j) After swap: num1 = (6.43+3.7j) num2 = (5.32+2.1j)
5. Using map Function
We can also use map function in python to interchange or swap two complex numbers in python as shown below –
def swap_complex_numbers(a, b): a_real, a_imag = a.real, a.imag b_real, b_imag = b.real, b.imag a = b_real + 1j * b_imag b = a_real + 1j * a_imag return a, b # Example usage num1 = 65.34 + 3.2j num2 = 1.32 + 2.1j print("Before swap: num1 =", num1, "num2 =", num2) num1, num2 = map(lambda x: num2 if x == num1 else num1, (num1, num2)) print("After swap: num1 =", num1, "num2 =", num2)
Here,
We use the map function with a lambda function. Then, we compare each number with num1 and replaces it with num2 if it matches, and vice versa.
Output:
Before swap: num1 = (65.34+3.2j) num2 = (1.32+2.1j) After swap: num1 = (1.32+2.1j) num2 = (65.34+3.2j)
That’s how we can swap two complex numbers in python with examples. Learn more about python at official documentation