在Python中计算第n个Unity根 [英] Calculating nth Roots of Unity in Python

查看:107
本文介绍了在Python中计算第n个Unity根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试编写一个算法croot(k,n),该算法返回n == n的第k个单位根.我基本上得到了正确的答案,但是它给了我真正奇怪的表示,对于某些数字来说似乎是错误的.这是一个例子.

So, I'm trying to write an algorithm croot(k, n), that returns the kth root of unity with n == n. I'm getting mostly the right answer, however it's giving me really weird representations that seem wrong for certain numbers. Here is an example.

import cmath

def croot(k, n):
    if n<=0:
        return None
    return cmath.exp((2 * cmath.pi * 1j * k) / n)


for k in range(8):
    print croot(k, 8)

输出为:

(1+0j)
(0.70710...+0.70710...j)
(6.12323399574e-17+1j)

哇哇哇.因此,当k = 2且n = 8时的根是错误的,因为它应该只是i,它的表示形式为1j或j或1.00000j等.有人可以在这里帮助我吗?我这样做是因为我正在尝试编写FFT算法.我对复数和Python不太熟悉,所以我很容易犯一个简单的错误.

Whoa whoa whoa. So the root when k = 2 and n = 8 is wrong, as it should just be i, which would be represented like 1j, or j, or 1.00000j, etc. Could somebody help me here? I'm doing this because I'm trying to write an FFT algorithm. I'm not very experienced with complex numbers and Python so I could very well be making a simple mistake.

谢谢

如果您需要任何其他信息,请询问.

If you guys need any additional information just ask.

推荐答案

查看此数字

(6.12303176911e-17+1j)

6.12303176911e-17 = 0.0000000000000000612303176911确实很小(接近零).由于浮点数的表示形式有限,您看到的是舍入错误

6.12303176911e-17 = 0.0000000000000000612303176911 which is really small (close to zero). What you are seeing is rounding errors due to the limited representation of floating point numbers

该误差等效于测量到太阳的距离在10微米左右.如果您对真实世界的数据运行FFT,则测量误差通常远大于此.

The error is equivalent to measuring the distance to the sun to within 10 microns or so. If you're running FFTs on data from the real world the measurement errors are usually far larger than this.

这篇关于在Python中计算第n个Unity根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆