Download presentation
Presentation is loading. Please wait.
Published bySucianty Vera Gunawan Modified over 5 years ago
1
CS 152: Programming Language Paradigms February 19 Class Meeting
Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
2
Symbolic Differentiation
Recall these differentiation rules from freshman calculus: For a constant c or a variable other than x: Also: SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak
3
Symbolic Differentiation, cont’d
For simplicity, the polynomial expressions and their derivatives will be written in Lisp’s prefix notation. We’ll also limit the arithmetic operators to two arguments. Example: (+ (+ 1 2) 3) and not: ( ) Example: Find the derivative of xy(x + 3) with respect to x. (deriv '(* (* x y) (+ x 3)) 'x) (+ (* x y) (* y (+ x 3))) SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak
4
Symbolic Differentiation: Helper Procedures
A top-down approach! Assume we already have the following procedures: (variable? e) Is e a variable? (same-variable? v1 v2) Are v1 and v2 the same variable? (sum? e) Is e a sum? (addend e) Addend of the sum e. (augend e) Augend of the sum e. (make-sum a1 a2) Construct the sum of a1 and a2. (product? e) Is e a product? (multiplier e) Multiplier of the product e. (multiplicand e) Multiplicand of the product e. (make-product m1 m2) Construct the product of m1 and m2. SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak
5
Symbolic Differentiation: Main Procedure
(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)) )) deriv1.lisp SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak
6
Symbolic Differentiation: Helper Procedures
(define (variable? x) (symbol? x)) (define (same-variable? v1 v2) (and (variable? v1) (variable? v2) (eq? v1 v2))) (define (make-sum a1 a2) (list '+ a1 a2)) (define (make-product m1 m2) (list '* m1 m2)) (define (sum? x) (and (pair? x) (eq? (car x) '+))) (define (addend s) (cadr s)) (define (augend s) (caddr s)) (define (product? x) (and (pair? x) (eq? (car x) '*))) (define (multiplier p) (cadr p)) (define (multiplicand p) (caddr p)) deriv1.lisp A sum is a list where the first element is the symbol + Example: (+ a b) A product is a list where the first element is the symbol * Example: (* a b) SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak
7
Symbolic Differentiation: Examples
(deriv 3 'x) 0 (deriv '(+ x 3) 'x) (+ 1 0) (deriv 'a 'x) 0 (deriv '(* 3 x) 'x) (+ (* 3 1) (* 0 x)) (deriv 'x 'x) 1 (deriv '(* x y) 'x) (+ (* x 0) (* 1 y)) (deriv '(+ (* 3 x) 2) 'x) (+ (+ (* 3 1) (* 0 x)) 0) (deriv '(* (* x y) (+ x 3)) 'x) (+ (* (* x y) (+ 1 0)) (* (+ (* x 0) (* 1 y)) (+ x 3))) SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak
8
Symbolic Differentiation: Simplifications
Can we simplify: (+ e 0), (+ 0 e), (* 1 e), and (* e 1) each to just e ? (* e 0) and (* 0 e) to just 0 ? Start with: We can perform the simplifications there by performing the additions by 0 and the multiplications by 0 and 1. _ (define (make-sum a1 a2) (list '+ a1 a2)) (define (make-product m1 m2) (list '* m1 m2)) SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak
9
Symbolic Differentiation: Simplifications, cont’d
(define (=number? exp num) (and (number? exp) (= exp num))) (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)))) (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)))) deriv2.lisp SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak
10
Symbolic Differentiation: Examples
(deriv '(+ x 3) 'x) 1 (deriv '(* 3 x) 'x) 3 (deriv '(* x y) 'x) y (deriv '(+ (* 3 x) 2) 'x) 3 (deriv '(* (* x y) (+ x 3)) 'x) (+ (* x y) (* y (+ x 3))) SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak
11
Symbolic Differentiation: Exponentiation
What about exponentiation? Use symbol ^ for exponentiation. Example: (^ x 2) represents x2 deriv3.lisp (define (exponentiation? x) (and (pair? x) (eq? (car x) '^))) (define (base e) (cadr e)) (define (exponent e) (caddr e)) (define (make-exponentiation base exponent) (cond ((=number? exponent 0) 1) ((=number? exponent 1) base) ((and (number? base) (number? exponent)) (expt base exponent)) (else (list '^ base exponent)))) SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak
12
Symbolic Differentiation: Exponentiation, cont’d
(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)))) ((exponentiation? exp) (make-product (make-product (exponent exp) (make-exponentiation (base exp) (make-sum (exponent exp) -1))) (deriv (base exp) var))) (else (error "unknown expression type -- DERIV" exp)) )) deriv3.lisp SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak
13
Symbolic Differentiation: Examples
(deriv '(^ x 2) 'x) (* 2 x) (deriv '(* a (^ x 2)) 'x) (* a (* 2 x)) (deriv '(* 4 (^ x 3)) 'x) (* 4 (* 3 (^ x 2))) We should simplify (* 4 (* 3 (^ x 2))) to (* 12 (^ x 2)) SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak
14
Symbolic Differentiation: Simplifications
(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)))) From (* 4 (* 3 (^ x 2))) to (* 12 (^ x 2)) (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)) ((and (number? m1) (product? m2) (number? (cadr m2))) (list '* (* m1 (cadr m2)) (caddr m2))) (else (list '* m1 m2)))) deriv4.lisp SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak
15
Symbolic Differentiation: Simplifications, cont’d
(deriv '(* 4 (^ x 3)) 'x) (* 12 (^ x 2)) (deriv '(+ (+ (+ (+ (+ (* a (^ x 5)) (* b (^ x 4))) (* 2 (^ x 3))) (* 6 (^ x 2))) (* 3 x)) 7) 'x) (+ (+ (+ (+ (* a (* 5 (^ x 4))) (* b (* 4 (^ x 3)))) (* (6 (^ x 2)))) (* 12 x)) 3) SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak
16
Symbolic Differentiation: Enhancements
Further enhancements: Allow the + operator to have more than two operands. Use infix notation. References (deriv '(+ (* a (^ x 5)) (* b (^ x 4)) (* 2 (^ x 3)) (* 6 (^ x 2)) (* 3 x) 7) 'x) (deriv '(a * x ^ 5 + b * x ^ * x ^ * x ^ * x + 7) 'x) (5 * a * x ^ * b * x ^ * x ^ * x + 3) SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak
17
Scheme and f(x) and f '(x)
For the polynomial function For a given set of coefficient values (a, b, etc.), we can evaluate both f(x) and f '(x) for some value x. Let a = 1 and b = 1 Then f(0) = 7 and f(1) = f '(0) = 3 and f '(1) = 30 For any polynomial function f such as the ones we’ve been working with and a set of coefficient values: Can we generate Scheme procedures that evaluate f(x) and f '(x) for any value x we have SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak
18
Scheme and f(x) and f '(x) , cont’d
First rewrite the polynomial expression in Scheme: But what we really want is: (define f '(+ (+ (+ (+ (+ (* a (^ x 5)) (* b (^ x 4))) (* 2 (^ x 3))) (* 6 (^ x 2))) (* 3 x)) 7)) f.lisp or simply: (lambda (x) (+ (+ (+ (+ (+ (* a (^ x 5)) (* b (^ x 4))) (* 2 (^ x 3))) (* 6 (^ x 2))) (* 3 x)) 7)) (lambda (x) f) SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak
19
Scheme and f(x) and f '(x) , cont’d
(lambda (x) (+ (+ (+ (+ (+ (* a (^ x 5)) (* b (^ x 4))) (* 2 (^ x 3))) (* 6 (^ x 2))) (* 3 x)) 7)) (lambda (x) f) We can generate the above as a list expression: (list 'lambda '(x) f) (lambda (x) (+ (+ (+ (+ (+ (* a (^ x 5)) (* b (^ x 4))) (* 2 (^ x 3))) (* 6 (^ x 2))) (* 3 x)) 7)) SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak
20
Scheme and f(x) and f '(x) , cont’d
(list 'lambda '(x) f) (lambda (x) (+ (+ (+ (+ (+ (* a (^ x 5)) (* b (^ x 4))) (* 2 (^ x 3))) (* 6 (^ x 2))) (* 3 x)) 7)) But this is just a list expression generated with the built-in list procedure! To generate a procedure object, we need to tell Scheme to evaluate the generated list expression: (list 'a 'b 'c) (a b c) (eval (list 'lambda '(x) f)) #<procedure> SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak
21
Scheme and f(x) and f '(x) , cont’d
(eval (list 'lambda '(x) f)) #<procedure> Now we can generate a Scheme procedure from any polynomial expression f in terms of x : f.lisp (define make-proc (lambda (f) (eval (list 'lambda '(x) f)) )) (make-proc f) #<procedure> SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak
22
Scheme and f(x) and f '(x) , cont’d
Back to our original problem: Let a = 1 and b = 1 Then f(0) = 7 and f(1) = f '(0) = 3 and f '(1) = 30 (define ^ expt) (^ 4 2) 16 (define a 1) (define b 1) ((make-proc f) 0) 7 ((make-proc f) 1) 20 ((make-proc (deriv f 'x)) 0) 3 ((make-proc (deriv f 'x)) 1) 30 f.lisp SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak
23
CS 152: Programming Language Paradigms © R. Mak
eval Procedure eval evaluates its list argument. Just like in the Scheme command interpreter’s read-eval-print loop. (+ 2 3) 5 '(+ 2 3) (+ 2 3) (eval '(+ 2 3)) 5 SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak
24
CS 152: Programming Language Paradigms © R. Mak
apply Scheme procedure apply allows you to apply a procedure of k parameters to a list of k items. ( ) 6 (apply + '(1 2 3)) 6 (apply (lambda (a b c) (* (+ a b) c)) '(1 2 4)) 12 SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak
25
CS 152: Programming Language Paradigms © R. Mak
Metalinguistic Power Metalinguistic power gives Scheme the capability to Dynamically build, manipulate, and transform a list of symbols at runtime. And then evaluate the resulting list. (cons 'lambda (list (list 'a 'b) '(+ a b))) (lambda (a b) (+ a b)) (eval (cons 'lambda (list (list 'a 'b) '(+ a b)))) #<procedure> ((eval (cons 'lambda (list (list 'a 'b) '(+ a b)))) 2 3) 5 (apply (eval (cons 'lambda (list (list 'a 'b) '(+ a b)))) '(2 3)) SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak
26
CS 152: Programming Language Paradigms © R. Mak
Flat Recursion In our recursive procedures so far with a list argument: We operated on the car of the list. We made a recursive call on the cdr of the list. This is known as flat recursion. The procedure operates on the top-level elements of the argument list. Deep recursion operates also on the nested sublists of the argument list. _ SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak
27
Flat Recursion Example: append
Recursive procedure append takes two argument lists and appends the first list to the head of the second list. One element at a time, cons the tail element of the first list into the head of the second list. What is the base case? If the first list is empty, simply return the second list. (append '(1 2 3) '(3 4)) '( ) (if (null? lst1) lst2 SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak
28
Flat Recursion Example: append cont’d
Think recursively! Assume that append can process the rest of the first list. In other words, assume that the rest of the first list has already been appended to the head of the second list. Then you only have to cons the head of the first list into a recursive call that process the rest of the list. (define append (lambda (lst1 lst2) (if (null? lst1) lst2 (cons (car lst1) (append (cdr lst1) lst2))) )) Trace SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak
29
CS 152: Programming Language Paradigms © R. Mak
Mutual Recursion Two recursive procedures can be mutually recursive. Example Trace both procedures to verify that they can really work together! _ (define even? (lambda (n) (if (zero? n) #t (odd? (sub1 n))) )) (define odd? (lambda (n) (if (zero? n) #f (even? (sub1 n))) )) SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak
30
CS 152: Programming Language Paradigms © R. Mak
Deep Recursion Flatly recursive procedure remove-top removes all top-level occurrences of an item from a list: Deeply recursive procedure remove-all removes all occurrences of an item from a list. No matter how deeply nested. (define remove-top (lambda (item lst) (cond ((null? lst) '()) ((equal? item (car lst)) (remove-top item (cdr lst))) (else (cons (car lst) (remove-top item (cdr lst))))) )) (remove-top 2 '(1 2 3 (1 2 3 (1 2)) 4)) (1 3 (1 2 3 (1 2)) 4) SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak
31
Deep Recursion Example: remove-all
(define remove-top (lambda (item lst) (cond ((null? lst) '()) ((equal? item (car lst)) (remove item (cdr lst))) (else (cons (car lst) (remove item (cdr lst))))) )) (define remove-all (lambda (item lst) (cond ((null? lst) '()) ((equal? item (car lst)) (remove-all item (cdr lst))) ((pair? (car lst)) (cons (remove-all item (car lst)) (remove-all item (cdr lst)))) (else (cons (car lst) (remove-all item (cdr lst))))) )) (remove-all 2 '(1 2 3 (1 2 3 (1 2)) 4)) (1 3 (1 3 (1)) 4) SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak
32
CS 152: Programming Language Paradigms © R. Mak
Flat vs. Deep Recursion Flat recursion Apply the recursive call only to the cdr of the list argument. Deep recursion Apply the recursive call also to the car of the list argument if the car is a nested sublist. _ SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.