C# と VB.NET の質問掲示板

わんくま同盟

ASP.NET、C++/CLI、Java 何でもどうぞ

C# と VB.NET の入門サイト


■91848 / )  Re[4]: C++からVBへの変換
□投稿者/ えん (4回)-(2019/08/07(Wed) 14:28:48)
ありがとうございます。

実は
http://www.kurims.kyoto-u.ac.jp/~ooura/fftman/ftmn1_23.html#sec1_2_3

このページのコードを翻訳しています。



    theta = -8 * atan(1.0) / n;
    for (m = n; (mq = m >> 2) >= 1; m = mq) {
        for (i = 0; i < mq; i++) {
            w1r = cos(theta * i);
            w1i = sin(theta * i);
            w2r = cos(theta * 2 * i);
            w2i = sin(theta * 2 * i);
            w3r = cos(theta * 3 * i);
            w3i = sin(theta * 3 * i);
            for (j = i; j < n; j += m) {
                j1 = j + mq;
                j2 = j1 + mq;
                j3 = j2 + mq;
                x0r = ar[j] + ar[j2];
                x0i = ai[j] + ai[j2];
                x1r = ar[j] - ar[j2];
                x1i = ai[j] - ai[j2];
                x2r = ar[j1] + ar[j3];
                x2i = ai[j1] + ai[j3];
                x3r = ar[j3] - ar[j1];
                x3i = ai[j3] - ai[j1];
                ar[j] = x0r + x2r;
                ai[j] = x0i + x2i;
                x0r = x0r - x2r;
                x0i = x0i - x2i;
                ar[j1] = w2r * x0r - w2i * x0i;
                ai[j1] = w2r * x0i + w2i * x0r;
                x0r = x1r - x3i;
                x0i = x1i + x3r;
                ar[j2] = w1r * x0r - w1i * x0i;
                ai[j2] = w1r * x0i + w1i * x0r;
                x0r = x1r + x3i;
                x0i = x1i - x3r;
                ar[j3] = w3r * x0r - w3i * x0i;
                ai[j3] = w3r * x0i + w3i * x0r;
            }
        }
        theta *= 4;
    }

の部分は以下で合っていると思います。


        Dim theta = -Math.PI * 2 / n

        Dim m As Integer = n
        For g As Integer = 1 To CInt(Math.Log(n, 4))

            m = CInt(m / 4)          
            Dim mq As Integer = CInt(m / 4)

            For i As Integer = 0 To mq

                Dim w1r As Double = Math.Cos(theta * i)
                Dim w1i As Double = Math.Sin(theta * i)
                Dim w2r As Double = Math.Cos(theta * (2 * i))
                Dim w2i As Double = Math.Sin(theta * (2 * i))
                Dim w3r As Double = Math.Cos(theta * (3 * i))
                Dim w3i As Double = Math.Sin(theta * (3 * i))

                For j As Integer = i To n Step m

                    Dim j1 As Integer = j + mq
                    Dim j2 As Integer = j1 + mq
                    Dim j3 As Integer = j2 + mq
                    Dim x0r As Double = xr(j) + xr(j2)
                    Dim x0i As Double = xi(j) + xi(j2)
                    Dim x1r As Double = xr(j) - xr(j2)
                    Dim x1i As Double = xi(j) - xi(j2)
                    Dim x2r As Double = xr(j1) + xr(j3)
                    Dim x2i As Double = xi(j1) + xi(j3)
                    Dim x3r As Double = xr(j3) - xr(j1)
                    Dim x3i As Double = xi(j3) - xi(j1)
                    xr(j) = x0r + x2r
                    xi(j) = x0i + x2i
                    x0r = x0r - x2r
                    x0i = x0i - x2i
                    xr(j1) = (w2r * x0r) - (w2i * x0i)
                    xi(j1) = (w2r * x0i) + (w2i * x0r)
                    x0r = x1r - x3i
                    x0i = x1i + x3r
                    xr(j2) = (w1r * x0r) - (w1i * x0i)
                    xi(j2) = (w1r * x0i) + (w1i * x0r)
                    x0r = x1r + x3i
                    x0i = x1i - x3r
                    xr(j3) = (w3r * x0r) - (w3i * x0i)
                    xi(j3) = (w3r * x0i) + (w3i * x0r)

                Next j

            Next i

            theta *= 4

        Next g


しかし、
Dim x0r As Double = xr(j) + xr(j2)
のところで、
j2にj + mq + mqが代入されるため、
xrの要素数であるnを超えてしまい、エラーとなってしまいます。

一体どうすれば良いでしょうか?

もしかして、元のコードが間違っていますか?

返信 編集キー/


管理者用

- Child Tree -