-2

Why this code throws out null pointer exception? I'm a beginner in java so i'm not familiar with this mistake, i read about it, but i wasn't able to remove the bug ... I want to multiply Complex matrix DFT with vector.

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 的方式,您应该只需要它的 n x newN 矩形。

于 2012-12-16T14:57:52.473 回答
0

可能是 in 的使用j错误dft[i][j].times(pol1[k])

于 2012-12-16T14:59:56.597 回答
0

问题是这一行:

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

dft为空i=0并且j=4

于 2012-12-16T14:47:51.247 回答