Python numpy.outer () 函数:计算向量外积用法与示例


发布日期 : 2021-09-04 10:39:50 UTC

访问量: 10 次浏览

Python numpy.outer() 函数

numpy.outer() 函数计算两个向量的外积。

语法:

numpy.outer(a, b, out = None)

参数 :

a :[array_like] 第一个输入向量。如果输入不是一维的,就会被扁平化。

b :[array_like] 第二个输入向量。如果输入不是一维的,则被扁平化。

out :[ndarray, optional] 一个存储结果的位置。

返回 :[ndarray] 返回两个向量的外积。 out[i, j] = a[i] * b[j]

代码#1:

# Python program explaining
# numpy.outer() function
   
# importing numpy as geek 
import numpy as geek 
  
a = geek.ones(4)
b = geek.linspace(-1, 2, 4)
  
gfg = geek.outer(a, b)
  
print (gfg)

输出 :

[[-1.  0.  1.  2.]
 [-1.  0.  1.  2.]
 [-1.  0.  1.  2.]
 [-1.  0.  1.  2.]]

代码#2:

# Python program explaining
# numpy.outer() function
   
# importing numpy as geek 
import numpy as geek 
  
a = geek.ones(5)
b = geek.linspace(-2, 2, 5)
  
gfg = geek.outer(a, b)
  
print (gfg)

输出 :

[[-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.]]