[英]Multiple roots in the complex plane with R
I've been trying to find a function that returns all complex solutions of an equation such as: 我一直试图找到一个返回方程的所有复杂解的函数,例如:
16^(1/4) = 2+i0, -2+i0, 0+i2, 0-i2
As it stands, if I enter 16^(1/4)
into the console, it only returns 2. I can write a function for this but I was wondering if there is a simple way to do this in R. 就目前而言,如果我在控制台中输入
16^(1/4)
,它只返回2.我可以为此编写一个函数,但我想知道是否有一种简单的方法在R中执行此操作。
You need polyroot()
: 你需要
polyroot()
:
polyroot(z = c(-16,0,0,0,1))
# [1] 0+2i -2-0i 0-2i 2+0i
Where z
is a "vector of polynomial coefficients in increasing order". 其中
z
是“按递增顺序的多项式系数的矢量”。
The vector I passed to z
in the example above is a compact representation of this equation: 我在上面的例子中传递给
z
的向量是这个等式的紧凑表示:
-16x^0 + 0x^1 + 0x^2 + 0x^3 + 1x^4 = 0
x^4 - 16 = 0
x^4 = 16
x = 16^(1/4)
Edit: 编辑:
If polyroot
's syntax bothers you, you just could write a wrapper function that presents you with a nicer (if less versatile) interface: 如果
polyroot
的语法困扰你,你就可以编写一个包装器函数,它为你提供了一个更好的(如果不太通用的)接口:
nRoot <- function(x, root) {
polyroot(c(-x, rep(0, root-1), 1))
}
nRoot(16, 4)
# [1] 0+2i -2-0i 0-2i 2+0i
nRoot(16, 8)
# [1] 1.000000+1.000000i -1.000000+1.000000i -1.000000-1.000000i
# [4] 1.000000-1.000000i 0.000000+1.414214i -1.414214-0.000000i
# [7] 0.000000-1.414214i 1.414214+0.000000i
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.