2020年1月5日日曜日

学習環境

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



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

    2. 12 1 + 9 - 1 , 3 = 6 5 - 1 , 3 = - 6 5 , 18 5

    3. - 2 - 1 + 5 4 + 1 + 25 2 , - 1 , 5 = 2 30 2 , - 1 , 5 = 2 15 , - 1 15 , 1 3

    4. 1 - 6 - 12 1 + 4 + 9 - 1 , - 2 , 3 = - 17 14 - 1 , - 2 , 3 = 17 14 , 17 7 , - 51 14

    5. 2 π 2 - 9 - 7 π 2 + 9 + 1 π , 3 , - 1 = 2 π 2 - 16 π 2 + 10 π , 3 , - 1

    6. 15 π - 6 - 4 225 + 4 + 16 15 , - 2 , 4 = 15 π - 10 245 15 , - 2 , 4 = 3 π - 2 49 15 , - 2 , 4

コード

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

print('4.')


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(6, 5), Rational(3, 5)),
               (-Rational(6, 5), Rational(18, 5)),
               (Rational(2, 15), -Rational(1, 15), Rational(1, 3)),
               (Rational(17, 14), Rational(17, 7), -Rational(51, 14)),
               [(2 * pi ** 2 - 16) / (pi ** 2 + 10) * x for x in [pi, 3, -1]],
               [(3 * pi - 2) / 49 * x for x in [15, -2, 4]]]
        for A, B, C in zip(As, Bs, egg):
            A = Matrix(A)
            B = Matrix(B)
            C = Matrix(C)
            self.assertTrue(eq(B.dot(A) / A.dot(A) * A, C))


if __name__ == '__main__':
    main()

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

% ./sample4.py -v
4.
test (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.644s

OK
%

0 コメント:

コメントを投稿