## 2017年4月19日水曜日

### JavaScript - 数学 - ベクトル - 上三角線形方程式を解く - 後退代入、実装(加法、反転、ドット積、スカラー倍、等号)

コード(Emacs)

HTML5

```<button id="run0">run</button>
<button id="clear0">clear</button>
<pre id="output0"></pre>

<script src="sample11_4.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);
},
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.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 output = () => {
let rowArray = [[1, 0.5, -2, 4],
[0, 3, 3, 2],
[0, 0, 1, 5],
[0, 0, 0, 2]].map(arrayToVector),
b = [-8, 3, -4, 6],
x = triangularSolveN(rowArray, b);

p(x);
p(x.isEqual(Vector([0, 1, 2, 3], {0:-67, 1:18, 2:-19, 3:3})));
};

btn0.onclick = output;
btn1.onclick = () => pre0.textContent = '';

output();
```