Download presentation
Presentation is loading. Please wait.
Published byCori Riley Modified over 9 years ago
1
Reusing computations Jordi Cortadella Department of Computer Science
2
Reusing computations Introduction to Programming© Dept. CS, UPC2
3
Calculating Introduction to Programming© Dept. CS, UPC3
4
Calculating Introduction to Programming© Dept. CS, UPC4
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; } Introduction to Programming© Dept. CS, UPC5 It should be a real division
6
Calculating : reusing computations Introduction to Programming© Dept. CS, UPC6
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, UPC7
8
Calculating = 3.14159265358979323846264338327950288... The series approximation: Introduction to Programming© Dept. CS, UPC8 ntermsPi(nterms) 12.000000 53.098413 103.140578 153.141566 203.141592 253.141593
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, UPC9
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, UPC10
11
Calculating sin x Reducing the computation to the (-2 ,2 ) interval: Incremental computation of terms: Introduction to Programming© Dept. CS, UPC11
12
Calculating sin x #include // 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, UPC12
13
Combinations Introduction to Programming© Dept. CS, UPC13
14
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; } Introduction to Programming© Dept. CS, UPC14 Problem: integer division c n/k may not be integer
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; } Introduction to Programming© Dept. CS, UPC15 Always an integer division!
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, UPC16
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.