HubbardGPU
Hubbard diagonalisation on the GPU (and CPU)
 All Classes Files Functions Variables Typedefs Friends Macros
lanczos.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2012 Ward Poelmans
2 
3 This file is part of Hubbard-GPU.
4 
5 Hubbard-GPU is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9 
10 Hubbard-GPU is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with Hubbard-GPU. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 /*
20  * =====================================================================================
21  *
22  * Filename: lanczos.cpp
23  *
24  * Description: Lanczos algorithm
25  *
26  * Version: 1.0
27  * Created: 11-05-12 18:33:27
28  * Revision: none
29  * Compiler: gcc
30  *
31  * Author: Ward Poelmans (), wpoely86@gmail.com
32  * Organization:
33  *
34  * =====================================================================================
35  */
36 #include <iostream>
37 #include <cstdlib>
38 #include <cmath>
39 #include <armadillo>
40 
41 using namespace std;
42 using namespace arma;
43 
44 int main(void)
45 {
46  int size = 1000;
47  int m = 5*size;
48 
49  mat matrix(size,size);
50  matrix.zeros();
51 
52  srand(time(0));
53 
54  for(int i=0;i<size;i++)
55  for(int j=i;j<size;j++)
56  matrix(i,j) = matrix(j,i) = rand()*1.0/RAND_MAX;
57 
58  vector<double> a(m,0);
59  vector<double> b(m,0);
60 
61  int i;
62 
63  vec qa(size);
64  vec qb(size);
65 
66  qa.zeros();
67 
68  qb = randu(size);
69  double norm = sqrt(dot(qb,qb));
70  qb *= 1.0/norm;
71 
72  vec *f1 = &qa;
73  vec *f2 = &qb;
74  vec *tmp;
75 
76  for(i=1;i<m;i++)
77  {
78  (*f1) *= -b[i-1];
79  (*f1) += matrix*(*f2);
80 
81  a[i-1] = dot(*f1,*f2);
82 
83  (*f1) -= a[i-1]*(*f2);
84 
85  b[i] = sqrt(dot(*f1,*f1));
86 
87  if( fabs(b[i]) < 1e-10 )
88  break;
89 
90  (*f1) /= b[i];
91  tmp = f2;
92  f2 = f1;
93  f1 = tmp;
94  }
95 
96  mat T(i,i);
97 
98  T.zeros();
99  T(0,0) = a[0];
100 
101  for(int j=1;j<i;j++)
102  {
103  T(j,j) = a[j];
104  T(j,j-1) = T(j-1,j) = b[j];
105  }
106 
107  vec eigs1 = eig_sym(T);
108  vec eigs2 = eig_sym(matrix);
109 
110  cout << eigs1[0] << endl;
111  cout << endl;
112  cout << eigs2[0] << endl;
113 
114  return 0;
115 }
116 
117 /* vim: set ts=8 sw=4 tw=0 expandtab :*/