-2

このコードがnullポインタ例外をスローするのはなぜですか?私はJavaの初心者なので、この間違いに精通していません。それについて読みましたが、バグを取り除くことができませんでした...複素行列DFTにベクトルを掛けたいです。

public class Naloga {
    /**
     * @param args
     */
    public static void main(String[] args) {
        double polinom1[] = {1,1,0,1};
        double polinom2[] = {1,1,0,1};
        int n=4;
        //System.out.println(n);
        int newN=2*n-1;
        //zapis matrike dft
        Complex dft[][]=new Complex [newN][newN];
        Complex omega = new Complex(Math.cos((2*Math.PI)/newN),Math.sin((2*Math.PI)/newN));
        for (int j=0;j<newN;j++){
            for (int k=0;k<n;k++){
                dft[j][k] = omega.pow((j*k)%newN);
                //System.out.println(dft[j][k]);
            }
        }
        //System.out.println("To je pol1:");
        //dopolnitev do n2
        double pol1[]=new double[newN];
        for (int i=0;i<newN;i++){
            if (i<n){
                pol1[i]=polinom1[i];
            }
            else{
                pol1[i]=0;
            }
            //System.out.println(pol1[i]);
        }
        //mnozenje polinoma z dft
        Complex p1[] = new Complex[newN];
        for (int i=0;i<newN;i++){
            Complex sum = new Complex(0,0); 
            for(int k=0;k<n;k++){
                for(int j=0;j<newN;j++){
                    sum=sum.plus(dft[i][j].times(pol1[k]));
                    System.out.println(sum);
                }
            }
            p1[i]=sum;
            System.out.println(p1[i]+" ");
        }
    }
}
class Complex{
    double re;
    double im;

    public Complex(double real, double imag) {
        re = real;
        im = imag;
    }

    public String toString() {
        double tRe = (double)Math.round(re * 100000) / 100000;
        double tIm = (double)Math.round(im * 100000) / 100000;
        if (tIm == 0) return tRe + "";
        if (tRe == 0) return tIm + "i";
        if (tIm <  0) return tRe + "-" + (-tIm) + "i";
        return tRe + "+" + tIm + "i";
    }

    // sestevanje 
    public Complex plus(Complex b) {
        Complex a = this;             
        double real = a.re + b.re;
        double imag = a.im + b.im;
        return new Complex(real, imag);
    }

    // odstevanje
    public Complex minus(Complex b) {
        Complex a = this;
        double real = a.re - b.re;
        double imag = a.im - b.im;
        return new Complex(real, imag);
    }

    // mnozenje z drugim kompleksnim stevilo
    public Complex times(Complex b) {
        Complex a = this;
        double real = a.re * b.re - a.im * b.im;
        double imag = a.re * b.im + a.im * b.re;
        return new Complex(real, imag);
    }

    // mnozenje z realnim stevilom
    public Complex times(double alpha) {
        return new Complex(alpha * re, alpha * im);
    }

    // reciprocna vrednost kompleksnega stevila
    public Complex reciprocal() {
        double scale = re*re + im*im;
        return new Complex(re / scale, -im / scale);
    }

    // deljenje
    public Complex divides(Complex b) {
        Complex a = this;
        return a.times(b.reciprocal());
    }

    // e^this
    public Complex exp() {
        return new Complex(Math.exp(re) * Math.cos(im), Math.exp(re) * Math.sin(im));
    }


    //potenca komplesnega stevila
    public Complex pow(int k) {

        Complex c = new Complex(1,0);
        for (int i = 0; i <k ; i++) {
            c = c.times(this);
        }
        return c;
    }
}
4

3 に答える 3

2

初期化するよりも多くのdftを使用しています。初期化を次のように変更した場合:

    for (int j=0;j<newN;j++){
        for (int k=0;k<newN;k++){
            dft[j][k] = omega.pow((j*k)%newN);
            //System.out.println(dft[j][k]);
        }
    }

プログラムは終了するまで実行されます。もちろん、バグが実際にdftの使用方法にある可能性があり、必要なのはnxnewNの長方形だけです。

于 2012-12-16T14:57:52.473 に答える
0

問題はこの行です:

sum=sum.plus(dft[i][j].times(pol1[k]));

dfti=0およびの場合はnullj=4

于 2012-12-16T14:47:51.247 に答える
0

jたぶんの使用法のエラーdft[i][j].times(pol1[k])

于 2012-12-16T14:59:56.597 に答える