When we write programs, we implement a particular algorithm to perform a task.
Some algorithms are faster and more accurate than others for the same task.
Speed can be measured by clock time, iteration count, flop count, etc.
Accuracy can be measured by absolute error, relative error, precision, etc. (Ch2.1)
Adding up numbers seems like an important but basic task.
We will look at several algorithms for summations.
What could possibly make one algorithm better than another?
\[ x_1 + x_2 + \cdots + x_n = \sum_{i=1}^n x_i \]
Given a vector x of data values, the naive approach is to add successive terms, one at a time, in the order that they appear in x.
naivesum1 <- function(x) {
s <- 0
n <- length(x)
for(i in 1:n)
s <- s + x[i]
return(s)
}
x <- c(1,1,3,5,8,13,21,34)
naivesum1 <- function(x) {
s <- 0
n <- length(x)
for(i in 1:n)
s <- s + x[i]
return(s)
}
x <- c(1,1,3,5,8,13,21,34)
naivesum1(x)
[1] 86
naivesum2 <- function(x) {
s <- 0
n <- length(x)
for(i in 1:n){
s <- s + x[i]
cat("i = ", i,",", "s = ", s, "\n")}
return(s)
}
x <- c(1,1,3,5,8,13,21,34)
naivesum2(x)
i = 1 , s = 1
i = 2 , s = 2
i = 3 , s = 5
i = 4 , s = 10
i = 5 , s = 18
i = 6 , s = 31
i = 7 , s = 52
i = 8 , s = 86
[1] 86
naivesum1 <- function(x) {
s <- 0
n <- length(x)
for(i in 1:n)
s <- s + x[i]
return(s)
}
x <- c(1,1,3,5,8,13,21,34)
naivesum1(x)
[1] 86
naivesum1<-function(x){
s <- 0
n <- length(x)
for(i in 1:n)
s <- s + x[i]
return(s)
}
x <- c(1,1,3,5,8,13,21,34)
naivesum1(x)
[1] 86
naivesum3<-function(x){
s <- x[1]
n <- length(x)
for(i in 2:n)
s <- s + x[i]
return(s)
}
x <- c(1,1,3,5,8,13,21,34)
naivesum3(x)
[1] 86
naivesum1<-function(x){
s <- 0
n <- length(x)
for(i in 1:n)
s <- s + x[i]
return(s)
}
x<-c(20,0.01,3.14,8)
naivesum1(x)
[1] 31.15
naivesum4<-function(x){
y <- sort(x)
s <- 0
n <- length(y)
for(i in 1:n)
s <- s + y[i]
return(s)
}
x<-c(20,0.01,3.14,8)
naivesum4(x)
[1] 31.15
pwisesum <- function(x) {
n <- length(x)
if(n == 1)
return(x)
m = floor(n / 2)
return(pwisesum(x[1:m]) + pwisesum(x[(m + 1):n]))
}
x <- c(1,1,3,5,8,13,21,34)
pwisesum(x)
[1] 86
kahansum <- function(x) {
comp <- s <- 0
n <- length(x)
for(i in 1:n) {
y <- x[i] - comp
t <- x[i] + s
comp <- (t - s) - y
s <- t
}
return(s)
}
kahansum <- function(x) {
comp <- s <- 0
n <- length(x)
for(i in 1:n) {
y <- x[i] - comp
t <- x[i] + s
comp <- (t - s) - y
s <- t }
return(s)
}
kahansum <- function(x) {
comp <- s <- 0
n <- length(x)
for(i in 1:n) {
y <- x[i] - comp
t <- x[i] + s
comp <- (t - s) - y
s <- t }
return(s)
}
x <- c(1,1,3,5,8,13,21,34)
kahansum(x)
[1] 86
x <- c(1,1,3,5,8,13,21,34)
sum(x)
[1] 86