1

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.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Deema Bowgen
  • 131
  • 1
  • 1
  • 7

2 Answers2

4
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));
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
-1

To fix this, just change
ArrayList<Complex> derivativeCoeff = coeff; to ArrayList<Complex> derivativeCoeff = new ArrayList(coeff);

This has happened because Java is call by reference and coeff is a field of object Polynomial and when you mutate your Polynomial object it changes the state of coeff and change is reflected everywhere it was referenced.

  • "This has happened because Java is call by reference" [No, it's not](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value). But anyway, that's irrelevant, as this is an assignment, not a method call. – Andy Turner Mar 15 '20 at 18:07