(load "2.36.txt") ; for accumulate and accumulate-n (define (dot-product v w) (accumulate + 0 (map * v w))) (define (matrix-*-vector m v) (map (lambda (row) (dot-product row v)) m)) (define (transpose m) (accumulate-n cons '() m)) (define (matrix-*-matrix m n) (let ((cols (transpose n))) (map (lambda (row) (map (lambda (col) (dot-product row col)) cols)) m))) ; 1 ]=> (dot-product '(1 2 3) '(4 5 6)) ; ; ;Value: 32 ; ; 1 ]=> (transpose '((1 2 3) (4 5 6))) ; ; ;Value: ((1 4) (2 5) (3 6)) ; ; 1 ]=> (matrix-*-vector '((1 2 3) (4 5 6)) '(-1 0 1)) ; ; ;Value: (2 2) ; ; 1 ]=> (matrix-*-vector '((1 2 3) (6 5 4)) '(-1 0 1)) ; ; ;Value: (2 -2) ; ; 1 ]=> (matrix-*-matrix '((1 2) (3 4) (5 6)) '((1 2) (-1 3))) ; ; ;Value: ((-1 8) (-1 18) (-1 28))