学習環境
- Surface
- Windows 10 Pro (OS)
- Nebo(Windows アプリ)
- iPad
- MyScript Nebo - MyScript(iPad アプリ(iOS))
- 参考書籍
ラング線形代数学(上) (ちくま学現文庫)(S.ラング (著)、芹沢 正三 (翻訳)、筑摩書房)の3章(行列)、2(行列の積)、練習問題11の解答を求めてみる。
4x4行列の場合。
コード
#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import Matrix
print('11.')
def f(i, j):
if i < j:
return 1
return 0
A3 = Matrix([[f(i, j) for j in range(3)]
for i in range(3)])
A4 = Matrix([[f(i, j) for j in range(4)]
for i in range(4)])
class MyTestCase(TestCase):
def test3_2(self):
self.assertEqual(A3 ** 2,
Matrix([[0, 0, 1],
[0, 0, 0],
[0, 0, 0]]))
def test3_3(self):
self.assertEqual(A3 ** 3,
Matrix([[0 for _ in range(3)]
for _ in range(3)]))
def test4_2(self):
self.assertEqual(A4 ** 2,
Matrix([[0, 0, 1, 2],
[0, 0, 0, 1],
[0, 0, 0, 0],
[0, 0, 0, 0]]))
def test4_3(self):
self.assertEqual(A4 ** 3,
Matrix([[0, 0, 0, 1],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]))
def test4_4(self):
self.assertEqual(A4 ** 4,
Matrix([[0 for _ in range(4)]
for _ in range(4)]))
if __name__ == '__main__':
main()
入出力結果(Zsh、PowerShell、Terminal、Jupyter(IPython))
% ./sample11.py -v
11.
test3_2 (__main__.MyTestCase) ... ok
test3_3 (__main__.MyTestCase) ... ok
test4_2 (__main__.MyTestCase) ... ok
test4_3 (__main__.MyTestCase) ... ok
----------------------------------------------------------------------
Ran 4 tests in 0.043s
OK
%
0 コメント:
コメントを投稿