## 2020年6月3日水曜日

### 数学 - Pytyhon - 解析学 - ベクトル - スカラー積 - 性質、可換律、スカラー倍、和と差、それ自身の内積

1. $\begin{array}{l}{\left(A+B\right)}^{2}\\ =\left(A+B\right)·\left(A+B\right)\end{array}$

性質 S P 2 より

$\begin{array}{l}=\left(A+B\right)·A+\left(A+B\right)·B\\ =A·A+B·A+A·B+B·B\\ ={A}^{2}+B·A+A·B+{B}^{2}\end{array}$

性質 SP 1より

$\begin{array}{l}={A}^{2}+A·B+A·B+{B}^{2}\\ ={A}^{2}+2A·B+{B}^{2}\end{array}$

また、 B を一 B に置き換えると、

$\begin{array}{l}{\left(A-B\right)}^{2}\\ ={\left(A+\left(-B\right)\right)}^{2}\\ ={A}^{2}+2A·\left(-B\right)+{\left(-B\right)}^{2}\end{array}$

性質 SP 3より

$\begin{array}{l}={A}^{2}-2A·B+\left(-B\right)·\left(-B\right)\\ ={A}^{2}-2A·B+{\left(-1\right)}^{2}B·B\\ ={A}^{2}-2A·B+{B}^{2}\end{array}$

よって、

$\begin{array}{l}{\left(A+B\right)}^{2}={A}^{2}+2A·B+{B}^{2}\\ {\left(A-B\right)}^{2}={A}^{2}-2A·B+{B}^{2}\end{array}$

（証明終）

コード

#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import symbols, Matrix, pi

print('3.')

As = [Matrix(t) for t in [(2, -1), (-1, 3), (2, -1, 5),
(-1, -2, 3), (pi, 3, -1), (15, -2, 4)]]
Bs = [Matrix(t) for t in [(-1, 1), (0, 4), (-1, 1, 1),
(-1, 3, -4), (2 * pi, -3, 7), (pi, 3, -1)]]

class TestScalarProduct(TestCase):
def test(self):
for A, B in zip(As, Bs):
self.assertEqual((A + B).dot(A + B).expand(),
A.dot(A) + 2 * A.dot(B) + B.dot(B))
self.assertEqual((A - B).dot(A - B).expand(),
A.dot(A) - 2 * A.dot(B) + B.dot(B))

if __name__ == "__main__":
main()

% ./sample3.py -v
3.
test (__main__.TestScalarProduct) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.045s

OK
%