2017年7月7日金曜日

開発環境

行列プログラマー(Philip N. Klein (著)、 松田 晃一 (翻訳)、 弓林 司 (翻訳)、 脇本 佑紀 (翻訳)、 中田 洋 (翻訳)、 齋藤 大吾 (翻訳)、オライリージャパン)の4章(行列)、4.10(線形関数)、4.10.8(対角行列)、クイズ4.10.21を JavaScript で取り組んでみる。

クイズ4.10.21

コード(Emacs)

HTML5

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

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

<script src="sample10.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) => 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) =>
        fn[i] === undefined ? 0 : fn[i][j] === undefined ? 0 : fn[i][j],
        toString = () => {
            let [m, n] = labels,
                s = Object.keys(fn).map((k) => {
                    let o = fn[k],
                        s = Object.keys(o).map((k) => `${k}: ${o[k]}`).join(', ');

                    return `${k}: {${s}}`;
                });
            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 transpose = (M) => {
    let [m, n] = M.getDomain(),
        o = {};

    n.forEach((j) => {
        let o0 = {};
        m.forEach((i) => {
            o0[i] = M.getElement(i, j);
        })
        o[j] = o0;
    });
    return Matrix([n, m], o);
};
let diag = (D, entries) => {
    let labels = [D, D],
        fn = {};

    D.forEach((d) => {
        let o = {};

        o[d] = entries[d];
        fn[d] = o;
    });
    
    return Matrix(labels, fn);
};

let output = () => {
    [[['a'], {a:1}], [['a', 'b'], {a:1, b:2}]].forEach((o) => {
        let [D, entries] = o,
            M = diag(D, entries);

        p(M);
    });
};

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














						

0 コメント:

コメントを投稿