title: Trying to implement Durand-Kerner-Method in C++ using Matrices
body: <p>My implementation of the Durand-Kerner-Method (<a href="https://en.wikipedia.org/wiki/Durand%E2%80%93Kerner_method" rel="nofollow noreferrer">https://en.wikipedia.org/wiki/Durand%E2%80%93Kerner_method</a>) does not seem to work. I believe (see following code) that I am not calculating new approximation correctly in the algorithm part itself. I cannot seem to be able to fix the problem. Very grateful for any advice.</p>
  <pre><code>#include &lt;complex&gt;
  #include &lt;cmath&gt;
  #include &lt;vector&gt;
  #include &lt;iostream&gt;
  #include &quot;DurandKernerWeierstrass.h&quot;
  
  using namespace std;
  using Complex = complex&lt;double&gt;;
  using vec = vector&lt;Complex&gt;;
  using Matrix = vector&lt;vector&lt;Complex&gt;&gt;;
  
  
  //PRE: Recieves input value of polynomial, degree and coefficients
  //POST: Outputs y(x) value
  Complex Polynomial(vec Z, int n, Complex x) {
  
      Complex y = pow(x, n);
      for (int i = 0; i &lt; n; i++){
          y += Z[i] * pow(x, (n - i - 1));
      }
      return y;
  }
  
  /*PRE: Takes a test value, degree of polynomial, vector of coefficients and the desired
  precision of polynomial roots to calculate the roots*/
  //POST: Outputs the roots of Polynomial
  
  Matrix roots(vec Z, int n, int iterations, const double precision) {
      Complex z = Complex(0.4, 0.9);
      Matrix P(iterations, vec(n, 0));
      Complex w;
      
      //Creating Matrix with initial starting values
      
      for (int i = 0; i &lt; n; i++) {
          P[0][i] = pow(z, i);
      }
  
      //Durand Kerner Algorithm
  
      for (int col = 0; col &lt; iterations; col++) {
  
          *//I believe this is the point where everything is going wrong*
  
          for (int row = 0; row &lt; n; row++) {
              Complex g = Polynomial(Z, n, P[col][row]);
              for (int k = 0; k &lt; n; k++) {
                  if (k != row) {
                      g = g / (P[col][row] - P[col][k]);
                  }
  
              }
                  
              P[col][row] -= g;
  
          }
              
          return P;
      }   
      
  
  }
  </code></pre>
  <p>The following Code is the code I am using to test the function:</p>
  <pre><code>int main() {
      //Initializing section
  
      vec A = {1, -3, 3,-5 };
      int n = 3;
      int iterations = 10;
      const double precision = 1.0e-10;
      Matrix p = roots(A, n, iterations,precision);
      for (int i = 0; i &lt; iterations; i++) {
          for (int j = 0; j &lt; n; j++) {
              cout &lt;&lt; &quot;p[&quot; &lt;&lt; i &lt;&lt; &quot;][&quot; &lt;&lt; j &lt;&lt; &quot;] = &quot; &lt;&lt; p[i][j] &lt;&lt; &quot; &quot;;
              
          }
          cout &lt;&lt; endl;
      }
      return 0;
  
  }
  
  </code></pre>
  <p>Important to note the Durand-Kerner-Algorithm is connected to a header file which is not included in this code.</p>