title: Solving a simple sparse linear system of equation using csparse: cs_cholsol
body: <p>I am using Microsoft Visual Studio 2008 on a Windows 7 x64. I am trying to solve the following linear system <code>Ax=b</code> by using <a href="http://www.cise.ufl.edu/research/sparse/CSparse/" rel="nofollow">csparse</a>, where <code>A</code> is positive definite.</p>
  
  <pre><code>    | 1  0  0  1 |
  A = | 0  3  1  0 |
      | 0  1  2  1 |
      | 1  0  1  2 |
  
      | 1 |
  b = | 1 |
      | 1 |
      | 1 |
  </code></pre>
  
  <p>I have used the following codes</p>
  
  <pre><code>int Ncols = 4, Nrows = 4, nnz = 10; 
  int cols[]    = {0, 3, 1, 2, 1, 2, 3, 0, 2, 3};
  int rows[]    = {0, 0, 1, 1, 2, 2, 2, 3, 3, 3};
  double vals[] = {1, 1, 3, 1, 1, 2, 1, 1, 1, 2};
  
  cs *Operator = cs_spalloc(Ncols,Nrows,nnz,1,1);
  
  int j;
  for(j = 0; j &lt; nnz; j++)
  {
      Operator-&gt;i[j] = rows[j];
      Operator-&gt;p[j] = cols[j];
      Operator-&gt;x[j] = vals[j];
      Operator-&gt;nz++;
  }
  
  for(j = 0; j &lt; nnz; j++)
      cout &lt;&lt; Operator-&gt;i[j] &lt;&lt; " " &lt;&lt; Operator-&gt;p[j] &lt;&lt; " " &lt;&lt; Operator-&gt;x[j] &lt;&lt; endl;
  
  Operator = cs_compress(Operator);
  
  for(j = 0; j &lt; nnz; j++)
      cout &lt;&lt; Operator-&gt;i[j] &lt;&lt; " " &lt;&lt; Operator-&gt;p[j] &lt;&lt; " " &lt;&lt; Operator-&gt;x[j] &lt;&lt; endl;
  
  // Right hand side
  double b[] = {1, 1, 1, 1};
  
  // Solving Ax = b
  int status = cs_cholsol(0, Operator, &amp;b[0]); // status = 0 means error.  
  </code></pre>
  
  <p>In order to make sure that I have created the sparse variable correctly, I tried to print out the rows and columns index as well as their values to the console before and after <code>cs_compress</code>. The following is the result of this print-out.</p>
  
  <p>Before:</p>
  
  <pre><code>0 0 1
  0 3 1
  1 1 3
  1 2 1
  2 1 1
  2 2 2
  2 3 1
  3 0 1
  3 2 1
  3 3 2
  </code></pre>
  
  <p>After:</p>
  
  <pre><code>0 0 1
  3 2 1
  1 4 3
  2 7 1
  1 10 1
  2 -6076574517017313795 2
  3 -6076574518398440533 1
  0 -76843842582893653 1
  2 0 1
  3 0 2
  </code></pre>
  
  <p>Because of the trash values that can be observed above after calling <code>cs_compress</code>, the solution of <code>Ax=b</code> does not match with the one that I have calculated with MATLAB. MATLAB results in the following solution.</p>
  
  <pre><code>    | 2.0000 |
  x = | 0.0000 |
      | 1.0000 |
      |-1.0000 |
  </code></pre>
  
  <p>Interestingly, I don't have this problem for the following codes which solve <code>Ax=b</code>, where <code>A</code> is a <code>3×3</code> identity matrix.</p>
  
  <pre><code>int Ncols = 3, Nrows = 3, nnz = Nrows; 
  
  cs *Operator = cs_spalloc(Ncols,Nrows,nnz,1,1);
  int j;
  for(j = 0; j &lt; nnz; j++) {
      Operator-&gt;i[j] = j;
      Operator-&gt;p[j] = j;
      Operator-&gt;x[j] = 1.0;
      Operator-&gt;nz++;
  }
  
  Operator = cs_compress(Operator);
  
  double b[] = {1, 2, 3};
  
  int status = cs_cholsol(0, Operator, &amp;b[0]); // status = 1 means no error.
  </code></pre>
  
  <p>Could someone please help me fix the problem that I have with <code>cs_compress</code>?</p>