设置复数系数数组,避免前导零 [英] Setting up array of complex coefficients, avoiding the leading zero's

查看:82
本文介绍了设置复数系数数组,避免前导零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为复数创建了一个类:

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

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

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

将多项式定义为

Polynomial p = new Polynomial(coeff);

这是问题的表达方式:

在这里,输入复杂的数组系数后,多项式应该是什么样子

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.

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

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.

如何创建这样的功能?

我对此的尝试是:


   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"; :

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

int a = coeff.length-j-1;

但是如果我打印出


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?

推荐答案

我认为,进入这里的方法是从头开始遍历Array,就像您尝试过的那样.代码的问题是下一个:

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
            }

在执行j = + 1时,您使j始终具有1的值.因此,将j = + 1更改为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.

这篇关于设置复数系数数组,避免前导零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆