Reusing computations - Computer Science Department

Reusing computations
Jordi Cortadella
Department of Computer Science
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
2
Calculating 
 can be calculated using the following series:
πœ‹
=
2
∞
𝑛=0
𝑛!
2𝑛 + 1 β€Ό
where 𝑛‼ = 1 × 3 × 5 × 7 × β‹― × π‘› (𝑛 odd)
Introduction to Programming
© Dept. CS, UPC
3
Calculating 
πœ‹ 0! 1! 2! 3!
=
+
+
+
+β‹―
2 1β€Ό 3β€Ό 5β€Ό 7β€Ό
πœ‹
1
1βˆ™2
1βˆ™2βˆ™3
=1+
+
+
+β‹―
2
1βˆ™3 1βˆ™3βˆ™5 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
4
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
5
Calculating : reusing computations
πœ‹
1
1βˆ™2
1βˆ™2βˆ™3
=1+
+
+
+β‹―
2
1βˆ™3 1βˆ™3βˆ™5 1βˆ™3βˆ™5βˆ™7
πœ‹
= 𝑑0 + 𝑑1 + 𝑑2 + 𝑑3 + β‹―
2
𝑛!
π‘›βˆ’1 !βˆ™π‘›
𝑛
𝑑𝑛 =
=
= π‘‘π‘›βˆ’1
2𝑛 + 1 β€Ό
2𝑛 βˆ’ 1 β€Ό βˆ™ (2𝑛 + 1)
2𝑛 + 1
Introduction to Programming
© Dept. CS, UPC
6
Calculating 
// Pre: nterms > 0
// Returns an estimation of  using nterms terms
// of the series.
double Pi(int nterms) {
double sum = 1;
double term = 1;
// Approximation of /2
// 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
7
Calculating 
β€’  = 3.14159265358979323846264338327950288...
β€’ The series approximation:
Introduction to Programming
nterms
1
5
Pi(nterms)
2.000000
3.098413
10
15
20
25
3.140578
3.141566
3.141592
3.141593
© Dept. CS, UPC
8
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
9
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
10
Calculating sin x
β€’ Reducing the computation to the (-2,2)
interval:
β€’ Incremental computation of terms:
Introduction to Programming
© Dept. CS, UPC
11
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
12
Combinations
Calculating
𝑛
=
π‘˜!
π‘˜
𝑛!
π‘›βˆ’π‘˜ !
=
𝑛(π‘›βˆ’1)β‹―(π‘›βˆ’π‘˜+1)
π‘˜(π‘˜βˆ’1)β‹―1
– Naïve method: 2𝑛 multiplications and 1 division
(potential overflow with 𝑛!)
– Recursion:
𝑛
𝑛
=
=1
0
𝑛
𝑛 π‘›βˆ’1
π‘›βˆ’π‘˜+1
𝑛
𝑛
=
=
π‘˜
π‘˜βˆ’1
π‘˜ π‘˜βˆ’1
π‘˜
𝑛
π‘›βˆ’1
π‘›βˆ’1
π‘›βˆ’1
=
=
+
π‘˜
π‘˜βˆ’1
π‘˜
π‘›βˆ’π‘˜
Introduction to Programming
© Dept. CS, UPC
13
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
14
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
15
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
16