2017年4月19日水曜日

開発環境

行列プログラマー(Philip N. Klein (著)、 松田 晃一 (翻訳)、 弓林 司 (翻訳)、 脇本 佑紀 (翻訳)、 中田 洋 (翻訳)、 齋藤 大吾 (翻訳)、オライリージャパン)の2章(ベクトル)、2.11(上三角線形方程式を解く)、2.11.2(後退代入)、練習問題 2.11.4 を JavaScript で取り組んでみる。

練習問題 2.11.4

コード(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);
        },
        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 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();












						

0 コメント:

コメントを投稿