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
Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read