2020年5月29日金曜日

学習環境

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


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

    よって同値ではない。


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

    同値である。


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

    同値ではない。


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

    同値である。

コード

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

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

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

0 コメント:

コメントを投稿