给定两个向量,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()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 #要获得两个数组的外积,请使用 Python 中的 numpy.outer() 方法 #numpy.ones() 返回一个给定形状和类型的新数组,填充为 1。 #numpy.linspace() 在指定的时间间隔内返回均匀分布的数字。 #真实的部分 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 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]]