2017年5月1日月曜日

開発環境

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

問題 2.14.9

コード(Emacs)

HTML5

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

<button id="draw0">draw</button>
<button id="clear0">clear</button>

<script src="sample14_9.js"></script>    

JavaScript

let div0 = document.querySelector('#graph0'),
    pre0 = document.querySelector('#output0'),
    width = 600,
    height = 600,
    padding = 50,
    btn0 = document.querySelector('#draw0'),
    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] = f[k] * a;
            });
            return Vector(d, func);
        },
        add = (v) => {
            let func = {};

            d.forEach((k) => {
                func[k] = f[k] + v.getItem(k);
            });
            return Vector(d, func);
        },
        sub = (v) => that.add(v.scalarMul(-1)),
        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.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)) {
                    if (that[key]()[0]) {
                        p(`${key} - ok`);
                    } else {
                        p(`${key} - failure`);
                        p(that[key]()[1]);
                    }
                }
                that.tearDown();
            });        
        },
        assertEqual = (x, y) => [x === y, `${x} !== ${y}`];

    that.run = run;
    that.assertEqual = assertEqual;

    return that;
};

let Test = () => {
    let that = UnitTest();
    
    that.setUp = () => {},
    that.tearDown = () => {};
    
    that.test_a = () => {
        let v = arrayToVector([1, 0]).dot(arrayToVector([5, 4321]));
        
        return that.assertEqual(v, 5);
    };
    that.test_b = () => {
        let v = arrayToVector([0, 1]).dot(arrayToVector([12345, 6]));
        
        return that.assertEqual(v, 6);
    };
    that.test_c = () => {
        let v = arrayToVector([-1, 3]).dot(arrayToVector([5, 7]));
        
        return that.assertEqual(v, 16);
    };
    that.test_d = () => {
        let v = arrayToVector([-Math.sqrt(2) / 2, Math.sqrt(2) / 2])
            .dot(arrayToVector([Math.sqrt(2) / 2, -Math.sqrt(2) / 2]));
        
        return that.assertEqual(v, -1);
    };

    return that;
};

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

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














						

0 コメント:

コメントを投稿