title: How to use custom preconditioner with Eigen
body: <p>I am trying to use a custom preconditioner for an iterative solver (CG for instance) with Eigen. Specifically, I have to solve a similar problem multiple times: the matrix changes slightly but stays close to a mean matrix. I would like to compute a Cholesky decomposition of my mean matrix and then use this as a preconditioner.</p>
  
  <p>What I had in mind is something like:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>ConjugateGradient&lt; SparseMatrix&lt;double&gt;, Lower, CholmodSupernodalLLT&lt;SparseMatrix&lt;double&gt;&gt; &gt; solver(meanMatrix);
  solver.preconditioner().compute(meanMatrix);
  // Loop on n similar matrices
  for(int i = 0; i &lt; n; i++){
    // create matrix: it is similar (in structure and in values) to meanMatrix
    SparseMatrix&lt;double&gt; matrix = ...;
    // create right-hand-side
    VectorXd rhs = ...;
    // update matrix reference for solver
    solver.compute(matrix);
    // solve using the preconditioned CG
    solver.solve(rhs);
  }
  </code></pre>
  
  <p>The problem is that calling <strong>solver.compute(matrix)</strong> actually causes ConjugateGradient (in fact IterativeSolverBase) to call <strong>compute</strong> on its preconditioner (see l. 111 of IterativeSolverBase.h, Eigen 3.2.9):</p>
  
  <pre><code>m_preconditioner.compute(*mp_matrix);
  </code></pre>
  
  <p>In other words, the preconditioner based on the mean matrix is replaced by the Cholesky decomposition of the new matrix, and thus the CG solve converges in 1 iteration. On the contrary, I would like to keep the <strong>same preconditioner</strong> (the Cholesky decomposition of the mean matrix, computed once and for all before the loop), and solve for the different matrices using the preconditioned CG.</p>
  
  <p>Is there an easy way to achieve what I am trying to do?</p>
  
  <p>Many thanks in advance for your help! I hope this makes sense. If not, please do not hesitate to ask me to clarify.</p>