Skip to content

Commit ed22fd5

Browse files
committed
cpp function binded. Added a class
2 parents 0d478ee + ec12afd commit ed22fd5

File tree

4 files changed

+488
-132
lines changed

4 files changed

+488
-132
lines changed

src/pyclassify/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
cmake_minimum_required(VERSION 3.12)
2+
3+
# This is a comment.
4+
project(QR_binding VERSION 1.0
5+
DESCRIPTION "QR method"
6+
LANGUAGES CXX)
7+
8+
9+
# Set C++ standard and compiler options
10+
set(CMAKE_CXX_STANDARD 17)
11+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
12+
set(CMAKE_CXX_EXTENSIONS OFF)
13+
14+
# Add compile options
15+
add_compile_options(-O3 -Wall -Werror -Wpedantic)
16+
find_package(pybind11 REQUIRED)
17+
include_directories(SYSTEM ${pybind11_INCLUDE_DIRS})
18+
19+
pybind11_add_module(QR_cpp QR_bindings.cpp)

src/pyclassify/QR_alg_Wilkison.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -173,18 +173,19 @@ void print_matrix(const std::vector<std::vector<double> > & Q){
173173
}
174174
std::cout<<"\n";
175175
}
176+
std::cout<<"--------------------------------------------------------"<<"\n";
176177

177178
std::cout<<"------------------------------------------------" <<"\n";
178179

179180
}
180181

181182

182183
//std::pair<std::vector<double>, std::vector<std::vector<double>> >
183-
void QR_algorithm(std::vector<double> diag, std::vector<double> off_diag, const double toll=1e-8, const unsigned int max_iter=1000){
184+
void QR_algorithm(std::vector<double> diag, std::vector<double> off_diag, const double toll=1e-8, const unsigned int max_iter=10000){
184185

185186
const unsigned int n = diag.size();
186187

187-
std::vector<std::vector<double>> Q(n, std::vector<double>(n, 0)), Q_posterior(n, std::vector<double>(n, 0)), R(n);
188+
std::vector<std::vector<double>> Q(n, std::vector<double>(n, 0)), R(n);
188189

189190
for(unsigned int i = 0; i < n; i++){
190191
Q[i][i] = 1;
@@ -315,15 +316,16 @@ void QR_algorithm(std::vector<double> diag, std::vector<double> off_diag, cons
315316

316317
unsigned j;
317318
//Uncomment to compute the eigenvalue
318-
#pragma omp parallel for//collapse(2)
319+
//collapse(2)
319320
for(unsigned int i=0; i<n-1; i++){
321+
//std::vector<std::vector<double>> Q_posterior(Q);
320322
c=Matrix_trigonometric[i][0];
321323
s=Matrix_trigonometric[i][1];
322-
324+
#pragma omp parallel for
323325
for(j=0; j<n;j=j+5){
324326
tmp=Q[i][j];
325327
Q[i][j]=tmp*c-Q[i+1][j]*s;
326-
Q_posterior[i+1][j]=tmp*s+Q[i+1][j]*c;
328+
Q[i+1][j]=tmp*s+Q[i+1][j]*c;
327329
tmp=Q[i][j+1];
328330
Q[i][j+1]=tmp*c-Q[i+1][j+1]*s;
329331
Q[i+1][j+1]=tmp*s+Q[i+1][j+1]*c;
@@ -332,7 +334,7 @@ void QR_algorithm(std::vector<double> diag, std::vector<double> off_diag, cons
332334
Q[i+1][j+2]=tmp*s+Q[i+1][j+2]*c;
333335
tmp=Q[i][j+3];
334336
Q[i][j+3]=tmp*c-Q[i+1][j+3]*s;
335-
Q[i+1][j+3]=tmp*s+Q[i+1][j+3]*c;
337+
Q[i+1][j+3]=tmp*s+Q[i+1][j+3]*c;
336338
tmp=Q[i][j+4];
337339
Q[i][j+4]=tmp*c-Q[i+1][j+4]*s;
338340
Q[i+1][j+4]=tmp*s+Q[i+1][j+4]*c;
@@ -343,6 +345,7 @@ void QR_algorithm(std::vector<double> diag, std::vector<double> off_diag, cons
343345
Q[i][j]=tmp*c-Q[i+1][j]*s;
344346
Q[i+1][j]=tmp*s+Q[i+1][j]*c;
345347
}
348+
346349

347350
}
348351

@@ -353,12 +356,10 @@ void QR_algorithm(std::vector<double> diag, std::vector<double> off_diag, cons
353356
}
354357
}
355358

356-
print_matrix(Q);
357-
358-
for(const auto t: diag){
359-
std::cout<<t<<"\t";
359+
if(iter==max_iter){
360+
std::cout<<"Converges failed"<<std::endl;
360361
}
361-
std::cout<<"\n";
362+
std::cout<<"Iteration: "<<iter<<std::endl;
362363

363364

364365

@@ -368,13 +369,12 @@ void QR_algorithm(std::vector<double> diag, std::vector<double> off_diag, cons
368369

369370
int main(){
370371

371-
std::vector<double> diag(10, 5), offdiag(9, 20);
372+
std::vector<double> diag(2000, 5), offdiag(1999, 20);
372373

373-
QR_algorithm(diag, offdiag);
374374

375375
auto start = std::chrono::high_resolution_clock::now();
376376

377-
QR_algorithm(diag, offdiag);
377+
QR_algorithm(diag, offdiag, 1e-8, 50000);
378378

379379
// Capture the end time
380380
auto end = std::chrono::high_resolution_clock::now();

0 commit comments

Comments
 (0)