## 2020年5月24日日曜日

### 数学 - Pytyhon - 解析学 - ベクトル - 空間の点の定義 - 座標、加法、乗法、グラフ

1. $\begin{array}{l}A+B\\ =\left(2,-1\right)+\left(-1,1\right)\\ =\left(1,0\right)\\ A-B\\ =\left(2,-1\right)+\left(1,-1\right)\\ =\left(3,-2\right)\\ 3A=\left(6,-3\right)\\ -2B=\left(2,-2\right)\end{array}$

2. $\begin{array}{l}A+B=\left(-1,7\right)\\ A-B=\left(-1,-1\right)\\ 3A=\left(-3,9\right)\\ -2B=\left(0,-8\right)\end{array}$

3. $\begin{array}{l}A+B=\left(1,0,6\right)\\ A-B=\left(3,-2,4\right)\\ 3A=\left(6,-3,15\right)\\ -2B=\left(2,-2,-2\right)\end{array}$

4. $\begin{array}{l}A+B=\left(-2,1,-1\right)\\ A-B=\left(0,-5,7\right)\\ 3A=\left(-3,-6,9\right)\\ -2B=\left(2,-6,8\right)\end{array}$

5. $\begin{array}{l}A+B=\left(3\pi ,0,6\right)\\ A-B=\left(-\pi ,6,-8\right)\\ 3A=\left(3\pi ,9,-3\right)\\ -2B=\left(-4\pi ,6,-14\right)\end{array}$

6. $\begin{array}{l}A+B=\left(15+\pi ,1,3\right)\\ A-B=\left(15-\pi ,-5,5\right)\\ 3A=\left(45,-6,12\right)\\ -2B=\left(-2\pi ,-6,2\right)\end{array}$

コード

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

print('1.')

class TextVector(TestCase):
def test(self):
AS = [[2, -1],
[-1, 3],
[2, -1, 5],
[-1, -2, 3],
[pi, 3, -1],
[15, -2, 4]]
BS = [[-1, 1],
[0, 4],
[-1, 1, 1],
[-1, 3, -4],
[2 * pi, -3, 7],
[pi, 3, -1]]
CS = [[1, 0],
[-1, 7],
[1, 0, 6],
[-2, 1, -1],
[3 * pi, 0, 6],
[15 + pi, 1, 3]]
DS = [[3, -2],
[-1, -1],
[3, -2, 4],
[0, -5, 7],
[-pi, 6, -8],
[15 - pi, -5, 5]]
ES = [[6, -3],
[-3, 9],
[6, -3, 15],
[-3, -6, 9],
[3 * pi, 9, -3],
[45, -6, 12]]
FS = [[2, -2],
[0, -8],
[2, -2, -2],
[2, -6, 8],
[-4 * pi, 6, -14],
[-2 * pi, -6, 2]]
for i, (A, B, C, D, E, F) in enumerate(zip(AS, BS, CS, DS, ES, FS), 1):
print(f'({i})')
A = Matrix(A)
B = Matrix(B)
self.assertEqual(A + B, Matrix(C))
self.assertEqual(A - B, Matrix(D))
self.assertEqual(3 * A, Matrix(E))
self.assertEqual(-2 * B, Matrix(F))

t = symbols('t')
p = plot_parametric(*[(t * x, t * y, (t, 0, 1))
for x, y in [(2, -1), (-1, 1), (3, -2)]],
(2 + t, -1 - t, (t, 0, 1)),
xlim=(-5, 5),
ylim=(-5, 5),
legend=True,
show=False)
colors = ['red', 'green', 'blue', 'brown', 'orange',
'purple', 'pink', 'gray', 'skyblue', 'yellow']

for o, color in zip(p, colors):
o.line_color = color
p.show()
p.save('sample1.png')

if __name__ == "__main__":
main()


% ./sample1.py -v
1.
test (__main__.TextVector) ... (1)
(2)
(3)
(4)
(5)
(6)
ok

----------------------------------------------------------------------
Ran 1 test in 0.004s

OK
%