開発環境
- macOS Sierra - Apple (OS)
- Emacs (Text Editor)
- JavaScript (プログラミング言語)
- Node.js, Safari(JavaScript エンジン)
- Learning JavaScript [邦訳](参考書籍)
行列プログラマー(Philip N. Klein (著)、 松田 晃一 (翻訳)、 弓林 司 (翻訳)、 脇本 佑紀 (翻訳)、 中田 洋 (翻訳)、 齋藤 大吾 (翻訳)、オライリージャパン)の2章(ベクトル)、2.11(上三角線形方程式を解く)、2.11.5(任意の定義域のベクトルを用いた後退代入) を JavaScript で取り組んでみる。
練習問題 2.11.4
コード(Emacs)
HTML5
<pre id="output0"></pre> <button id="run0">run</button> <button id="clear0">clear</button> <script src="sample11_5.js"></script>
JavaScript
let btn0 = document.querySelector('#run0'), btn1 = document.querySelector('#clear0'), pre0 = document.querySelector('#output0'), 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={}) => { 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] = f[k] * a; }); return Vector(d, func); }, add = (v) => { let func = {}; d.forEach((k) => { func[k] = f[k] + k.getItem(k); }); return Vector(d, func); }, neg = () => that.scalarMul(-1), dot = (v) => { return d.map((i) => that.getItem(i) * v.getItem(i)) .reduce((x, y) => x + y); }, isEqual = (v) => { return d.every((x, i) => that.getItem(i) === v.getItem(i)); }, 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.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 triangularSolveN = (rowArray, b) => { let d = rowArray[0].d(), n = d.length, x = Vector(d); range(0, n).reverse().forEach((i) => { x.setItem(i, (b[i] - rowArray[i].dot(x)) / rowArray[i].getItem(i)); }); return x; }; let triangularSolve = (rows, labels, b) => { let d = rows[0].d(), x = Vector(labels); range(0, d.length).reverse().forEach((j) => { let c = labels[j], row = rows[j]; x.setItem(c, (b[j] - row.dot(x)) / row.getItem(c)); }); return x; }; let output = () => { let labels = ['a', 'b', 'c', 'd'], rows = [Vector(labels, {a:4, b:-2, c:0.5, d:1}), Vector(labels, {b:2, c:3, d:3}), Vector(labels, {c:5, d:1}), Vector(labels, {d:2})], b = [6, -4, 3, -8]; let v = triangularSolve(rows, labels, b); p(v); p(v.isEqual(Vector(labels, {a: 3.275, b:1.9, c:1.4, d:-0.4}))); }; btn0.onclick = output; btn1.onclick = () => pre0.textContent = ''; output();
0 コメント:
コメントを投稿