</table>
<hr>
<pre>/***************************************************************************
 * $Id: lanczos3.cpp,v 1.8 2004/02/21 09:51:20 troyer Exp $
 *
 * An example of the Lanczos method with vectorspace wrapper.
 *
 * Copyright (C) 2001-2003 by Prakash Dayal &lt;prakash@comp-phys.org&gt;
 *                            Matthias Troyer &lt;troyer@comp-phys.org&gt;
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 **************************************************************************/

#include &lt;ietl/interface/ublas.h&gt;
#include &lt;ietl/lanczos.h&gt;
#include &lt;ietl/vectorspace.h&gt;
#include &lt;ietl/iteration.h&gt;
#include &lt;boost/numeric/ublas/matrix_sparse.hpp&gt;
#include &lt;boost/numeric/ublas/io.hpp&gt;
#include &lt;boost/random.hpp&gt;
#include &lt;boost/limits.hpp&gt;
#include &lt;cmath&gt;
#include &lt;limits&gt;

//typedef boost::numeric::ublas::symmetric_matrix&lt;double, boost::numeric::ublas::lower&gt; Matrix; 
typedef boost::numeric::ublas::compressed_matrix&lt;double,boost::numeric::ublas::row_major&gt;  Matrix;
typedef boost::numeric::ublas::vector&lt;double&gt; Vector;

int main() {
  // Creation of a sample matrix:
  int N = 10; 
  Matrix mat(N, N);
  int n = 1;
  for(int i=0;i&lt;N;i++)
    for(int j=0;j&lt;=i;j++){
	  mat(j,i) = n;
      mat(i,j) = n++;
	}
  std::cout &lt;&lt; std::endl &lt;&lt; &quot;Printing matrix\n&quot;;
  std::cout &lt;&lt; &quot;--------------------------------\n\n&quot;;
  std::cout &lt;&lt; mat &lt;&lt; std::endl;
  
  typedef ietl::wrapper_vectorspace&lt;Vector&gt; Vecspace;
  typedef boost::lagged_fibonacci607 Gen; 
  Vecspace vec(N);
  Gen mygen;
  ietl::lanczos&lt;Matrix,Vecspace&gt; lanczos(mat,vec);

  // Creation of an iteration object:    
  int max_iter = 10*N;
  double rel_tol = 500*std::numeric_limits&lt;double&gt;::epsilon();
  double abs_tol = std::pow(std::numeric_limits&lt;double&gt;::epsilon(),2./3.);  
  std::cout &lt;&lt; &quot;Calculation of 2 lowest converged eigenvalues\n\n&quot;;
  std::cout &lt;&lt; &quot;-----------------------------------\n\n&quot;;
  int n_lowest_eigenval = 2;

  std::vector&lt;double&gt; eigen;
  std::vector&lt;double&gt; err;
  std::vector&lt;int&gt; multiplicity;
  ietl::lanczos_iteration_nlowest&lt;double&gt; 
    iter(max_iter, n_lowest_eigenval, rel_tol, abs_tol);
  try {
    lanczos.calculate_eigenvalues(iter,mygen);
    eigen = lanczos.eigenvalues();
    err = lanczos.errors();
    multiplicity = lanczos.multiplicities();
  }
  catch (std::runtime_error&amp; e) {
    std::cout &lt;&lt; e.what() &lt;&lt; std::endl;
  } 
 
  // Printing eigenvalues with error &amp; multiplicities:  
  std::cout &lt;&lt; &quot;#        eigenvalue            error         multiplicity\n&quot;;
  std::cout.precision(10);
  for (int i=0;i&lt;eigen.size();++i)
    std::cout &lt;&lt; i &lt;&lt; &quot;\t&quot; &lt;&lt; eigen[i] &lt;&lt; &quot;\t&quot; &lt;&lt; err[i] &lt;&lt; &quot;\t&quot; 
	      &lt;&lt; multiplicity[i] &lt;&lt; &quot;\n&quot;;
  
  // Another set of computations for more converged eigenvalues:  
  n_lowest_eigenval = 7; 
  try {
    ietl::lanczos_iteration_nlowest&lt;double&gt; 
      iter(max_iter, n_lowest_eigenval, rel_tol, abs_tol); 
    lanczos.more_eigenvalues(iter); 
    eigen = lanczos.eigenvalues();
    err = lanczos.errors();
    multiplicity = lanczos.multiplicities();
  }
  catch (std::runtime_error&amp; e) {
    std::cout &lt;&lt; e.what() &lt;&lt; std::endl;
  } 
  
  std::cout &lt;&lt; &quot;\nMore converged eigenvalues\n\n&quot;;
  std::cout &lt;&lt; &quot;-----------------------------------\n\n&quot;;
  std::cout &lt;&lt; &quot;#        eigenvalue            error         multiplicity\n&quot;;
  for (int i=0;i&lt;eigen.size();++i) 
    std::cout &lt;&lt; i &lt;&lt; &quot;\t&quot; &lt;&lt; eigen[i] &lt;&lt; &quot;\t&quot; &lt;&lt; err[i] &lt;&lt; &quot;\t&quot; 
	      &lt;&lt; multiplicity[i] &lt;&lt; &quot;\n&quot;;  
  
  // call of eigenvectors function follows:   
  std::cout &lt;&lt; &quot;\nEigen vectors computations for 3 lowest eigenvalues:\n\n&quot;;  
  std::vector&lt;double&gt;::iterator start = eigen.begin();
  std::vector&lt;double&gt;::iterator end = eigen.begin()+3;
  std::vector&lt;Vector&gt; eigenvectors; // for storing the eigen vectors. 
  ietl::Info&lt;double&gt; info; // (m1, m2, ma, eigenvalue, residual, status).
  
  try {
    lanczos.eigenvectors(start, end, std::back_inserter(eigenvectors), info, mygen); 
  }
  catch (std::runtime_error&amp; e) {
    std::cout &lt;&lt; e.what() &lt;&lt; std::endl;
  }  
  
  std::cout &lt;&lt; &quot;Printing eigen Vectors:\n\n&quot;; 
  for(std::vector&lt;Vector&gt;::iterator it = eigenvectors.begin(); it != eigenvectors.end(); it++){
    std::copy((it)-&gt;begin(),(it)-&gt;end(),std::ostream_iterator&lt;double&gt;(std::cout,&quot;\n&quot;));
    std::cout &lt;&lt; &quot;\n\n&quot;;
  }
  std::cout &lt;&lt; &quot; Information about the eigen vectors computations:\n\n&quot;;
  for(int i = 0; i &lt; info.size(); i++) {
    std::cout &lt;&lt; &quot; m1(&quot; &lt;&lt; i+1 &lt;&lt; &quot;): &quot; &lt;&lt; info.m1(i) &lt;&lt; &quot;, m2(&quot; &lt;&lt; i+1 &lt;&lt; &quot;): &quot;
	      &lt;&lt; info.m2(i) &lt;&lt; &quot;, ma(&quot; &lt;&lt; i+1 &lt;&lt; &quot;): &quot; &lt;&lt; info.ma(i) &lt;&lt; &quot; eigenvalue(&quot;
	      &lt;&lt; i+1 &lt;&lt; &quot;): &quot; &lt;&lt; info.eigenvalue(i) &lt;&lt; &quot; residual(&quot; &lt;&lt; i+1 &lt;&lt; &quot;): &quot;
	      &lt;&lt; info.residual(i) &lt;&lt; &quot; error_info(&quot; &lt;&lt; i+1 &lt;&lt; &quot;): &quot;
	      &lt;&lt; info.error_info(i) &lt;&lt; std::endl &lt;&lt; std::endl;
  }
  return 0;
}
</pre>
