R/compan.R

Defines functions compan

Documented in compan

###
### COMPAN.R  Polynom
###

compan <- function(p) {
	if (length(p) == 0) return(c())
	if ( !is.vector(p, mode="numeric") &&
	    (!is.vector(p, mode="complex")) )
		stop("Argument p must be a vector of real or complex numbers.")
	while(p[1] == 0 && length(p) >= 2) {
		p <- p[2:length(p)]
	}

	n <- length(p)
	if (n <= 1) {
		a <- c()
	} else {
		if (n == 2) {
			a <- -p[2]/p[1]
		} else {
			a <- diag(0, n-1, n-1)
			for (i in 2:(n-1)) {
				a[i, i-1] <- 1
			}
			a[1, ] <- -p[2:n]/p[1]
		}
	}
	return(a)
}

Try the pracma package in your browser

Any scripts or data that you put into this service are public.

pracma documentation built on March 19, 2024, 3:05 a.m.