3.9小节完成
This commit is contained in:
42
cookbook/c03/p10_matrix_linear.py
Normal file
42
cookbook/c03/p10_matrix_linear.py
Normal file
@@ -0,0 +1,42 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- encoding: utf-8 -*-
|
||||
"""
|
||||
Topic: 矩阵和线性代数
|
||||
Desc :
|
||||
"""
|
||||
import numpy as np
|
||||
import numpy.linalg
|
||||
|
||||
|
||||
def matrix_linear():
|
||||
m = np.matrix([[1,-2,3],[0,4,5],[7,8,-9]])
|
||||
print(m)
|
||||
|
||||
# Return transpose 转置矩阵
|
||||
print(m.T)
|
||||
|
||||
# Return inverse # 逆矩阵
|
||||
print(m.I)
|
||||
|
||||
|
||||
# Create a vector and multiply
|
||||
v = np.matrix([[2],[3],[4]])
|
||||
print(v)
|
||||
print(m * v)
|
||||
|
||||
# Determinant 行列式
|
||||
print(numpy.linalg.det(m))
|
||||
|
||||
# Eigenvalues 特征值
|
||||
print(numpy.linalg.eigvals(m))
|
||||
|
||||
# Solve for x in m*x = v
|
||||
x = numpy.linalg.solve(m, v)
|
||||
print(x)
|
||||
print(m * x)
|
||||
print(v)
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
matrix_linear()
|
||||
@@ -5,14 +5,85 @@
|
||||
----------
|
||||
问题
|
||||
----------
|
||||
todo...
|
||||
你需要执行矩阵和线性代数运算,比如矩阵乘法、寻找行列式、求解线性方程组等等。
|
||||
|
||||
|
|
||||
|
||||
----------
|
||||
解决方案
|
||||
----------
|
||||
todo...
|
||||
NumPy库有一个矩阵对象可以用来解决这个问题。
|
||||
矩阵类似于3.9小节中数组对象,但是遵循线性代数的计算规则。下面的一个例子展示了矩阵的一些基本特性:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
>>> import numpy as np
|
||||
>>> m = np.matrix([[1,-2,3],[0,4,5],[7,8,-9]])
|
||||
>>> m
|
||||
matrix([[ 1, -2, 3],
|
||||
[ 0, 4, 5],
|
||||
[ 7, 8, -9]])
|
||||
|
||||
>>> # Return transpose
|
||||
>>> m.T
|
||||
matrix([[ 1, 0, 7],
|
||||
[-2, 4, 8],
|
||||
[ 3, 5, -9]])
|
||||
|
||||
>>> # Return inverse
|
||||
>>> m.I
|
||||
matrix([[ 0.33043478, -0.02608696, 0.09565217],
|
||||
[-0.15217391, 0.13043478, 0.02173913],
|
||||
[ 0.12173913, 0.09565217, -0.0173913 ]])
|
||||
|
||||
>>> # Create a vector and multiply
|
||||
>>> v = np.matrix([[2],[3],[4]])
|
||||
>>> v
|
||||
matrix([[2],
|
||||
[3],
|
||||
[4]])
|
||||
>>> m * v
|
||||
matrix([[ 8],
|
||||
[32],
|
||||
[ 2]])
|
||||
>>>
|
||||
|
||||
可以在numpy.linalg子包中找到更多的操作函数,比如:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
>>> import numpy.linalg
|
||||
|
||||
>>> # Determinant
|
||||
>>> numpy.linalg.det(m)
|
||||
-229.99999999999983
|
||||
|
||||
>>> # Eigenvalues
|
||||
>>> numpy.linalg.eigvals(m)
|
||||
array([-13.11474312, 2.75956154, 6.35518158])
|
||||
|
||||
>>> # Solve for x in mx = v
|
||||
>>> x = numpy.linalg.solve(m, v)
|
||||
>>> x
|
||||
matrix([[ 0.96521739],
|
||||
[ 0.17391304],
|
||||
[ 0.46086957]])
|
||||
>>> m * x
|
||||
matrix([[ 2.],
|
||||
[ 3.],
|
||||
[ 4.]])
|
||||
>>> v
|
||||
matrix([[2],
|
||||
[3],
|
||||
[4]])
|
||||
>>>
|
||||
|
||||
|
|
||||
|
||||
----------
|
||||
讨论
|
||||
----------
|
||||
todo...
|
||||
很显然线性代数是个非常大的主题,已经超出了本书能讨论的范围。
|
||||
但是,如果你需要操作数组和向量的话,NumPy是一个不错的入口点。
|
||||
可以访问NumPy官网 http://www.numpy.org 获取更多信息。
|
||||
|
||||
|
||||
Reference in New Issue
Block a user