# 数学¶

NumPy的目标是提供使数值计算更容易的函数和类。有关详细概述，请参阅小节 routines.linalg 。NumPy文档的。他们中的一些人需要这个角色 linalg 之后 numpy 。与

import numpy as np


np.linalg.<function>


## 分量式数学运算¶

NumPy arrays 支持中引入的典型数学运算 基础知识 并且它们是按组件方式执行的。这里有几个例子：

>>> x = np.array([1, 2, 3, 4, 5])
>>> y = np.array([6, 5, 8, 9, 10])
>>> z = 2.0
>>> x + z
array([ 3.,  4.,  5.,  6.,  7.])
>>> x + y
array([ 7,  7, 11, 13, 15])
>>> y % z
array([ 0.,  1.,  0.,  1.,  0.])
>>> y / x
array([ 6.        ,  2.5       ,  2.66666667,  2.25      ,  2.        ])


## 点¶

>>> x = np.array([1, 2, 3, 4, 5])
>>> y = np.array([6, 5, 8, 9, 10])
>>> np.dot(x, y)
126
>>> x.dot(y)  # alternative syntax
126
>>> x = np.array([[1, 2, 3],
...               [4, 5, 6],
...               [7, 8, 9]])
>>> y = np.array([[10, 11, 12],
...               [13, 14, 15],
...               [16, 17, 18]])
>>> z = np.array([[1/19, 1/20, 1/21],
...               [1/22, 1/23, 1/24],
...               [1/25, 1/26, 1/27]])
>>> np.dot(x, np.dot(y, z))  # this is a bit convoluted
array([[ 12.35196172,  11.80535117,  11.30555556],
[ 29.63712919,  28.32591973,  27.12698413],
[ 46.92229665,  44.84648829,  42.9484127 ]])
>>> x.dot(y).dot(z)  # better
array([[ 12.35196172,  11.80535117,  11.30555556],
[ 29.63712919,  28.32591973,  27.12698413],
[ 46.92229665,  44.84648829,  42.9484127 ]])


## 特征值¶

>>> x = np.array([[1, 2, 3],
...               [4, 5, 6],
...               [7, 8, 9]])
>>> np.linalg.eig(x)
(array([  1.61168440e+01,  -1.11684397e+00,  -1.30367773e-15]), array([[-0.23197069, -0.78583024,  0.40824829],
[-0.52532209, -0.08675134, -0.81649658],
[-0.8186735 ,  0.61232756,  0.40824829]]))


>>> eigenvalues, eigenvectors = np.linalg.eig(x)
>>> for i in range(eigenvalues.size):
...     print(eigenvalues[i], eigenvectors[:, i])
...
16.1168439698 [-0.23197069 -0.52532209 -0.8186735 ]
-1.11684396981 [-0.78583024 -0.08675134  0.61232756]
-1.30367772647e-15 [ 0.40824829 -0.81649658  0.40824829]


## 规范¶

>>> x = np.array([3, 4])
>>> np.linalg.norm(x)
5.0


## 行列式¶

>>> x = np.array([[1, 2, 3],
...               [4, 5, 4],
...               [3, 2, 1]])
>>> np.linalg.det(x)
-7.9999999999999982