2017年6月14日水曜日

開発環境

行列プログラマー(Philip N. Klein (著)、 松田 晃一 (翻訳)、 弓林 司 (翻訳)、 脇本 佑紀 (翻訳)、 中田 洋 (翻訳)、 齋藤 大吾 (翻訳)、オライリージャパン)の行列プログラマー(Philip N. Klein (著)、 松田 晃一 (翻訳)、 弓林 司 (翻訳)、 脇本 佑紀 (翻訳)、 中田 洋 (翻訳)、 齋藤 大吾 (翻訳)、オライリージャパン)の4章(行列)、4.3(ベクトルとしての行列)、クイズ4.3.1を JavaScript で取り組んでみる。

クイズ4.3.1

コード(Emacs)

HTML5

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

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

<script src="sample8.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 matrixToColObj = (A) => {
    let result = {};

    A.getDomain()[1]
        .forEach((j) => {
            let o = {};

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

            result[j] = Vector(A.getDomain()[0], o);
        });
    
    return result;
};


let matrixToVector = (M) => {
    let domain = M.getDomain(),
        labels = domain[0]
        .map((i) => domain[1].map((j) => `(${i}, ${j})`))
        .reduce((x, y) => x.concat(y), []),
        func = {};

    console.log(M.getMap());
    M.getMap()
        .forEach((x) => {
            func[`(${x[0][0]}, ${x[0][1]})`] = x[1];
        });
    console.log(func);
    return Vector(labels, func);
};
let output = () => {
    let labels = [['a', 'b'], ['@', '#', '?']],
        fn = [[['a', '@'], 1], [['a', '#'], 2], [['a', '?'], 3],
              [['b', '@'], 10], [['b', '#'], 20], [['b', '?'], 30]],
        M = Matrix(labels, fn),
        v = matrixToVector(M);

    p(M);
    p(v);
};

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














						

0 コメント:

コメントを投稿