2020年1月31日金曜日

学習環境

ラング線形代数学(上) (ちくま学現文庫)(S.ラング (著)、芹沢 正三 (翻訳)、筑摩書房)の3章(行列)、1(1次方程式)、練習問題4の解答を求めてみる。



    1. - x - 2 i y = i x + i y = 2 - i y = 2 + i y = - 1 + 2 i x = 2 - i y = 2 - i - 1 + 2 i = 4 + i

    2. { 2 x + i y - 1 + i z = 1 2 x - 4 y + 2 i z = 0 2 x + 2 i y - 2 + 4 i z = 2 i i + 4 y + - 1 - 3 i z = 1 i y + - 1 - 3 i z = 2 i - 1 4 y = 2 - 2 i y = 1 - i 2 i · 1 - i 2 + - 1 - 3 i z = 2 i - 1 - 1 - 3 i z = 2 i - 1 - i + 1 2 - 1 - 3 i z = - 3 + 3 i 2 z = 3 1 - i 2 1 + 3 i = 3 1 - i 1 - 3 i 2 1 + 9 = 3 - 2 - 4 i 20 = - 3 - 6 i 10 x = 2 y - i z = 1 - i + 3 i - 6 10 = 4 - 7 i 10

    3. 1 + 2 i x = 3 - i x = 3 - i 1 + 2 i = 3 - i 1 - 2 i 5 = 1 - 7 i 5 y = 1 + i 1 - 7 i 5 = 8 - 6 i 5

    4. i x - 2 + i y = 1 i x + 1 + 2 i y = - 1 + i - 3 - 3 i y = 2 - i y = 2 - i - 3 1 + i = 2 - i 1 - i - 3 · 2 . . = 1 - 3 i - 3 · 2 = - 1 + 3 i 6 x = 1 + i - 2 - i · - 1 + 3 i 6 = 1 + i - 1 + 7 i 6 = 5 - i 6

コード

#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import symbols, solve, Rational, I

print('4.')

x, y, z = symbols('x, y, z', imag=True)


class MyTestCase(TestCase):
    def test_a(self):
        s = solve((I * x - 2 * y - 1, x + I * y - 2))
        self.assertEqual(s[x].expand(), 4 + I)
        self.assertEqual(s[y].expand(), -1 + 2 * I)

    def test_b(self):
        self.assertEqual(solve((2 * x + I * y - (1 + I) * z - 1,
                                x - 2 * y + I * z,
                                -I * x + y - (2 - I) * z - 1)),
                         {x: (4 - 7 * I) / 10, y: (1 - I) / 2, z: (-3 - 6 * I) / 10})

    def test_c(self):
        s = solve(((1 + I) * x - y,
                   I * x + y - (3 - I)))
        self.assertEqual(s[x].expand(), ((1 - 7 * I) / 5).expand())
        self.assertEqual(s[y].expand(), ((8 - 6 * I) / 5).expand())

    def test_d(self):
        s = solve((I * x - (2 + I) * y - 1,
                   x + (2 - I) * y - (1 + I)))
        self.assertEqual(s[x].expand(), ((5 - I) / 6).expand())
        self.assertEqual(s[y].expand(), ((-1 + 3 * I) / 6).expand())


if __name__ == '__main__':
    main()

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

% ./sample4.py -v
4.
test_a (__main__.MyTestCase) ... ok
test_b (__main__.MyTestCase) ... ok
test_c (__main__.MyTestCase) ... ok
test_d (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 4 tests in 0.729s

OK
%

0 コメント:

コメントを投稿