線形代数とPython
Page content
例1: 3 x 4 行列の表示
import numpy as np
A = np.array([
[1,2,3,4],
[5,6,7,8],
[9, 10, 11, 12],
])
A
結果:
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
例2: 単位行列(identity matrix)の表示
import numpy as np
I = np.identity(3)
I
結果:
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
例3:転置行列の表示
import numpy as np
A = np.array([
[1,2,3,4],
[5,6,7,8],
[9, 10, 11, 12],
])
A.transpose()
結果:
array([[ 1, 5, 9],
[ 2, 6, 10],
[ 3, 7, 11],
[ 4, 8, 12]])
例4:行列式の計算
import numpy as np
A = np.array([
[1, 2],
[3, 4]
])
np.linalg.det(A)
結果:
-2.0000000000000004
例5:逆行列
行列式det(A) = 0のときに、Aの逆行列存在しない
import numpy as np
A = np.array([
[1, 2],
[3, 4]
])
inverse_A = np.linalg.inv(A)
inverse_A
結果:
array([[-2. , 1. ],
[ 1.5, -0.5]])
例6
$AA^{-1} = A^{-1}A = I$
A = np.array([
[1, 2],
[3, 4]
])
inverse_A = np.linalg.inv(A)
result1 = np.dot(A,inverse_A)
print("result1: \n", result1)
result2 = np.dot(inverse_A, A)
print("result2: \n", result2)
結果:
result1:
[[ 1.00000000e+00 1.11022302e-16]
[ 0.00000000e+00 1.00000000e+00]]
result2:
[[ 1.00000000e+00 4.44089210e-16]
[ 0.00000000e+00 1.00000000e+00]]
例7: 行列のランク(rank)
A = np.array([
[1.,0.,0.]
,[0.,1.,1.]
,[0.,1.,1.]
])
rank_A = np.linalg.matrix_rank(A)
rank_A
例8: 行列のreduced row-echelon form (RREF)
import sympy
import numpy as np
A = np.array([
[1.,0.,0.]
,[0.,1.,1.]
,[0.,1.,1.]
])
rref = sympy.Matrix(A).rref()
rref
結果:
(Matrix([
[ 1, 0, 0],
[0.0, 1, 1.0],
[ 0, 0, 0]]), (0, 1))
例9: A=LU, LU分解
import pprint as pp
import scipy
import scipy.linalg # 線形代数のライブラリ
A = scipy.array([ [7, 3, -1, 2], [3, 8, 1, -4], [-1, 1, 4, -1], [2, -4, -1, 6] ])
P, L, U = scipy.linalg.lu(A)
pp.pprint( P)
pp.pprint(L)
pp.pprint(U)
結果:
P:
array([[ 1., 0., 0., 0.],
[ 0., 1., 0., 0.],
[ 0., 0., 1., 0.],
[ 0., 0., 0., 1.]])
L:
array([[ 1. , 0. , 0. , 0. ],
[ 0.42857143, 1. , 0. , 0. ],
[-0.14285714, 0.21276596, 1. , 0. ],
[ 0.28571429, -0.72340426, 0.08982036, 1. ]])
U:
array([[ 7. , 3. , -1. , 2. ],
[ 0. , 6.71428571, 1.42857143, -4.85714286],
[ 0. , 0. , 3.55319149, 0.31914894],
[ 0. , 0. , 0. , 1.88622754]])