灯下 登录
计算机科学 / SICP / 2.2.3 Sequences as Conventional Interfaces

Exercise 2.37 · 习题

Exercise 2.37:
Suppose we represent vectors v = (

v
i

) as sequences of numbers, and
matrices m = (

m

i
j

) as sequences of vectors (the rows of the
matrix). For example, the matrix
(

1

2

3

4

4

5

6

6

6

7

8

9

)
is represented as the sequence ((1 2 3 4) (4 5 6 6) (6 7 8 9)). With
this representation, we can use sequence operations to concisely express the
basic matrix and vector operations. These operations (which are described in
any book on matrix algebra) are the following:
(dot-product v w)

returns the sum

Σ
i

v
i

w
i

;

(matrix-*-vector m v)

returns the vector

t

,

where

t
i

=

Σ
j

m

i
j

v
j

;

(matrix-*-matrix m n)

returns the matrix

p

,

where

p

i
j

=

Σ
k

m

i
k

n

k
j

;

(transpose m)

returns the matrix

n

,

where

n

i
j

=

m

j
i

.
We can define the dot product as

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

Fill in the missing expressions in the following procedures for computing the
other matrix operations. (The procedure accumulate-n is defined in
Exercise 2.36.)

(define (matrix-*-vector m v)
(map ⟨??⟩ m))

(define (transpose mat)
(accumulate-n ⟨??⟩ ⟨??⟩ mat))

(define (matrix-*-matrix m n)

(let ((cols (transpose n)))

(map ⟨??⟩ m)))

练习 2.37:假设我们将向量 v = (v_i) 表示为数的序列,将矩阵 m = (m_{ij}) 表示为向量的序列(即矩阵的各行)。例如,矩阵

(1 2 3 4 / 4 5 6 6 / 6 7 8 9)

被表示为序列 ((1 2 3 4) (4 5 6 6) (6 7 8 9))。有了这种表示,我们就可以用序列操作简洁地表达基本的矩阵和向量运算。这些运算(在任何一本矩阵代数书中都有描述)如下:

(dot-product v w) 返回 Σ_i v_i w_i;

(matrix-*-vector m v) 返回向量 t,其中 t_i = Σ_j m_{ij} v_j;

(matrix-*-matrix m n) 返回矩阵 p,其中 p_{ij} = Σ_k m_{ik} n_{kj};

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

我们可以将点积定义为

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

填写下列各过程中缺失的表达式,以计算其他矩阵运算。(过程 accumulate-n 在练习 2.36 中定义。)

(define (matrix-*-vector m v)
(map ⟨??⟩ m))

(define (transpose mat)
(accumulate-n ⟨??⟩ ⟨??⟩ mat))

(define (matrix-*-matrix m n)
(let ((cols (transpose n)))
(map ⟨??⟩ m)))

Racket #lang sicp
(define (dot-product v w)
 (accumulate + 0 (map * v w)))
Racket #lang sicp
(define (matrix-*-vector m v)
 (map ⟨??⟩ m))

(define (transpose mat)
 (accumulate-n ⟨??⟩ ⟨??⟩ mat))

(define (matrix-*-matrix m n)
 (let ((cols (transpose n)))
 (map ⟨??⟩ m)))