【问题标题】:Setting up array of complex coefficients, avoiding the leading zero's设置复系数数组,避免前导零
【发布时间】:2020-02-26 16:30:43
【问题描述】:

我为复数创建了一个类:

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);
    }
}

我想做的是创建一个函数多项式,它会采用一个系数数组,然后对其进行过滤,例如 [1,0,0,1,0,0,0,0,0. ..],它将返回一个长度为 4 的数组。由于剩下的零在多项式中没有用处。

这是一个复杂数组的样子

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

多项式将被定义为

Polynomial p = new Polynomial(coeff);

这是问题的表述:

这是多项式的样子,输入复数数组系数

我正在考虑构建一个算法来搜索零序列的第一个零(直到数组的末尾),然后删除零。

我也在考虑反转数组的条目,以便 [0,1,1,0,1,0,0,0] 将是 [0,0,0,1,0,1,1, 0] 然后创建一个函数,该函数将从第一个非平凡条目开始“记录”我的新数组。

我将如何创建这样一个函数?

我的尝试是:


   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];     
        }
     }

例如我想打印:

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};

输出是:


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

但应该是:

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

我尝试在 int a = coeff.length-j; 中添加“-1” :

int a = coeff.length-j-1;

但是如果我打印出来


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

它会给我相同的结果(即存储微不足道的系数)。

我怎样才能让构造函数不存储那些微不足道的系数?

【问题讨论】:

    标签: java math numbers polynomials polynomial-math


    【解决方案1】:

    我认为进入这里的方法是遍历数组以结束到开始,就像您尝试的那样。您的代码的问题是下一个:

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

    在执行 j=+1 时,您使 j 的值始终为 1。因此,将 j=+1 更改为 j+=1 将解决此问题。

    另外,如果你想检查它,我做了一个不同的代码。最后,它的作用相同,但我认为更清洁。

    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;
        }
    }
    

    希望这个回答对你有帮助。

    【讨论】:

      【解决方案2】:

      这可以通过以下方式相对轻松地完成:

      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;
      }
      

      为此,您需要定义 equals 方法,您只需在其中检查实部和虚部,但这应该可以让您到达您需要去的地方。

      【讨论】:

        猜你喜欢
        • 2012-07-19
        • 2022-01-07
        • 2019-05-07
        • 1970-01-01
        • 2019-05-24
        • 2012-06-10
        • 2018-04-09
        • 2016-10-18
        • 2013-04-19
        相关资源
        最近更新 更多