Skip to content

Instantly share code, notes, and snippets.

@glesica
Created November 26, 2012 17:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glesica/4149649 to your computer and use it in GitHub Desktop.
Save glesica/4149649 to your computer and use it in GitHub Desktop.
convolution <- function(A, B) {
# Computes the convolution of two vectors.
#
# Args:
# A, B: column vectors representing polynomial coefficients.
#
# Returns:
# The coefficient vector resulting from multiplying the polynomial
# represented by A by the polynomial represented by B.
Ap <- c(A, B*0)
Bp <- c(B, A*0)
C <- fft(Ap) * fft(Bp)
D <- round(fft(C, inverse=T))
E <- as.numeric(D)
R <- E/(length(A)+length(B))
return(R)
}
zeroes <- function(Z) {
if (length(Z) == 1) {
return(c(1, -Z[1]))
}
ml <- length(Z) / 2
mr <- ml + 1
A <- zeroes(Z[1:ml])
B <- zeroes(Z[mr:length(Z)])
return(convolution(A, B))
}
# Example
A <- c(1,2,3,4)
B <- c(5,6,7,8)
print(convolution(A, B))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment