2016年8月13日土曜日

開発環境

線型代数入門 (松坂 和夫(著)、岩波書店)の第3章(線型写像)、8(線型写像の空間)、問5.をJavaScript、matrix.jsを使って取り組んでみる。

問5.

コード(Emacs)

var nl = '<br><br>',
    result0 = document.querySelector('#result0'),
    run0 = document.querySelector('#run0');

run0.onclick = function () {
    var l = Math.floor(Math.random() * 10) + 1,
        m = Math.floor(Math.random() * 10) + 1,
        n = Math.floor(Math.random() * 10) + 1,        
        a1 = new Matrix(m, n),
        a2 = new Matrix(m, n),
        b1 = new Matrix(l, m),
        b2 = new Matrix(l, m),
        c = Math.floor(Math.random() * 10),
        i,
        j,
        matrix1,
        matrix2,
        matrix3,
        result = '';

    for (i = 1; i <= m; i += 1) {
        for (j = 1; j <= n; j += 1) {
            a1.setElement(i, j, Math.floor(Math.random() * 10));
            a2.setElement(i, j, Math.floor(Math.random() * 10));
        }
    }
    for (i = 1; i <= l; i += 1) {
        for (j = 1; j <= m; j += 1) {
            b1.setElement(i, j, Math.floor(Math.random() * 10));
            b2.setElement(i, j, Math.floor(Math.random() * 10));
        }
    }
    result +=
        'A = ' + a1 + ", A' = " + a2 + nl +
        'B = ' + b1 + ", B' = " + b2 + nl +
        'c = ' + c + nl;

    matrix1 = b1.mulMatrix(a1.add(a2));
    result += "B(A + A') = " + matrix1 + nl;
    matrix2 = b1.mulMatrix(a1).add(b1.mulMatrix(a2));
    result += "BA + BA' = " + matrix2 + nl;

    result += "B(A + A')";
    result += matrix1.isEqual(matrix2) ? ' = ' : ' /= ';
    result += "BA + BA'" + nl + nl;

    matrix1 = b1.add(b2).mulMatrix(a1);
    result += "(B + B')A = " + matrix1 + nl;
    matrix2 = b1.mulMatrix(a1).add(b2.mulMatrix(a1));
    result += "BA + B'A = " + matrix2 + nl;

    result += "(B + B')A";
    result += matrix1.isEqual(matrix2) ? ' = ' : ' /= ';
    result += "BA + B'A" + nl + nl;

    matrix1 = b1.mulMatrix(a1.mulScalar(c));
    result += "B(cA) = " + matrix1 + nl;
    matrix2 = b1.mulScalar(c).mulMatrix(a1);
    result += "(cB)A = " + matrix2 + nl;
    matrix3 = b1.mulMatrix(a1).mulScalar(c);
    result += "c(BA) = " + matrix3 + nl;

    result += "B(cA)";
    result += matrix1.isEqual(matrix2) ? ' = ' : ' /= ';
    result += "(cB)A";
    result += matrix2.isEqual(matrix3) ? ' = ' : ' /= ';
    result += "c(BA)" + nl;

    result0.innerHTML = result;
};

ランダムに生成した行列で、等号が成り立つことを確認できた。

0 コメント:

コメントを投稿