目录
正在加载目录…
正在阅读专栏 SICP:Structure and Interpretation of Computer Programs 第 25 篇 · 共 80 篇 · 31%

SICP:2.3.2 Example: Symbolic Differentiation

发布于 2026-09-18 08:13 · 最后编辑于 2026-09-18 09:40 · 版本 v2 · 字数 5,780 · 预计阅读 12 分钟 👁 11 次阅读

SICP:2.3.2 Example: Symbolic Differentiation

本文逐段翻译 SICP 第二版 §2.3.2《Example: Symbolic Differentiation》,覆盖 abstract data differentiator、algebraic-expression representation、simplification,以及 Exercises 2.56–2.58;保留原始公式图件和 Scheme code。

TL;DR

  • Symbolic differentiation program 可以先针对 abstract objects(sums、products、variables)编写,再独立决定 representation。
  • Data abstraction 使 deriv algorithm 不依赖 expressions 的具体表示;simplification 可以封装在 constructors 中。
  • Prefix notation、infix notation 和 arbitrary-arity expressions 都可以通过替换 selectors、constructors 与 predicates 来支持。

目录

章节说明
2.3.2 Example: Symbolic Differentiationsymbolic differentiation
The differentiation program with abstract dataabstract algorithm
Representing algebraic expressionsprefix representation
Exercises 2.56–2.58differentiator extensions

2.3.2 Example: Symbolic Differentiation

作为 symbol manipulation 和 data abstraction 的进一步 illustration,考虑设计一个执行 algebraic expressions symbolic differentiation 的 procedure。希望这个 procedure 接收 algebraic expression 和一个 variable 作为 arguments,并返回 expression 关于该 variable 的 derivative。例如,如果 arguments 是 ax² + bx + c 和 x,procedure 应当返回 2ax + b。

Symbolic differentiation 在 Lisp 中具有 special historical significance:它是推动 computer language for symbol manipulation development 的 motivating examples 之一。此外,它开启了一条 research line,最终形成了 powerful systems for symbolic mathematical work;这些 systems 目前正被越来越多的 applied mathematicians 和 physicists 使用。

开发 symbolic-differentiation program 时,我们将采用与 §2.1.1 rational-number system 相同的 data-abstraction strategy。也就是说,先定义一个对 “sums”“products”“variables” 等 abstract objects 操作的 differentiation algorithm,不考虑这些 objects 如何表示;之后再处理 representation problem。

The differentiation program with abstract data

为了保持简单,考虑一个只处理由 two-argument addition 和 multiplication 构成的 expressions 的 symbolic-differentiation program。任何这类 expression 的 differentiation,都可以应用下面的 reduction rules 完成:

ch2 Z G 45

ch2 Z G 46

ch2 Z G 47

ch2 Z G 48

注意,后两个 rules 本质上是 recursive 的。也就是说,为了得到 sum 的 derivative,首先要找出 terms 的 derivatives 并把它们相加;每个 term 又可能是需要继续 decompose 的 expression。不断 decomposing 成越来越小的 pieces,最终会产生 constants 或 variables;它们的 derivatives 分别是 0 或 1。

为了把这些 rules 体现为 procedure,我们像设计 rational-number implementation 时一样,进行一点 wishful thinking。假设有表示 algebraic expressions 的方式,就应该能够判断 expression 是 sum、product、constant 还是 variable;也应该能够提取 expression 的 parts。对于 sum,例如希望能够提取 addend(first term)和 augend(second term)。还应该能够从 parts 构造 expressions。

假设已经有 procedures 实现以下 selectors、constructors 和 predicates:

Procedure含义
(variable? e)e 是 variable 吗?
(same-variable? v1 v2)v1 与 v2 是同一个 variable 吗?
(sum? e)e 是 sum 吗?
(addend e)sum e 的 addend
(augend e)sum e 的 augend
(make-sum a1 a2)构造 a1 与 a2 的 sum
(product? e)e 是 product 吗?
(multiplier e)product e 的 multiplier
(multiplicand e)product e 的 multiplicand
(make-product m1 m2)构造 m1 与 m2 的 product

借助这些 procedures 和识别 numbers 的 primitive predicate number?,可以将 differentiation rules 表达为:

scheme
(define (deriv exp var)
  (cond ((number? exp) 0)
        ((variable? exp)
         (if (same-variable? exp var) 1 0))
        ((sum? exp)
         (make-sum (deriv (addend exp) var)
                   (deriv (augend exp) var)))
        ((product? exp)
         (make-sum
          (make-product (multiplier exp)
                        (deriv (multiplicand exp) var))
          (make-product (deriv (multiplier exp) var)
                        (multiplicand exp))))
        (else
         (error "unknown expression type -- DERIV" exp))))

这个 deriv procedure 包含完整的 differentiation algorithm。由于它用 abstract data 表达,无论选择怎样表示 algebraic expressions,只要设计出 proper set of selectors 和 constructors,它都能工作。接下来必须处理的就是这个 representation issue。

Representing algebraic expressions

可以想象许多用 list structure 表示 algebraic expressions 的 ways。例如,使用 mirror usual algebraic notation 的 symbols lists,把 ax + b 表示为 (a * x + b)。但一种特别 straightforward 的选择,是使用 Lisp combinations 相同的 parenthesized prefix notation,把 ax + b 表示为 (+ (* a x) b)。

于是,differentiation problem 的 data representation 如下:

  • Variables 是 symbols,用 primitive predicate symbol? 识别:
scheme
(define (variable? x) (symbol? x))
  • 如果表示两个 variables 的 symbols eq?,则 two variables 相同:
scheme
(define (same-variable? v1 v2)
  (and (variable? v1) (variable? v2) (eq? v1 v2)))
  • Sums 和 products 构造为 lists:
scheme
(define (make-sum a1 a2) (list '+ a1 a2))

(define (make-product m1 m2) (list '* m1 m2))
  • Sum 是 first element 为 symbol + 的 list:
scheme
(define (sum? x)
  (and (pair? x) (eq? (car x) '+)))
  • Addend 是 sum list 的 second item:
scheme
(define (addend s) (cadr s))
  • Augend 是 sum list 的 third item:
scheme
(define (augend s) (caddr s))
  • Product 是 first element 为 symbol * 的 list:
scheme
(define (product? x)
  (and (pair? x) (eq? (car x) '*)))
  • Multiplier 是 product list 的 second item:
scheme
(define (multiplier p) (cadr p))
  • Multiplicand 是 product list 的 third item:
scheme
(define (multiplicand p) (caddr p))

把这些 definitions 与前面的 deriv algorithm combine,就得到一个 working symbolic-differentiation program。看看它在几个 examples 中的 behavior:

scheme
(deriv '(+ x 3) 'x)
(+ 1 0)
(deriv '(* x y) 'x)
(+ (* x 0) (* 1 y))
(deriv '(* (* x y) (+ x 3)) 'x)
(+ (* (* x y) (+ 1 0))
   (* (+ (* x 0) (* 1 y))
      (+ x 3)))

这个 program 产生的 answers 是 correct,但没有 simplify。确实,

ch2 Z G 49

但希望 program 知道 x · 0 = 0、1 · y = y、0 + y = y。第二个 example 的 answer 应该只是 y。第三个 example 表明,当 expressions complex 时,这会成为 serious issue。

我们的 difficulty 与 rational-number implementation 中遇到的类似:还没有把 answers reduce 到 simplest form。对于 rational-number reduction,只需要改变 implementation 的 constructors 和 selectors。这里可以采用类似 strategy:完全不改变 deriv,而是修改 make-sum,使它在两个 summands 都是 numbers 时把它们相加并返回 sum;如果某个 summand 是 0,就返回另一个 summand。

scheme
(define (make-sum a1 a2)
  (cond ((=number? a1 0) a2)
        ((=number? a2 0) a1)
        ((and (number? a1) (number? a2)) (+ a1 a2))
        (else (list '+ a1 a2))))

这里使用 procedure =number?,它检查 expression 是否等于给定 number:

scheme
(define (=number? exp num)
  (and (number? exp) (= exp num)))

类似地,修改 make-product,把“0 乘任何东西是 0”“1 乘任何东西是该东西”的 rules 内置进去:

scheme
(define (make-product m1 m2)
  (cond ((or (=number? m1 0) (=number? m2 0)) 0)
        ((=number? m1 1) m2)
        ((=number? m2 1) m1)
        ((and (number? m1) (number? m2)) (* m1 m2))
        (else (list '* m1 m2))))

这个 version 在前面三个 examples 上的 behavior 是:

scheme
(deriv '(+ x 3) 'x)
1
(deriv '(* x y) 'x)
y
(deriv '(* (* x y) (+ x 3)) 'x)
(+ (* x y) (* y (+ x 3)))

虽然这是很大的 improvement,第三个 example 表明,要得到一个大家可能同意的“simplest” form,仍然有很长的 way。Algebraic simplification 的 problem 很 complex,其中一个原因是:对某种 purpose 最 simple 的 form,对另一种 purpose 可能并不是最 simple 的。

Exercises 2.56–2.58

Exercise 2.56. 扩展 basic differentiator,使其能够处理更多 kinds of expressions。例如,通过向 deriv program 添加新 clause,并定义适当的 exponentiation?、base、exponent 和 make-exponentiation procedures,实现如下 differentiation rule:

ch2 Z G 50

(可以使用 symbol ** 表示 exponentiation。)把任何东西 raised to power 0 是 1、raised to power 1 是它本身的 rules 内置进去。

Exercise 2.57. 扩展 differentiation program,使其能够处理任意数量(two or more)的 terms 的 sums 和 products。这样,前面的最后一个 example 可以写为:

scheme
(deriv '(* x y (+ x 3)) 'x)

尝试只改变 sums 和 products 的 representation,而完全不改变 deriv procedure。例如,sum 的 addend 是 first term,augend 是 rest terms 的 sum。

Exercise 2.58. 修改 differentiation program,使它使用 ordinary mathematical notation,其中 + 和 * 是 infix 而非 prefix operators。由于 differentiation program 定义在 abstract data 上,只需改变定义 algebraic-expression representation 的 predicates、selectors 和 constructors,就能让它处理不同 representations 的 expressions。

a. 展示如何处理 infix form 的 algebraic expressions,例如 (x + (3 * (x + (y + 2))))。为简化 task,假设 + 和 * 总是接收 two arguments,且 expressions fully parenthesized。

b. 如果允许 standard algebraic notation,问题会 substantially harder,例如 (x + 3 * (x + y + 2));这种 notation 删除 unnecessary parentheses,并假设 multiplication 在 addition 之前执行。能否为这种 notation 设计 appropriate predicates、selectors 和 constructors,使 derivative program 仍然能够工作?

图与格式复核

  • 保留 differentiation rules、simplification identity、exponentiation rule 的 6 个原始公式 GIF,并按原文位置嵌入及保留显示尺寸。
  • deriv、representation selectors/constructors、make-sum、make-product 的 code 与 Exercises 2.56–2.58 顺序保持不变。

术语对照

English译法/处理
symbolic differentiation保留 symbolic differentiation;必要处译作符号微分
algebraic expression保留 algebraic expression;必要处译作代数表达式
addend / augend保留 addend、augend,不强译
multiplier / multiplicand保留 multiplier、multiplicand,不强译
prefix / infix notation保留 prefix、infix notation;必要处译作前缀、 中缀表示法
simplification保留 simplification;必要处译作化简
exponentiation保留 exponentiation;必要处译作幂运算

参考资料

← 返回列表
← 上一篇 SICP:2.3.1 Quotation 下一篇 → SICP:2.3.3 Example: Representing Sets

评论 (0)

暂无评论,来留下第一条吧。
登录 或 注册 后才能发表评论