线性代数
矩阵和向量积
import numpy as np
x = np. array( [ 1 , 2 , 3 , 4 , 5 ] )
y = np. array( [ 2 , 3 , 4 , 5 , 6 ] )
z = np. dot( x, y)
print ( z)
x = np. array( [ [ 1 , 2 , 3 ] , [ 3 , 4 , 5 ] , [ 6 , 7 , 8 ] ] )
print ( x)
y = np. array( [ [ 5 , 4 , 2 ] , [ 1 , 7 , 9 ] , [ 0 , 4 , 5 ] ] )
print ( y)
z = np. dot( x, y)
print ( z)
z = np. dot( y, x)
print ( z)
70
[[1 2 3]
[3 4 5]
[6 7 8]]
[[5 4 2]
[1 7 9]
[0 4 5]]
[[ 7 30 35]
[ 19 60 67]
[ 37 105 115]]
[[ 29 40 51]
[ 76 93 110]
[ 42 51 60]]
矩阵特征值和特征向量
import numpy as np
x = np. diag( ( 1 , 2 , 3 ) )
print ( x)
print ( np. linalg. eigvals( x) )
a, b = np. linalg. eig( x)
print ( a)
print ( b)
for i in range ( 3 ) :
if np. allclose( a[ i] * b[ : , i] , np. dot( x, b[ : , i] ) ) :
print ( 'Right' )
else :
print ( 'Error' )
[[1 0 0]
[0 2 0]
[0 0 3]]
[1. 2. 3.]
[1. 2. 3.]
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
Right
Right
Right
import numpy as np
A = np. arange( 16 ) . reshape( 4 , 4 )
print ( A)
A = A + A. T
print ( A)
B = np. linalg. eigvals( A)
print ( B)
if np. all ( B > 0 ) :
print ( 'Yes' )
else :
print ( 'No' )
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
[[ 0 5 10 15]
[ 5 10 15 20]
[10 15 20 25]
[15 20 25 30]]
[ 6.74165739e+01 -7.41657387e+00 2.28055691e-15 -1.91945916e-15]
No
矩阵分解
奇异值分解
import numpy as np
A = np. array( [ [ 4 , 11 , 14 ] , [ 8 , 7 , - 2 ] ] )
print ( A)
u, s, vh = np. linalg. svd( A, full_matrices= False )
print ( u. shape)
print ( u)
print ( s. shape)
print ( np. diag( s) )
print ( vh. shape)
print ( vh)
a = np. dot( u, np. diag( s) )
a = np. dot( a, vh)
print ( a)
[[ 4 11 14]
[ 8 7 -2]]
(2, 2)
[[ 0.9486833 -0.31622777]
[ 0.31622777 0.9486833 ]]
(2,)
[[18.97366596 0. ]
[ 0. 9.48683298]]
(2, 3)
[[ 0.33333333 0.66666667 0.66666667]
[ 0.66666667 0.33333333 -0.66666667]]
[[ 4. 11. 14.]
[ 8. 7. -2.]]
import numpy as np
A = np. array( [ [ 1 , 1 ] , [ 1 , - 2 ] , [ 2 , 1 ] ] )
print ( A)
u, s, vh = np. linalg. svd( A, full_matrices= False )
print ( u. shape)
print ( u)
print ( s. shape)
print ( np. diag( s) )
print ( vh. shape)
print ( vh)
a = np. dot( u, np. diag( s) )
a = np. dot( a, vh)
print ( a)
[[ 1 1]
[ 1 -2]
[ 2 1]]
(3, 2)
[[-5.34522484e-01 -1.11022302e-16]
[ 2.67261242e-01 -9.48683298e-01]
[-8.01783726e-01 -3.16227766e-01]]
(2,)
[[2.64575131 0. ]
[0. 2.23606798]]
(2, 2)
[[-0.70710678 -0.70710678]
[-0.70710678 0.70710678]]
[[ 1. 1.]
[ 1. -2.]
[ 2. 1.]]
QR分解
import numpy as np
A = np. array( [ [ 2 , - 2 , 3 ] , [ 1 , 1 , 1 ] , [ 1 , 3 , - 1 ] ] )
print ( A)
q, r = np. linalg. qr( A)
print ( q. shape)
print ( q)
print ( r. shape)
print ( r)
print ( np. dot( q, r) )
a = np. allclose( np. dot( q. T, q) , np. eye( 3 ) )
print ( a)
[[ 2 -2 3]
[ 1 1 1]
[ 1 3 -1]]
(3, 3)
[[-0.81649658 0.53452248 0.21821789]
[-0.40824829 -0.26726124 -0.87287156]
[-0.40824829 -0.80178373 0.43643578]]
(3, 3)
[[-2.44948974 0. -2.44948974]
[ 0. -3.74165739 2.13808994]
[ 0. 0. -0.65465367]]
[[ 2. -2. 3.]
[ 1. 1. 1.]
[ 1. 3. -1.]]
True
import numpy as np
A = np. array( [ [ 1 , 1 ] , [ 1 , - 2 ] , [ 2 , 1 ] ] )
print ( A)
q, r = np. linalg. qr( A, mode= 'complete' )
print ( q. shape)
print ( q)
print ( r. shape)
print ( r)
print ( np. dot( q, r) )
a = np. allclose( np. dot( q, q. T) , np. eye( 3 ) )
print ( a)
[[ 1 1]
[ 1 -2]
[ 2 1]]
(3, 3)
[[-0.40824829 0.34503278 -0.84515425]
[-0.40824829 -0.89708523 -0.16903085]
[-0.81649658 0.27602622 0.50709255]]
(3, 2)
[[-2.44948974 -0.40824829]
[ 0. 2.41522946]
[ 0. 0. ]]
[[ 1. 1.]
[ 1. -2.]
[ 2. 1.]]
True
import numpy as np
A = np. array( [ [ 1 , 1 ] , [ 1 , - 2 ] , [ 2 , 1 ] ] )
print ( A)
q, r = np. linalg. qr( A)
print ( q. shape)
print ( q)
print ( r. shape)
print ( r)
print ( np. dot( q, r) )
a = np. allclose( np. dot( q. T, q) , np. eye( 2 ) )
print ( a)
[[ 1 1]
[ 1 -2]
[ 2 1]]
(3, 2)
[[-0.40824829 0.34503278]
[-0.40824829 -0.89708523]
[-0.81649658 0.27602622]]
(2, 2)
[[-2.44948974 -0.40824829]
[ 0. 2.41522946]]
[[ 1. 1.]
[ 1. -2.]
[ 2. 1.]]
True
Cholesky分解
import numpy as np
A = np. array( [ [ 1 , 1 , 1 , 1 ] , [ 1 , 3 , 3 , 3 ] ,
[ 1 , 3 , 5 , 5 ] , [ 1 , 3 , 5 , 7 ] ] )
print ( A)
print ( np. linalg. eigvals( A) )
L = np. linalg. cholesky( A)
print ( L)
print ( np. dot( L, L. T) )
[[1 1 1 1]
[1 3 3 3]
[1 3 5 5]
[1 3 5 7]]
[13.13707118 1.6199144 0.51978306 0.72323135]
[[1. 0. 0. 0. ]
[1. 1.41421356 0. 0. ]
[1. 1.41421356 1.41421356 0. ]
[1. 1.41421356 1.41421356 1.41421356]]
[[1. 1. 1. 1.]
[1. 3. 3. 3.]
[1. 3. 5. 5.]
[1. 3. 5. 7.]]
范数和其他数字
矩阵的范数
import numpy as np
x = np. array( [ 1 , 2 , 3 , 4 ] )
print ( np. linalg. norm( x, ord = 1 ) )
print ( np. sum ( np. abs ( x) ) )
print ( np. linalg. norm( x, ord = 2 ) )
print ( np. sum ( np. abs ( x) ** 2 ) ** 0.5 )
print ( np. linalg. norm( x, ord = - np. inf) )
print ( np. min ( np. abs ( x) ) )
print ( np. linalg. norm( x, ord = np. inf) )
print ( np. max ( np. abs ( x) ) )
10.0
10
5.477225575051661
5.477225575051661
1.0
1
4.0
4
import numpy as np
A = np. array( [ [ 1 , 2 , 3 , 4 ] , [ 2 , 3 , 5 , 8 ] ,
[ 1 , 3 , 5 , 7 ] , [ 3 , 4 , 7 , 11 ] ] )
print ( A)
print ( np. linalg. norm( A, ord = 1 ) )
print ( np. max ( np. sum ( A, axis= 0 ) ) )
print ( np. linalg. norm( A, ord = 2 ) )
print ( np. max ( np. linalg. svd( A, compute_uv= False ) ) )
print ( np. linalg. norm( A, ord = np. inf) )
print ( np. max ( np. sum ( A, axis= 1 ) ) )
print ( np. linalg. norm( A, ord = 'fro' ) )
print ( np. sqrt( np. trace( np. dot( A. T, A) ) ) )
[[ 1 2 3 4]
[ 2 3 5 8]
[ 1 3 5 7]
[ 3 4 7 11]]
30.0
30
20.24345358700576
20.24345358700576
25.0
25
20.273134932713294
20.273134932713294
方阵的行列式
import numpy as np
x = np. array( [ [ 1 , 2 ] , [ 3 , 4 ] ] )
print ( x)
print ( np. linalg. det( x) )
[[1 2]
[3 4]]
-2.0000000000000004
矩阵的秩
import numpy as np
I = np. eye( 3 )
print ( I)
r = np. linalg. matrix_rank( I)
print ( r)
I[ 1 , 1 ] = 0
print ( I)
r = np. linalg. matrix_rank( I)
print ( r)
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
3
[[1. 0. 0.]
[0. 0. 0.]
[0. 0. 1.]]
2
矩阵的迹
import numpy as np
x = np. array( [ [ 1 , 2 , 3 ] , [ 3 , 4 , 5 ] , [ 6 , 7 , 8 ] ] )
print ( x)
y = np. array( [ [ 5 , 4 , 2 ] , [ 1 , 7 , 9 ] , [ 0 , 4 , 5 ] ] )
print ( y)
print ( np. trace( x) )
print ( np. trace( np. transpose( x) ) )
print ( np. trace( x + y) )
print ( np. trace( x) + np. trace( y) )
[[1 2 3]
[3 4 5]
[6 7 8]]
[[5 4 2]
[1 7 9]
[0 4 5]]
13
13
30
30
解方程和逆矩阵
逆矩阵(inverse matrix)
import numpy as np
A = np. array( [ [ 1 , - 2 , 1 ] , [ 0 , 2 , - 1 ] , [ 1 , 1 , - 2 ] ] )
print ( A)
A_det = np. linalg. det( A)
print ( A_det)
A_inverse = np. linalg. inv( A)
print ( A_inverse)
x = np. allclose( np. dot( A, A_inverse) , np. eye( 3 ) )
print ( x)
x = np. allclose( np. dot( A_inverse, A) , np. eye( 3 ) )
print ( x)
A_companion = A_inverse * A_det
print ( A_companion)
[[ 1 -2 1]
[ 0 2 -1]
[ 1 1 -2]]
-2.9999999999999996
[[ 1.00000000e+00 1.00000000e+00 -1.11022302e-16]
[ 3.33333333e-01 1.00000000e+00 -3.33333333e-01]
[ 6.66666667e-01 1.00000000e+00 -6.66666667e-01]]
True
True
[[-3.00000000e+00 -3.00000000e+00 3.33066907e-16]
[-1.00000000e+00 -3.00000000e+00 1.00000000e+00]
[-2.00000000e+00 -3.00000000e+00 2.00000000e+00]]
求解线性方程组
import numpy as np
A = np. array( [ [ 1 , 2 , 1 ] , [ 2 , - 1 , 3 ] , [ 3 , 1 , 2 ] ] )
b = np. array( [ 7 , 7 , 18 ] )
x = np. linalg. solve( A, b)
print ( x)
x = np. linalg. inv( A) . dot( b)
print ( x)
y = np. allclose( np. dot( A, x) , b)
print ( y)
[ 7. 1. -2.]
[ 7. 1. -2.]
True
练习
import numpy as np
a = np. array( [ 1 , 2 , 3 , 4 , 5 ] )
b = np. array( [ 4 , 5 , 6 , 7 , 8 ] )
d = np. sqrt( np. sum ( ( a - b) ** 2 ) )
print ( d)
d = np. linalg. norm( a - b)
print ( d)
6.708203932499369
6.708203932499369
np. diag( [ 5 , 5 , 5 , 5 , 5 ] )
array([[5, 0, 0, 0, 0],
[0, 5, 0, 0, 0],
[0, 0, 5, 0, 0],
[0, 0, 0, 5, 0],
[0, 0, 0, 0, 5]])
result= np. diag( [ 5 , 5 , 5 , 5 , 5 ] )
np. linalg. det( result)
3124.999999999999
np. linalg. inv( result)
array([[ 0.2, 0. , 0. , 0. , 0. ],
[ 0. , 0.2, 0. , 0. , 0. ],
[ 0. , 0. , 0.2, 0. , 0. ],
[-0. , -0. , -0. , 0.2, -0. ],
[ 0. , 0. , 0. , 0. , 0.2]])