Thanks to numba.vectorize in recent versions of numba, creating a numpy universal function for the task is very easy:

@numba.vectorize([numba.float64(numba.complex128),numba.float32(numba.complex64)])
def abs2(x):
    return x.real**2 + x.imag**2

On my machine, I find a threefold speedup compared to a pure-numpy version that creates intermediate arrays:

>>> x = np.random.randn(10000).view('c16')
>>> y = abs2(x)
>>> np.all(y == x.real**2 + x.imag**2)   # exactly equal, being the same operation
True
>>> %timeit np.abs(x)**2
10000 loops, best of 3: 81.4 µs per loop
>>> %timeit x.real**2 + x.imag**2
100000 loops, best of 3: 12.7 µs per loop
>>> %timeit abs2(x)
100000 loops, best of 3: 4.6 µs per loop
Answer from burnpanck on Stack Overflow
🌐
Medium
medium.com › @whyamit404 › working-with-complex-numbers-in-numpy-c3eae8876a88
Working with Complex Numbers in NumPy | by whyamit404 | Medium
February 8, 2025 - import numpy as np # Applying a function that doesn't support complex numbers complex_num = 3 + 4j # Trying to calculate square root (will raise an error in some cases) result = np.sqrt(complex_num) print("Result:", result)
🌐
GeeksforGeeks
geeksforgeeks.org › python › finding-magnitude-of-a-complex-number-in-python
Finding Magnitude of a Complex Number in Python - GeeksforGeeks
July 23, 2025 - Below, are the ways to Finding Magnitude of a Complex Number in Python. ... We can manually calculate the magnitude by taking the square root of the sum of the squares of the real and imaginary parts.
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.square.html
numpy.square — NumPy v2.2 Manual
Return the element-wise square of the input · A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple (possible only as a keyword argument) must have length equal to ...
🌐
Medium
medium.com › @amit25173 › what-is-numpy-square-and-when-to-use-it-20528b14ac86
What is numpy.square and When to Use It? | by Amit Yadav | Medium
February 9, 2025 - Here’s the good news — numpy.square automatically converts them into positive squares. Think of it as a friendly tool that always gives you the magnitude of squared values, no matter the sign.
Top answer
1 of 2
4

"Maximum" is ambiguous when it comes to complex values. The complex value itself doesn't have a min or max. Which is greater, 1+0j or 0+1j? What about 1+0j and 0+.5j? The answer to these questions determines what exactly what you want to do.

You can get the maximum real part (max(1+0j, 0+10j) == 1), maximum imaginary part (max(10+0j, 0+1j) == 1), maximum absolute value (max(10+0j, 0+1j) == 10, max(1+0j, 0+10j) == 10), complex value with the maximum real part (max(1+0j, 0+10j) == 1+0j), complex value with the maximum imaginary part (max(10+0j, 0+1j) == 0+1j), or complex value with the maximum absolute value (max(10+0j, 0+1j) == 10+0j, max(1+0j, 0+10j) == 0+10j). All are possible with numpy arrays.

The default in numpy (arr.max() or np.max(arr) when arr is complex) is the complex value with the maximum real part.

import numpy as np

arr = np.random.random(1000)+np.random.random(1000)*1j  # generate example data

maxreal = arr.real.max()  # maximum real part
maximag = arr.imag.max()  # maximum imaginary part
maxabs = np.abs(arr).max()  # maximum absolute value

maxcompreal = arr[arr.real.argmax()]  # complex value with maximum real part
maxcomp = arr.max()  # complex value with maximum real part, same as above
maxcompimag = arr[arr.imag.argmax()]  # complex value with maximum imaginary part
maxcompabs = arr[np.abs(arr).argmax()]  # complex value with maximum absolute value
2 of 2
0

Complex numbers don't have an ordering. But you can calculate the squared magnitude where is the real part of the complex number and is its imaginary part (with the imaginary unit removed). Scan your array and find the largest squared magnitude. You might be able to use the built in function max, possibly with a custom ordering function.

🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.exp.html
numpy.exp — NumPy v2.5.dev0 Manual
>>> x = np.linspace(-2*np.pi, 2*np.pi, 100) >>> xx = x + 1j * x[:, np.newaxis] # a + ib over complex plane >>> out = np.exp(xx) >>> plt.subplot(121) >>> plt.imshow(np.abs(out), ... extent=[-2*np.pi, 2*np.pi, -2*np.pi, 2*np.pi], cmap='gray') >>> plt.title('Magnitude of exp(x)')
🌐
GitHub
github.com › numpy › numpy › issues › 3994
abs() is slow for complex, add abs2() · Issue #3994 · numpy/numpy
October 29, 2013 - In [1]: import numpy as np In [2]: b = np.random.rand(500, 500) + 1j * np.random.rand(500, 500) In [3]: %timeit np.sqrt(b.real**2 + b.imag**2) 100 loops, best of 3: 4.15 ms per loop In [4]: %timeit np.abs(b) 100 loops, best of 3: 6.06 ms per loop
Author   Nodd
Find elsewhere
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.absolute.html
numpy.absolute — NumPy v2.5.dev0 Manual
An ndarray containing the absolute value of each element in x. For complex input, a + ib, the absolute value is \(\sqrt{ a^2 + b^2 }\).
🌐
Codepointtech
codepointtech.com › home › numpy angle & absolute: unlock complex number power
NumPy Angle & Absolute: Unlock Complex Number Power - codepointtech.com
January 18, 2026 - For a complex number z = a + bj, its magnitude (often denoted as |z|) is calculated using the Pythagorean theorem: sqrt(a^2 + b^2). It’s essentially the length of the vector from the origin to the point representing the complex number. The syntax for numpy.absolute() is straightforward:
🌐
Answermind
answermind.blog › numpy-magnitude-complex-number-guide
How to Find NumPy Magnitude of a Complex Number: Quick Guide - Answermind.blog
Solving for Magnitude: To find |z|, we take the square root of both sides: ... This brings us to the fundamental formula for calculating the magnitude of any complex number.
🌐
SciPy
docs.scipy.org › doc › numpy-1.12.0 › reference › generated › numpy.absolute.html
numpy.absolute — NumPy v1.12 Manual
Plot the function over the complex plane: >>> xx = x + 1j * x[:, np.newaxis] >>> plt.imshow(np.abs(xx), extent=[-10, 10, -10, 10]) >>> plt.show() (png, pdf) numpy.square · numpy.fabs · © Copyright 2008-2009, The Scipy community. Last updated on Jan 16, 2017.
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.angle.html
numpy.angle — NumPy v2.1 Manual
Changed in version 1.16.0: This function works on subclasses of ndarray like ma.array. ... This function passes the imaginary and real parts of the argument to arctan2 to compute the result; consequently, it follows the convention of arctan2 when the magnitude of the argument ...
🌐
Pythontutorials
pythontutorials.net › blog › numpy-magnitude-of-complex-number
Understanding the Numpy Magnitude of Complex Numbers | PythonTutorials.net
June 21, 2025 - Here, we calculate the magnitude squared of the complex wave function, which gives us the probability density. When working with large arrays of complex numbers, it’s important to consider memory usage. numpy arrays are stored in a contiguous block of memory, which is more memory - efficient ...
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.absolute.html
numpy.absolute — NumPy v2.4 Manual
An ndarray containing the absolute value of each element in x. For complex input, a + ib, the absolute value is \(\sqrt{ a^2 + b^2 }\).
🌐
TutorialsPoint
tutorialspoint.com › calculate-the-absolute-value-of-complex-numbers-in-numpy
Calculate the absolute value of complex numbers in Numpy
February 8, 2022 - import numpy as np # Create an array with complex type using the array() method arr = np.array([56.+0.j, 27.+0.j, 68.+0.j, 49.+0.j, 120.+0.j,3 + 4.j]) # Display the array print("Array... ", arr) # Get the type of the array print(" Our Array type... ", arr.dtype) # Get the dimensions of the Array print(" Our Array Dimension...
🌐
SciPy
docs.scipy.org › doc › numpy-1.11.0 › reference › generated › numpy.absolute.html
numpy.absolute — NumPy v1.11 Manual
May 29, 2016 - Plot the function over the complex plane: >>> xx = x + 1j * x[:, np.newaxis] >>> plt.imshow(np.abs(xx), extent=[-10, 10, -10, 10]) >>> plt.show() (png, pdf) numpy.square · numpy.fabs · © Copyright 2008-2009, The Scipy community. Last updated on May 29, 2016.
🌐
Real Python
realpython.com › python-complex-numbers
Simplify Complex Numbers With Python – Real Python
October 21, 2023 - You might remember from an earlier section that a complex number multiplied by its conjugate produces its magnitude squared.
🌐
Learning About Electronics
learningaboutelectronics.com › Articles › Complex-numbers-in-Python.php
Complex Numbers in Python
So you can see that we have the complex number, 3+4j · Using the abs() function, we get the output, 5.0, which is the magnitude of the complex number.