計算機科学のブログ

Building Abstractions with Data - Hierarchical Data and the Closure Property - Sequences as Conventional Interfaces - Sequence Operations - vectors, matrices, dot product, times, transpose

Structure and Interpretation of Computer Programs: JavaScript Edition(Harold Abelson(著)、Gerald Jay Sussman(著)、Julie Sussman(著)、The MIT Press)のChapter 2(Building Abstractions with Data)、2.2(Hierarchical Data and the Closure Property)、2.2.3(Sequences as Conventional Interfaces)、Sequence Operations、Exercise 2.37の解答を求めてみる。

コード

function stringfy(x) {
    if (is_null(x)) {
        return "null";
    }
    if (is_pair(x)) {
        return "[" + stringfy(head(x)) + ", " + stringfy(tail(x)) + "]";
    }
    return x.toString();
}
function pair(x, y) {
    return [x, y];
}
function head(z) {
    return z[0];
}
function tail(z) {
    return z[1];
}
function is_pair(x) {
    return Array.isArray(x);
}
function is_null(x) {
    return x === null;
}
function list(...args) {
    return args.length === 0 ?
        null :
        pair(args[0], list(...args.slice(1)));
}
function display(x) {
    return console.log(stringfy(x));
}
function accumulate(op, initial, sequence) {
    return is_null(sequence) ?
        initial :
        op(
            head(sequence),
            accumulate(op, initial, tail(sequence))
        );
}
function map(f, sequence) {
    return accumulate(
        (x, y) => pair(f(x), y),
        null,
        sequence
    );
}
function accumulate_n(op, init, seqs) {
    return is_null(head(seqs)) ?
        null :
        pair(
            accumulate(
                op,
                init,
                map(
                    head,
                    seqs
                )
            ),
            accumulate_n(
                op,
                init,
                map(
                    tail,
                    seqs
                )
            )
        );
}
function plus(x, y) {
    return x + y;
}
function times(x, y) {
    return x * y;
}
function dot_product(v, w) {
    return accumulate(plus, 0, accumulate_n(times, 1, list(v, w)));
}
function matrix_times_vector(m, v) {
    return map(w => dot_product(w, v), m);
}
function transpose(mat) {
    return accumulate_n(pair, null, mat);
}
function matrix_times_matrix(m, n) {
    const cols = transpose(n);
    return map(row => matrix_times_vector(cols, row), m);
}
const v = list(1, 2, 3, 4);
const w = list(4, 5, 6, 6);
const m = list(
    list(1, 2, 3, 4),
    list(4, 5, 6, 6),
    list(6, 7, 8, 9)
);
const n = list(
    list(1, 1, 2),
    list(2, 2, 1),
    list(1, 2, 3),
    list(2, 3, 4)
)
display("v = " + stringfy(v));
display("w = " + stringfy(w));
display("m = " + stringfy(m));
display("n = " + stringfy(n));
display("dot_product(v, w)");
display(dot_product(v, w));
display("matrix_times_vector(m, v)");
display(matrix_times_vector(m, v));
display("matrix_times_matrix(m, n)");
display(matrix_times_matrix(m, n));
display("transpost(m)");
display(transpose(m));

入出力結果(Terminal, Zsh)

% node answer2.37.js
v = [1, [2, [3, [4, null]]]]
w = [4, [5, [6, [6, null]]]]
m = [[1, [2, [3, [4, null]]]], [[4, [5, [6, [6, null]]]], [[6, [7, [8, [9, null]]]], null]]]
n = [[1, [1, [2, null]]], [[2, [2, [1, null]]], [[1, [2, [3, null]]], [[2, [3, [4, null]]], null]]]]
dot_product(v, w)
56
matrix_times_vector(m, v)
[30, [56, [80, null]]]
matrix_times_matrix(m, n)
[[16, [23, [29, null]]], [[32, [44, [55, null]]], [[46, [63, [79, null]]], null]]]
transpost(m)
[[1, [4, [6, null]]], [[2, [5, [7, null]]], [[3, [6, [8, null]]], [[4, [6, [9, null]]], null]]]]
%