sicp每日一题[2.36-2.37]

41 篇文章 0 订阅
27 篇文章 0 订阅

果然习惯不能停,就两天没学,昨天就忘的干干净净了。。今天把昨天的补上

Exercise 2.36

The procedure a c c u m u l a t e − n accumulate-n accumulaten is similar to a c c u m u l a t e accumulate accumulate except that it takes as its third argument a sequence of sequences, which are all assumed to have the same number of elements. It applies the designated accumulation procedure to combine all the first elements of the sequences, all the second elements of the sequences, and so on, and returns a sequence of the results. For instance, if s is a sequence containing four sequences, ((1 2 3) (4 5 6) (7 8 9) (10 11 12)),then the value of (accumulate-n + 0 s) should be the sequence (22 26 30). Fill in the missing expressions in the following definition of a c c u m u l a t e − n accumulate-n accumulaten:

(define (accumulate-n op init seqs)
  (if (null? (car seqs))
      nil
      (cons (accumulate op init ⟨??⟩)
            (accumulate-n op init ⟨??⟩))))

这道题我自己没做出来,搜了一下别人的答案才发现原来这么简单。。

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


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

; 执行结果
'(22 26 30)

Exercise 2.37

Suppose we represent vectors v = ( v i ) \boldsymbol v = (v_i) v=(vi) as sequences of numbers, and matrices m = ( m i j ) \boldsymbol m = (m_{ij}) m=(mij) 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 ) \begin{pmatrix} 1 & 2 & 3 & 4 \\ 4 & 5 & 6 & 6 \\ 6 & 7 & 8 & 9 \end{pmatrix} 146257368469
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:
( d o t − p r o d u c t   v   w )   r e t u r n s   t h e   s u m   Σ i v i w i ; (dot-product\ v\ w)\ returns\ the\ sum\ \Sigma_i v_i w_i; (dotproduct v w) returns the sum Σiviwi;
( m a t r i x − ∗ − v e c t o r   m   v )   r e t u r n s   t h e   v e c t o r   t , w h e r e   t i = Σ j m i j v j ; (matrix-*-vector\ m\ v)\ returns\ the\ vector\ \boldsymbol t, where\ t_i= \Sigma_j m_{ij} v_j; (matrixvector m v) returns the vector t,where ti=Σjmijvj;
( m a t r i x − ∗ − m a t r i x   m   n )   r e t u r n s   t h e   m a t r i x   p , w h e r e   p i j = Σ k m i k n k j ; (matrix-*-matrix\ m\ n)\ returns\ the\ matrix\ \boldsymbol p, where\ p_{ij} = \Sigma_k m_{ik}n_{kj}; (matrixmatrix m n) returns the matrix p,where pij=Σkmiknkj;
( t r a n s p o s e   m )   r e t u r n s   t h e   m a t r i x   n , w h e r e   n i j = m j i . (transpose\ m)\ returns\ the\ matrix\ \boldsymbol n, where\ n_{ij} = m_{ji}. (transpose m) returns the matrix n,where nij=mji.
We can define the do tproduct 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 a c c u m u l a t e − n accumulate-n accumulaten is defined in Exercise 2.36.)

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

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

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

这道题目还是挺难的,第一个矩阵乘向量比较简单,让向量依次跟矩阵的每一行点乘即可;第二个其实我不会做,我就是随便把 cons 和 nil 填到了空出的位置,没想到一执行恰好就是我要的结果,然后我回过头去看了一下 accumulate-n 的代码,发现它其实实现的就是转置的功能;第三个首先要理解它对 n 进行转置的目的,其实 m x n 就等价于用 m 的每一行跟 n 的每一列依次做点乘,现在对 n 做了转置之后,就相当于让 m 的每一行和 n 的每一行做点乘,明白了这一点这道题就可以很容易地实现了。

; m 表示矩阵,v 表示向量,m 的行数必须等于 v 中元素的个数
; 矩阵乘向量,相当于用矩阵的每一行跟向量做点乘
(define (matrix-*-vector m v)
  (map (lambda (x) (dot-product x v)) m))

; mat 表示矩阵
(define (transpose mat)
  (accumulate-n cons nil mat))

; m, n 都表示矩阵,m 的列数必须等于 n 的行数
; 最后的结果矩阵行数等于 m 的行数,列数等于 n 的列数
(define (matrix-*-matrix m n)
  (let ((cols (transpose n)))
    (map (lambda (mat) (matrix-*-vector cols mat)) m)))


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

(dot-product v (list 2 3 5 7))
(matrix-*-vector mat v)
(transpose mat)
(matrix-*-matrix mat mat2)

; 执行结果
33
'(20 43 60)
'((1 4 6) (2 5 7) (3 6 8) (4 6 9))
'((31 41 51 59) (66 87 108 124) (91 121 151 174))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值