開発環境
- macOS Sierra - Apple (OS)
- Emacs (Text Editor)
- Python 3.6 (プログラミング言語)
行列プログラマー(Philip N. Klein (著)、 松田 晃一 (翻訳)、 弓林 司 (翻訳)、 脇本 佑紀 (翻訳)、 中田 洋 (翻訳)、 齋藤 大吾 (翻訳)、オライリージャパン)の4章(行列)、4.1(行列とは何か?)、4.1.5(単位行列)、列の辞書表現、クイズ4.1.7、4.1.8を取り組んでみる。
クイズ4.1.7、4.1.8
コード(Emacs)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
class Mat:
def __init__(self, labels, function):
self.D = labels
self.f = function
def __str__(self):
return 'Mat({0}, {1})'.format(labels, function)
print('4.1.7')
s = {'a', 'b', 'c'}
labels = (s, s)
function = {(i, i): 1 for i in labels[0]}
m = Mat(labels, function)
print(m)
print('4.1.8')
def identity(D):
return Mat((D, D), {(i, i): 1 for i in D})
D = {'a', 'b', 'c'}
print(identity(D))
入出力結果(Terminal, IPython)
$ ./sample5.py
4.1.7
Mat(({'c', 'b', 'a'}, {'c', 'b', 'a'}), {('c', 'c'): 1, ('b', 'b'): 1, ('a', 'a'): 1})
4.1.8
Mat(({'c', 'b', 'a'}, {'c', 'b', 'a'}), {('c', 'c'): 1, ('b', 'b'): 1, ('a', 'a'): 1})
$
0 コメント:
コメントを投稿