In this tutorial, we will look at how to get the absolute value (also called the magnitude) of a complex number in Python with the help of some examples.

In mathematics, the absolute value (or the magnitude) of the complex number `a + bj`

is defined as the euclidean norm. The below image illustrates its calculation.

## How to get the absolute value of a complex number in Python?

You can use the Python built-in `abs()`

function or the `numpy.abs()`

function to calculate the absolute value (or magnitude) of a complex number in Python.

The following is the syntax –

# using abs() function abs(complex_num) # using numpy.abs() function - make sure to import numpy numpy.abs(complex_num)

Pass the complex number as an argument to the above functions and it will return its magnitude.

In this tutorial, we will look at the following methods to get the absolute value of a complex number –

- Using the Python built-in
`abs()`

function. - Using the
`numpy.abs()`

function. - Creating our own custom function to compute the absolute value of a complex number.

## 3 Methods to get the magnitude of a complex number in Python

Let’s now look at the usage of the methods mentioned above with the help of some examples.

### Method 1 – Use the Python built-in `abs()`

function

The Python built-in `abs()`

function is used to get the absolute value of a number. You can also apply this function to a complex number to get its absolute value.

Let’s look at an example. We’ll apply the `abs()`

function to three values – a real number, a complex number with non-zero real and imaginary parts, and a complex number with only an imaginary part (an imaginary number).

# absolute value (or magnitude) of complex number using abs() print(abs(12)) print(abs(3+4j)) print(abs(-5j))

Output:

12 5.0 5.0

We get the magnitude for each of the above numbers correctly.

### Method 2 – Use the `numpy.abs()`

function

Alternatively, you can also use the `numpy.abs()`

function (alias for the `numpy.absolute()`

function) to get the absolute value of a complex number. Additionally, you can apply this function on a Numpy array as well to get the element-wise absolute values in the array.

Let’s now apply this function to the same values as in the above example.

import numpy as np # absolute value (or magnitude) of complex number using np.abs() print(np.abs(12)) print(np.abs(3+4j)) print(np.abs(-5j))

Output:

12 5.0 5.0

We get the correct value for the magnitude (same as what we got in the example above).

### Method 3 – Write a custom function to calculate the absolute value of a complex number

We know how to mathematically calculate the absolute value of a complex number. We can use this knowledge to write a custom function to return the absolute value for a complex number.

Let’s build the custom function and apply it to the same inputs used in the above examples.

# absolute value (or magnitude) of complex number using custom function def get_abs_value(num): # get the real part of a+bj a = np.real(num) # get the imaginary part of a+bj b = np.imag(num) # return the absolute value return np.sqrt(a**2 + b**2) print(get_abs_value(12)) print(get_abs_value(3+4j)) print(get_abs_value(-5j))

Output:

12.0 5.0 5.0

We get the same result as above.

In the custom function created above, you can see that we’re using the `numpy.real()`

function to extract the real part of the complex number and the `numpy.imag()`

function to extract its imaginary part. We’re then computing its magnitude by taking the square root of the sum of squares of the real part and the imaginary part.

Note that the above custom function is only to show you how the magnitude of a complex number is calculated. It is recommended that you use the built-in functions shown above – `abs()`

or `numpy.abs()`

as they are better equipped to handle edge cases and are the optimal implementations for calculating the absolute value.

## Summary

In this tutorial, we looked at how to calculate the absolute value of a complex number in Python using the Python built-in `abs()`

function and the `numpy.abs()`

function.

You might also be interested in –

- Numpy – Get the Complex Conjugate
- Numpy – Get the Real Part of a Complex Number
- Numpy – Get the Imaginary Part of a Complex Number
- Numpy – Check If a Number is Real
- Numpy – Check If a Number is Complex

**Subscribe to our newsletter for more informative guides and tutorials. ****We do not spam and you can opt out any time.**