为什么我得到这个 [1, 2, 4, 8, 16, 1, 16, 8, 4, 2, 1]?

发布于 2024-10-28 07:03:50 字数 571 浏览 6 评论 0 原文

经过多次试验和错误,我发现了以下几行Python代码,

for N in range(2**1,2**3):
    print [(2**n % (3*2**(2*N - n))) % (2**N-1) for n in range(2*N+1)]

它们产生以下输出,

[1, 2, 1, 2, 1]
[1, 2, 4, 1, 4, 2, 1]
[1, 2, 4, 8, 1, 8, 4, 2, 1]
[1, 2, 4, 8, 16, 1, 16, 8, 4, 2, 1]
[1, 2, 4, 8, 16, 32, 1, 32, 16, 8, 4, 2, 1]
[1, 2, 4, 8, 16, 32, 64, 1, 64, 32, 16, 8, 4, 2, 1]

即2的幂到2**(N-1),1,以及两个相反的幂。这正是我的问题所需要的(fft 和小波相关)。但是,我不太确定为什么它有效?我理解的最后一个模运算,它提供了级数中间的 1。第一个模运算中的因子 3 让我头疼。有人能提供解释吗?具体来说,我的底数 2 和因子 3 之间有什么关系?

Through much trial and error I found the following lines of python code,

for N in range(2**1,2**3):
    print [(2**n % (3*2**(2*N - n))) % (2**N-1) for n in range(2*N+1)]

which produce the following output,

[1, 2, 1, 2, 1]
[1, 2, 4, 1, 4, 2, 1]
[1, 2, 4, 8, 1, 8, 4, 2, 1]
[1, 2, 4, 8, 16, 1, 16, 8, 4, 2, 1]
[1, 2, 4, 8, 16, 32, 1, 32, 16, 8, 4, 2, 1]
[1, 2, 4, 8, 16, 32, 64, 1, 64, 32, 16, 8, 4, 2, 1]

i.e. powers of 2 up to 2**(N-1), 1, and the powers of two reversed. This is exactly what I need for my problem (fft and wavelet related). However, I'm not quite sure why it works? The final modulo operation I understand, it provides the 1 in the middle of the series. The factor 3 in the first modulo operation is giving me headaches. Can anyone offer an explanation? Specifically, what is the relationship between my base, 2, and the factor, 3?

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

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

发布评论

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

评论(3

幸福丶如此 2024-11-04 07:03:50

首先,正如其他人所说,可能有更简单的实现,您可能应该使用它们。

但要回答您的问题,这就是您得到此结果的原因:

当 n

2n % (3*22N-n) = 2n,因为 2n < 3*22N-n。然后 2n % (2N-1) = 2n,给出预期结果。

当 n=N 时:

2N % (3*22N-N) = 2N,且 2 N % (2N-1) = 1。

当 N

令 n = 2N - k。那么:

2n % (3*22N-n) = 22N-k % (3*2k support>) = 2k*(22N-2k % 3) = 2k * (4Nk % 3)

4 的任何幂都等于 1 模 3(因为 4=1 (mod 3),所以 4m=1m< /sup>=1 (mod 3) 也是如此)。所以最终结果是 2k = 22N-n,正如预期的那样。

使用其他数字:

如果您使用基数 a 而不是 2,使用数字 b 而不是 3,最后一部分将为您提供:

ak * ((a2)Nk % b)

因此,您需要选择 b 作为 a 的任意因数2-1,这将确保对于任何 k,((a2)Nk % b) = 1。

First of all, as others have said, there are much simpler implementations possible, and you should probably use these.

But to answer your question, here's why you get this result:

When n<N:

2n % (3*22N-n) = 2n, because 2n < 3*22N-n. Then 2n % (2N-1) = 2n, giving the expected result.

When n=N:

2N % (3*22N-N) = 2N, and 2N % (2N-1) = 1.

When N<n<=2N:

Let n = 2N - k. Then:

2n % (3*22N-n) = 22N-k % (3*2k) = 2k*(22N-2k % 3) = 2k * (4N-k % 3)

Any power of 4 is equal to 1 modulo 3 (because 4=1 (mod 3), so 4m=1m=1 (mod 3) as well). So the final result is 2k = 22N-n, as expected.

Using other numbers:

If you use the base a instead of 2, and the number b instead of 3, the last part would give you:

ak * ((a2)N-k % b)

So you'd need to choose b to be any factor of a2-1, which will ensure that ((a2)N-k % b) = 1 for any k.

苏别ゝ 2024-11-04 07:03:50

虽然我和下一个极客一样喜欢聪明的解决方案,但如果您在理解自己的代码时遇到困难,为什么不使用简单的解决方案呢?维护起来会容易得多,而且速度也不会慢:

def fft_func(ex):
    if ex == 0:
        return [0, 0, 0]
    else:
        return [2**n for n in range(0, ex+1)] + [1] + [2**n for n in range(ex, -1, -1)]

While I love clever solutions as much as the next geek, why don't you use a simple solution if you're having trouble understanding your own code? It'll be much easier to maintain and it's not really slower:

def fft_func(ex):
    if ex == 0:
        return [0, 0, 0]
    else:
        return [2**n for n in range(0, ex+1)] + [1] + [2**n for n in range(ex, -1, -1)]
硪扪都還晓 2024-11-04 07:03:50

生成该列表的更简单方法:

for N in range(2**1,2**3):
    print [2**((N-abs(N-k))%N) for k in range(2*N+1)]

A simpler way to produce that list:

for N in range(2**1,2**3):
    print [2**((N-abs(N-k))%N) for k in range(2*N+1)]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文