返回Python中线性代数的Cholesky分解
要返回Cholesky分解,请使用numpy.linalg.cholesky()方法。返回方阵a的Cholesky分解L * L.H,其中L是下三角矩阵,.H是共轭转置运算符。a必须是Hermitian(埃尔米特)且正定的。不会执行任何检查以验证a是否为Hermitian矩阵。此外,只使用a的下三角和对角元素。实际上只返回L。
参数a是Hermitian(如果所有元素都是实数,则为对称)正定输入矩阵。该方法返回a的上三角或下三角Cholesky因子。如果a是矩阵对象,则返回矩阵对象。
步骤
首先,导入所需的库:
import numpy as np
使用numpy.array()方法创建一个二维numpy数组:
arr = np.array([[1,-2j],[2j,5]])
显示数组:
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)
要返回Cholesky分解,请使用numpy.linalg.cholesky()方法:
print("\nCholesky decomposition in Linear Algebra...\n",np.linalg.cholesky(arr))
示例
import numpy as np # Creating a 2D numpy array using the numpy.array() method arr = np.array([[1,-2j],[2j,5]]) # 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 return the Cholesky decomposition, use the numpy.linalg.cholesky() method. print("\nCholesky decomposition in Linear Algebra...\n",np.linalg.cholesky(arr))
输出
Our Array... [[ 1.+0.j -0.-2.j] [ 0.+2.j 5.+0.j]] Dimensions of our Array... 2 Datatype of our Array object... complex128 Shape of our Array object... (2, 2) Cholesky decomposition in Linear Algebra... [[1.+0.j 0.+0.j] [0.+2.j 1.+0.j]]
广告