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

คำนวณผกผันการคูณของวัตถุเมทริกซ์ด้วย matrix() ใน Python


ในการคำนวณผกผันการคูณของวัตถุเมทริกซ์ด้วย matrix() ให้ใช้เมธอด 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

สร้างอาร์เรย์ -

arr = np.array([[ 5, 10], [ 15, 20 ]])

แสดงอาร์เรย์ -

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)

ในการคำนวณผกผันการคูณของวัตถุเมทริกซ์ด้วย matrix() ให้ใช้เมธอด numpy.linalg.inv() ใน Python -

print("\nResult...\n",np.linalg.inv(np.matrix(arr)))

ตัวอย่าง

import numpy as np
from numpy.linalg import inv

# Create an array
arr = np.array([[ 5, 10], [ 15, 20 ]])

# 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 object with matrx(), use the numpy.linalg.inv() method in Python.
print("\nResult...\n",np.linalg.inv(np.matrix(arr)))

ผลลัพธ์

Our Array...
[[ 5 10]
[15 20]]

Dimensions of our Array...
2

Datatype of our Array object...
int64

Shape of our Array object...
(2, 2)

Result...
[[-0.4 0.2]
[ 0.3 -0.1]]