## 2020年5月30日土曜日

### 数学 - Pytyhon - 解析学 - ベクトル - 定置ベクトル - 平行、向き

1. $\begin{array}{l}Q-P=\left(3,4\right)\\ B-A=\left(8,-4\right)\end{array}$

平行ではない。

2. $\begin{array}{l}Q-P=\left(-4,1\right)\\ B-A=\left(4,-1\right)\\ Q-P=-\left(B-A\right)\end{array}$

平行である。

3. $\begin{array}{l}Q-P=\left(-3,4,-9\right)\\ B-A=\left(-6,8,-18\right)\\ B-A=2\left(Q-P\right)\end{array}$

平行である。

4. $\begin{array}{l}Q-P=\left(-3,0,9\right)\\ B-A=\left(-9,0,-29\right)\end{array}$

平行ではない。

コード

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

print('5, 6, 7, 8.')

ps2 = [Matrix(p) for p in [(1, -1), (1, 4)]]
qs2 = [Matrix(q) for q in [(4, 3), (-3, 5)]]
as2 = [Matrix(a) for a in [(-1, 5), (5, 7)]]
bs2 = [Matrix(b) for b in [(7, 1), (9, 6)]]
ps3 = [Matrix(p) for p in [(1, -1, 5), (2, 3, -4)]]
qs3 = [Matrix(q) for q in [(-2, 3, -4), (-1, 3, 5)]]
as3 = [Matrix(a) for a in [(3, 1, 1), (-2, 3, 1)]]
bs3 = [Matrix(b) for b in [(-3, 9, -17), (-11, 3, -28)]]
c = symbols('c')

class Test(TestCase):
def test2(self):
for p, q, a, b, n in zip(ps2, qs2, as2, bs2, [0, 1]):
self.assertEqual(len(solve((q - p) - c * (b - a))), n)

def test3(self):
for p, q, a, b, n in zip(ps3, qs3, as3, bs3, [1, 0]):
self.assertEqual(len(solve((q - p) - c * (b - a))), n)

t = symbols('t')
p2 = plot_parametric(*[(*(p + t * (q - p)), (t, 0, 1))
for p, q in zip(ps2, qs2)],
*[(*(a + t * (b - a)), (t, 0, 1))
for a, b in zip(as2, bs2)],
xlim=(-10, 10),
ylim=(-10, 10),
legend=True,
show=False)
colors = ['red', 'green', 'blue', 'brown', 'orange',
'purple', 'pink', 'gray', 'skyblue', 'yellow']

for o, color in zip(p2, colors):
o.line_color = color
p2.save('sample5_2.png')

p3 = plot3d_parametric_line(*[(*(p + t * (q - p)), (t, 0, 1))
for p, q in zip(ps3[:1], qs3[:1])],
*[(*(a + t * (b - a)), (t, 0, 1))
for a, b in zip(as3[:1], bs3[:1])],
xlim=(-10, 10),
ylim=(-10, 10),
legend=True,
show=False)
colors = ['red', 'green', 'blue', 'brown', 'orange',
'purple', 'pink', 'gray', 'skyblue', 'yellow']

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

if __name__ == "__main__":
main()


% ./sample5.py -v
5, 6, 7, 8.
/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/sympy/plotting/plot.py:1065: MatplotlibDeprecationWarning:
The set_smart_bounds function was deprecated in Matplotlib 3.2 and will be removed two minor releases later.
self.ax[i].spines['left'].set_smart_bounds(True)
/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/sympy/plotting/plot.py:1066: MatplotlibDeprecationWarning:
The set_smart_bounds function was deprecated in Matplotlib 3.2 and will be removed two minor releases later.
self.ax[i].spines['bottom'].set_smart_bounds(False)
test2 (__main__.Test) ... ok
test3 (__main__.Test) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.022s

OK
%