# 2.37

## 练习 2.37 假定我们将向量 v=(vi) 表示为数的序列，将矩阵m=(mij)表示为向量（矩阵行）的序列。例如，矩阵：

$\left[ {\begin{array}{cc} 1 & 2 & 3 & 4 \\ 4 & 5 & 6 & 6 \\ 6 & 7 & 8 & 9 \end{array} } \right]$

(dot-product v w) 返回和 $\sum_i v_i w_i$

(matrix-*-vector m v) 返回向量 t，其中 $t_{i} = \sum_j m_{ij} v_j$

(matrix-*-matrix m n) 返回矩阵 p，其中 $p_{ij} = \sum_k m_{ik} n_{kj}$

(transpose m) 返回矩阵 n，其中 $n_{ij}=m_{ji}$

(define (dot-product v w)
(accumulate + 0 (map * v w)))


(define (matrix-*-vector m v)
(map <??> m))

(define (transpose mat)
(accumulate-n <??> <??> mat))

(define (matrix-*-matrix m n)
(let ((cols (transpose n)))
(map <??> m)))


(define (accumulate op initial sequence)
(if (null? sequence)
initial
(op (car sequence)
(accumulate op initial (cdr sequence)))
)
)

(define (dot-product v w)
(accumulate + 0 (map * v w)))

(dot-product (list 1 2 3 4) (list 5 6 7 8))


(define (matrix-*-vector m v)
(map (lambda (m_i) (dot-product m_i v)) m))

(define m (list (list 1 2 3 4) (list 4 5 6 6) (list 6 7 8 9)))
(define v (list 1 2 3 4))

(matrix-*-vector m v)


(define (accumulate-n op init seqs)
(if (null? (car seqs))
'()
(cons (accumulate op init (map car seqs))
(accumulate-n op init (map cdr seqs)))))

(define s (list (list 1 2 3) (list 4 5 6) (list 7 8 9) (list 10 11 12)))
(accumulate-n + 0 s)

(define (transpose mat)
(accumulate-n cons '() mat))

(transpose m)

(define (matrix-*-matrix m n)
(let ((cols (transpose n)))
(map (lambda (v) (matrix-*-vector cols v)) m)))

(define n (transpose m))

(matrix-*-matrix m n)