Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 56 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
56
Dung lượng
5,99 MB
Nội dung
-282- ;; make-even : N -> N[even] ;; to compute the i-th even number (define (make-even i) (* 2 i)) ;; make-odd : N -> N[odd] ;; to compute the i-th odd number (define (make-odd i) (+ (* 2 i) 1)) In short, functions from natural numbers to numbers are representations of infinite sequences. A mathematical series is the sum of a sequence. The three finite sequences have the sums 20, 25, and 75, respectively. In the case of infinite sequences it is often interesting to consider a finite portion, staring with the first one. 51 For example, adding the first 10 even numbers yields 90, and adding the first 10 odd numbers yields 100. Computing a series is clearly a job for a computer. Here are functions that add up the first n odd or even numbers, respectively, using make-even and make-odd to compute the required numbers: ;; series-even : N -> number ;; to sum up the first ;; n even numbers (define (series-even n) (cond [(= n 0) (make-even n)] [else (+ (make-even n) (series-even (- n 1)))])) ;; series-odd : N -> number ;; to sum up the first ;; n odd numbers (define (series-odd n) (cond [(= n 0) (make-odd n)] [else (+ (make-odd n) (series-odd (- n 1)))])) The two functions are natural candidates for abstraction and here is the result of following our basic abstraction recipe: ;; series : N (N -> number) -> number ;; to sum up the first n numbers in the sequence a-term, (define (series n a-term) (cond [(= n 0) (a-term n)] [else (+ (a-term n) (series (- n 1) a-term))])) The first argument specifies where the addition starts. The second argument of series is a function that maps a natural number to the corresponding term in the series. To test series , we apply it to make-even and make-odd : ;; series-even1 : N -> number (define (series-even1 n) (series n make-even)) ;; series-odd1 : N -> number (define (series-odd1 n) (series n make-odd)) For over a century, mathematicians have used the Greek symbol Sigma to communicate about series. The two series above would be expressed as A true (lazy) mathematician would also replace make-even and make-odd by their definitions, that is, 2 · i and 2 · i + 1, but we refrain from doing so to emphasize the analogy to our (well- organized) functions. TEAMFLY TEAM FLY PRESENTS -283- Exercise 23.1.1. Use local to create series-local from series-even and series-odd. Show with a hand-evaluation that (series-local make-even) is equivalent to series-even . 23.2 Arithmetic Sequences and Series In an arithmetic sequence each successor term a n +1 is the result of adding a fixed constant to a n . Here is a concrete example, matched up with the natural numbers: Here the starting point is 3 and the constant is 5. From these two facts, called starting point and summand, respectively, all other terms in the sequence can be determined. Exercise 23.2.1. Develop the recursive function a-fives , which consumes a natural number and recursively determines the corresponding term in the above series. Exercise 23.2.2. Develop the non-recursive function a-fives-closed . It consumes a natural number and determines the corresponding term in the above series. A non-recursive function is sometimes called a closed form. Exercise 23.2.3. Use series to determine the sum of the a-fives sequence for the bounds 3, 7, and 88. Can an infinite arithmetic series have a sum? Exercise 23.2.4. Develop the function seq-a-fives , which consumes a natural number n and creates the sequence of the first n terms according to a-fives or a-fives-closed . Hint: Use build-list . Exercise 23.2.5. Develop arithmetic-series . The function consumes two numbers: start and s . Its result is a function that represents the arithmetic series whose starting point is start and whose summand is s . For example, (arithmetic-series 3 5) yields a-fives (or a- fives-closed ). Similarly, (arithmetic-series 0 2) produces a function that represents the series of even numbers. 23.3 Geometric Sequences and Series In a geometric sequence each succesor term g n +1 is the result of multiplying a fixed constant with g n . Here is a concrete example, matched up with the natural numbers: TEAMFLY TEAM FLY PRESENTS -284- Here the starting point is 3 and the constant is 5. From these, called starting point and factor, respectively, every other term in the sequence is determined. Exercise 23.3.1. Develop the recursive function g-fives, which consumes a natural number and recursively determines the corresponding term in the above geometric sequence. Exercise 23.3.2. Develop the non-recursive function g-fives-closed . It consumes a natural number and determines the corresponding term in the above series. Exercise 23.3.3. Develop the function seq-g-fives , which consumes a natural number n and creates the sequence of the first n terms according to g-fives or g-fives-closed . Hint: Use build-list . Exercise 23.3.4. Develop geometric-series . The function consumes two numbers: start and s . Its result is a function that represents the geometric series whose starting point is start and whose factor is s . For example, (geometric-series 3 5) yields g-fives (or g-fives- closed ). Exercise 23.3.5. Use series to determine the sum of the g-fives sequence for the bounds 3, 7, and 88. Use series to determine the sum of (geometric-series 1 .1) for the bounds 3, 7, 88. Can an infinite geometric series have a sum? Taylor Series Mathematical constants like and e or functions like sin, cos, log are difficult to compute. Since these functions are important for many daily engineering applications, mathematicians have spent a lot of time and energy looking for better ways to compute these functions. One method is to replace a function with its Taylor series, which is, roughly speaking, an infinitely long polynomial. A Taylor series is the sum of a sequence of terms. In contrast to arithmetic or geometric sequences, the terms of a Taylor series depend on two unknowns: some variable x and the position i in the sequence. Here is the Taylor series for the exponential function: That is, if we wish to compute e x for any specific x, we replace x with the number and determine the value of the series. In other words, for a specific value of x , say, 1, the Taylor series becomes an ordinary series, that is, a sum of some sequence of numbers: While this series is the sum of an infinitely long sequence, it actually is a number, and it often suffices to add just the first few terms to have an idea what the number is. TEAMFLY TEAM FLY PRESENTS -285- The key to computing a Taylor series is to formulate each term in the underlying sequence as a function of x and its position i . In our running example, the Taylor sequence for the exponential function has the shape Assuming a fixed x , here is an equivalent Scheme definition: ;; e-taylor : N -> number (define (e-taylor i) (/ (expt x i) (! i))) ;; ! : N -> number (define (! n) (cond [(= n 0) 1] [else (* n (! (sub1 n)))])) The first function computes the term; the second computes the factorial of a natural number. To compute the value of e x , we now just need to ask for (series 10 e-taylor) , assuming we want the first 10 items of the sequence included. Putting everything together, we can define a function that computes the x th power of e . Since the function requires two auxiliaries, we use a local : (define (e-power x) (local ((define (e-taylor i) (/ (expt x i) (! i))) (define (! n) (cond [(= n 0) 1] [else (* n (! (sub1 n)))]))) (series 10 e-taylor))) Exercise 23.3.6. Replace 10 by 3 in the definition of e-power and evaluate (e-power 1) by hand. Show only those lines that contain new applications of e-taylor to a number. The results of e-power are fractions with large numerators and denominators. In contrast, Scheme's built-in exp function produces an inexact number. We can turn exact fractions into inexact numbers with the following function: ;; exact-> inexact : number [exact] -> number [inexact] Test the function and add it to e-power 's body. Then compare the results of exp and e-power . Increase the number of items in the series until the difference between the results is small. Exercise 23.3.7. Develop the function ln, which computes the Taylor series for the natural logarithm. The mathematical definition of the series is TEAMFLY TEAM FLY PRESENTS -286- This Taylor series has a value for all x that are greater than 0 . DrScheme also provides log , a primitive for computing the natural logarithm. Compare the results for ln and log . Then use exact-> inexact (see exercise 23.3.6) to get results that are easier to compare. Exercise 23.3.8. Develop the function my-sin , which computes the Taylor series for sin , one of the trigonometric functions. The Taylor series is defined as follows: It is defined for all x . Hint: The sign of a term is positive if the index is even and negative otherwise. Mathematicians compute ( - 1) i to determine the sign; programmers can use cond instead. Exercise 23.3.9. Mathematicians have used series to determine the value of for many centuries. Here is the first such sequence, discovered by Gregory (1638-1675): Define the function greg , which maps a natural number to the corresponding term in this sequence. Then use series to determine approximations of the value of . Note on : The approximation improves as we increase the number of items in the series. Unfortunately, it is not practical to compute with this definition. 23.4 The Area Under a Function Consider the function graph in figure 64. Suppose we wish to know the area between the x axis, the fat lines labeled a and b, and the graph. Determining the area under the graph of a function for some specific interval is called integrating a function. Since engineers had to solve this kind of problem before computers were available, mathematicians have studied it extensively. For a small class of functions, it is indeed possible to determine the area exactly. For the other cases, mathematicians have developed several methods to determine close estimates. Since these methods involve lots of mechanical calculations, they are natural candidates for computer functions. TEAMFLY TEAM FLY PRESENTS -287- Figure 64: Integrating a function f between a and b A general integration function must consume three inputs: a , b , and the function f . The fourth part, the x axis, is implied. This suggests the following contract: ;; integrate : (number -> number) number number -> number ;; to compute the area under the graph of f between a and b (define (integrate f a b) ) Kepler suggested one simple integration method. It consists of three steps: 1. divide the interval into two parts: [a,(a + b/2)] and [(a + b/2),b]; 2. compute the area of each trapezoid; and 3. add the two areas to get an estimate at the integral. Exercise 23.4.1. Develop the function integrate-kepler . It computes the area under some the graph of some function f between left and right using Kepler's rule. Another simple method is to think of the area as a sequence of many small rectangles. Each rectangle is as tall as the function graph in, say, the middle of the rectangle. Figure 64 shows two examples. By adding up the area of the rectangles, we get a good estimate at the area under the graph. The more rectangles we consider, the closer the estimate is to the actual area. Let us agree that R stands for the number of rectangles that we wish to consider. To determine how large these rectangles are, we need to figure out how large their sides are. The length of the side on the x axis is the length of the interval divided by the number of rectangles: For the height of the rectangle, we need to determine its midpoint and then the value of f at the midpoint. The first midpoint is clearly at a plus half of the width of the rectangle, that is, if TEAMFLY TEAM FLY PRESENTS -288- the area is where W stands for width and S for step from now on. To get from the rectangle starting at a to the next one on the right, we must add the width of one rectangle. That is, the next midpoint (called x 1 in figure 64) is at the third one at and so on. The following table explains the three sequences that are involved in the usual manner: In the second row, M stands for midpoint. The first rectangle has index 0, the last one R - 1. Using this sequence of rectangles, we can now determine the area under the graph as a series: Exercise 23.4.2. Develop the function integrate . It computes the area under some the graph of some function f between left and right using the rectangle-series method. Use test cases for f , a , and b for which one can determine the area exactly and easily by hand, for example, (define (id x) x) . Compare the results with those of integrate from exercise 23.4.1. Make R a top-level constant: ;; R : number of rectangles to approximate integral (define R ) Test integrate on sin and increase R gradually from 10 to 10000 . What happens to the result? 23.5 The Slope of a Function Let us take another look at the function graph in figure 64. For many problems, we need to be able to draw a line that has the same slope as some curve at a certain point. Indeed, computing TEAMFLY TEAM FLY PRESENTS -289- the slope is often the true goal. In economics problems, the slope is the growth rate of a company if the curve represents the income over time. In a physics problem, the curve could represent the velocity of some object; its slope, at any point, is then the current acceleration of the object. Determining the slope of some function f at some point x is to differentiate the function. The differential operator (also called a functional) returns a function f' (pronounced ``f prime''). It tells us for any x what the slope of f is at that point. Computing f' is complicated, so it is again a good task for a computer program. The program consumes some function f and produces f'. Figure 65: The graph of some function To design a ``differentiator'' we must study how we could construct lines that have the same slope as a curve. In principle, such a line touches the curve at just that point. But suppose we relax this constraint for a moment and look at straight lines that intersect the curve close to the point of interest. We pick two points that are equally far away from x, say, x - and x + ; the constant , pronounced epsilon, represents some small distance. Using the two corresponding points on the curve, we can determine a straight line that has the proper slope. The situation is sketched in figure 65. If the point of interest has coordinate x, the two points are (x,f(x - )) and (x,f(x + )). Hence the slope of the line is That is, the difference between the height of the right point and the left point divided by their horizontal distance. Determining the line from the slope and one of the points or even from two points is an exercise. Exercise 23.5.1. The equation for a line is By now, it is straightforward to translate this equation into Scheme: (define (y x) TEAMFLY TEAM FLY PRESENTS -290- (+ (* a x) b)) To obtain a concrete line we must replace a and b with numbers. The teachpack graphing.ss provides one operation for drawing lines: graph-line . The operation consumes a line like y and a color, say, 'red . Use graph-line to draw the graphs of the following lines: 1. y 1 (x) = x + 4 2. y 2 (x) = 4 - x 3. y 3 (x) = x + 10 4. y 4 (x) = 10 - x 5. y 5 (x) = 12 Exercise 23.5.2. It is a standard mathematical exercise to develop the equation for a line from a point on the line and its slope. Look up the method in your mathematics book. Then develop the function line-from-point+slope , which implements the method. The function consumes a posn (the point) and a number (the slope). It produces a function that represents the line in the spirit of exercise 23.5.1. Testing a function-producing function like line-from-point+slope can be done in two ways. Suppose we apply the function to (0,4) and 1. The result should be line y 1 from exercise 23.5.1. To check this, we can either apply the result of (line-from-point+slope (make-posn 0 4) 1) to some numbers, or we can draw the result using the operations in graphing.ss . In the first case, we must use y 1 to compare outputs; in the second case we can draw the result in one color and the hand-constructed line in a different one and observe the effect. Once we have an intersecting line through (x,f(x - )) and (x,f(x + )), we can also get a line with the proper slope. By decreasing until it is (almost) indistinguishable from 0, the two intersection points move closer and closer until they are one, namely, (x,f(x)), the point for which we wish to know the slope. 52 Exercise 23.5.3. Use the operation graph-fun in the teachpack graphing.ss to draw the mathematical function The operation works just like draw-line (see exercise 23.5.1.) Suppose we wish to determine the slope at x = 2. Pick an > 0 and determine the slope of the line that goes through (x,f(x - )) and (x,f(x + )) with the above formula. Compute the line with line- from-point+slope from exercise 23.5.2 and use draw-line to draw it into the same coordinate system as y . Repeat the process with /2 and then with /4. If our goal is to define the differential operator as a Scheme function, we can approximate it by setting to a small number and by translating the mathematical formula into a Scheme expression: TEAMFLY TEAM FLY PRESENTS -291- ;; d/dx : (num -> num) -> (num -> num) ;; to compute the derivative function of f numerically (define (d/dx f) (local ((define (fprime x) (/ (- (f (+ x )) (f (- x ))) (* 2 ))) (define )) fprime)) Note that d/dx consumes and produces functions just like the differential operator in mathematics. As mentioned in the introduction to this section, the differential operator computes the function f' from some function f. The former computes the slope of f for any x. For straight lines, the slope is always known. Hence a function that represents a straight line is an ideal test case for d/dx . Let us consider (define (a-line x) (+ (* 3 x) 1)) The evaluation of (d/dx a-line) proceeds as follows: (d/dx a-line) = (local ((define (fprime x) (/ (- (a-line (+ x )) (a-line (- x ))) (* 2 ))) (define )) fprime) = (define (fprime x) (/ (- (a-line (+ x )) (a-line (- x ))) (* 2 ))) (define ) fprime Now, if we think of (+ x ) and (- x ) as numbers, we can evaluate the application of a-line in the definition of fprime : 53 (define (fprime x) (/ (- (+ (* 3 (+ x )) 1) (+ (* 3 (- x )) 1)) (* 2 ))) = (define (fprime x) (/ (- (* 3 (+ x )) (* 3 (- x ))) (* 2 ))) = (define (fprime x) (/ (* 3 (- (+ x ) (- x ))) (* 2 ))) = (define (fprime x) (/ (* 3 (* 2 )) (* 2 ))) = (define (fprime x) 3) TEAMFLY TEAM FLY PRESENTS [...]... the trivial cases determine how to generate new problems from the given problem, possibly using auxiliary functions determine how to combine the solutions of these problems into a solution for the given problem Termin to argue that the show that the inputs to the recursive applications are algorithm terminates smaller than the given input for all possible inputs Figure 69 : Designing algorithms -310TEAM... be explored If it is chosen, it is important to document the problem generation with good examples and to give a good termination argument Y L F M Exercise 26. 3.5 Evaluate (quick-sort (list 10 6 8 9 14 12 3 11 14 16 2)) by hand Show only those lines that introduce a new recursive call to quick-sort How many recursive applications of quick-sort are required? How many recursive applications of append?... example, however: (gcd-structural 101135853 4501 464 0) A E T The result is 177 and to get there gcd-structural had to compare 10113 567 6, that is, 101135853 - 177, numbers This is a large effort and even reasonably fast computers spend several minutes on this task Exercise 26. 3.1 Enter the definition of gcd-structural into the Definitions window and evaluate (time (gcd-structural 101135853 4501 464 0)) in... a much faster solution to our problem Exercise 26. 3.2 Formulate informal answers to the four key questions for gcdgenerative Exercise 26. 3.3 Define gcd-generative and evaluate (time (gcd-generative 101135853 4501 464 0)) in the Interactions window Evaluate (clever-gcd 101135853 4501 464 0) by hand Show only those lines that introduce a new recursive call to clever-gcd Exercise 26. 3.4 Formulate a termination... -> true ;; to model the movement of a ball until it goes out of bounds (define (move-until-out a-ball) ) Because the function consumes a ball and draws its movement on a canvas, it produces true like all other functions that draw onto a canvas Designing it with the recipe for structures makes no sense, however After all, it is already clear how to draw-and-clear the ball and how to move it, too What... for a recursive call brings us closer to a solution than the original input A E T Phase Goal Activity Examples to characterize the inputoutput relationship and the computational process via examples create and show examples of trivially solvable problems create and show examples that require recursive processing illustrate how to work through the examples Body to define an algorithm formulate tests... are shown in figure 68 ;; quick-sort : (listof number) -> (listof number) ;; to create a list of numbers with the same numbers as ;; alon sorted in ascending order (define (quick-sort alon) (cond [(empty? alon) empty] [else (append (quick-sort (smaller-items alon (first alon))) (list (first alon)) (quick-sort (larger-items alon (first alon))))])) ;; larger-items : (listof number) number -> (listof number)... again, 6 and 25 are both numbers with several divisors: 1 6 is evenly divisible by 1, 2, 3, and 6; 2 25 is evenly divisible by 1, 5, and 25 Y L F M A E T Still, the greatest common divisior of 25 and 6 is 1 In contrast, 18 and 24 have many common divisors: 1 18 is evenly divisible by 1, 2, 3, 6, 9, and 18; 2 24 is evenly divisible by 1, 2, 3, 4, 6, 8, 12, and 24 The greatest common divisor is 6 Following... best to formulate tests as boolean-valued expressions that automatically compare the expected value with the computed value (see section 17.8) Exercise 26. 0.7 Formulate informal answers to the four key questions for the problem of modeling a ball's movement across a canvas until it is out of bounds A E T Exercise 26. 0.8 Formulate informal answers to the four key questions for the quick-sort problem How. .. description into a function definition may cause an infinite loop The problem is most easily understood with an example Consider the following definition of smaller-items, one of the two ``problem generators'' for quick-sort: -309TEAM FLY PRESENTS ;; smaller-items : (listof number) number -> (listof number) ;; to create a list with all those numbers on alon ;; that are smaller than or equal to threshold . that draw onto a canvas. Designing it with the recipe for structures makes no sense, however. After all, it is already clear how to draw-and-clear the ball and how to move it, too. What is. Figure 65 : The graph of some function To design a ``differentiator'' we must study how we could construct lines that have the same slope as a curve. In principle, such a line touches. estimate is to the actual area. Let us agree that R stands for the number of rectangles that we wish to consider. To determine how large these rectangles are, we need to figure out how large