java - 用有理指数计算复数

标签 java math complex-numbers

昨天我创建了这段代码,可以计算 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/

相关文章:

java - JFoenix JFXTreeTableView 样式不起作用

javascript - 找到两点之间 Angular 最快方法

使用定点的复数除法

python - 如何使用python从文件中读取复数

Java - 使用递归基于搜索显示目录内容

java - java中httpResponse返回null

Java 多播套接字未在特定网络接口(interface)上接收

ruby - 根据相邻值计算数组中的缺失值

c - 用 C 编写一个程序,输出给定数字之间的丰富数字序列的第一项和项数

c - 复数数组的 FFT