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