学習環境
- Surface
- Windows 10 Pro (OS)
- Nebo(Windows アプリ)
- iPad
- MyScript Nebo - MyScript(iPad アプリ(iOS))
- 参考書籍
続 解析入門 (原書第2版) (S.ラング(著)、松坂 和夫(翻訳)、片山 孝次(翻訳)、岩波書店)の第1章(ベクトル)、4(ベクトルのノルム)の練習問題3、4の解答を求めてみる。
コード
#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import symbols, Matrix, pi, Rational
print('3, 4.')
def eq(a, b):
for a0, b0 in zip(a, b):
if a0.simplify() != b0.simplify():
return False
return True
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 TestProjection(TestCase):
def test_b(self):
projs = [Matrix(t)
for t in [(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 i, (A, B, proj) in enumerate(zip(As, Bs, projs)):
print(f'({chr(ord("a") + i)})')
self.assertTrue(eq(A.dot(B) / B.dot(B) * B, proj))
def test_a(self):
projs = [Matrix(t) for t in [
(-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 ** 3 - 16 * pi) / (pi ** 2 + 10),
(6 * pi ** 2 - 48) / (pi ** 2 + 10),
(-2 * pi ** 2 + 16) / (pi ** 2 + 10)),
((45 * pi - 30) / 49, (-6 * pi + 4) / 49, (12 * pi - 8) / 49)]]
for i, (A, B, proj) in enumerate(zip(As, Bs, projs)):
print(f'({chr(ord("a") + i)})')
self.assertTrue(eq(B.dot(A) / A.dot(A) * A, proj))
if __name__ == "__main__":
main()
入出力結果(Zsh、PowerShell、Terminal、Jupyter(IPython))
% ./sample3.py -v
3, 4.
test_a (__main__.TestProjection) ... (a)
(b)
(c)
(d)
(e)
(f)
ok
test_b (__main__.TestProjection) ... (a)
(b)
(c)
(d)
(e)
(f)
ok
----------------------------------------------------------------------
Ran 2 tests in 1.044s
OK
%
0 コメント:
コメントを投稿