【发布时间】: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