2020年1月4日土曜日

学習環境

解析入門 原書第3版 (S.ラング(著)、松坂 和夫(翻訳)、片山 孝次(翻訳)、岩波書店)の第Ⅵ部(多変数の関数)、第17章(ベクトル)、4(ベクトルのノルム)の練習問題3の解答を求めてみる。



    1. - 2 - 1 1 + 1 - 1 , 1 = - 3 2 - 1 , 1 = 3 2 , - 3 2

    2. 12 16 0 , 4 = 0 , 3

    3. - 2 - 1 + 5 1 + 1 + 1 - 1 , 1 , 1 = - 2 3 , 2 3 , 2 3

    4. 1 - 6 - 12 1 + 9 + 16 - 1 , 3 , - 4 = - 17 26 - 1 , 3 , - 4 = 17 26 , - 51 26 , 34 13

    5. 2 π 2 - 9 - 7 4 π 2 + 9 + 49 2 π , - 3 , 7 = 2 π 2 - 16 4 π 2 + 58 2 π , - 3 , 7 = π 2 - 8 2 π 2 + 29 2 π , - 3 , 7 = 2 π 3 - 16 π 2 π 2 + 29 , - 3 π 2 + 24 2 π 2 + 29 , 7 π 2 - 56 2 π 2 + 29

    6. 15 π - 6 - 4 π 2 + 9 + 1 π , 3 , - 1 = 15 π - 10 π 2 + 10 π , 3 , - 1 = 15 π 2 - 10 π π 2 + 10 , 45 π - 30 π 2 + 10 , - 15 π + 10 π 2 + 10

コード

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

print('3.')


def eq(A, B):
    for a, b in zip(A, B):
        if a.simplify() != b.simplify():
            return False
    return True


class MyTestCase(TestCase):
    def test(self):
        As = [(2, -1),
              (-1, 3),
              (2, -1, 5),
              (-1, -2, 3),
              (pi, 3, -1),
              (15, -2, 4)]
        Bs = [(-1, 1),
              (0, 4),
              (-1, 1, 1),
              (-1, 3, -4),
              (2 * pi, -3, 7),
              (pi, 3, -1)]
        egg = [(Rational(3, 2), -Rational(3, 2)),
               (0, 3),
               (-Rational(2, 3), Rational(2, 3), Rational(2, 3)),
               (Rational(17, 26), -Rational(51, 26), Rational(34, 13)),
               ((2 * pi ** 3 - 16 * pi) / (2 * pi ** 2 + 29), (-3 * pi ** 2 + 24) /
                (2 * pi ** 2 + 29), (7 * pi ** 2 - 56) / (2 * pi ** 2 + 29)),
               ((15 * pi ** 2 - 10 * pi) / (pi ** 2 + 10), (45 * pi - 30) /
                (pi ** 2 + 10), (-15 * pi + 10) / (pi ** 2 + 10))]
        for A, B, C in zip(As, Bs, egg):
            A = Matrix(A)
            B = Matrix(B)
            C = Matrix(C)
            self.assertTrue(eq(A.dot(B) / B.dot(B) * B, C))


if __name__ == '__main__':
    main()

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

% ./sample3.py -v
3.
test (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.878s

OK
%

0 コメント:

コメントを投稿