Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Python에서 외적을 사용하여 Mandelbrot 집합을 계산하기 위한 그리드 만들기

<시간/>

두 벡터 a =[a0, a1, ..., aM] 및 b =[b0, b1, ..., bN]이 주어지면 외적 [1]은 -

[[a0*b0 a0*b1 ... a0*bN ]
[a1*b0 .
[ ... .
[aM*b0    aM*bN ]]

두 배열의 외부 곱을 얻으려면 Python에서 numpy.outer() 메서드를 사용합니다. numpy.ones()는 1로 채워진 주어진 모양과 유형의 새 배열을 반환합니다. numpy.linspace()는 지정된 간격 동안 균일한 간격의 숫자를 반환합니다.

단계

먼저 필요한 라이브러리를 가져옵니다 -

import numpy as np

실제 부분 -

rl = np.outer(np.ones((5,)), np.linspace(-2, 2, 5))
print("The real part of the complex number...\n",rl)

허수부 -

im = np.outer(1j*np.linspace(2, -2, 5), np.ones((5,)))
print("\nThe imaginary part of the complex numbers...\n",rl)

그리드 형성 -

grid = rl + im

예시

import numpy as np

# To get the Outer product of two arrays, use the numpy.outer() method in Python
# The numpy.ones() return a new array of given shape and type, filled with ones.
# The numpy.linspace() returns evenly spaced numbers over a specified interval.
# The real part
rl = np.outer(np.ones((5,)), np.linspace(-2, 2, 5))
print("The real part of the complex number...\n",rl)

# The imaginary part
im = np.outer(1j*np.linspace(2, -2, 5), np.ones((5,)))
print("\nThe imaginary part of the complex numbers...\n",rl)

# Forming a grid
grid = rl + im
print("\nDisplaying the grid...\n",grid)

출력

The real part of the complex number...
[[-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.]]

The imaginary part of the complex numbers...
[[-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.]]

Displaying the 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]]