4
4
#include < array>
5
5
#include < cmath>
6
6
#include < stdexcept>
7
-
8
- #include " pybind11/pybind11.h"
9
- #include " pybind11/stl.h"
10
- namespace py = pybind11;
11
7
12
8
9
+
13
10
std::pair<std::vector<double >, std::vector<std::vector<double >> >
14
- QR_algorithm (std::vector<double > diag, std::vector<double > off_diag, const double tol =1e-8 , const unsigned int max_iter=5000 ){
11
+ QR_algorithm (std::vector<double > diag, std::vector<double > off_diag, const double toll =1e-8 , const unsigned int max_iter=5000 ){
15
12
16
13
if (diag.size () != (off_diag.size ()+1 )){
17
14
std::invalid_argument (" The dimension of the diagonal and off-diagonal vector are not compatible" );
@@ -38,7 +35,7 @@ std::pair<std::vector<double>, std::vector<std::vector<double>> >
38
35
double tmp=0 ;
39
36
double x=0 , y=0 ;
40
37
unsigned int m=n-1 ;
41
- double tol_equivalence =1e-10 ;
38
+ double toll_equivalence =1e-10 ;
42
39
double w=0 , z=0 ;
43
40
44
41
@@ -50,7 +47,7 @@ std::pair<std::vector<double>, std::vector<std::vector<double>> >
50
47
b_m_1=off_diag[m-1 ];
51
48
d=(diag[m-1 ]-a_m)*0.5 ;
52
49
53
- if (std::abs (d)<tol_equivalence ){
50
+ if (std::abs (d)<toll_equivalence ){
54
51
mu=diag[m]-std::abs (b_m_1);
55
52
} else {
56
53
mu= a_m - b_m_1*b_m_1/( d*( 1 +sqrt (d*d+b_m_1*b_m_1)/std::abs (d) ) );
@@ -86,7 +83,7 @@ std::pair<std::vector<double>, std::vector<std::vector<double>> >
86
83
}
87
84
88
85
}else {
89
- if (std::abs (d)<tol_equivalence ){
86
+ if (std::abs (d)<toll_equivalence ){
90
87
if (off_diag[0 ]*d>0 )
91
88
{
92
89
c=std::sqrt (2 )/2 ;
@@ -134,11 +131,11 @@ std::pair<std::vector<double>, std::vector<std::vector<double>> >
134
131
135
132
136
133
unsigned j, k;
137
- for (unsigned int i=0 ; i<n- 1 ; i++){
134
+ for (unsigned int i=0 ; i<m ; i++){
138
135
c=Matrix_trigonometric[i][0 ];
139
136
s=Matrix_trigonometric[i][1 ];
140
137
141
- for (j=0 ; j <n;j=j+5 ){
138
+ for (j=0 ; (j+ 5 ) <n;j=j+5 ){
142
139
k=n*i+j;
143
140
tmp=Q[k];
144
141
Q[k]=tmp*c-Q[k+n]*s;
@@ -168,8 +165,7 @@ std::pair<std::vector<double>, std::vector<std::vector<double>> >
168
165
}
169
166
170
167
iter++;
171
- if (iter >= max_iter) {std::cout << " Max iteration has been reached. Maybe tol was too low?" << std::endl;}
172
- if ( std::abs (off_diag[m-1 ]) < tol*( std::abs (diag[m]) + std::abs (diag[m-1 ]) ) )
168
+ if ( std::abs (off_diag[m-1 ]) < toll*( std::abs (diag[m]) + std::abs (diag[m-1 ]) ) )
173
169
{
174
170
--m;
175
171
}
@@ -181,8 +177,9 @@ std::pair<std::vector<double>, std::vector<std::vector<double>> >
181
177
182
178
std::vector<std::vector<double >> eig_vec (n,std::vector<double > (n, 0 ));
183
179
// std::cout<<"Iteration: "<<iter<<std::endl;
184
- for (unsigned int i=0 ; i<n; i++){
185
- for (unsigned j=0 ; j<n; j++){
180
+
181
+ for (unsigned j=0 ; j<n; j++){
182
+ for (unsigned int i=0 ; i<n; i++){
186
183
eig_vec[i][j]=Q[i+j*n];
187
184
}
188
185
}
@@ -193,7 +190,7 @@ std::pair<std::vector<double>, std::vector<std::vector<double>> >
193
190
}
194
191
195
192
std::vector<double >
196
- Eigen_value_calculator (std::vector<double > diag, std::vector<double > off_diag, const double tol =1e-8 , const unsigned int max_iter=5000 ){
193
+ Eigen_value_calculator (std::vector<double > diag, std::vector<double > off_diag, const double toll =1e-8 , const unsigned int max_iter=5000 ){
197
194
198
195
if (diag.size () != (off_diag.size ()+1 )){
199
196
std::invalid_argument (" The dimension of the diagonal and off-diagonal vector are not compatible" );
@@ -214,7 +211,7 @@ std::vector<double>
214
211
double a_m=0 , b_m_1=0 ;
215
212
double x=0 , y=0 ;
216
213
unsigned int m=n-1 ;
217
- double tol_equivalence =1e-10 ;
214
+ double toll_equivalence =1e-10 ;
218
215
double w=0 , z=0 ;
219
216
220
217
@@ -226,7 +223,7 @@ std::vector<double>
226
223
b_m_1=off_diag[m-1 ];
227
224
d=(diag[m-1 ]-a_m)*0.5 ;
228
225
229
- if (std::abs (d)<tol_equivalence ){
226
+ if (std::abs (d)<toll_equivalence ){
230
227
mu=diag[m]-std::abs (b_m_1);
231
228
} else {
232
229
mu= a_m - b_m_1*b_m_1/( d*( 1 +sqrt (d*d+b_m_1*b_m_1)/std::abs (d) ) );
@@ -262,7 +259,7 @@ std::vector<double>
262
259
}
263
260
264
261
}else {
265
- if (std::abs (d)<tol_equivalence ){
262
+ if (std::abs (d)<toll_equivalence ){
266
263
if (off_diag[0 ]*d>0 )
267
264
{
268
265
c=std::sqrt (2 )/2 ;
@@ -309,8 +306,7 @@ std::vector<double>
309
306
310
307
311
308
iter++;
312
- if (iter >= max_iter) {std::cout << " Max iteration has been reached. Maybe tol was too low?" << std::endl;}
313
- if ( std::abs (off_diag[m-1 ]) < tol*( std::abs (diag[m]) + std::abs (diag[m-1 ]) ) )
309
+ if ( std::abs (off_diag[m-1 ]) < toll*( std::abs (diag[m]) + std::abs (diag[m-1 ]) ) )
314
310
{
315
311
--m;
316
312
}
@@ -322,14 +318,20 @@ std::vector<double>
322
318
323
319
324
320
return diag;
321
+
325
322
326
323
}
327
324
325
+ #include " pybind11/pybind11.h"
326
+ #include " pybind11/stl.h"
327
+
328
328
329
329
330
+ namespace py = pybind11;
331
+
330
332
PYBIND11_MODULE (QR_cpp, m) {
331
333
m.doc () = " Function that computes the eigenvalue and eigenvector" ; // Optional module docstring.
332
334
333
- m.def (" QR_algorithm" , &QR_algorithm, py::arg (" diag" ), py::arg (" off_diag" ), py::arg (" tol " )=1e-8 , py::arg (" max_iter" )=5000 );
334
- m.def (" Eigen_value_calculator" , &Eigen_value_calculator, py::arg (" diag" ), py::arg (" off_diag" ), py::arg (" tol " )=1e-8 , py::arg (" max_iter" )=5000 );
335
- }
335
+ m.def (" QR_algorithm" , &QR_algorithm, py::arg (" diag" ), py::arg (" off_diag" ), py::arg (" toll " )=1e-8 , py::arg (" max_iter" )=5000 );
336
+ m.def (" Eigen_value_calculator" , &Eigen_value_calculator, py::arg (" diag" ), py::arg (" off_diag" ), py::arg (" toll " )=1e-8 , py::arg (" max_iter" )=5000 );
337
+ }
0 commit comments