2020年5月30日土曜日

学習環境

続 解析入門 (原書第2版) (S.ラング(著)、松坂 和夫(翻訳)、片山 孝次(翻訳)、岩波書店)の第1章(ベクトル)、2(定置ベクトル)の練習問題5、6、7、8の解答を求めてみる。


  1. Q - P = 3 , 4 B - A = 8 , - 4

    平行ではない。


  2. Q - P = - 4 , 1 B - A = 4 , - 1 Q - P = - B - A

    平行である。


  3. Q - P = - 3 , 4 , - 9 B - A = - 6 , 8 , - 18 B - A = 2 Q - P

    平行である。


  4. Q - P = - 3 , 0 , 9 B - A = - 9 , 0 , - 29

    平行ではない。

コード

#!/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()

入出力結果(Zsh、PowerShell、Terminal、Jupyter(IPython))

% ./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
%

0 コメント:

コメントを投稿