在Python中使用NumPy将复数矩阵相乘
在这篇文章中,我们将讨论如何使用NumPy将两个含有复数的矩阵相乘,但首先,让我们知道什么是复数。复数是可以用x+yj形式表示的任何数字,其中x是实部,y是虚部。两个复数的乘法可以用下面的公式来完成
NumPy提供了vdot()方法,返回向量a和b的点积。这个函数处理复数的方式与dot( a , b )不同。
语法:
numpy.vdot(vector_a, vector_b)
示例 1:
# importing numpy as library
import numpy as np
# creating matrix of complex number
x = np.array([2+3j, 4+5j])
print("Printing First matrix:")
print(x)
y = np.array([8+7j, 5+6j])
print("Printing Second matrix:")
print(y)
# vector dot product of two matrices
z = np.vdot(x, y)
print("Product of first and second matrices are:")
print(z)
输出:
Printing First matrix:
[2.+3.j 4.+5.j]
Printing Second matrix:
[8.+7.j 5.+6.j]
Product of first and second matrices are:
(87-11j)
例子2:现在假设我们有二维矩阵。
# importing numpy as library
import numpy as np
# creating matrix of complex number
x = np.array([[2+3j, 4+5j], [4+5j, 6+7j]])
print("Printing First matrix:")
print(x)
y = np.array([[8+7j, 5+6j], [9+10j, 1+2j]])
print("Printing Second matrix:")
print(y)
# vector dot product of two matrices
z = np.vdot(x, y)
print("Product of first and second matrices are:")
print(z)
输出:
Printing First matrix:
[[2.+3.j 4.+5.j]
[4.+5.j 6.+7.j]]
Printing Second matrix:
[[8. +7.j 5. +6.j]
[9.+10.j 1. +2.j]]
Product of first and second matrices are:
(193-11j)