在 PyTorch 中查找复杂矩阵的行列式
线性代数模块torch.linalg 的 PyTorch提供了函数torch.linalg.det() 计算方阵(实数或复数)矩阵的行列式。此函数还计算批处理中每个方阵的行列式。
句法:
torch.linalg.det(input) –> Tensor
input is an (n, n) matrix or a batch of matrices of size (b, n, n) where b is the batch dimension (number of matrices in a batch). This input must be a tensor. This function returns a tensor of the determinant value/s.
注意:该函数支持 float、double、 cfloat 、 和cdouble 数据类型。
让我们先看看如何在 PyTorch 中创建一个复杂的矩阵,然后计算矩阵的行列式。为了在 PyTorch 中创建复杂矩阵,我们使用2D Complex Tensors 。我们还可以创建一批复杂的矩阵。支持的复杂数据类型是torch.cfloat和torch.cdouble。
下面是一些创建复矩阵(二维复张量)并找到它们的行列式的示例。
示例 1:
通过生成随机数并计算行列式来创建大小为 2×2 的二维复张量。
Python3
# import library
import torch
# create 2x2 complex matrix using
# random numbers
mat = torch.randn(2,2, dtype = torch.cfloat)
# display the matrix
print("Complex Matrix: \n", mat)
# Compute the determinant of Matrix
# using torch.linalg.det()
det = torch.linalg.det(mat)
print("Determinant: \n", det)
Python3
# import library
import torch
# create real parts the complex numbers
real = torch.tensor([[1, 2],[5,6]], dtype=torch.float32)
# create imaginary parts of the complex numbers
imag = torch.tensor([[3, 4],[8,9]], dtype=torch.float32)
# create complex tensor (matrix)
z = torch.complex(real, imag)
print("Complex Matrix: \n", z)
# Compute the determinant of Matrix
# using torch.linalg.det()
det = torch.linalg.det(z)
print("Determinant: \n", det)
Python3
# import library
import torch
# create a real tensor
mat = torch.randn(3,3,2)
# display the real tensor
print("Real Tensor: \n",mat)
# convert the above real tensor
# into complex tensor
mat = torch.view_as_complex(mat)
# display the complex tensor (matrix)
print("Complex Tensor (Matrix): \n", mat)
# Compute the determinant of Matrix
# using torch.linalg.det()
det = torch.linalg.det(mat)
print("Determinant: \n", det)
Python3
# import library
import torch
# create matrix
mat = torch.randn(3,2,2, dtype = torch.cfloat)
# display matrix
print("Batch of Complex Matrices: \n", mat)
# Compute the determinant
det = torch.linalg.det(mat)
print("Determinant: \n", det)
Python3
# import library
import torch
# create comlex matrix
mat = torch.randn(3,4,4, dtype = torch.cfloat)
# display matrix
print("Batch of Complex Matrices: \n", mat)
# Compute the determinant
det = torch.linalg.det(mat)
print("Determinant: \n", det)
输出:
示例 2:
在 PyTorch 中创建复杂矩阵的另一种方法是使用torch.complex(real, imag)。这会创建一个复张量,其实部和虚部分别为real和imag。 real和imag 都必须是 float 或 double 并且imag必须与real类型相同。在下面的程序中,我们创建了一个大小为 2×2 的二维复张量并计算其行列式。
蟒蛇3
# import library
import torch
# create real parts the complex numbers
real = torch.tensor([[1, 2],[5,6]], dtype=torch.float32)
# create imaginary parts of the complex numbers
imag = torch.tensor([[3, 4],[8,9]], dtype=torch.float32)
# create complex tensor (matrix)
z = torch.complex(real, imag)
print("Complex Matrix: \n", z)
# Compute the determinant of Matrix
# using torch.linalg.det()
det = torch.linalg.det(z)
print("Determinant: \n", det)
输出:
示例 3:
复张量也可以从已经存在的真实张量中创建。形状 (..., 2) 的真实张量可以使用torch.view_as_complex()轻松转换为复张量,并且可以使用torch.linalg.det()方法计算它们的行列式。
蟒蛇3
# import library
import torch
# create a real tensor
mat = torch.randn(3,3,2)
# display the real tensor
print("Real Tensor: \n",mat)
# convert the above real tensor
# into complex tensor
mat = torch.view_as_complex(mat)
# display the complex tensor (matrix)
print("Complex Tensor (Matrix): \n", mat)
# Compute the determinant of Matrix
# using torch.linalg.det()
det = torch.linalg.det(mat)
print("Determinant: \n", det)
输出:
示例 4:
在这里,我们创建了一批大小为 2×2 的 3 个复张量并计算其行列式。
蟒蛇3
# import library
import torch
# create matrix
mat = torch.randn(3,2,2, dtype = torch.cfloat)
# display matrix
print("Batch of Complex Matrices: \n", mat)
# Compute the determinant
det = torch.linalg.det(mat)
print("Determinant: \n", det)
输出:
示例 5:
这是另一个程序,我们创建一批大小为 4×4 的 3 个复张量并计算行列式。
蟒蛇3
# import library
import torch
# create comlex matrix
mat = torch.randn(3,4,4, dtype = torch.cfloat)
# display matrix
print("Batch of Complex Matrices: \n", mat)
# Compute the determinant
det = torch.linalg.det(mat)
print("Determinant: \n", det)
输出: