Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Python에서 matrix()를 사용하여 행렬 객체의 역 곱셈 계산

<시간/>

matrix()를 사용하여 행렬 객체의 역곱셈을 계산하려면 Python에서 numpy.linalg.inv() 메서드를 사용합니다. 정사각 행렬 a가 주어지면 dot(a, ainv) =dot(ainv, a) =eye(a.shape[0])를 만족하는 행렬 ainv를 반환합니다.

이 메서드는 행렬 a의 (승법) 역행렬을 반환합니다. 첫 번째 매개변수인 a는 반전할 Matrix입니다.

단계

먼저 필요한 라이브러리를 가져옵니다-

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()를 사용하여 행렬 객체의 역곱셈을 계산하려면 Python에서 numpy.linalg.inv() 메서드를 사용하십시오 -

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]]