## 2020年7月8日水曜日

### 数学 - Python - 解析学 - ベクトル - ベクトル積 - 定義

1. $A×B$
$=\left(1,-1,1\right)×\left(-2,3,1\right)$
$=\left(-1-3,-2-1,3-2\right)$
$=\left(-4,-3,1\right)$

2. $\left(-1,2-1,-1\right)=\left(-1,1,-1\right)$

3. $\left(-3-6,3-\left(-3\right),-2-\left(-1\right)\right)=\left(-9,6,-1\right)$

コード

#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import Matrix
from sympy.plotting import plot3d_parametric_line
from sympy.abc import t

print('1, 2, 3.')

a = [(1, -1, 1),
(-1, 1, 2),
(1, 1, -3)]
b = [(-2, 3, 1),
(1, 0, -1),
(-1, -2, -3)]
c = [(-4, -3, 1),
(-1, 1, -1),
(-9,  6, -1)]

class TestCross(TestCase):
def test(self):
for i, (a0, b0, c0) in enumerate(zip(a, b, c), 1):
print(f'({i})')
self.assertEqual(Matrix(a0).cross(Matrix(b0)), Matrix(c0))

p = plot3d_parametric_line(
*[(t * x, t * y, t * z, (t, 0, 1))
for x, y, z in [a[0], b[0], c[0]]],
xlim=(-0.6, 0.4),
ylim=(-0.6, 0.4),
legend=True,
show=False,
)
p.xlabel = 'x'
p.ylabel = 'y'
colors = ['red', 'green', 'blue', 'brown', 'orange',
'purple', 'pink', 'gray', 'skyblue', 'yellow']

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

if __name__ == "__main__":
main()

% ./sample1.py -v
1, 2, 3.
test (__main__.TestCross) ... (1)
(2)
(3)
ok

----------------------------------------------------------------------
Ran 1 test in 0.001s

OK
%