Reusing computations Jordi Cortadella Department of Computer Science.
-
Upload
cori-riley -
Category
Documents
-
view
219 -
download
2
Transcript of Reusing computations Jordi Cortadella Department of Computer Science.
Reusing computations
Jordi CortadellaDepartment of Computer Science
© Dept. CS, UPC 2
Reusing computations• There are certain algorithms that require a
repeated sequence of similar computations. For example:
• 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:
where ( odd)
Introduction to Programming
© Dept. CS, UPC 4
Calculating
Since an infinite sum cannot be computed, it may often be sufficient to compute an approximation with a finite sum 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(2k + 1); sum = sum + term; } return 2sum;
}
Introduction to Programming
It should be a real division
© Dept. CS, UPC 6
Calculating : reusing computations
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 = termk/(2.0k + 1.0); sum = sum + term; } return 2sum;
}
Introduction to Programming
© Dept. CS, UPC 8
Calculating • = 3.14159265358979323846264338327950288...• The series approximation:
Introduction to Programming
nterms Pi(nterms)1 2.0000005 3.098413
10 3.14057815 3.14156620 3.14159225 3.141593
© 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/(2M_PI)); x = x – 2kM_PI; // reduce to the (-2,2) interval double term = x; double x2 = xx; int d = 1; double sum = term;
while (abs(term) >= 1e-8) { term = -termx2/((d+1)(d+2)); sum = sum + term; d = d + 2; }
return sum;}
Introduction to Programming
© Dept. CS, UPC 13
CombinationsCalculating
– Naïve method: 2 multiplications and 1 division(potential overflow with )
– Recursion:
Introduction to Programming
© Dept. CS, UPC 14
Combinations// Pre: n k 0// Returns combinations(n k)int combinations(int n, int k) { int c = 1; for (; k > 0; --k) { c = cn/k; --n; } return c;}
Introduction to Programming
Problem: integer divisioncn/k may not be integer
© 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 ncombinations(n – 1, k – 1)/k;}
Introduction to Programming
Always aninteger division!
© 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