Download presentation
Presentation is loading. Please wait.
Published byΞΟΞΉΟ ΞΞΏΟ Ξ²ΞΞ»Ξ·Ο Modified over 6 years ago
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.