Python線性代數中矩陣的n次冪
在Python的線性代數中,使用numpy.linalg.matrix_power()函式可以計算方陣的n次冪。對於正整數n,冪透過重複的矩陣平方和矩陣乘法計算。如果n == 0,則返回與M形狀相同的單位矩陣。如果n < 0,則計算逆矩陣,然後將其提升到abs(n)次冪。
返回值與M的形狀和型別相同;如果指數為正或零,則元素的型別與M的元素型別相同。如果指數為負,則元素為浮點數。第一個引數a是要“冪次”的矩陣。第二個引數n是指數,可以是任何整數或長整數,正數、負數或零。
步驟
首先,匯入所需的庫:
import numpy as np from numpy.linalg import matrix_power
建立一個二維陣列,作為虛數單位的矩陣等價物:
arr = np.array([[0, 1], [-1, 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)
在Python線性代數中,使用numpy.linalg.matrix_power()函式可以計算方陣的n次冪。對於正整數n,冪透過重複的矩陣平方和矩陣乘法計算。如果n == 0,則返回與M形狀相同的單位矩陣。如果n < 0,則計算逆矩陣,然後將其提升到abs(n)次冪:
print("\nResult...\n",matrix_power(arr, 0))示例
import numpy as np
from numpy.linalg import matrix_power
# Create a 2D array, matrix equivalent of the imaginary unit
arr = np.array([[0, 1], [-1, 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 raise a square matrix to the power n in Linear Algebra, use the numpy.linalg.matrix_power() in Python
print("\nResult...\n",matrix_power(arr, 0))輸出
Our Array... [[ 0 1] [-1 0]] Dimensions of our Array... 2 Datatype of our Array object... int64 Shape of our Array object... (2, 2) Result... [[1 0] [0 1]]
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP