CSC 344 - Algorithms and Complexity
CSC 344 - Algorithms and Complexity
Complexity
Lecture #11 – Numerical
Computation, Numerical Integration
and the Fast Fourier Transform
Calculating ex
• = ∑
! =1+ + + ⋯+ +…
! ! !
exp3()
double exp3(int x) {
double sum = 1.0, term = 1.0;
int i;
∫ f ( x )dx ≈ ∑ wi f ( xi )
a
i =0
b ∆x ∆x ∆x
∫a
f ( x )dx =
2
[ f ( a ) + f ( x1 )] +
2
[ f ( x1 ) + f ( x2 )] + .. +
2
[ f ( xn −1 ) + f (b)]
1 1
= ∆x[ f ( a ) + f ( x1 ) + .. f ( xn −1 ) + f (b)]
2 2
trapezoid()
// trapezoid() - Uses the Trapezoid rule to find
// the definite integral of f(x)
// Takes the bounds as parameters
// Uses f(x) that appears below.
float trapezoid(int lowBound, int hiBound){
int numDivisions = 4;
float x, increment, integral = 0.0;
increment = (float)(hiBound-lowBound)
/ (float) numDivisions;
x = lowBound;
Simpson’s Rule
Still, the more accurate integration formula can be achieved by
approximating the local curve by a higher order function, such as
a quadratic polynomial. This leads to the Simpson’s rule and the
formula is given as:
b ∆x
∫ a
f ( x )dx =
3
[ f ( a ) + 4 f ( x1 ) + 2 f ( x2 ) + 4 f ( x3 ) + ..
..2 f ( x2 m −2 ) + 4 f ( x2 m −1 ) + f (b)]
simpson.c
#include <iostream>
// Adds f(lowBound)
integral = f(x);
// Increment x to the next value, calculate
// f(x)
// Add 4f(x) for even numbered values
// Add 2f(x) for odd numbered values
for (int i = 1; i < numDivisions; i++){
x += increment;
if (i % 2 == 1)
integral = integral + 4.0*f(x);
else
integral = integral + 2.0*f(x);
}
// Add f(hiBound)
integral = integral + f(hiBound);
2-1
Using 4 subdivisions for the numerical integration: ∆x= = 0.25
4
Rectangular rule:
2
i xi* f(xi*)
∫1
x 3dx
1 1.125 1.42 = ∆x[ f (1.125) + f (1.375) + f (1.625) + f (1.875)]
2 1.375 2.60 = 0.25(14.9) = 3.725
3 1.625 4.29
4 1.875 6.59
Trapezoidal Rule
i xi f(xi) 2
∫1
x 3dx
1 1
1 1
1 1.25 1.95 = ∆x[ f (1) + f (1.25) + f (1.5) + f (1.75) + f (2)]
2 2
2 1.5 3.38 = 0.25(15.19) = 3.80
3 1.75 5.36
2 8
Simpson’s Rule
2 ∆x
∫ 1
x 3dx =
3
[ f (1) + 4 f (1.25) + 2 f (1.5) + 4 f (1.75) + f (2)]
0.25
= (45) = 3.75 ⇒ perfect estimation
3
Transforms
• Transform:
– In mathematics, a function that results when a
given function is multiplied by a so-called kernel
function, and the product is integrated between
suitable limits. (Britannica)
– = ,
Kernel
Fourier Transform
• Property of transforms:
– They convert a function from one domain to
another with no loss of information
• Fourier Transform:
• Amplitude = 100
• Frequency = number of cycles in one second = 200 Hz
• Example:
– Human ears do not hear wave-like oscilations, but
constant tone
Fourier Transform
• Because of the
property:
As We Continue To Divide…
• This works best if N=2n 000 000
001 001
• We re-order the elements in 010 010
110 110
111 111
Now, we combine them..
• We start with our Fourier transforms of length
one and we perform log2 N combinations
n = 2 * nn;
j = i
// Do the bit reversal
for (i = 1; i <= n; i+=2) {
if (j > i) {
// Swap 2 complex values
tempr = data[j];
tempi = data[j+1];
data[j] = data[i];
data[j+1] = data[i+1];
data[i] = tempr;
data[i+1] = tempi;
}
m = n/2;
while (m >= 2 && j > m) {
j = j - m;
m = m / 2;
}
j = j + m;
}
// Trig recurrence
wtemp = wr;
wr = wr*wpr - wi*wpi + wr;
wi = wi*wpi + wtemp*wpi + wi;
}
max = istep;
}
}
Applications
• In image processing:
– Instead of time domain: spatial domain (normal
image space)
– frequency domain: space in which each image
value at image position F represents the amount
that the intensity values in image I vary over a
specific distance related to F