開発環境
- macOS Sierra - Apple (OS)
- Emacs (Text Editor)
- JavaScript (プログラミング言語)
- Node.js, Safari(JavaScript エンジン)
- Learning JavaScript [邦訳](参考書籍)
行列プログラマー(Philip N. Klein (著)、 松田 晃一 (翻訳)、 弓林 司 (翻訳)、 脇本 佑紀 (翻訳)、 中田 洋 (翻訳)、 齋藤 大吾 (翻訳)、オライリージャパン)の3章(ベクトル空間)、3.1(線形結合)、3.1.3(係数から線形結合へ)のクイズ 3.1.7 を JavaScript で取り組んでみる。
クイズ 3.1.7
コード(Emacs)
HTML5
<pre id="output0"></pre> <button id="run0">run</button> <button id="clear0">clear</button> <script src="sample1.js"></script>
JavaScript
let div0 = document.querySelector('#graph0'), pre0 = document.querySelector('#output0'), btn0 = document.querySelector('#run0'), btn1 = document.querySelector('#clear0'), p = (x) => pre0.textContent += x + '\n'; let Vector = (labels, func={}) => { let that = {}, d = labels, f = func, setItem = (d, val) => { f[d] = val; }, getItem = (d) => { return f[d] === undefined ? 0 : f[d]; }, scalarMul = (a) => { let func = {}; d.forEach((k) => { func[k] = that.getItem(k) * a; }); return Vector(d, func); }, add = (v) => { let func = {}, d0 = d.concat(v.d().filter((x) => d.indexOf(x) === -1)); d0.forEach((d) => { func[d] = that.getItem(d) + v.getItem(d); }); return Vector(d0, func); }, sub = (v) => that.add(v.scalarMul(-1)), neg = () => that.scalarMul(-1), dot = (v) => { return d.map((x) => that.getItem(x) * v.getItem(x)) .reduce((x, y) => x + y); }, isEqual = (v) => { return d.every((x) => that.getItem(x) === v.getItem(x)); }, toString = () => { return '{' + d.map((k) => `${k}: ${that.getItem(k)}`).join(', ') + '}'; }; that.d = () => d; that.f = () => f; that.getItem = getItem; that.setItem = setItem; that.scalarMul = scalarMul; that.add = add; that.sub = sub; that.neg = neg; that.dot = dot; that.isEqual = isEqual; that.toString = toString; return that; }; let arrayToVector = (a) => { let l = [], f = {}; a.forEach((x, i) => { l.push(i); f[i] = x; }); return Vector(l, f); }; let linComb = (vlist, clist) => vlist.reduce((prev, v, i) => prev.add(v.scalarMul(clist[i])), arrayToVector([])); let UnitTest = () => { let that = {}, run = () => { Object.keys(that).forEach((key) => { that.setUp(); if (/^test/.test(key)) { p(key); that[key](); } that.tearDown(); }); }, assertEqual = (x, y) => { if (x === y) { p('ok'); } else { p(`failure - ${x} !== ${y}`); } }, assertTrue = (x) => { if (x) { p('ok'); } else { p(`failure`); } }, assertFalse = (x) => { if (x) { p('failure'); } else { p(`ok`); } }; that.run = run; that.assertEqual = assertEqual; that.assertTrue = assertTrue; that.assertFalse = assertFalse; return that; }; let Test = () => { let that = UnitTest(); that.setUp = () => {}; that.tearDown = () => {}; that.test0 = () => { let vlist = [] clist = []; that.assertTrue(linComb(vlist, clist).isEqual(arrayToVector([]))); }; that.test1 = () => { let vlist = [arrayToVector([0, 1])], clist = [5]; that.assertTrue(linComb(vlist, clist).isEqual(arrayToVector([0, 5]))) }; that.test2 = () => { let vlist = [arrayToVector([0, 1]), arrayToVector([2, 3])], clist = [5, 10]; that.assertTrue(linComb(vlist, clist).isEqual(arrayToVector([20, 35]))); }; that.test5 = () => { let vlist = [arrayToVector([0, 1]), arrayToVector([2, 3]), arrayToVector([4, 5]), arrayToVector([6, 7]), arrayToVector([8, 9])], clist = [5, 4, 3, 2, 1]; that.assertTrue(linComb(vlist, clist).isEqual( arrayToVector([40, 55]))); }; return that; }; let output = () => { Test().run(); }; btn0.onclick = output; btn1.onclick = () => pre0.textContent = ''; output();
0 コメント:
コメントを投稿