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

คำนวณ condition number ของเมทริกซ์ด้วย norm -2 (smallest singular value) ใน Python


ภาพรวม

ใช้ numpy.linalg.cond() คำนวณ condition number ของเมทริกซ์ รองรับ 7 norms ตามพารามิเตอร์ p condition number = norm(x) × norm(inv(x)) อาจเป็นค่า infinite

พารามิเตอร์ p = -2 หมายถึง negative 2 norm (smallest singular value)

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

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

import numpy as np
from numpy import linalg as LA

2. สร้างเมทริกซ์ 3x3

arr = np.array([[1, 1, 0], [1, 0, 1], [1, 0, 0]])

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. คำนวณ condition number ด้วย p = -2

print("\nResult...\n", LA.cond(arr, -2))

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

import numpy as np
from numpy import linalg as LA
arr = np.array([[1, 1, 0], [1, 0, 1], [1, 0, 0]])
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("\nResult...\n", LA.cond(arr, -2))

ผลลัพธ์

Our Array...
[[1 1 0]
 [1 0 1]
 [1 0 0]]
Dimensions of our Array...
2
Datatype of our Array object...
int64
Shape of our Array object...
(3, 3)
Result...
0.2679491924311227

หมายเหตุ: Condition number ด้วย norm -2 ใช้ค่า singular value ที่เล็กที่สุด ค่าที่ได้ ~0.268 แสดงว่าเมทริกซ์นี้ well-conditioned สำหรับ norm นี้