使用 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]]
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP