Why is one of the variables in an object being changed within a constructor?

Deema Bowgen :

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.

Andy Turner :
ArrayList<Complex> derivativeCoeff = coeff;

Doesn't copy the list: updating derivativeCoeff updates coeff too.

Take a copy:

ArrayList<Complex> derivativeCoeff = new ArrayList<>(coeff);

Or, you can remove the first element before taking the copy (obviously checking for emptiness first):

ArrayList<Complex> derivativeCoeff = new ArrayList<>(coeff.subList(1, coeff.size()));

Or you can copy and update the elements directly with a stream.

ArrayList<Complex> derivativeCoeff =
    IntStream.rangeClosed(1, coeff.size() - 1)
        .mapToObj(i -> coeff.get(i).multiply(i))
        .collect(Collectors.toCollection(ArrayList::new));

Or even

return
    IntStream.rangeClosed(1, coeff.size() - 1)
        .mapToObj(i -> coeff.get(i).multiply(i))
        .collect(
            Collectors.collectingAndThen(
                Collectors.toCollection(ArrayList::new)),
                Polynomial::new));

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=292125&siteId=1