昨天我创建了这段代码,可以计算 z^n,其中 z 是一个复数,n 是任何正整数。
--snip--
float real = 0;
float imag = 0;
// d is the power the number is raised to [(x + yi)^d]
for (int n = 0; n <= d; n++) {
if (n == 0) {
real += pow(a, d);
} else { // binomial theorem
switch (n % 4) {
case 1: // i
imag += bCo(d, n) * pow(a, d - n) * pow(b, n);
break;
case 2: // -1
real -= bCo(d, n) * pow(a, d - n) * pow(b, n);
break;
case 3: // -i
imag -= bCo(d, n) * pow(a, d - n) * pow(b, n);
break;
case 0: // 1
real += bCo(d, n) * pow(a, d - n) * pow(b, n);
break;
}
}
}
--snip--
int factorial(int n) {
int total = 1;
for (int i = n; i > 1; i--) { total *= i; }
return total;
}
// binomial cofactor
float bCo(int n, int k) {
return (factorial(n)/(factorial(k) * factorial(n - k)));
}
我用二项式定理展开 z^n,并且知道根据虚数的次方将每一项视为实数还是虚数。
我想做的是能够计算 z^n,其中 n 是任何正实数(分数)。我知道二项式定理可用于非整数的幂,但我不确定如何处理复数。因为 i^0.1 有一个实部和一个虚部,所以我无法将它分类为实部或虚部变量,我什至不知道如何编写可以计算它的程序。
有没有人知道可以帮助我完成此任务的算法,或者是否有更好的方法来处理复数,从而使这成为可能?
哦,我在用java。
谢谢。
最佳答案
首先,它可能有多种解法。参见 Wikipedia: Complex number / exponentiation .
Similar considerations show that we can define rational real powers just as for the reals, so z1/n is the n:th root of z. Roots are not unique, so it is already clear that complex powers are multivalued, thus careful treatment of powers is needed; for example (81/3)4 ≠ 16, as there are three cube roots of 8, so the given expression, often shortened to 84/3, is the simplest possible.
我认为您应该将其分解为极坐标符号并从那里开始。
关于java - 用有理指数计算复数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3099403/