2017年6月10日土曜日

開発環境

行列プログラマー(Philip N. Klein (著)、 松田 晃一 (翻訳)、 弓林 司 (翻訳)、 脇本 佑紀 (翻訳)、 中田 洋 (翻訳)、 齋藤 大吾 (翻訳)、オライリージャパン)の行列プログラマー(Philip N. Klein (著)、 松田 晃一 (翻訳)、 弓林 司 (翻訳)、 脇本 佑紀 (翻訳)、 中田 洋 (翻訳)、 齋藤 大吾 (翻訳)、オライリージャパン)の4章(行列)、4.1(行列とは何か?)、4.1.6(行列の表現間の変換)、クイズ4.1.9を JavaScript で取り組んでみる。

クイズ4.1.9

コード(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',
    pObj = (obj) => Object.keys(obj).forEach((k) => p(`${k}: ${obj[k]}`));
    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 `Vector([${d.join(', ')}], ` +
                '{' +
                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 Matrix = (labels, fn) => {
    let that = {},
        getDomain = () => labels,
        getMap = () => fn,
        getElement = (i, j) => {
            let result = fn.filter((x) => x[0][0] === i && x[0][1] === j);

            return result.length === 0 ? 0 : result[0][1];
        },
        toString = () => {
            let [m, n] = labels,
                s = fn.map((k) => `[[${k[0].join(', ')}], ${k[1]}]`);

            return `Matrix([[${m.join(', ')}], [${n.join(', ')}]], [${s}])`;
        };

    that.getDomain = getDomain;
    that.getMap = getMap;
    that.getElement = getElement;
    that.toString = toString;
    
    return that;
};

let identity = (d) => Matrix([d, d], d.map((x) => [[x, x], 1]));

let matrixToRowObj = (A) => {
    let result = {};

    A.getDomain()[0]
        .forEach((i) => {
            let o = {};

            A.getDomain()[1].forEach((j) => o[j] = A.getElement(i, j));

            result[i] = Vector(A.getDomain()[1], o);
        });
    
    return result;
};


let output = () => {
    let labels = [['a', 'b'], ['@', '#', '?']],
        fn = [[['a', '@'], 1], [['a', '#'], 2], [['a', '?'], 3],
              [['b', '@'], 10], [['b', '#'], 20], [['b', '?'], 30]],
        M = Matrix(labels, fn),
        A = matrixToRowObj(M);

    p(M);
    pObj(A);
};

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














						

0 コメント:

コメントを投稿