2020年5月29日金曜日

数学 - Pytyhon - 解析学 - ベクトル - 定置ベクトル - 同値

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

よって同値ではない。

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

同値である。

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

同値ではない。

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

同値である。

コード

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

print('1, 2, 3, 4.')

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 [(5, 2), (1, 8)]]
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 [(0, 5, 10), (-5, 3, 8)]]

class Test(TestCase):
def test2(self):
for p, q, a, b, bln in zip(ps2, qs2, as2, bs2, [False, True]):
self.assertEqual(q - p == b - a, bln)

def test3(self):
for p, q, a, b, bln in zip(ps3, qs3, as3, bs3, [False, True]):
self.assertEqual(q - p == b - a, bln)

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('sample1_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('sample1_3.png')
p3.show()

if __name__ == "__main__":
main()


% ./sample1.py -v
1, 2, 3, 4.
/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.001s

OK
%