ในการคำนวณค่าผกผัน (การคูณ) ของเมทริกซ์ ให้ใช้เมธอด numpy.linalg.inv() ใน Python รับเมทริกซ์สี่เหลี่ยม a ส่งคืนเมทริกซ์ ainv dot(a, ainv) =dot(ainv, a) =eye(a.shape[0]) ที่น่าพอใจ วิธีการส่งคืน (การคูณ) ผกผันของเมทริกซ์ a พารามิเตอร์ที่ 1 a คือเมทริกซ์ที่จะกลับด้าน
ขั้นตอน
ขั้นแรก นำเข้าไลบรารีที่จำเป็น-
import numpy as np from numpy.linalg import inv
สร้างเมทริกซ์หลายตัวโดยใช้ array() -
arr = np.array([[[1., 2.], [3., 4.]], [[1, 3], [3, 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) ในการคำนวณค่าผกผัน (การคูณ) ของเมทริกซ์ ให้ใช้เมธอด numpy.linalg.inv() ใน Python -
print("\nResult...\n",np.linalg.inv(arr)) ตัวอย่าง
import numpy as np
from numpy.linalg import inv
# Create several matrices using array()
arr = np.array([[[1., 2.], [3., 4.]], [[1, 3], [3, 5]]])
# 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 (multiplicative) inverse of a matrix, use the numpy.linalg.inv() method in Python.
print("\nResult...\n",np.linalg.inv(arr)) ผลลัพธ์
Our Array... [[[1. 2.] [3. 4.]] [[1. 3.] [3. 5.]]] Dimensions of our Array... 3 Datatype of our Array object... float64 Shape of our Array object... (2, 2, 2) Result... [[[-2. 1. ] [ 1.5 -0.5 ]] [[-1.25 0.75] [ 0.75 -0.25]]]