title: Convert a matrix A in a sparse formats CSR, COO, etc
body: <p>I have a little problem, I would like to convert a matrix 10*10 in a <a href="http://docs.cusp-library.googlecode.com/hg/classcusp_1_1csr__matrix.html#_details" rel="nofollow"><strong>CSR</strong></a> or <a href="http://docs.cusp-library.googlecode.com/hg/classcusp_1_1coo__matrix.html#_details" rel="nofollow"><strong>COO</strong></a> sparse matrix/format. The matrix is:</p>
  
  <pre><code> 1.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00
  -0.45  0.10 -0.45  0.00  0.00  0.00  0.00  0.00  0.00  0.00
   0.00 -0.45  0.10 -0.45  0.00  0.00  0.00  0.00  0.00  0.00
   0.00  0.00 -0.45  0.10 -0.45  0.00  0.00  0.00  0.00  0.00
   0.00  0.00  0.00 -0.45  0.10 -0.45  0.00  0.00  0.00  0.00
   0.00  0.00  0.00  0.00 -0.45  0.10 -0.45  0.00  0.00  0.00
   0.00  0.00  0.00  0.00  0.00 -0.45  0.10 -0.45  0.00  0.00
   0.00  0.00  0.00  0.00  0.00  0.00 -0.45  0.10 -0.45  0.00
   0.00  0.00  0.00  0.00  0.00  0.00  0.00 -0.45  0.10 -0.45
   0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  1.00
  </code></pre>
  
  <p>I am using the <a href="http://code.google.com/p/cusp-library/" rel="nofollow">"CUSP"</a> functions but it did not work, once tha matrix <strong>A</strong> I would like just to convert in other format. <em>Can you help me?</em> </p>
  
  <p>Well I would like also to use this matrix to solve the system <strong>Ax=b</strong>, using <a href="http://docs.cusp-library.googlecode.com/hg/group__krylov__methods.html" rel="nofollow">bicgstab</a>:</p>
  
  <pre><code>b=
     0.00000
     0.34202
     0.64279
     0.86603
     0.98481
     0.98481
     0.86603
     0.64279
     0.34202
     0.00000
  </code></pre>
  
  <p>My code for this is:</p>
  
  <pre><code>int n = 10, r;
  
  cusp::coo_matrix&lt;int,float,cusp::device_memory&gt; A(n, n, 3*n - 4);
  cusp::array1d&lt;float, cusp::device_memory&gt; x(A.num_rows, 0);
  cusp::array1d&lt;float, cusp::device_memory&gt; b(A.num_rows, 1);
  
     b[0]=0.00000;
     b[1]=0.34202;
     b[2]=0.64279;
     b[3]=0.86603;
     b[4]=0.98481;
     b[5]=0.98481;
     b[6]=0.86603;
     b[7]=0.64279;
     b[8]=0.34202;
     b[9]=0.00000;
  
  i=0;
  // row 0
  A.row_indices[i] = 0.0;
  A.column_indices[i] = 0.0;
  A.values[i] = 1.00;
  
  ++i;
  // rows 1 through n - 2
  for (r = 1; r != n - 1; ++r) {
    A.row_indices[i] = r;
    A.column_indices[i] = r - 1;
    A.values[i] = -0.45;
    ++i;
    A.row_indices[i] = r;
    A.column_indices[i] = r;
    A.values[i] = 0.10;
    ++i;
    A.row_indices[i] = r;
    A.column_indices[i] = r + 1;
    A.values[i] = -0.45;
    ++i;
  }
  // row n - 1
  A.row_indices[i] = n - 1;
  A.column_indices[i] = n - 1;
  A.values[i] = 1.00;
  ++i;
  
  // set stopping criteria:
  //  iteration_limit    = 100
  //  relative_tolerance = 1e-3
      cusp::verbose_monitor&lt;ValueType&gt; monitor(b, 100, 1e-3);
  
  // set preconditioner (identity)
  cusp::identity_operator&lt;ValueType, MemorySpace&gt; M(A.num_rows, A.num_rows);
  
  // solve the linear system A x = b
  cusp::krylov::bicgstab(A, x, b, monitor, M);
  
  cusp::print(x);
  </code></pre>
  
  <p>The result using Octave should be something similar to:</p>
  
  <pre><code>   0.00000
     0.32441
     0.60970
     0.82144
     0.93411
     0.93411
     0.82144
     0.60970
     0.32441
     0.00000
  </code></pre>
  
  <p>But is also with negative numbers, so <strong>WRONG</strong>.</p>