forked from rgmelko/1D_NLC
/
lapack.cpp
174 lines (168 loc) · 4.93 KB
/
lapack.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
///
/// @file lapack.h
/// @Synopsis Definition of the wrapper function for CLAPACK diag
/// @author Ivan Gonzalez
/// @date 2007-02-06
///
/// Definitions for LAPACK related function declared in lapack.h
/// This is a quote of a post in comp.lang.c++:
/// (search for "preventing multiple definition")
///
///-----------------------------------------------------------------
/// In fact, my last answer is wrong. What really happened here
/// is the following:
/// You can't DEFINE (not declare) a function in a file that is
/// included by other files unless this function is inlined
/// (explicitly or inside a class). What you should do in this case
/// is the following:
/// 1) Put the definition of the function f() in a separate file
/// and compile it to an object file as you did with a.cpp an
/// b.cpp.
/// 2) Create a header file with the declaration of the function
/// f() and include it by a.cpp, b.cpp and foo.c
/// - OR -
/// 1) Inline the function f() in your file 'c'.
///-----------------------------------------------------------------
///
#include"lapack.h"
/*****************************************************************************/
///
/// Function to take a complex my_Matrix and diag it
///
void diagWithLapack(Array<complex<double>, 2>& DMpart,
vector<double>& EigenVals)
{
int rows_=DMpart.rows();
int cols_=DMpart.cols();
///
/// CLAPACK function to diagonalize an Hermitian matrix
///
char jobz='V';
char uplo='U';
int n=cols_;
int lda=rows_;
int info;
int elems=rows_*cols_;
//
// DMPart is hermitian so we use it to get the fortran array
//
complex<double>* a;
a=( DMpart.transpose(secondDim,firstDim) ).data();
//
// Output
//
//for (int j=0; j<elems; j++) cout<<j<<" "<<" "<<a[j]<<endl;
//
// Prepare to do a workspace query
//
int lwork=-1;
int lwork_=1;
complex<double> *work_query= new complex<double> [lwork_];
double rwork[(3*n-2)];
//
// Workspace query
//
double w[n];
int info_=zheev_(&jobz, &uplo, &n, a, &lda, w, work_query,
&lwork, rwork, &info);
//
// Get sizes of the workspace and reallocate
//
lwork_=(int)abs((work_query[0]).real());
//cout<<"after query\n"<<" work_query[0] "<<lwork<<endl;
delete[] work_query;
complex<double> *work= new complex<double> [lwork_];
//
// Call to zheev_
//
info_=zheev_(&jobz, &uplo, &n, a, &lda, w, work,
&lwork_, rwork, &info);
//
// Free all
//
delete[] work;
//
// Transpose the DM part (a is row ordered)
//
DMpart.transposeSelf(secondDim,firstDim);
//
// Output
//
for(int i=0; i<n; i++) EigenVals.push_back(w[i]);
//
// Output
//
//for (int j=0; j<elems; j++) cout<<j<<" "<<" "<<a[j]<<endl;
//for(int i=0; i<EigenVals.size(); i++) cout<<EigenVals[i]<<endl;
//cout<<"Info "<<info<<endl;
};
/*****************************************************************************/
///
/// Function to take a real my_Matrix and diag it with lapack
///
void diagWithLapack_R(Array<double , 2>& DMpart,
vector<double>& EigenVals)
{
int rows_=DMpart.rows();
int cols_=DMpart.cols();
///
/// CLAPACK function to diagonalize an Hermitian matrix
///
char jobz='V';
char uplo='U';
int n=cols_;
int lda=rows_;
int info;
int elems=rows_*cols_;
//
// DMPart is hermitian so we use it to get the fortran array
//
double* a;
a=( DMpart.transpose(secondDim,firstDim) ).data();
//
// Output
//
//for (int j=0; j<elems; j++) cout<<j<<" "<<" "<<a[j]<<endl;
//
// Prepare to do a workspace query
//
int lwork=-1;
int lwork_=1;
double *work_query= new double [lwork_];
//
// Workspace query
//
double w[n];
int info_=dsyev_(&jobz, &uplo, &n, a, &lda, w, work_query,
&lwork, &info);
//
// Get sizes of the workspace and reallocate
//
lwork_=(int)abs((work_query[0]));
//cout<<"after query\n"<<" work_query[0] "<<lwork<<endl;
delete[] work_query;
double *work= new double [lwork_];
//
// Call to dsyev_
//
info_=dsyev_(&jobz, &uplo, &n, a, &lda, w, work,
&lwork_, &info);
//
// Free all
//
delete[] work;
//
// Transpose the DM part (a is row ordered)
//
DMpart.transposeSelf(secondDim,firstDim);
//
// Output
//
for(int i=0; i<n; i++) EigenVals.push_back(w[i]);
//
// Output
//
//for (int j=0; j<elems; j++) cout<<j<<" "<<" "<<a[j]<<endl;
//for(int i=0; i<EigenVals.size(); i++) cout<<EigenVals[i]<<endl;
//cout<<"Info "<<info<<endl;
};