2020年6月23日火曜日

学習環境

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


  1. 問題の2直線の法線ベクトルはそれぞれ

    ( 3 , - 5 ) , ( 2 , 3 )

    この内積は

    6 - 15 0

    よって2直線は直交しない。



    1. ( 3 , - 5 ) · ( 2 , 1 ) = 6 - 5 0

      直交しない。


    2. ( 2 , 7 ) · ( 1 , - 1 ) = 2 - 7 0

      直交しない。


    3. ( 3 , - 5 ) · ( 5 , 3 ) = 15 - 15 = 0

      直交する。


    4. ( - 1 , 1 ) · ( 1 , 1 ) = 0

      直交する。

コード

#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import plot, Matrix
from sympy.abc import x

print('5, 6.')


class Test(TestCase):
    def test5(self):
        self.assertNotEqual(
            Matrix([3, -5]).dot(Matrix([2, 3])),
            0
        )

    def test6(self):
        ABs = [(Matrix(a), Matrix(b)) for a, b in [((3, -5), (2, 1)),
                                                   ((2, 7), (1, -1)),
                                                   ((3, -5), (5, 3)),
                                                   ((-1, 1), (1, 1))]]

        for i, (A, B) in enumerate(ABs[:2]):
            print(f'({chr(ord("a") + i)})')
            self.assertNotEqual(A.dot(B), 0)
        for i, (A, B) in enumerate(ABs[2:]):
            print(f'({chr(ord("c") + i)})')
            self.assertEqual(A.dot(B), 0)


p = plot((3 * x - 1) / 5,
         (-2 * x + 5) / 3,
         -2 * x + 2,
         (-2 * x + 1) / 7,
         x - 5,
         (-5 * x + 7) / 3,
         x + 2,
         -x + 9,
         (x, -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(p, colors):
    o.line_color = color
p.save(f'sample5.png')
p.show()

if __name__ == "__main__":
    main()

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

% ./sample5.py -v
5, 6.
test5 (__main__.Test) ... ok
test6 (__main__.Test) ... (a)
(b)
(c)
(d)
ok

----------------------------------------------------------------------
Ran 2 tests in 0.001s

OK
%

0 コメント:

コメントを投稿