I apologise for the poorly worded question - it is hard to explain consisely.
I have written 3 classes - one to represent complex numbers, one to represent polynomials with complex coefficients and the final to perform Newton-Raphson algorithm on said polynomials to find estimates of roots.
The problem is, when i create an instance of the Newton class, with this being the constructor:
public Newton(Polynomial p) {
this.f = p;
this.fp = this.f.derivative();
it has no problem with setting f to p, but then when
this.fp = this.f.derivative();
is run it changes this.f to this.f.derivative(); too.
this is the code for the derivative:
public Polynomial derivative() {
ArrayList<Complex> derivativeCoeff = coeff;
if (derivativeCoeff.size() == 1){
return new Polynomial();
}
derivativeCoeff.remove(0);
for (int i = 1; i <= derivativeCoeff.size() ;i++){
derivativeCoeff.set(i-1,derivativeCoeff.get(i-1).multiply(i));
}
Polynomial test = new Polynomial(derivativeCoeff);
return test;
}
This is the main for Newton:
ArrayList<Complex> coeff = new ArrayList<Complex>();
coeff.add(new Complex(-1.0,0.0));
coeff.add(new Complex());
coeff.add(new Complex());
coeff.add(new Complex(1.0,0.0));
Polynomial p = new Polynomial(coeff);
System.out.println("the polynomial being used = "+p);
Newton n = new Newton(p);
System.out.println("f = "+n.getF());
System.out.println("fp = "+n.getFp());
and this is the output I get:
the polynomial being used = (-1.0+0.0i)+(0.0+0.0i)X+(0.0+0.0i)X^2+(1.0+0.0i)X^3
f = (0.0+0.0i)+(0.0+0.0i)X+(3.0+0.0i)X^2
fp = (0.0+0.0i)+(0.0+0.0i)X+(3.0+0.0i)X^2
when f should be equal to p.
Please ask if any more information or code is required. I am not experienced with asking questions on SO and would appreciate any help I can get.