-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlanczos.cpp
117 lines (88 loc) · 2.28 KB
/
lanczos.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* Copyright (C) 2012 Ward Poelmans
This file is part of Hubbard-GPU.
Hubbard-GPU 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 3 of the License, or
(at your option) any later version.
Hubbard-GPU 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.
You should have received a copy of the GNU General Public License
along with Hubbard-GPU. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* =====================================================================================
*
* Filename: lanczos.cpp
*
* Description: Lanczos algorithm
*
* Version: 1.0
* Created: 11-05-12 18:33:27
* Revision: none
* Compiler: gcc
*
* Author: Ward Poelmans (), wpoely86@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <armadillo>
using namespace std;
using namespace arma;
int main(void)
{
int size = 1000;
int m = 5*size;
mat matrix(size,size);
matrix.zeros();
srand(time(0));
for(int i=0;i<size;i++)
for(int j=i;j<size;j++)
matrix(i,j) = matrix(j,i) = rand()*1.0/RAND_MAX;
vector<double> a(m,0);
vector<double> b(m,0);
int i;
vec qa(size);
vec qb(size);
qa.zeros();
qb = randu(size);
double norm = sqrt(dot(qb,qb));
qb *= 1.0/norm;
vec *f1 = &qa;
vec *f2 = &qb;
vec *tmp;
for(i=1;i<m;i++)
{
(*f1) *= -b[i-1];
(*f1) += matrix*(*f2);
a[i-1] = dot(*f1,*f2);
(*f1) -= a[i-1]*(*f2);
b[i] = sqrt(dot(*f1,*f1));
if( fabs(b[i]) < 1e-10 )
break;
(*f1) /= b[i];
tmp = f2;
f2 = f1;
f1 = tmp;
}
mat T(i,i);
T.zeros();
T(0,0) = a[0];
for(int j=1;j<i;j++)
{
T(j,j) = a[j];
T(j,j-1) = T(j-1,j) = b[j];
}
vec eigs1 = eig_sym(T);
vec eigs2 = eig_sym(matrix);
cout << eigs1[0] << endl;
cout << endl;
cout << eigs2[0] << endl;
return 0;
}
/* vim: set ts=8 sw=4 tw=0 expandtab :*/