Setting up array of complex coefficients, avoiding the leading zero's

半世苍凉 提交于 2020-03-25 04:22:41

问题


I have created a class for complex numbers:

public class Complex {
    private double x;   //Real part x of the complex number x+iy.
    private double y;   //Imaginary part y of the complex number x+iy.

     public Complex(double x, double y) {   //Constructor: Initializes x, y.
        this.x=x;
        this.y=y;
    }

    public Complex(double x) { //Real constructor - initialises with a real number.
        this(x, 0.0);
    }

    public Complex() {   //Default constructor; initialiase x and y to zero.
        this(0.0, 0.0);
    }
}

What I would like to do is create a function Polynomial, which would take an array of coefficients, and filter it so that if for example [1,0,0,1,0,0,0,0,0...], it would return an array of length 4. Since the zero's that are left, have no use in a polynomial.

Here's how a complex array would look like

Complex [] coeff = new Complex [] {
    new Complex(-1.0 ,0.0), new Complex(),
    new Complex() , new Complex(1.0, 0.0)
};

A polynomial would be defined as

Polynomial p = new Polynomial(coeff);

Here's the problem formulation:

Here is how the polynomial would have to look like, typing in the complex array coefficients

I was thinking of constructing an algorithm which searches for the first zero of the zero sequence(which is until the end of the array), and then deletes the zeros.

Also I was thinking of inverting the entries of the array so that [0,1,1,0,1,0,0,0] would be [0,0,0,1,0,1,1,0] and then creating a function which would start "recording" my new array from the first non Trivial entry.

How would I go with creating such a function?

My attempt for this is:


   int j=0;
        for(int i=coeff.length-1; i>=0; i-=1)
        {    
            if(coeff[i].getReal()== 0 && coeff[i].getImag() == 0 ){
                 j=+1;     
            }
            else {
                break;
            }

        }

        int a = coeff.length-j;    
        this.coeff = new Complex[a];
        for (int i=0;i<this.coeff.length;i+=1){
            this.coeff[i]=coeff[i];     
        }
     }

And for example I would like to print :

Complex a1=new Complex(-3, 1);
        Complex a2=new Complex(2, 0.3);
        Complex a3=new Complex(); 
        Complex b=new Complex(); 
        Complex[] com=new Complex[] {a1,b, a2, a3,b};

and the output is :


(-3.0+1.0i)+ (0.0+0.0i)X^1+(2.0+0.3i)X^2+(0.0+0.0i)X^3

But is supposed to be :

(-3.0+1.0i)+ (0.0+0.0i)X^1+(2.0+0.3i)X^2

And I've tried adding a "-1" to int a = coeff.length-j; :

int a = coeff.length-j-1;

but then if i print out


Complex[] com=new Complex[] {a1,b, a2, a3,b,b,b,b,b,b};

It's going to give me the same results (ie storing the trivial coefficients).

How can i make the contructor not store those trivial coefficients?


回答1:


I think that the way to go in here is by iterating over the Array for the end to the start, just like you were trying. The problem with your code is the next:

if(coeff[i].getReal()== 0 && coeff[i].getImag() == 0 ){
                 j=+1;     //Here! I think you wanted to do j+=1
            }

At doing j=+1 you are making j to always have a value of 1. So, changing j=+1 to j+=1 will fix this.

Also, I did a different code if you want to check it. At the end, it does the same but I think is cleaner.

public class Polynomial {

    private Complex[] coeff; 

    public Polynomial(Complex[] coeff) {
        this.coeff = cleanCoeff(coeff);
    }

    private Complex[] cleanCoeff(Complex[] coeff) {
        int length = coeff.length;
        Complex complex = null;
        for (int i = coeff.length - 1; i >= 0 ; i--) {
            complex = coeff[i];
            if(complex.getX() == 0 && complex.getY() == 0) {
                length--; 
            }else {
                break;
            }
        }
        return Arrays.copyOf(coeff, length);

    }

    public Complex[] getCoeff() {
        return coeff;
    }
}

I hope this answer helps you.




回答2:


This could be done relatively easily using something like the following:

int effective_len(Complex coeff[]) {
  int pos = 0;
  Complex zero();

  for (int i=0; i<coeff.lengh; i++) {
    if (!zero.equals(coeff[i])) {
      pos = i;
    }
  }

  return pos + 1;
}

For this you will need to define the equals method where you just check the real and imaginary components, but this should get you where you need to go.



来源:https://stackoverflow.com/questions/60418436/setting-up-array-of-complex-coefficients-avoiding-the-leading-zeros

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!