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

คำนวณหมายเลขเงื่อนไขของเมทริกซ์ในพีชคณิตเชิงเส้นโดยใช้ค่าลบ 2 บรรทัดฐานในPython


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

จำนวนเงื่อนไขของ x ถูกกำหนดให้เป็นบรรทัดฐานของ x คูณบรรทัดฐานของค่าผกผันของ x; บรรทัดฐานสามารถเป็นบรรทัดฐาน L2 ปกติหรือบรรทัดฐานของเมทริกซ์อื่นจำนวนหนึ่ง พารามิเตอร์ที่ 1 คือ x เมทริกซ์ที่มีการค้นหาหมายเลขเงื่อนไข พารามิเตอร์ตัวที่ 2 คือ p ลำดับของบรรทัดฐานที่ใช้ในการคำนวณหมายเลขเงื่อนไข ค่า "2" ที่กำหนดเป็นพารามิเตอร์จะเป็นค่าลบ 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)

ในการคำนวณหมายเลขเงื่อนไขของเมทริกซ์ในพีชคณิตเชิงเส้น ให้ใช้เมธอด numpy.linalg.cond() วิธีนี้สามารถคืนค่าหมายเลขเงื่อนไขโดยใช้หนึ่งในเจ็ดบรรทัดฐานที่แตกต่างกัน ขึ้นอยู่กับค่าของ p -

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

ตัวอย่าง

import numpy as np
from numpy import linalg as LA

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

# 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 condition number of a matrix in linear algebra, use the numpy.linalg.cond() method in Python
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