Presentation is loading. Please wait.

Presentation is loading. Please wait.

Jordi Cortadella Department of Computer Science

Similar presentations


Presentation on theme: "Jordi Cortadella Department of Computer Science"β€” Presentation transcript:

1 Jordi Cortadella Department of Computer Science
Reusing computations Jordi Cortadella Department of Computer Science

2 Reusing computations There are certain algorithms that require a repeated sequence of similar computations. For example: 𝑆 π‘₯ = 𝑖=0 π‘˜ 𝑑 𝑖 (π‘₯) Strategy: try to reuse the work done from previous computations as much as possible. Introduction to Programming Β© Dept. CS, UPC

3 Calculating   can be calculated using the following series: πœ‹ 2 = 𝑛=0 ∞ 𝑛! 2𝑛+1 β€Ό where 𝑛‼=1Γ—3Γ—5Γ—7×⋯×𝑛 (𝑛 odd) Introduction to Programming Β© Dept. CS, UPC

4 Calculating  πœ‹ 2 = 0! 1β€Ό + 1! 3β€Ό + 2! 5β€Ό + 3! 7β€Ό +β‹― πœ‹ 2 =1+ 1 1βˆ™3 + 1βˆ™2 1βˆ™3βˆ™5 + 1βˆ™2βˆ™3 1βˆ™3βˆ™5βˆ™7 +β‹― Since an infinite sum cannot be computed, it may often be sufficient to compute an approximation with a finite number of terms. Introduction to Programming Β© Dept. CS, UPC

5 Calculating : naïve approach
// Pre: nterms > 0 // Returns an estimation of  using nterms terms // of the series. double Pi(int nterms) { double sum = 1; // Approximation of /2 // Inv: sum is an approximation of /2 with k terms, // term is the k-th term of the series. for (int k = 1; k < nterms; ++k) { term = factorial(k)/double_factorial(2ο€ͺk + 1); sum = sum + term; } return 2ο€ͺsum; } It should be a real division Introduction to Programming Β© Dept. CS, UPC

6 Calculating : reusing computations
πœ‹ 2 =1+ 1 1βˆ™3 + 1βˆ™2 1βˆ™3βˆ™5 + 1βˆ™2βˆ™3 1βˆ™3βˆ™5βˆ™7 +β‹― πœ‹ 2 = 𝑑 0 + 𝑑 1 + 𝑑 2 + 𝑑 3 +β‹― 𝑑 𝑛 = 𝑛! 2𝑛+1 β€Ό = π‘›βˆ’1 !βˆ™π‘› 2π‘›βˆ’1 β€Όβˆ™(2𝑛+1) = 𝑑 π‘›βˆ’1 𝑛 2𝑛+1 Introduction to Programming Β© Dept. CS, UPC

7 Calculating  // Pre: nterms > 0 // Returns an estimation of  using nterms terms // of the series. double Pi(int nterms) { double sum = 1; // Approximation of /2 double term = 1; // Current term of the sum // Inv: sum is an approximation of /2 with k terms, // term is the k-th term of the series. for (int k = 1; k < nterms; ++k) { term = termο€ͺk/(2.0ο€ͺk + 1.0); sum = sum + term; } return 2ο€ͺsum; } Introduction to Programming Β© Dept. CS, UPC

8 Calculating   = The series approximation: nterms Pi(nterms) 1 5 10 15 20 25 Introduction to Programming © Dept. CS, UPC

9 Taylor and McLaurin series
Many functions can be approximated by using Taylor or McLaurin series, e.g.: Example: sin x Introduction to Programming Β© Dept. CS, UPC

10 Calculating sin x McLaurin series:
It is a periodic function (period is 2) Convergence improves as x gets closer to zero Introduction to Programming © Dept. CS, UPC

11 Calculating sin x Reducing the computation to the (-2,2) interval:
Incremental computation of terms: Introduction to Programming Β© Dept. CS, UPC

12 Calculating sin x #include <cmath> // Returns an approximation of sin x. double sin_approx(double x) { int k = int(x/(2ο€ͺM_PI)); x = x – 2ο€ͺkο€ͺM_PI; // reduce to the (-2,2) interval double term = x; double x2 = xο€ͺx; int d = 1; double sum = term; while (abs(term) >= 1e-8) { term = -termο€ͺx2/((d+1)ο€ͺ(d+2)); sum = sum + term; d = d + 2; } return sum; } Introduction to Programming Β© Dept. CS, UPC

13 Combinations Calculating 𝑛 π‘˜ = 𝑛! π‘˜! π‘›βˆ’π‘˜ ! = 𝑛(π‘›βˆ’1)β‹―(π‘›βˆ’π‘˜+1) π‘˜(π‘˜βˆ’1)β‹―1
NaΓ―ve method: 2𝑛 multiplications and 1 division (potential overflow with 𝑛!) Recursion: 𝑛 0 = 𝑛 𝑛 =1 𝑛 π‘˜ = 𝑛 π‘˜ π‘›βˆ’1 π‘˜βˆ’1 = π‘›βˆ’π‘˜+1 π‘˜ 𝑛 π‘˜βˆ’ = 𝑛 π‘›βˆ’π‘˜ π‘›βˆ’1 π‘˜ = π‘›βˆ’1 π‘˜βˆ’1 + π‘›βˆ’1 π‘˜ Introduction to Programming Β© Dept. CS, UPC

14 Problem: integer division cο€ͺn/k may not be integer
Combinations // Pre: n ο‚³ k ο‚³ 0 // Returns combinations(n k) int combinations(int n, int k) { int c = 1; for (; k > 0; --k) { c = cο€ͺn/k; n; } return c; } Problem: integer division cο€ͺn/k may not be integer Introduction to Programming Β© Dept. CS, UPC

15 Combinations: recursive solution
// Pre: n ο‚³ k ο‚³ 0 // Returns combinations(n k) int combinations(int n, int k) { if (k == 0) return 1; return nο€ͺcombinations(n – 1, k – 1)/k; } Always an integer division! Introduction to Programming Β© Dept. CS, UPC

16 Conclusions Try to avoid the brute force to perform complex computations. Reuse previous computations whenever possible. Use your knowledge about the domain of the problem to minimize the number of operations. Introduction to Programming Β© Dept. CS, UPC


Download ppt "Jordi Cortadella Department of Computer Science"

Similar presentations


Ads by Google