In this article let’s learn how to make a grid for computing a Mandelbrot set with an outer product in Numpy using Python.
numpy.outer() method:
In Python, the numpy.outer() method is used to acquire the outer product of an array and a vector of elements. In linear algebra, a matrix is the outer product of two coordinate vectors. The m*n matrix is the outer product of two vectors having dimensions of n and m. A tensor is defined as the outer product of two tensors (multidimensional arrays of numbers). The tensor product, often known as the outer product of tensors, defines tensor algebra. To put it another way, the outer product is the sum of all the elements of the first and second vectors. A Mandelbrot series is a set of complex numbers with a very twisted fractal boundary when plotted.
Example:
Input: A = [A0, A1, A2, A3..., AM] and B = [B0, B1, B2, B3 ..., BN]
Output:
[[A0*B0 A0*B1 A0*B2 A0*B3... A0*BN ] [A1*B0 A1*B1 A1*B2 A1*B3... A1*BN ] [ ... ... ... ... ... ] [AM*B0 AM*B1 AM*B2 ... AM*BN ]]
Syntax : numpy.outer(a, b, out=None)
Parameters:
- a: (M,) array_like object. The initial input vector. If the input is not already 1-dimensional, it is flattened.
- b: (N,) array_like object. Second vector of input. If the input is not already 1-dimensional, it is flattened.
- out: (M, N) ndarray, optional value. The location where the outcome is saved
Return: out (M, N) ndarray. result is out[i, j] = a[i] * b[j].
Example 1
In Python, use the numpy.outer() method to get the Outer product of two arrays. The numpy. ones() function is used to create an array of ones of the specified type and shape. The numpy.linspace() function returns numbers that are evenly spaced over a given interval. To create a Mandelbrot set we need to create an imaginary part, and imaginary parts and a complex number set is created by combining them.
Python3
# importing import numpy as np # Real number is created using the # np.outer method real_part = np.outer(np.ones(( 3 ,)), np.linspace( 1 , 3 , 3 )) print ( "The real part " ) print (real_part) # imaginary number is created using the # np.outer method imaginary_part = np.outer( 1j * np.linspace( 3 , 6 , 3 ), np.ones(( 3 ,))) print ( "The imaginary part" ) print (imaginary_part) # Forming a grid by combainaing rael # and imaginary part mandelbrot_grid = real_part + imaginary_part print ( "Mandelbrot grid" ) print (mandelbrot_grid) |
Output:
The real part [[1. 2. 3.] [1. 2. 3.] [1. 2. 3.]] The imaginary part [[0.+3.j 0.+3.j 0.+3.j ] [0.+4.5j 0.+4.5j 0.+4.5j] [0.+6.j 0.+6.j 0.+6.j ]] Mandelbrot grid [[1.+3.j 2.+3.j 3.+3.j ] [1.+4.5j 2.+4.5j 3.+4.5j] [1.+6.j 2.+6.j 3.+6.j ]]
Example 2
In this example, we used numpy.linspace(3, 6, 3) function with a range starting from 3 and ending with 6, whereas 3 is the No. of samples to generate, then it will returns numbers that are evenly spaced over a given interval. To create a Mandelbrot set we need to create an imaginary part, and imaginary parts and a complex number set is created by combining them.
Python3
# importing import numpy as np # Real number is created using the # np.outer method real_part = np.outer(np.ones(( 3 ,)), np.linspace( 3 , 6 , 3 )) print ( "The real part " ) print (real_part) # imaginary number is created using # the np.outer method imaginary_part = np.outer( 1j * np.linspace( 6 , 9 , 3 ), np.ones(( 3 ,))) print ( "The imaginary part" ) print (imaginary_part) # Forming a grid by combainaing real # and imaginary part mandelbrot_grid = real_part + imaginary_part print ( "Mandelbrot grid" ) print (mandelbrot_grid) |
Output:
The real part [[3. 4.5 6. ] [3. 4.5 6. ] [3. 4.5 6. ]] The imaginary part [[0.+6.j 0.+6.j 0.+6.j ] [0.+7.5j 0.+7.5j 0.+7.5j] [0.+9.j 0.+9.j 0.+9.j ]] Mandelbrot grid [[3. +6.j 4.5+6.j 6. +6.j ] [3. +7.5j 4.5+7.5j 6. +7.5j] [3. +9.j 4.5+9.j 6. +9.j ]]