Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Make a grid for computing a Mandelbrot set with outer product in Python
The Mandelbrot set is a famous fractal that requires computing complex numbers on a 2D grid. To create this grid efficiently, we can use NumPy's outer product to combine real and imaginary components.
Understanding the Outer Product
Given two vectors, a = [a0, a1, ..., aM] and b = [b0, b1, ..., bN], the outer product is −
[[a0*b0 a0*b1 ... a0*bN ] [a1*b0 . . ] [ ... . . ] [aM*b0 ... aM*bN ]]
Creating the Complex Grid
To build a Mandelbrot grid, we need to create a 2D array of complex numbers where each point represents coordinates in the complex plane −
import numpy as np
# Create the real part using outer product
# np.ones creates a column vector, np.linspace creates the x-axis values
real_part = np.outer(np.ones((5,)), np.linspace(-2, 2, 5))
print("Real part of the grid:")
print(real_part)
Real part of the grid: [[-2. -1. 0. 1. 2.] [-2. -1. 0. 1. 2.] [-2. -1. 0. 1. 2.] [-2. -1. 0. 1. 2.] [-2. -1. 0. 1. 2.]]
Creating the Imaginary Component
import numpy as np
# Create the imaginary part
# 1j multiplies by imaginary unit, creating y-axis values
imaginary_part = np.outer(1j * np.linspace(2, -2, 5), np.ones((5,)))
print("Imaginary part of the grid:")
print(imaginary_part)
Imaginary part of the grid: [[0.+2.j 0.+2.j 0.+2.j 0.+2.j 0.+2.j] [0.+1.j 0.+1.j 0.+1.j 0.+1.j 0.+1.j] [0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j] [0.-1.j 0.-1.j 0.-1.j 0.-1.j 0.-1.j] [0.-2.j 0.-2.j 0.-2.j 0.-2.j 0.-2.j]]
Complete Mandelbrot Grid
Combining both components creates a grid of complex numbers suitable for Mandelbrot set computation −
import numpy as np
# Create real and imaginary components
real_part = np.outer(np.ones((5,)), np.linspace(-2, 2, 5))
imaginary_part = np.outer(1j * np.linspace(2, -2, 5), np.ones((5,)))
# Combine to form the complex grid
mandelbrot_grid = real_part + imaginary_part
print("Complete Mandelbrot grid:")
print(mandelbrot_grid)
print("\nGrid shape:", mandelbrot_grid.shape)
print("Data type:", mandelbrot_grid.dtype)
Complete Mandelbrot grid: [[-2.+2.j -1.+2.j 0.+2.j 1.+2.j 2.+2.j] [-2.+1.j -1.+1.j 0.+1.j 1.+1.j 2.+1.j] [-2.+0.j -1.+0.j 0.+0.j 1.+0.j 2.+0.j] [-2.-1.j -1.-1.j 0.-1.j 1.-1.j 2.-1.j] [-2.-2.j -1.-2.j 0.-2.j 1.-2.j 2.-2.j]] Grid shape: (5, 5) Data type: complex128
How It Works
-
np.outer()computes the outer product of two arrays -
np.ones((5,))creates a vector of ones for broadcasting -
np.linspace(-2, 2, 5)generates evenly spaced values -
1jis Python's imaginary unit notation - Adding real and imaginary parts creates complex numbers
Conclusion
Using NumPy's outer product efficiently creates a complex grid for Mandelbrot set computation. This approach leverages broadcasting to generate coordinate matrices without explicit loops, making it both memory-efficient and computationally fast.
