Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

สร้างตารางสำหรับคำนวณชุด Mandelbrot ด้วยผลิตภัณฑ์ภายนอกใน Python


จากเวกเตอร์สองเวกเตอร์ a =[a0, a1, ..., aM] และ b =[b0, b1, ..., bN] ผลคูณภายนอก [1] คือ −

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

ในการรับผลิตภัณฑ์ Outer ของสองอาร์เรย์ ให้ใช้เมธอด numpy.outer() ใน Python numpy.ones() ส่งคืนอาร์เรย์ใหม่ของรูปร่างและประเภทที่กำหนด ซึ่งเต็มไปด้วยอาร์เรย์ 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]]