3 [sicp [ch2-2 :only (accumulate)]
4 [ex2_36 :only (accumulate-n)]]))
6 ;; (define (dot-product v w)
7 ;; (accumulate + 0 (map * v w)))
8 (defn dot-product [v w]
9 (accumulate + 0 (map * v w)))
11 (deftest test-dot-prod
14 (is [= (dot-product v w)
17 ;; (define (matrix-*-vector m v)
19 (defn matrix-*-vector [m v]
20 (map (fn [r] (dot-product r v)) m))
22 (deftest test-matrix*-vector
23 (let [m '((1 2 3) (4 5 6) (7 8 9))
25 (is [= (matrix-*-vector m v)
28 ;; (define (transpose mat)
29 ;; (accumulate-n <??> <??> mat))
31 (accumulate-n cons nil mat))
33 (deftest test-transpose
34 (let [s '((1 2 3) (4 5 6) (7 8 9))]
36 '((1 4 7) (2 5 8) (3 6 9))])))
38 ;; (define (matrix-*-matrix m n)
39 ;; (let ((cols (transpose n)))
41 (defn matrix-*-matrix [m n]
42 (let [cols (transpose n)]
44 (matrix-*-vector cols v))
47 (deftest test-matrix-*-matrix
49 (matrix-*-matrix '((8 9) (5 -1)) '((-2 3) (4 0))) '((20 24) (-14 15))
50 (matrix-*-matrix '((0 -1 2) (4 11 2)) '((3 -1) (1 2) (6 1))) '((11 0) (35 20))))