topic 10
play

Topic 10 Polynomial a + bx + cx 2 can be represented by the - PDF document

Symbolic differentiation Topic 10 Polynomial a + bx + cx 2 can be represented by the Example: Symbolic list (+ a (+ (* b x) (* c (* x x)))) Differentiation Derivative with respect to x is b + 2cx, which can be represented by (+


  1. Symbolic differentiation Topic 10 • Polynomial a + bx + cx 2 can be represented by the Example: Symbolic list (+ a (+ (* b x) (* c (* x x)))) Differentiation • Derivative with respect to x is b + 2cx, which can be represented by (+ b (* 2 (* c x))) Section 2.3.2 • How can the derivative be computed? October 2008 Fall 2008 Programming Development 1 Fall 2008 Programming Development 2 Techniques Techniques Want to build a procedure to do Basic calculus (2 arguments only) differentiation dy/dx = 0 if y is a constant or a variable other than x • Think of the first rules as base conditions, then other rules can be used to decompose a problem into something easier. dx/dx = 1 • What do we need to do to tell which rule is d(u+ v)/dx = du/dx + dv/dx applicable? (NOTE Recursive!) – Differentiate between a constant, variable (and what it is), product, and sum d(u* v)/dx = u * dv/dx + v * du/dx – Extract parts of an expression (NOTE Recursive!) Fall 2008 Programming Development 3 Fall 2008 Programming Development 4 Techniques Techniques Now we can compute Data abstraction to the rescue! derivatives ; takes an expression and a variable and Some constructors, selectors and predicates: ; returns the derivitive of expr wrt var (variable? x) (same-variable? x y) (define (deriv expr var) (sum? x) (product? x) (cond ((number? expr) 0) (make-sum x y) (make-product x y) ((variable? expr) (sum-arg1 x) (product-arg1 x) (if (same-variable? expr var) (sum-arg2 x) (product-arg2 x) 1 0)) ((sum? expr) (make-sum (deriv (sum-arg1 expr) var) (deriv (sum-arg2 expr) var))) Fall 2008 Programming Development 5 Fall 2008 Programming Development 6 Techniques Techniques 1

  2. ( deriv continued) Implementation of lower layer ((product? expr) ; takes an expression and returns #t (make-sum ; if it is a variable (make-product (define (variable? x) (symbol? x)) (product-arg1 expr) (deriv (product-arg2 expr) ; takes two expressions and is #t if var)) (make-product ; they are both the same variable (product-arg2 expr) (define (same-variable? x y) (deriv (product-arg1 expr) (and (variable? x) var)))) (variable? y) (else (error "Unknown type“ (eq? x y))) expr)))) Fall 2008 Programming Development 7 Fall 2008 Programming Development 8 Techniques Techniques Sums Sum Selectors ; takes two expressions and creates a sum ; with them as the arguments -- sums are ; selectors for a sum retrieve ; simply represented as lists ; the first and second arguments (define (make-sum x y) ; to be added (list '+ x y)) (define (sum-arg1 x) (cadr x)) (define (sum-arg2 x) (caddr x)) ; returns #t if the argument is a sum ; a sum is a list whose first element is ; the symbol + (define (sum? x) (and (pair? x) (eq? (car x) '+))) Fall 2008 Programming Development 9 Fall 2008 Programming Development 10 Techniques Techniques Product Selectors Products ; takes two expressions and creates a product ; with them as the arguments -- products are ; selectors for a product retrieve ; simply represented as lists ; the first and second arguments (define (make-product x y) ; to be multiplied (list '* x y)) (define (product-arg1 x) (cadr x)) (define (product-arg2 x) (caddr x)) ; returns #t if the argument is a product ; a product is a list whose first element is ; the symbol * (define (product? x) (and (pair? x) (eq? (car x) '*))) Fall 2008 Programming Development 11 Fall 2008 Programming Development 12 Techniques Techniques 2

  3. A better make-sum It works! (sort of) (define expr (make-product 'x 'y)) ; a new constructor that simplifies the sum a bit expr --> (* x y) (define (make-sum x y) (cond ((and (number? x) (= x 0)) y) (deriv expr 'x) --> ((and (number? y) (= y 0)) x) (+ (* x 0) (* y 1)) ((and (number? x) (number? y)) Should be y (+ x y)) (else (list '+ x y)))) Need to make simple reductions – use same trick as was used to reduce rational numbers – in the constructor Fall 2008 Programming Development 13 Fall 2008 Programming Development 14 Techniques Techniques A better make-product Still room for improvement ; a new constructor that simplifies the product a bit (define expr (make-product 'x 'y)) (define (make-product x y) (cond ((or (and (number? x) (= x 0)) expr --> (* x y) (and (number? y) (= y 0))) (deriv expr 'x) --> y 0) but ((and (number? x) (= x 1)) y) ((and (number? y) (= y 1)) x) (define expr (make-product 'x 'x)) ((and (number? x) (number? y)) (* x y)) expr --> (* x x) (else (list '* x y)))) (deriv expr 'x) --> (+ x x) (* 2 x) would be better Fall 2008 Programming Development 15 Fall 2008 Programming Development 16 Techniques Techniques Always room for improvement More sophisticated simplifications are done in Reduce, Macsyma, Mathematica Theoretically, no matter how many simplifications we build into the software, there are always more simplifications that can be made Fall 2008 Programming Development 17 Techniques 3

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend