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

คำนวณเลขสภาพ (Condition Number) ของเมทริกซ์ในพีชคณิตเชิงเส้นด้วยโนม Negative Infinity ใน Python

บทนำ

ในการคำนวณเลขสภาพ (condition number) ของเมทริกซ์ในพีชคณิตเชิงเส้น สามารถใช้เมธอด numpy.linalg.cond() ใน Python เมธอดนี้สามารถคืนเลขสภาพโดยใช้นอร์ม (norm) ต่างๆ ได้ 7 แบบ ขึ้นอยู่กับค่าของพารามิเตอร์ p

พารามิเตอร์สำคัญ

  • x: เมทริกซ์ที่ต้องการหาเลขสภาพ
  • p: ลำดับของนอร์มที่ใช้ในการคำนวณ -np.inf หมายถึง นอร์ม Negative Infinity

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

1. นำเข้าไลบรารีที่จำเป็น

import numpy as np
from numpy import linalg as LA

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

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. คำนวณเลขสภาพด้วยนอร์ม Negative Infinity

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

ตัวอย่างโค้ดสมบูรณ์

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)

# คำนวณเลขสภาพด้วยนอร์ม Negative Infinity
print("\nResult...\n", LA.cond(arr, -np.inf))

ผลลัพธ์

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...
 1.0

สรุป

เลขสภาพ (condition number) ของเมทริกซ์นี้ภายใต่นอร์ม Negative Infinity คือ 1.0 ซึ่งแสดงว่าเมทริกซ์นี้มีสภาพดี (well-conditioned) ภายใต่นอร์มนี้ นอร์ม Negative Infinity ถูกนิยามเป็นค่าสูงสุดของผลรวมค่าสัมบูรณ์ของแต่ละแถว