NumPy 中的 inv函数

首页 / Numpy入门教程 / NumPy 中的 inv函数

无涯教程使用 numpy.linalg.inv()函数来计算矩阵的逆。矩阵的逆是这样的:如果将其与原始矩阵相乘,则会得到单位矩阵。

import numpy as np 

x=np.array([[1,2],[3,4]]) 
y=np.linalg.inv(x) 
print x 
print y 
print np.dot(x,y)

它应该产生以下输出-

[[1 2]                                                                        
 [3 4]]                                                                       
[[-2.   1. ]                                                                  
 [ 1.5 -0.5]]                                                                 
[[  1.00000000e+00   1.11022302e-16]                                          
 [  0.00000000e+00   1.00000000e+00]]

现在在示例中创建矩阵A的逆。

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/numpy/numpy-inv.html

来源:LearnFk无涯教程网

import numpy as np 
a=np.array([[1,1,1],[0,2,5],[2,5,-1]]) 

print 'Array a:” 
print a 
ainv=np.linalg.inv(a) 

print 'Inverse of a:' 
print ainv  

print 'Matrix B is:' 
b=np.array([[6],[-4],[27]]) 
print b 

print 'Compute A-1B:' 
x=np.linalg.solve(a,b) 
print x  
# this is the solution to linear equations x=5, y=3, z=-2

它将产生以下输出-

Array a:
[[ 1 1 1]
 [ 0 2 5]
 [ 2 5 -1]]

Inverse of a:
[[ 1.28571429 -0.28571429 -0.14285714]
 [-0.47619048 0.14285714 0.23809524]
 [ 0.19047619 0.14285714 -0.0952381 ]]

Matrix B is:
[[ 6]
 [-4]
 [27]]

Compute A-1B:
[[ 5.]
 [ 3.]
 [-2.]]

使用函数可以获得相同的输出-

x=np.dot(ainv,b)

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

猜你喜欢

深入浅出gRPC -〔李林锋〕

软件测试52讲 -〔茹炳晟〕

软件工程之美 -〔宝玉〕

设计模式之美 -〔王争〕

大厂晋升指南 -〔李运华〕

手把手带你写一门编程语言 -〔宫文学〕

徐昊 · TDD项目实战70讲 -〔徐昊〕

超级访谈:对话玉伯 -〔玉伯〕

好记忆不如烂笔头。留下您的足迹吧 :)