You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
sicp-all-tasks/sicp/2_002e37

250 lines
2.6 KiB

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 as83
(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)))