在Python中使用 NumPy 将复数矩阵相乘
在本文中,我们将讨论如何使用 NumPy 将两个包含复数的矩阵相乘,但首先,让我们知道什么是复数。复数是可以以x+yj形式表示的任何数字,其中 x 是实部,y 是虚部。两个复数的乘法可以使用以下公式完成 -
NumPy 提供了返回向量 a 和 b 的点积的 vdot() 方法。此函数处理复数的方式与 dot( a , b ) 不同。
句法:
numpy.vdot(vector_a, vector_b)
示例 1:
Python3
# 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)
Python3
# 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]
Printing Second matrix:
[8.+7.j 5.+6.j]
Product of first and second matrices are:
(87-11j)
示例 2:现在假设我们有 2D 矩阵:
Python3
# 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)