title: EIGEN : Sparse matrix multiplication not producing a pruned result
body: <p>I'm using EIGEN for the sparse matrix optimizations and functions. Everything is "working" fine, except in some case and I can't figure this case out.</p>
  
  <p>Here is the thing :</p>
  
  <pre><code>Matrix A
  2 0 0
  0 2 0
  0 0 2
  
  Matrix B
  6 3
  0 1
  2 8
  
  Result
  12 6 
  0 2 
  4 16 
  </code></pre>
  
  <p>As you can see, this is the right result, and I'm always having the right result.</p>
  
  <p>The problem is about the values considered as "Non Zero". In this case :</p>
  
  <pre><code>SparseMatrix&lt;int, RowMajor&gt; A;
  SparseMatrix&lt;int, RowMajor&gt; B;
  //Parsing and initializing matrix
  SparseMatrix&lt;int, RowMajor&gt; result = A*B;
  result.pruned();
  cout &lt;&lt; result.nonZeros();
  </code></pre>
  
  <p>The result of <code>nonZeros()</code> is 5, which is the "normal" case, since Result got only 5 non Zero values.
  Now, let's consider this code :</p>
  
  <pre><code>SparseMatrix&lt;int, RowMajor&gt; A;
  SparseMatrix&lt;int, RowMajor&gt; B;
  //Parsing and initializing matrix
  SparseMatrix&lt;int, RowMajor&gt; result = (A*B).pruned();
  cout &lt;&lt; result.nonZeros();
  </code></pre>
  
  <p>The result of <code>nonZeros()</code> is 6. I do not understand why, this is the syntax available on <a href="https://eigen.tuxfamily.org/dox/group__TutorialSparse.html#TutorialSparse_Products" rel="nofollow noreferrer">the documentation</a>.</p>
  
  <p>And now the strangest part is that on very large matrix <code>result = A*B; result.pruned()</code> is sometimes ALSO saving 0 as nonZero, but less than if I used <code>(A*B).pruned();</code> </p>
  
  <p>I got three questions :</p>
  
  <ul>
  <li>Why are <code>result = (A*B).pruned()</code> and <code>result=A*B;result.pruned();</code> giving different results concerning nonZero values (and not effective result, which is right in both case)?</li>
  <li>Why are 0 values from a product sometimes not considered as Zero Values?</li>
  <li>Are you having the same result than me?</li>
  </ul>
  
  <p>I'm using Visual Studio Professional 2013 and Eigen 3.3.2, using DEBUG mode and targeting WIN32.</p>
  
  <p>Thanks for your help.</p>
  
  <p><strong>EDIT :</strong> Benchmark of sparse multiplication with VS2013, DEBUG/Release mode for W32 (With or without SSE2 instruction set, same results in both case); the result is always the right one so i'm not pasting it here, it's not bringing any information. With :</p>
  
  <p>1 = <code>SparseMatrix&lt;int, RowMajor&gt; resultA = A*B;</code></p>
  
  <p>2 = <code>SparseMatrix&lt;int, RowMajor&gt; resultB = (A*B);resultB.pruned();</code></p>
  
  <p>3 = <code>SparseMatrix&lt;int, RowMajor&gt; resultC = (A*B).pruned();</code></p>
  
  <p><strong>Case 1</strong></p>
  
  <pre><code>Matrix A
  2 0 0
  0 2 0
  0 0 2
  
  Matrix B
  5 3
  0 1
  2 8
  </code></pre>
  
  <p>Expected value = 5</p>
  
  <p>1 = 5</p>
  
  <p>2 = 5</p>
  
  <p>3 = 6</p>
  
  <p><strong>Case 2</strong>
  Matrix are too large, <a href="http://s000.tinyupload.com/download.php?file_id=68463106545818632889&amp;t=6846310654581863288912929" rel="nofollow noreferrer">here</a> is the file</p>
  
  <p>Expected value = 0</p>
  
  <p>1 = 1444</p>
  
  <p>2 = 1444</p>
  
  <p>3 = 0</p>
  
  <p>As you can see, depending both on the input and the way I'm calling functions, the result is optimized or not, and none of 1, 2 or 3 are working in every case.</p>
  
  <p><strong>EDIT² :</strong> Problem fixed by ggael solution (thanks).</p>