代码

>>> import numpy
>>> ac = [[3, 4], [2,8]]
>>> ac = numpy.array(ac)
>>> print(numpy.linalg.inv(ac))
[[ 0.5    -0.25  ]
 [-0.125   0.1875]]
>>> print(numpy.linalg.inv(ac)*16)
[[ 8. -4.]
 [-2.  3.]]
>>> 

经过手动计算,额,技巧,得到的结果也是如此。
若是换成不可逆的矩阵呢?
看代码!

>>> import numpy
>>> bc = [[0,1],[0,3]]
>>> bc = numpy.array(bc)
>>> print(numpy.linalg.inv(bc))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<__array_function__ internals>", line 6, in inv
  File "/home/jiangyun/anaconda3/lib/python3.7/site-packages/numpy/linalg/linalg.py", line 547, in inv
    ainv = _umath_linalg.inv(a, signature=signature, extobj=extobj)
  File "/home/jiangyun/anaconda3/lib/python3.7/site-packages/numpy/linalg/linalg.py", line 97, in _raise_linalgerror_singular
    raise LinAlgError("Singular matrix")
numpy.linalg.LinAlgError: Singular matrix
>>> 

好像不行,那就换一个

>>> cd = [[1, 2], [3, 4], [22, 3]]
>>> cd = numpy.array(cd)
>>> print(numpy.linalg.inv(cd))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<__array_function__ internals>", line 6, in inv
  File "/home/jiangyun/anaconda3/lib/python3.7/site-packages/numpy/linalg/linalg.py", line 542, in inv
    _assert_stacked_square(a)
  File "/home/jiangyun/anaconda3/lib/python3.7/site-packages/numpy/linalg/linalg.py", line 213, in _assert_stacked_square
    raise LinAlgError('Last 2 dimensions of the array must be square')
numpy.linalg.LinAlgError: Last 2 dimensions of the array must be square

所以,这个函数是一个求方阵的逆矩阵的函数,要求方阵本身可逆,所以挺鸡肋的,对于很多不可逆的方阵是没办法的!

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐