在 Python 中计算复厄米特矩阵或实对称矩阵的特征值
要计算复厄米特矩阵或实对称矩阵的特征值,可以使用 numpy.eigvalsh() 方法。该方法按升序返回特征值,每个特征值根据其重数重复。
第一个参数 a 是一个复数或实数矩阵,其特征值需要计算。第二个参数 UPLO 指定计算是在 a 的下三角部分('L',默认)还是上三角部分('U')进行。无论此值如何,在计算中只会考虑对角线的实部,以保持厄米特矩阵的概念。因此,对角线的虚部将始终被视为零。
步骤
首先,导入所需的库 -
import numpy as np from numpy import linalg as LA
使用 numpy.array() 方法创建一个二维 numpy 数组 -
arr = np.array([[5+2j, 9-2j], [0+2j, 2-1j]])
显示数组 -
print("Our Array...\n",arr)检查维度 -
print("\nDimensions of our Array...\n",arr.ndim)
获取数据类型 -
print("\nDatatype of our Array object...\n",arr.dtype)获取形状 -
print("\nShape of our Array object...\n",arr.shape)
要计算复厄米特矩阵或实对称矩阵的特征值,可以使用 numpy.eigvalsh() 方法 -
print("\nResult...\n",LA.eigvalsh(arr))示例
from numpy import linalg as LA
import numpy as np
# Creating a 2D numpy array using the numpy.array() method
arr = np.array([[5+2j, 9-2j], [0+2j, 2-1j]])
# Display the array
print("Our Array...\n",arr)
# Check the Dimensions
print("\nDimensions of our Array...\n",arr.ndim)
# Get the Datatype
print("\nDatatype of our Array object...\n",arr.dtype)
# Get the Shape
print("\nShape of our Array object...\n",arr.shape)
# To compute the eigenvalues of a complex Hermitian or real symmetric matrix, use the numpy.eigvalsh() method
print("\nResult...\n",LA.eigvalsh(arr))输出
Our Array... [[5.+2.j 9.-2.j] [0.+2.j 2.-1.j]] Dimensions of our Array... 2 Datatype of our Array object... complex128 Shape of our Array object... (2, 2) Result... [1. 6.]
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP