2017年12月20日水曜日

数学 - Python - もう１つの数学の基盤 - 行列と行列式 – 連立1次方程式と行列式 - n = 3 の場合(行列式、各行と列の加法、スカラー倍、線形性)

1. $\begin{array}{}\mathrm{det}\left(\stackrel{\to }{a},\stackrel{\to }{p},\stackrel{\to }{c}\right)\\ =\mathrm{det}\left(\stackrel{\to }{a},x\stackrel{\to }{a}+y\stackrel{\to }{b}+z\stackrel{\to }{c},\stackrel{\to }{c}\right)\\ =x\mathrm{det}\left(\stackrel{\to }{a},\stackrel{\to }{a},\stackrel{\to }{c}\right)+y\mathrm{det}\left(\stackrel{\to }{a},\stackrel{\to }{b},\stackrel{\to }{c}\right)+z\mathrm{det}\left(\stackrel{\to }{a},\stackrel{\to }{c},\stackrel{\to }{c}\right)\\ =y\mathrm{det}\left(\stackrel{\to }{a},\stackrel{\to }{b},\stackrel{\to }{c}\right)\end{array}$

2. $\begin{array}{}\mathrm{det}\left(\stackrel{\to }{a},\stackrel{\to }{b},\stackrel{\to }{p}\right)\\ =\mathrm{det}\left(\stackrel{\to }{a},\stackrel{\to }{b},x\stackrel{\to }{a}+y\stackrel{\to }{b}+z\stackrel{\to }{c}\right)\\ =x\mathrm{det}\left(\stackrel{\to }{a},\stackrel{\to }{b},\stackrel{\to }{a}\right)+y\mathrm{det}\left(\stackrel{\to }{a},\stackrel{\to }{b},\stackrel{\to }{b}\right)+z\mathrm{det}\left(\stackrel{\to }{a},\stackrel{\to }{b},\stackrel{\to }{c}\right)\\ =z\mathrm{det}\left(\stackrel{\to }{a},\stackrel{\to }{b},\stackrel{\to }{c}\right)\end{array}$

コード(Emacs)

Python 3

#!/usr/bin/env python3
from sympy import pprint, symbols, Matrix

x, y, z = symbols('x, y, z')
a = Matrix(symbols('a1, a2, a3')).reshape(3, 1)
b = Matrix(symbols('b1, b2, b3')).reshape(3, 1)
c = Matrix(symbols('c1, c2, c3')).reshape(3, 1)
p = x * a + y * b + z * c

A = Matrix([[q[i] for q in [a, b, c]]
for i in range(3)])

DA = A.det()
X1 = Matrix([[q[i] for q in [a, p, c]]
for i in range(3)])

DX1 = X1.det()
X2 = Matrix([[q[i] for q in [a, b, p]]
for i in range(3)])
DX2 = X2.det()

for t in [A, DA, X1, DX1, X2, DX2,
DX1.expand() == (y * DA).expand(),
DX2.expand() == (z * DA).expand()]:
pprint(t)
print()


$./sample35.py ⎡a₁ b₁ c₁⎤ ⎢ ⎥ ⎢a₂ b₂ c₂⎥ ⎢ ⎥ ⎣a₃ b₃ c₃⎦ a₁⋅b₂⋅c₃ - a₁⋅b₃⋅c₂ - a₂⋅b₁⋅c₃ + a₂⋅b₃⋅c₁ + a₃⋅b₁⋅c₂ - a₃⋅b₂⋅c₁ ⎡a₁ a₁⋅x + b₁⋅y + c₁⋅z c₁⎤ ⎢ ⎥ ⎢a₂ a₂⋅x + b₂⋅y + c₂⋅z c₂⎥ ⎢ ⎥ ⎣a₃ a₃⋅x + b₃⋅y + c₃⋅z c₃⎦ -a₁⋅c₂⋅(a₃⋅x + b₃⋅y + c₃⋅z) + a₁⋅c₃⋅(a₂⋅x + b₂⋅y + c₂⋅z) + a₂⋅c₁⋅(a₃⋅x + b₃⋅y + c₃⋅z) - a₂⋅c₃⋅(a₁⋅x + b₁⋅y + c₁⋅z) - a₃⋅c₁⋅(a₂⋅x + b₂⋅y + c₂⋅z) + a₃⋅c₂⋅(a₁⋅ x + b₁⋅y + c₁⋅z) ⎡a₁ b₁ a₁⋅x + b₁⋅y + c₁⋅z⎤ ⎢ ⎥ ⎢a₂ b₂ a₂⋅x + b₂⋅y + c₂⋅z⎥ ⎢ ⎥ ⎣a₃ b₃ a₃⋅x + b₃⋅y + c₃⋅z⎦ a₁⋅b₂⋅(a₃⋅x + b₃⋅y + c₃⋅z) - a₁⋅b₃⋅(a₂⋅x + b₂⋅y + c₂⋅z) - a₂⋅b₁⋅(a₃⋅x + b₃⋅y + c₃⋅z) + a₂⋅b₃⋅(a₁⋅x + b₁⋅y + c₁⋅z) + a₃⋅b₁⋅(a₂⋅x + b₂⋅y + c₂⋅z) - a₃⋅b₂⋅(a₁⋅x + b₁⋅y + c₁⋅z) True True$