伙计我正在进行多项式评估并使用Naive,Horner和FFT等算法 现在我的问题中有一条陈述说明。
Run a variation of the naïve algorithm, where the exponentiation
is performed by repeated squaring, a decrease-by-half algorithm
我不明白,我目前的Naive算法是:
public Complex naive(Polynomial poly, Complex x) {
Complex p = new Complex();
for (int i = poly.getCoef().length - 1; i >= 0; i--) {
Complex power = new Complex(1, 0);
for (int j = 1; j <= i; j++) {
power = power.multiply(x);
}
p = p.add(poly.getCoef()[i].multiply(power));
multiplyCountNaive++;
}
return p;
}
请解释需要修改的内容。 谢谢
答案 0 :(得分:0)
我知道它应该是这样的
public Complex naive2(Polynomial poly, Complex x) {
Complex p = new Complex();
for (int i = poly.getCoef().length - 1; i >= 0; i--) {
p = p.add(poly.getCoef()[i].multiply(expo(x, i)));
multiplyCountNaive2++;
}
return p;
}
private Complex expo(Complex a, int b) {
if (b == 0) {
return new Complex(1, 0);
} else if (b == 1) {
return a;
}
if (b % 2 == 0) {
return expo(a.multiply(a), b / 2);
} else {
return a.multiply(expo(a.multiply(a), (b - 1) / 2));
}
}