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

วิธีใช้ SciPy คำนวณ Determinant ของ Matrix ใน Python

Determinant คำนวณได้จาก matrix หรือ array มิติมากกว่า 1 ใช้เข้าใจ matrix ให้ดีขึ้น SciPy มี 'det' ใน 'linalg' (Linear Algebra)

Syntax 'det'

scipy.linalg.det(matrix)

วิธีใช้ SciPy คำนวณ Determinant ของ Matrix ใน Python

สมมติ a,b,c,d เป็นค่าใน matrix 2x2 determinant = ad - bc

ตัวอย่าง

from scipy import linalg
import numpy as np
two_d_matrix = np.array([
    [7, 9],
    [33, 8]
])
print("The determinant value of the matrix is :")
print(linalg.det(two_d_matrix))

ผลลัพธ์

The determinant value of the matrix is : -241.0

คำอธิบาย

  • นำเข้าไลบรารี
  • กำหนด matrix ค่าตัวเลข
  • ส่งพารามิเตอร์ให้ 'det' คำนวณ determinant
  • เรียกฟังก์ชันและแสดงผล

Determinant บอกว่า matrix invertible หรือไม่ (det ≠ 0) และ scaling factor ของ linear transformation