Make grid for computing a Mandelbrot set with outer product using NumPy in Python
Last Updated :
25 Jul, 2022
In this article let's learn how to make a grid for computing a Mandelbrot set with an outer product in Numpy using Python.
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 ]]
Similar Reads
Compute the inner product of vectors for 1-D arrays using NumPy in Python
Python has a popular package called NumPy which used to perform complex calculations on 1-D and multi-dimensional arrays. To find the inner product of two arrays, we can use the inner() function of the NumPy package. Syntax: numpy.inner(array1, array2) Parameters:Â array1, array2: arrays to be evalu
2 min read
Compute the inner product of vectors for 1-D arrays using NumPy in Python
Python has a popular package called NumPy which used to perform complex calculations on 1-D and multi-dimensional arrays. To find the inner product of two arrays, we can use the inner() function of the NumPy package. Syntax: numpy.inner(array1, array2) Parameters:Â array1, array2: arrays to be evalu
2 min read
Get the Outer Product of an array with vector of letters using NumPy in Python
In this article let's see how to get the outer product of an array with a vector of letters in Python. numpy.outer() method The numpy.outer() method is used to get the outer product of an array with a vector of elements in Python. A matrix is the outer product of two coordinate vectors in linear alg
4 min read
Vector outer product with Einstein summation convention using NumPy in Python
In this article, we will find vector outer product with Einstein summation convention in Python. numpy.einsum() method The numpy.einsum() method from the NumPy library is used to find the vector outer product with the Einstein summation convention in Python. Many common multi-dimensional, linear al
3 min read
How to compute the cross product of two given vectors using NumPy?
Let's see the program to compute the cross product of two given vectors using NumPy. For finding the cross product of two given vectors we are using numpy.cross() function of NumPy library.Syntax: numpy.cross(a, b, axisa=-1, axisb=-1, axisc=-1, axis=None)[Return: cross product of two (arrays of) vec
1 min read
Compute the inverse sine with scimath using NumPy in Python
In this article, we will cover how to compute the inverse sine with scimath in Python. np.emath.arcsin method A NumPy array can be created in different ways like, by various numbers, and by defining the size of the Array. It can also be created with the use of various data types such as lists, tuple
2 min read
Evaluate 3-D Hermite series on the Cartesian product of x, y and z using NumPy in Python
In this article, we will discuss how to Evaluate a 3-D Hermite series on the Cartesian product of x, y, and z  in Python and NumPy. NumPy.polynomial.hermite.hermgrid3d method Hermite polynomials are significant in approximation theory because the Hermite nodes are used as matching points for optimiz
3 min read
Compute the roots of a Legendre series in Python-NumPy
In this article, we will discuss how to compute the roots of a Legendre series in python. legendre.legroots method In python, the Legendre module provides many functions like legdre to perform arithmetic, and calculus operations on the Legendre series. It is one of the functions provided by the Lege
2 min read
Evaluate 2-D Hermite series on the Cartesian product of x and y with 1d array of coefficient using NumPy in Python
In this article, we will discuss how to Evaluate a 2-D Hermite series on the Cartesian product of x and y with a 1d array of coefficients in Python using NumPy. NumPy.polynomial.hermite.hermgrid2d method Hermite polynomials are significant in approximation theory because the Hermite nodes are used a
3 min read
Python Program to Get dot product of multidimensional Vectors using NumPy
Given two multidimensional Vectors, the task is to write a Python program to get the dot product of two multidimensional Vectors using NumPy.Example: Lets take 2 vectors a = [2,5,3] and b = [6,3,1]Dot Product(ab) = (a[0] * b[0])+ (a[1] * b[1]) + (a[2] * b[2]) = (2*6)+ (5*3) + (3*1) = 30Dot Product i
3 min read