Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

คืนค่า Cholesky decomposition ในพีชคณิตเชิงเส้นด้วย Python


ภาพรวม

ใช้ numpy.linalg.cholesky() คืน Cholesky decomposition L × Lᴴ ของ square matrix a โดย L เป็น lower-triangular และ .ᴴ คือ conjugate transpose a ต้องเป็น Hermitian และ positive-definite (ไม่ตรวจสอบอัตโนมัติ) ใช้เฉพาะ lower-triangular และ diagonal elements คืนแค่ L

พารามิเตอร์

  • a: เมทริกซ์ Hermitian (symmetric ถ้า real) positive-definite

ขั้นตอนการทำงาน

1. นำเข้าไลบรารี

import numpy as np

2. สร้างเมทริกซ์ 2x2 complex Hermitian

arr = np.array([[1, -2j], [2j, 5]])

3. แสดงข้อมูลเมทริกซ์

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)

4. คำนวณ Cholesky decomposition

print("\nCholesky decomposition in Linear Algebra...\n", np.linalg.cholesky(arr))

ตัวอย่างโค้ดเต็ม

import numpy as np
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)
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]]

ตรวจสอบ: L × Lᴴ = [[1,0],[0+2j,1]] × [[1,0-2j],[0,1]] = [[1, -2j], [2j, 4+1]] = [[1, -2j], [2j, 5]] ✓