2017年5月10日水曜日

開発環境

行列プログラマー(Philip N. Klein (著)、 松田 晃一 (翻訳)、 弓林 司 (翻訳)、 脇本 佑紀 (翻訳)、 中田 洋 (翻訳)、 齋藤 大吾 (翻訳)、オライリージャパン)の3章(ベクトル空間)、3.2(線形包)、3.2.4(線形結合の線形結合)のクイズ 3.2.12 を JavaScript で取り組んでみる。

クイズ 3.2.12

コード(Emacs)

HTML5

<pre id="output0"></pre>

<button id="run0">run</button>
<button id="clear0">clear</button>

<script src="sample3.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={}) => {
    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 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 Test = () => {
    let that = UnitTest(),
        v1, v2, v3;

    that.setUp = () => {
        v1 = arrayToVector([2, 0, 1]);
        v2 = arrayToVector([1, 0, 2]);
        v3 = arrayToVector([2, 2, 2]);
    };

    that.test1 = () => {
        let v = arrayToVector([3, 0, 0]),
            a1 = 2,
            a2 = -1,
            a3 = 0;

        that.assertTrue(v.isEqual(
            v1.scalarMul(a1).add(
                v2.scalarMul(a2)).add(
                    v3.scalarMul(a3))));
    };
    that.test2 = () => {
        let v = arrayToVector([0, 2, 0]),
            a1 = -2 / 3,
            a2 = -2 / 3,
            a3 = 1;

        that.assertTrue(v.isEqual(
            v1.scalarMul(a1).add(
                v2.scalarMul(a2)).add(
                    v3.scalarMul(a3))));
    };
    that.test3 = () => {
        let v = arrayToVector([0, 0, 1]),
            a1 = -1 / 3,
            a2 = 2 / 3,
            a3 = 0;

        that.assertTrue(v.isEqual(
            v1.scalarMul(a1).add(
                v2.scalarMul(a2)).add(
                    v3.scalarMul(a3))));
    };

    return that;
};
let output = () => {
    Test().run();
};

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














						

0 コメント:

コメントを投稿