2020年6月2日火曜日

学習環境

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



    1. A · A = 2 , - 1 · 2 , - 1 = 4 + 1 = 5

    2. A · A = 1 + 9 = 10

    3. 4 + 1 + 25 = 30

    4. 1 + 4 + 9 = 14

    5. π 2 + 9 + 1 = π 2 + 10

    6. 225 + 4 + 16 = 245


    1. A · B = 2 , - 1 · - 1 , 1 = - 2 - 1 = - 3

    2. 12

    3. - 2 - 1 + 5 = 2

    4. 1 - 6 - 12 = - 17

    5. 2 π 2 - 9 - 7 = 2 π 2 - 16

    6. 15 π - 6 - 4 = 15 π - 10

コード

#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import symbols, Matrix, pi

print('1, 2.')

As = [Matrix(t) for t in [(2, -1), (-1, 3), (2, -1, 5),
                          (-1, -2, 3), (pi, 3, -1), (15, -2, 4)]]
Bs = [Matrix(t) for t in [(-1, 1), (0, 4), (-1, 1, 1),
                          (-1, 3, -4), (2 * pi, -3, 7), (pi, 3, -1)]]


class TestScalarProduct(TestCase):
    def test1(self):
        ns = [5, 10, 30, 14, pi ** 2 + 10, 245]
        for i, (A, n) in enumerate(zip(As, ns)):
            print(f'({chr(ord("a") + i)})')
            self.assertEqual(A.dot(A), n)

    def test2(self):
        ns = [-3, 12, 2, -17, 2 * pi ** 2 - 16, 15 * pi - 10]
        for i, (A, B, n) in enumerate(zip(As, Bs, ns)):
            print(f'({chr(ord("a") + i)})')
            self.assertEqual(A.dot(B), n)


if __name__ == "__main__":
    main()

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

% ./sample1.py -v
1, 2.
test1 (__main__.TestScalarProduct) ... (a)
(b)
(c)
(d)
(e)
(f)
ok
test2 (__main__.TestScalarProduct) ... (a)
(b)
(c)
(d)
(e)
(f)
ok

----------------------------------------------------------------------
Ran 2 tests in 0.037s

OK
%

0 コメント:

コメントを投稿