2017年5月2日火曜日

開発環境

行列プログラマー(Philip N. Klein (著)、 松田 晃一 (翻訳)、 弓林 司 (翻訳)、 脇本 佑紀 (翻訳)、 中田 洋 (翻訳)、 齋藤 大吾 (翻訳)、オライリージャパン)の2章(ベクトル)、2.14(問題)、Vec クラス用のプロシージャを書く、問題 2.14.10 を JavaScript で取り組んでみる。

問題 2.14.10

コード(Emacs)

HTML5

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

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

<script src="sample14_10.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';

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) => {
                that.setUp();
                if (/^test/.test(key)) {
                    p(key);
                    that[key]();
                }
                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`);
            }
        };

    that.run = run;
    that.assertEqual = assertEqual;
    that.assertTrue = assertTrue;
    that.assertFalse = assertFalse;

    return that;
};

let Test = () => {
    let that = UnitTest(),
        v1,
        v2,
        a,
        b,
        c,
        d,
        e,
        f,
        u1,
        u2,
        v11,
        v12,
        w1,
        w2,
        v21,
        v22,
        zero,
        u3,
        u4;
    
    
    that.setUp = () => {
        v1 = Vector(['a', 'b', 'c', 'd'], {a:2, c:1, d:3});
        v2 = Vector(['a', 'b', 'c'], {b:0});
        a = Vector(['a','e','i','o','u'], {'a':0,'e':1,'i':2});
        b = Vector(['a','e','i','o','u'], {'o':4,'u':7})
        c = Vector(['a','e','i','o','u'], {'a':0,'e':1,'i':2,'o':4,'u':7});
        d = Vector(['x','y','z'], {'x':2,'y':1});
        e = Vector(['x','y','z'], {'z':4,'y':-1});
        f = Vector(['x','y','z'], {'x':2,'y':0,'z':4});
        u1 = Vector(['a','b'], {'a':1, 'b':2});
        u2 = Vector(['a','b'], {'b':2, 'a':1});
        v11 = Vector(['p','q','r','s'], {'p':2,'s':3,'q':-1,'r':0});
        v12 = Vector(['p','q','r','s'], {'p':-2,'r':5});
        w1 = Vector(['a','b','c'], {'a':2,'b':3,'c':4});
        w2 = Vector(['a','b','c'], {'a':12,'b':8,'c':6});
        v21 = Vector([1, 2], {1 : 3, 2 : 6});
        v22 = Vector([1, 2], {1 : 2, 2 : 1});
        zero = Vector(['x','y','z','w'], {});
        u3 = Vector(['x','y','z','w'], {'x':1,'y':2,'z':3,'w':4});
        u4 = Vector([1,3,5,7],{1:1,3:2,5:3,7:4});
    };
    that.tearDown = () => {};
    
    that.test_getItem1 = () => {
        that.assertEqual(v1.getItem('d'), 3);
    };
    that.test_getItem2 = () => {
        that.assertEqual(v1.getItem('b'), 0);
    };
    that.test_setItem1 = () => {
        v2.setItem('b', 5);
        that.assertEqual(v2.getItem('b'), 5);
    };
    that.test_setItem2 = () => {
        v2.setItem('b', 1);
        that.assertEqual(v2.getItem('b'), 1);
    };
    that.test_setItem3 = () => {
        v2.setItem('a', 0);
        that.assertEqual(v2.getItem('a'), 0);
    };
    that.test_isEqual1 = () => {
        that.assertTrue(
            Vector(['a', 'b', 'c'], {a:0})
                .isEqual(Vector(['a', 'b', 'c'], {b:0})));
    };
    that.test_isEqual2 = () => {
        that.assertTrue(
            Vector(['a', 'b', 'c'], {a:0})
                .isEqual(Vector(['a', 'b', 'c'], {})));
    };
    that.test_isEqual3 = () => {
        that.assertTrue(
            Vector(['a', 'b', 'c'], {})
                .isEqual(Vector(['a', 'b', 'c'], {a:0})));
    };
    that.test_isEqual4 = () => {
        that.assertFalse(
            Vector(['x', 'y', 'z'], {y:1, x:2})
                .isEqual(Vector(['x', 'y', 'z'], {y:1, z:0})));
    };
    that.test_isEqual5 = () => {
        that.assertFalse(
            Vector(['a', 'b', 'c'], {a:0, c:1})
                .isEqual(Vector(['a', 'b', 'c'], {a:0, c:1, b:4})));
    };
    that.test_isEqual6 = () => {
        that.assertFalse(
            Vector(['a', 'b', 'c'], {a:0, c:1, b:4})
                .isEqual(Vector(['a', 'b', 'c'], {a:0, c:1})));
    };
    that.test_isEqual7 = () => {
        that.assertFalse(
            Vector(['a', 'b'], {a:1})
                .isEqual(Vector(['a', 'b'], {b:1})));
    };
    that.test_isEqual8 = () => {
        that.assertFalse(
            Vector(['a', 'b'], {a:1})
                .isEqual(Vector(['a', 'b'], {a:2})));
    };
    that.test_add1 = () => {
        that.assertTrue(a.add(b).isEqual(c));
    };
    that.test_add2 = () => {
        that.assertTrue(
            a.isEqual(Vector(['a','e','i','o','u'], {'a':0,'e':1,'i':2})));
    };
    that.test_add3 = () => {
        that.assertTrue(
            b.isEqual(Vector(['a','e','i','o','u'], {'o':4,'u':7})));
    };
    that.test_add4 = () => {
        that.assertTrue(d.add(e).isEqual(f));
    };
    that.test_add5 = () => {
        that.assertTrue(d.isEqual(Vector(['x','y','z'], {'x':2,'y':1})));
    };
    that.test_add6 = () => {
        that.assertTrue(e.isEqual(Vector(['x','y','z'], {'z':4,'y':-1})));
    };
    that.test_add7 = () => {
        that.assertTrue(b.add(Vector(['a','e','i','o','u'], {})).isEqual(b));
    };
    that.test_dot1 = () => {
        that.assertEqual(u1.dot(u2), 5);
    };
    that.test_dot2 = () => {
        that.assertTrue(u1.isEqual(Vector(['a','b'], {'a':1, 'b':2})));
    };
    that.test_dot3 = () => {
        that.assertTrue(u2.isEqual(Vector(['a','b'], {'b':2, 'a':1})));
    };        
    that.test_dot4 = () => {
        that.assertEqual(w1.dot(w2), 72);
    };        
    that.test_dot5 = () => {
        that.assertEqual(v21.dot(v22), 12);
    };
    that.test_scalarMul1 = () => {
        that.assertTrue(u3.scalarMul(0).isEqual(zero));
    };
    that.test_scalarMul2 = () => {
        that.assertTrue(u3.scalarMul(1).isEqual(u3));
    };
    that.test_scalarMul3 = () => {
        that.assertTrue(u3.scalarMul(0.5).isEqual(
            Vector(['x','y','z','w'],{'x':0.5,'y':1,'z':1.5,'w':2})));
    };
    that.test_scalarMul3 = () => {
        that.assertTrue(u3.isEqual(
            Vector(['x','y','z','w'],{'x':1,'y':2,'z':3,'w':4})));
    };    
    that.test_neg1 = () => {
        that.assertTrue(u4.neg().isEqual(
            Vector([1, 3, 5, 7],{1: -1, 3: -2, 5: -3, 7: -4})));
    };    
    that.test_neg2 = () => {
        that.assertTrue(u4.isEqual(Vector([1,3,5,7],{1:1,3:2,5:3,7:4})));
    };
    that.test_neg3 = () => {
        that.assertTrue(Vector(['a','b','c'], {'a':1}).neg().isEqual(
            Vector(['a','b','c'], {'a':-1})));
    };

    return that;
};

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

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














						

0 コメント:

コメントを投稿