連立1次方程式

 次のような連立1次方程式を考える. \begin{align} &a_{11} x_1 + a_{12} x_2 + a_{13} x_3 + \cdots \ \ + a_{1n} x_n = b_1 \nonumber \\ &a_{21} x_1 + a_{22} x_2 + a_{23} x_3 + \cdots \ \ + a_{2n} x_n = b_2 \nonumber \\ &a_{31} x_1 + a_{32} x_2 + a_{33} x_3 + \cdots \ \ + a_{3n} x_n = b_3 \nonumber \\ &\cdots \nonumber \\ &a_{n1} x_1 + a_{n2} x_2 + a_{n3} x_3 + \cdots \ \ + a_{nn} x_n = b_n \nonumber \end{align} これを行列表示すると, $$ \begin{pmatrix} a_{11} & a_{12} & a_{13} & \cdots & a_{1n} \\ a_{21} & a_{22} & a_{23} & \cdots & a_{2n} \\ a_{31} & a_{32} & a_{33} & \cdots & a_{3n} \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ a_{n1} & a_{n2} & a_{n3} & \cdots & a_{nn} \end{pmatrix} \begin{pmatrix} x_1 \\ x_2 \\ x_3 \\ \vdots \\ x_n \end{pmatrix} = \begin{pmatrix} b_1 \\ b_2 \\ b_3 \\ \vdots \\ b_n \end{pmatrix} $$ あるいは, $$ [a] (x) = (b) $$ ここで, $$ [a] = \begin{pmatrix} a_{11} & a_{12} & a_{13} & \cdots & a_{1n} \\ a_{21} & a_{22} & a_{23} & \cdots & a_{2n} \\ a_{31} & a_{32} & a_{33} & \cdots & a_{3n} \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ a_{n1} & a_{n2} & a_{n3} & \cdots & a_{nn} \end{pmatrix}, \ \ \ \ \ (x) = \begin{pmatrix} x_1 \\ x_2 \\ x_3 \\ \vdots \\ x_n \end{pmatrix}, \ \ \ \ \ (b) = \begin{pmatrix} b_1 \\ b_2 \\ b_3 \\ \vdots \\ b_n \end{pmatrix} $$

モジュールのインポート

import numpy as np
from scipy import linalg

Numpy

https://numpy.org/doc/stable/reference/generated/numpy.linalg.solve.html
a = np.array([[3.0, 2.0, 1.0],[5.0, -5.0, 4.0],[6.0, 0.0, 1.0]])
a
array([[ 3.,  2.,  1.],
       [ 5., -5.,  4.],
       [ 6.,  0.,  1.]])
x = np.array([1.0, 2.0, 3.0])
b = a@x
b
array([10.,  7.,  9.])
x = np.linalg.solve(a,b) # numpy
x
array([1., 2., 3.])
a@x
array([10.,  7.,  9.])
np.allclose(np.dot(a, x), b) # 解の確認
True
np.dot(a, x) - b
array([-1.77635684e-15,  0.00000000e+00,  0.00000000e+00])
np.dot(a, x) == b
array([False,  True,  True])

Scipy

https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.solve.html
a2 = np.array([[3,2,3,10],[2,-2,5,8],[3,3,4,9],[3,4,-3,-7]])
a2
array([[ 3,  2,  3, 10],
       [ 2, -2,  5,  8],
       [ 3,  3,  4,  9],
       [ 3,  4, -3, -7]])
x2 = np.array([4,3,2,1])
x2
array([4, 3, 2, 1])
b2 = np.array([4,1,3,2])
b2 = a2@x2
b2
array([34, 20, 38, 11])
x2 = linalg.solve(a2,b2)# scipy
x2
array([4., 3., 2., 1.])
np.allclose(np.dot(a2, x2), b2) # 解の確認
True
np.dot(a2, x2) - b2
array([0.00000000e+00, 3.55271368e-15, 0.00000000e+00, 0.00000000e+00])
np.dot(a2, x2) == b2
array([ True, False,  True,  True])

逆行列

https://numpy.org/doc/stable/reference/generated/numpy.linalg.inv.html
a
array([[ 3.,  2.,  1.],
       [ 5., -5.,  4.],
       [ 6.,  0.,  1.]])
ai = np.linalg.inv(a) # 逆行列の計算
ai
array([[-0.09433962, -0.03773585,  0.24528302],
       [ 0.35849057, -0.05660377, -0.13207547],
       [ 0.56603774,  0.22641509, -0.47169811]])
np.linalg.inv(ai)
array([[ 3.00000000e+00,  2.00000000e+00,  1.00000000e+00],
       [ 5.00000000e+00, -5.00000000e+00,  4.00000000e+00],
       [ 6.00000000e+00,  6.54612632e-18,  1.00000000e+00]])
ai@b
array([1., 2., 3.])
np.dot(ai,b)
array([1., 2., 3.])

行列式

https://numpy.org/doc/stable/reference/generated/numpy.linalg.det.html
det_a = np.linalg.det(a)
det_a
52.999999999999986

行列のランク(階数)

https://numpy.org/doc/stable/reference/generated/numpy.linalg.matrix_rank.html
np.linalg.matrix_rank(a)
3