Skip to content

Instantly share code, notes, and snippets.

@rgrannell1
Created March 19, 2013 14:27
Show Gist options
  • Save rgrannell1/5196555 to your computer and use it in GitHub Desktop.
Save rgrannell1/5196555 to your computer and use it in GitHub Desktop.
monic <- function (max, order, a = 1, digits = 3) {
P <- matrix(0, order-1, order-2)
P[row(P)-1 == col(P)] <- 1
join_table <- function (a, b) {
n <- intersect(names(a), names(b))
c(
a[!(names(a) %in% n)],
b[!(names(b) %in% n)],
a[n] + b[n])
}
vector <- function (s) {
paste0(paste0(s, collapse = ','),'\n')
}
get_next <- function (x, max) {
i <- length(x)
repeat {
x[i] <- x[i] + 1
if (x[i] > max && i != 1) {
x[i] <- -max
i <- i-1
} else break
}
return(x)
}
coefficients <- rep(-max, order-1)
while (min(coefficients) != max) {
coefficients <- get_next(coefficients, max)
roots <- eigen(
cbind(P, coefficients),
only.values = TRUE)$values
coord_strings <- apply(
rbind(
round(a * Re(roots), digits),
round(a * Im(roots), digits)),
2, vector )
cat(
coord_strings,
file="/home/rgrannell1/Desktop/cfile.csv",
append=TRUE)
}
}
monic (400, 4, 1, 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment