sicp每日一题[2.36-2.37]

embedded/2024/10/8 22:38:36/

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

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

http://www.ppmy.cn/embedded/124759.html

相关文章

[C++]使用纯opencv部署yolov11-pose姿态估计onnx模型

【算法介绍】 使用纯OpenCV部署YOLOv11-Pose姿态估计ONNX模型是一项具有挑战性的任务,因为YOLOv11通常是用PyTorch等深度学习框架实现的,而OpenCV本身并不直接支持加载和运行PyTorch模型。然而,可以通过一些间接的方法来实现这一目标&#x…

掌握 C# 多线程与异步编程

现代应用程序通常需要执行复杂的计算或处理 I/O 操作,这些操作可能会导致主线程阻塞,从而降低用户体验。C# 提供了多线程与异步编程的多种工具,让我们能够高效地并发处理任务。本文将介绍 C# 中的多线程与异步编程,包括 Thread 类…

通过JS + PHP实现简易小说采集

先申明下,这个只是用来作为采集的一个样本,请大家还是尊重知识产权,看正版的书籍。 一、简要说明: 主要用到:jQuery, PHP 主要思路: 1. 通过js来循环访问本地的php文件,并传输书本网址&#xf…

端模一体,猎豹移动对大模型机器人发展路径清晰

今年世界机器人大会刚刚收官不久,接咖啡、拿苹果、摊煎饼……人形机器人在这届大会上备受关注,厂商们编排“整活”,展位几乎水泄不通。 自从AI大模型开始全面改变市场开始,关于机器人的方向性争论就不绝于耳,就在最近的…

解决方案:机器学习中,出现欠拟合和过拟合,这两种情况分别如何解决

文章目录 一、现象二、解决方案欠拟合(Underfitting)过拟合(Overfitting) 一、现象 在工作中,在机器学习中,出现欠拟合和过拟合的时候,需要有对应的解决方法,所以整理一下 二、解决…

健身管理|基于java的健身管理系统小程序(源码+数据库+文档)

健身管理系统|健身管理系统小程序 目录 基于java的健身管理系统小程序 一、前言 二、系统设计 三、系统功能设计 四、数据库设计 五、核心代码 六、论文参考 七、最新计算机毕设选题推荐 八、源码获取: 博主介绍:✌️大厂码农|毕设布道师&…

今日指数项目项目集成RabbitMQ与CaffienCatch

今日指数项目项目集成RabbitMQ与CaffienCatch 一. 为什么要集成RabbitMQ 首先CaffeineCatch 是作为一个本地缓存工具 使用CaffeineCatch 能够大大较少I/O开销 股票项目 主要分为两大工程 --> job工程(负责数据采集) , backend(负责业务处理) 由于股票的实时性也就是说 ,…

(一)Web 网站服务之 Apache

一、Apache 的作用和特点 作用:Apache 是一款开源的网站服务器端软件,为网站的运行提供了稳定的基础。特点: 开源免费:这使得任何人都可以免费使用和修改它。模块化设计:具有高度的灵活性,可以根据需求选择…