2019年12月16日月曜日

数学 - Python - 線形代数学 - R^nにおけるベクトル - 複素数 - 除法、逆数、共役、絶対値の平方

ラング線形代数学(上) (ちくま学現文庫)(S.ラング (著)、芹沢 正三 (翻訳)、筑摩書房)の1章(R^nにおけるベクトル)、6(複素数)、練習問題2の解答を求めてみる。

1. $\begin{array}{l}{\left(1+i\right)}^{-1}\\ =\frac{1-i}{1+1}\\ =\frac{1}{2}-\frac{1}{2}i\end{array}$

2. $\begin{array}{l}\frac{1}{3+i}\\ =\frac{3-i}{9+1}\\ =\frac{3}{10}-\frac{1}{10}i\end{array}$

3. $\begin{array}{l}\frac{2+i}{2-i}\\ =\frac{{\left(2+i\right)}^{2}}{4+1}\\ =\frac{3}{5}+\frac{4}{5}i\end{array}$

4. $\begin{array}{l}\frac{1}{2-i}\\ =\frac{2+i}{4+1}\\ =\frac{2}{5}+\frac{1}{5}i\end{array}$

5. $\begin{array}{l}\frac{1+i}{i}\\ =\left(1+i\right)\left(-i\right)\\ =1-i\end{array}$

6. $\begin{array}{l}\frac{i}{1+i}\\ =\frac{i\left(1-i\right)}{1+1}\\ =\frac{1}{2}+\frac{1}{2}i\end{array}$

7. $\begin{array}{l}\frac{2i}{3-i}\\ =\frac{2i\left(3+i\right)}{9+1}\\ =-\frac{1}{5}+\frac{3}{5}i\end{array}$

8. $\begin{array}{l}\frac{1}{-1+i}\\ =\frac{-1-i}{1+1}\\ =-\frac{1}{2}-\frac{1}{2}i\end{array}$

コード

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

print('2.')

class MyTestCase(TestCase):
def test(self):
spam = [(1 + I) ** -1,
1 / (3 + I),
(2 + I) / (2 - I),
1 / (2 - I),
(1 + I) / I,
I / (1 + I),
2 * I / (3 - I),
1 / (-1 + I)]
egg = [(Rational(1, 2), -Rational(1, 2)),
(Rational(3, 10), -Rational(1, 10)),
(Rational(3, 5), Rational(4, 5)),
(Rational(2, 5), Rational(1, 5)),
(1, -1),
(Rational(1, 2), Rational(1, 2)),
(-Rational(1, 5), Rational(3, 5)),
(-Rational(1, 2), -Rational(1, 2))]
for s, (x, y) in zip(spam, egg):
self.assertEqual(s.expand(), (x + I * y).expand())

if __name__ == '__main__':
main()


% ./sample2.py -v
2.
test (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.009s

OK
%