title: Cholesky decomposition in c fails
body: <p>I'm fairly new to c and wanted to implement the cholesky decomposition according to the pseudo code from wikipedia.
  It is required, that the ram is allocated dynamically.</p>
  
  <p>I tried my code with the following example matrix:</p>
  
  <pre><code> 4.000  2.000  0.000  0.000
   2.000  5.000  2.000  0.000
   0.000  2.000 10.000  3.000
   0.000  0.000  3.000  2.000
  </code></pre>
  
  <p>which should result in:</p>
  
  <pre><code>2.000 0.000 0.000 0.000
  1.000 2.000 0.000 0.000
  0.000 1.000 3.000 0.000
  0.000 0.000 1.000 1.000
  </code></pre>
  
  <p>But instead returns me.</p>
  
  <pre><code>4.000 2.000 0.000  0.000 
  0.000 5.000 2.000  0.000 
  0.000 0.000 10.000 3.000 
  0.000 0.000 0.000  2.000
  </code></pre>
  
  <p>I guess I have something misunderstood in using the pointers. I tried to allocate dynamically according to <a href="http://openbook.rheinwerk-verlag.de/c_von_a_bis_z/014_c_dyn_speicherverwaltung_011.htm" rel="nofollow">this link</a>.</p>
  
  <p>Can anybody tell me, why the correct values are not written into my matrix?
  Here is my code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;math.h&gt;
  
  int Cholesky(int n, double **A){
      double sum;
      sum = 0.0f;
      for(int i = 0; i &lt; n; i++)
      {
          for(int j = 0; j &lt; i; j++)
          {
              for(int k = 0; k &lt; j-1; k++)
              {
                  sum = sum - A[i][k]*A[j][k];
              }
              if(i &gt; j)
              {
                  A[i][j] = sum / A[j][j];
              } else
              {
                  if(sum &gt; 0)
                  {
                      A[i][i] = sqrt(sum);
                  } else {
                      printf("Die Matrix ist nicht symetrisch positiv\n");
                      return -1;
                  }
              }
          }
      }
  
      for (int i = 0; i &lt; n; i++) {
          for (int j = 0; j &lt; n; j++)
              printf("%.5f ", A[i][j]);
              printf("\n");
      }
  
  }
  
  int main(){
      int n = 4;
      double ** matrix;
  
      double test[4][4] = {{4.0f,2.0f,0.0f,0.0f},{2.0f,5.0f,2.0f,0.0f},{0.0f,2.0f,10.0f, 3.0f},{0.0f,0.0f,3.0f,2.0f}};
  
      /* Speicher reservieren für die int-Zeiger (=zeile) */
      matrix = malloc(n * sizeof(double *));
      if(NULL == matrix) {
         printf("Kein virtueller RAM mehr vorhanden ... !");
         return -1;
      }
      /* jetzt noch Speicher reservieren für die einzelnen Spalten
       * der i-ten Zeile */
      for(int i = 0; i &lt; n; i++) {
          matrix[i] = malloc(n * sizeof(double));
          if(NULL == matrix[i]) {
              printf("Kein Speicher mehr fuer Zeile %d\n",i);
              return -1;
          }
      }
      /* mit beliebigen Werten initialisieren */
      for (int i = 0; i &lt; n; i++)
          for (int j = 0; j &lt; n; j++)
              matrix[i][j] = test[i][j];
  
      /* Inhalt der Matrix entsprechend ausgeben */
      for (int i = 0; i &lt; n; i++) {
          for (int j = 0; j &lt; n; j++)
              printf("%.5f ", matrix[i][j]);
              printf("\n");
      }
  
      Cholesky(n, matrix);
  
      /* Spalten der i-ten Zeile freigeben */
      for(int i = 0; i &lt; n; i++)
          free(matrix[i]);
      /* Jetzt können die leeren Zeilen freigegeben werden. */
      free(matrix);
      int x;
      scanf("%d", x);
      return 0;
  }
  </code></pre>