開発環境
- macOS Sierra - Apple (OS)
- Emacs (Text Editor)
- JavaScript (プログラミング言語)
- Node.js, Safari(JavaScript エンジン)
- Learning JavaScript [邦訳](参考書籍)
行列プログラマー(Philip N. Klein (著)、 松田 晃一 (翻訳)、 弓林 司 (翻訳)、 脇本 佑紀 (翻訳)、 中田 洋 (翻訳)、 齋藤 大吾 (翻訳)、オライリージャパン)の3章(ベクトル空間)、3.2(線形包)、3.2.5(標準生成子)のクイズ 3.2.13 を JavaScript で取り組んでみる。
クイズ 3.2.13
コード(Emacs)
HTML5
<pre id="output0"></pre> <button id="run0">run</button> <button id="clear0">clear</button> <script src="sample4.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', range = (start, end, step=1) => { let result = []; for (let i = start; i < end; i += step) { result.push(i); } return result; }; let Vector = (labels, func={}) => { console.log(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 standard = (d, one) => d.map((k) => { let func = {}; func[k] = one; return Vector(d, func); }); let UnitTest = () => { let that = {}, run = () => { Object.keys(that).forEach((key) => { if (that.setUp) { that.setUp(); } if (/^test/.test(key)) { p(key); that[key](); } if (that.tearDown) { 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`); } }, assertThrow = (fn, name) => { try { fn(); p('failure'); } catch (e) { if (e.name === name) { p('ok'); } else { p('failure'); } } }; that.run = run; that.assertEqual = assertEqual; that.assertTrue = assertTrue; that.assertFalse = assertFalse; that.assertThrow = assertThrow; return that; }; let vectorsIsEqual = (vs1, vs2) => vs1.length === vs2.length && vs1.every((v, i) => v.isEqual(vs2[i])); let Test = () => { let that = UnitTest(), v1, v2, v3; that.setUp = () => {}; that.tearDown = () => {}; that.test0 = () => { let d = [], vs = standard(d, 1); that.assertTrue(vectorsIsEqual(vs, standard(d, 1))); }; that.test1 = () => { let d = ['a'], vs = standard(d, 1); that.assertTrue(vectorsIsEqual(vs, [Vector(d, {a: 1})])); }; that.test5 = () => { let d = ['a', 'b', 'c', 'd', 'e'], vs = standard(d, 1); that.assertTrue(vectorsIsEqual(vs, [Vector(d, {a:1}), Vector(d, {b:1}), Vector(d, {c:1}), Vector(d, {d:1}), Vector(d, {e:1})])); }; return that; }; let output = () => { Test().run(); }; btn0.onclick = output; btn1.onclick = () => pre0.textContent = ''; output();
0 コメント:
コメントを投稿