李哲源*_*李哲源 3 plot r polynomials
有人问我这个有趣的问题,我认为值得在这里发布,因为Stack Overflow上没有任何相关的线程.
假设我在长度n向量中有多项式系数pc,其中n - 1变量的多项式x可以用其原始形式表示:
pc[1] + pc[2] * x + pc[3] * x ^ 2 + ... + pc[n] * x ^ (n - 1)
Run Code Online (Sandbox Code Playgroud)
R核函数polyroot可以在复数域中找到该多项式的所有根.但是我们常常对极值感兴趣,对于单变量函数,局部最小值和最大值交替出现,将函数分解成单调的片段.
我的问题是:
将它写成函数会很好,这样我们就可以轻松地探索/可视化多项式.
例如,考虑5度的多项式:
pc <- c(1, -2.2, -13.4, -5.1, 1.9, 0.52)
Run Code Online (Sandbox Code Playgroud)
事实上,通过使用polyroot多项式的一阶导数可以找到鞍点.这是一个功能.
SaddlePoly <- function (pc) {
## a polynomial needs be at least quadratic to have saddle points
if (length(pc) < 3L) {
message("A polynomial needs be at least quadratic to have saddle points!")
return(numeric(0))
}
## polynomial coefficient of the 1st derivative
pc1 <- pc[-1] * seq_len(length(pc) - 1)
## roots in complex domain
croots <- polyroot(pc1)
## retain roots in real domain
## be careful when testing 0 for floating point numbers
rroots <- Re(croots)[abs(Im(croots)) < 1e-14]
## note that `polyroot` returns multiple root with multiplicies
## return unique real roots (in ascending order)
sort(unique(rroots))
}
xs <- SaddlePoly(pc)
#[1] -3.77435640 -1.20748286 -0.08654384 2.14530617
Run Code Online (Sandbox Code Playgroud)
我们需要能够评估多项式以绘制它.我的这个答案定义了一个g可以评估多项式及其任意导数的函数.在这里,我复制此功能并将其重命名为PolyVal.
PolyVal <- function (x, pc, nderiv = 0L) {
## check missing aruments
if (missing(x) || missing(pc)) stop ("arguments missing with no default!")
## polynomial order p
p <- length(pc) - 1L
## number of derivatives
n <- nderiv
## earlier return?
if (n > p) return(rep.int(0, length(x)))
## polynomial basis from degree 0 to degree `(p - n)`
X <- outer(x, 0:(p - n), FUN = "^")
## initial coefficients
## the additional `+ 1L` is because R vector starts from index 1 not 0
beta <- pc[n:p + 1L]
## factorial multiplier
beta <- beta * factorial(n:p) / factorial(0:(p - n))
## matrix vector multiplication
base::c(X %*% beta)
}
Run Code Online (Sandbox Code Playgroud)
例如,我们可以在其所有鞍点处评估多项式:
PolyVal(xs, pc)
#[1] 79.912753 -4.197986 1.093443 -51.871351
Run Code Online (Sandbox Code Playgroud)
这是一个查看/探索多项式的函数.
ViewPoly <- function (pc, extend = 0.1) {
## get saddle points
xs <- SaddlePoly(pc)
## number of saddle points (if 0 the whole polynomial is monotonic)
n_saddles <- length(xs)
if (n_saddles == 0L) {
message("the polynomial is monotonic; program exits!")
return(NULL)
}
## set a reasonable xlim to include all saddle points
if (n_saddles == 1L) xlim <- c(xs - 1, xs + 1)
else xlim <- extendrange(xs, range(xs), extend)
x <- c(xlim[1], xs, xlim[2])
## number of monotonic pieces
k <- length(xs) + 1L
## monotonicity (positive for ascending and negative for descending)
y <- PolyVal(x, pc)
mono <- diff(y)
ylim <- range(y)
## colour setting (red for ascending and green for descending)
colour <- rep.int(3, k)
colour[mono > 0] <- 2
## loop through pieces and plot the polynomial
plot(x, y, type = "n", xlim = xlim, ylim = ylim)
i <- 1L
while (i <= k) {
## an evaluation grid between x[i] and x[i + 1]
xg <- seq.int(x[i], x[i + 1L], length.out = 20)
yg <- PolyVal(xg, pc)
lines(xg, yg, col = colour[i])
i <- i + 1L
}
## add saddle points
points(xs, y[2:k], pch = 19)
## return (x, y)
list(x = x, y = y)
}
Run Code Online (Sandbox Code Playgroud)
我们可以通过以下方式可视化问题中的示例多项式:
ViewPoly(pc)
#$x
#[1] -4.07033952 -3.77435640 -1.20748286 -0.08654384 2.14530617 2.44128930
#
#$y
#[1] 72.424185 79.912753 -4.197986 1.093443 -51.871351 -45.856876
Run Code Online (Sandbox Code Playgroud)