2017年5月28日日曜日

開発環境

行列プログラマー(Philip N. Klein (著)、 松田 晃一 (翻訳)、 弓林 司 (翻訳)、 脇本 佑紀 (翻訳)、 中田 洋 (翻訳)、 齋藤 大吾 (翻訳)、オライリージャパン)の3章(ベクトル空間)、3.8(問題)、Vec の確認(コンテナの中のベクトル)問題3.8.1、3.8.2を JavaScript で取り組んでみる。

問題3.8.1、3.8.2

コード(Emacs)

HTML5

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

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

<script src="sample6.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 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 vecSelect = (veclist, k) => veclist.filter((v) => v.getItem(k) === 0);

let vecSum = (veclist, d) =>
    veclist.reduce((prev, next) => {
        let f = {};

        d.forEach((k) => f[k] = next.getItem(k));
        
        return prev.add(Vector(d, f));
    }, Vector(d, {}));

let vecSelectSum = (d, veclist, k) => vecSum(vecSelect(veclist, k), d);

let scaleVecs = (vecdict) =>
    Object.keys(vecdict)
    .map((k) => vecdict[k].scalarMul(1 / k));

let Test = () => {
    let that = UnitTest(),
        d = ['a', 'b'];

    that.setUp = () => {};
    that.tearDown = () => {};
    
    that.test_select0 = () => {
        let v1 = Vector(d, {a:1, b:2}),
            v2 = Vector(d, {a:3, b:4}),
            veclist = [v1, v2];
        
        that.assertTrue(vectorsIsEqual(vecSelect(veclist, 'a'), []))
    };
    that.test_select1 = () => {
        let v1 = Vector(d, {b:2}),
            v2 = Vector(d, {a:3, b:4}),
            veclist = [v1, v2];
        
        that.assertTrue(vectorsIsEqual(vecSelect(veclist, 'a'), [v1]))
    };
    that.test_select2 = () => {
        let v1 = Vector(d, {b: 1}),
            v2 = Vector(d, {b: 2}),
            veclist = [v1, v2];
        
        that.assertTrue(vectorsIsEqual(vecSelect(veclist, 'a'), [v1, v2]))
    };
    that.test_sum0 = () => that.assertTrue(vecSum([], []).isEqual(Vector([], {})));
    that.test_sum1 = () => {
        let v1 = Vector(d, {a:1})
        that.assertTrue(vecSum([v1], d).isEqual(v1));
    };
    that.test_sum2 = () => {
        let v1 = Vector(d, {a:1}),
            v2 = Vector(['a', 'b', 'c'], {a:3, b:4, c:5});
        
        that.assertTrue(vecSum([v1, v2], d).isEqual(Vector(d, {a:4, b:4})));
    };
    that.test_selectSum0 = () =>
        that.assertTrue(vecSelectSum([], []).isEqual(Vector([], {})));
    that.test_select_sum1 = () => {
        let v1 = Vector(d, {b:1}),
            v2 = Vector(d, {b:2});
        that.assertTrue(vecSelectSum(d, [v1, v2], 'a').isEqual(Vector(d, {b:3})));
    };
    that.test_scaleVecs0 = () => that.assertTrue(vectorsIsEqual(scaleVecs({}), []));
    that.test_scaleVecs1 = () => {
        let v1 = Vector(d, {'a':1}),
            v2 = Vector(d, {'b':2}),
            vecdict = {1: v1, 2: v2};
        
        that.assertTrue(scaleVecs(vecdict), [v1, v2.scalarMul(v2)]);
    };
    return that;
};
let output = () => {
    Test().run();
};

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














						

0 コメント:

コメントを投稿