计算有理指数的复数

发布于 2024-09-06 20:13:47 字数 1149 浏览 2 评论 0原文

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

谢谢。

Yesterday I created this piece of code that could calculate z^n, where z is a complex number and n is any positive integer.

--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)));
}

I use the binomial theorem to expand z^n, and know whether to treat each term as a real or imaginary number depending on the power of the imaginary number.

What I want to do is to be able to calculate z^n, where n is any positive real number (fractions). I know the binomial theorem can be used for powers that aren't whole numbers, but I'm not really sure how to handle the complex numbers. Because i^0.1 has a real and imaginary component I can't just sort it into a real or imaginary variable, nor do I even know how to program something that could calculate it.

Does anyone know of an algorithm that can help me accomplish this, or maybe even a better way to handle complex numbers that will make this possible?

Oh, I'm using java.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

亽野灬性zι浪 2024-09-13 20:13:47

首先,它可能有多种解决方案。请参阅维基百科:复数/求幂

类似的考虑表明,我们可以像定义实数一样定义有理实幂,因此 z1/nn:th z 的根。根不是唯一的,因此很明显复杂的幂是多值的,因此需要仔细对待幂;例如 (81/3)4 ≠ 16,因为 8 有 3 个立方根,所以给定的表达式,通常缩写为 84/3 ,是最简单的。

我认为你应该将其分解为极坐标符号并从那里开始。

First of all, it may have multiple solutions. See 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.

I think you should break it down to polar notation and go from there.

烟─花易冷 2024-09-13 20:13:47

考虑一个复数 z 使得 z = x + iy

因此, z 的极坐标形式是 = re^itheta,其中:

  • rzsqrt(x2+y2)
  • thetaatan y over x

完成此操作后,您可以使用 DeMoivre 定理 来计算 z^n 像这样:

z^n = r^ne^ θ

或更简单地为

z^n = r^n (cos (n θ) + i sin(n θ))

有关详细信息,请阅读 复数的极坐标形式

Consider a complex number z such that z = x + iy.

Thus, the polar form of z is = re^itheta, where:

  • r is the magnitude of z, or sqrt(x2+y2), and
  • theta is atan y over x.

Once you have done so, you can use DeMoivre's Theorem to calculate z^n like so:

z^n = r^n e^i n theta

or more simply as

z^n = r^n (cos (n theta) + i sin(n theta))

For more information read up on the polar form of a complex number.

宫墨修音 2024-09-13 20:13:47

我不太擅长数学,所以可能我对你的任务的理解是错误的。但据我所知 - apache commons math 可以帮助你: http://commons .apache.org/math/userguide/complex.html

示例:

Complex first  = new Complex(1.0, 3.0);
Complex second = new Complex(2.0, 5.0);

Complex answer = first.log();        // natural logarithm.
        answer = first.cos();        // cosine
        answer = first.pow(second);  // first raised to the power of second

I'm not really good at math, so probably I understood your task wrong. But as far as I got it - apache commons math can help you: http://commons.apache.org/math/userguide/complex.html

Example:

Complex first  = new Complex(1.0, 3.0);
Complex second = new Complex(2.0, 5.0);

Complex answer = first.log();        // natural logarithm.
        answer = first.cos();        // cosine
        answer = first.pow(second);  // first raised to the power of second
浅浅 2024-09-13 20:13:47

当 n 不是整数并且 a 不是正数时,a^n 是错误定义的。

如果 z 是复数,您仍然可以赋予 z^a = exp(a log z) 含义,但您必须弄清楚当 z 不是正数时 log z 的含义。

并且没有唯一的选择

a^n is ill defined when n is not an integer and a is not a positive number.

If z is a complex number, you can still give a meaning to z^a = exp(a log z) but you have to figure out what log z means when z is not a positive number.

And there is no unique choice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文