Numpy magnitude of complex number: Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.
Using a simple snippet, we can calculate the Magnitude of a Complex Number in Python.
Let us begin by learning the fundamentals of complex numbers in Python.
First, let’s look at how to initialize or declare a complex number in Python.
Complex Number:
How to find the magnitude of a complex number: By setting a variable to a + bi, we can generate a complex number. In this case, a and b are real numbers.
4+11i
In the above example, the real part(a) is 4 and the imaginary part(b) is 11.
The complex([real][,imaginary]) method can also be used to generate a complex number. Its parameters are the real and imaginary parts, respectively.
If a parameter is not specified, the complex number’s corresponding portion is set to the default value. The default setting is 0.
- Python NumPy dot() Function
- Python Program to Find Smallest Prime Divisor of a Number
- how to call a function in python Guide
Example:
cmplxnumber = complex(9,2)
In this application, we will utilize the abs() function to determine the magnitude of a complex number. The method abs() only accepts one argument.
The following is an example of an argument:
1. The Float Number
2. Complex Number
3. Integer
If the number is an integer or a floating-point, abs(number) returns
1. Modulus of the number.
2. The magnitude of the number if it is complex.
Examples:
Example1:
Input:
Given real part = 12 Given imaginary part = 16
Output:
The magnitude of the complex number (12+16j) = 20.0
Example2:
Input:
Given real part = 11 Given imaginary part = 47
Output:
The magnitude of the complex number (11+47j) = 48.27007354458868
Python Program to Find Magnitude of a Complex Number
Below are the ways to find the magnitude of a complex number in Python.
Method #1: Using abs Function (Static Input)
Approach:
- Give the real part and imaginary part of the complex number as static input and store it in two variables.
- Using a complex() function convert those two variables into a complex number.
- Calculate the magnitude of the complex number using the abs() function.
- Print the magnitude of the complex number.
- The Exit of the program.
Below is the implementation:
# Give the real part and imaginary part of the complex number # as static input and store it in two variables. realnumb = 12 imaginarynumb = 16 # Using a complex() function convert those two variables into a complex number. complexnumb = complex(realnumb, imaginarynumb) # Calculate the magnitude of the complex number using the abs() function. magcomplex = abs(complexnumb) # Print the magnitude of the complex number. print('The magnitude of the complex number', complexnumb, '=', magcomplex)
Output:
The magnitude of the complex number (12+16j) = 20.0
Method #2: Using abs Function (User Input)
Approach:
- Give the real part and imaginary part of the complex number as user input using map(), int(), split().
- Store it in two variables.
- Using a complex() function convert those two variables into a complex number.
- Calculate the magnitude of the complex number using the abs() function.
- Print the magnitude of the complex number.
- The Exit of the program.
Below is the implementation:
# Give the real part and imaginary part of the complex number as user input # using map(), int(), split(). # Store it in two variables. realnumb, imaginarynumb = map(int, input( 'Enter real part and complex part of the complex number = ').split()) # Using a complex() function convert those two variables into a complex number. complexnumb = complex(realnumb, imaginarynumb) # Calculate the magnitude of the complex number using the abs() function. magcomplex = abs(complexnumb) # Print the magnitude of the complex number. print('The magnitude of the complex number', complexnumb, '=', magcomplex)
Output:
Enter real part and complex part of the complex number = 11 47 The magnitude of the complex number (11+47j) = 48.27007354458868
Related Programs:
- python program to find the factorial of a number
- python program to find the factors of a number
- python program to find whether a number is a power of two
- python program to find all numbers in a range which are perfect squares and sum of all digits in the number is less than 10
- python program to find the power of a number using recursion
- python program to find smallest prime divisor of a number
- python program to compute the power of a number