Menu

[r182]: / trunk / include / zhematrix- / zhematrix-lapack.hpp  Maximize  Restore  History

Download this file

175 lines (155 with data), 5.8 kB

  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
//=============================================================================
/*! solve A*X=Y using zhesv\n
The argument is dmatrix Y. Y is overwritten and become the solution X.
A is also overwritten.
*/
inline long zhematrix::zhesv(zgematrix& mat)
{CPPL_VERBOSE_REPORT;
#ifdef CPPL_DEBUG
if(n!=mat.n){
ERROR_REPORT;
std::cerr << "These two matrices cannot be solved." << std::endl
<< "Your input was (" << n << "x" << n << ") and (" << mat.n << "x" << mat.n << ")." << std::endl;
exit(1);
}
#endif//CPPL_DEBUG
char UPLO('l');
long NRHS(mat.n), LDA(n), *IPIV(new long[n]), LDB(mat.n), LWORK(-1), INFO(1);
comple *WORK(new comple[1]);
zhesv_(UPLO, n, NRHS, array, LDA, IPIV, mat.array, LDB, WORK, LWORK, INFO);
INFO=1;
LWORK = long(std::real(WORK[0]));
delete [] WORK; WORK =new comple[LWORK];
zhesv_(UPLO, n, NRHS, array, LDA, IPIV, mat.array, LDB, WORK, LWORK, INFO);
delete [] WORK; delete [] IPIV;
if(INFO!=0){
WARNING_REPORT;
std::cerr << "Serious trouble happend. INFO = " << INFO << "." << std::endl;
}
return INFO;
}
//=============================================================================
/*! solve A*x=y using zhesv\n
The argument is zcovector y. y is overwritten and become the solution x.
A is also overwritten.
*/
inline long zhematrix::zhesv(zcovector& vec)
{CPPL_VERBOSE_REPORT;
#ifdef CPPL_DEBUG
if(n!=vec.l){
ERROR_REPORT;
std::cerr << "These matrix and vector cannot be solved." << std::endl
<< "Your input was (" << n << "x" << n << ") and (" << vec.l << ")." << std::endl;
exit(1);
}
#endif//CPPL_DEBUG
char UPLO('l');
long NRHS(1), LDA(n), *IPIV(new long[n]), LDB(vec.l), LWORK(-1), INFO(1);
comple *WORK( new comple[1] );
zhesv_(UPLO, n, NRHS, array, LDA, IPIV, vec.array, LDB, WORK, LWORK, INFO);
INFO=1;
LWORK = long(std::real(WORK[0]));
delete [] WORK; WORK = new comple[LWORK];
zhesv_(UPLO, n, NRHS, array, LDA, IPIV, vec.array, LDB, WORK, LWORK, INFO);
delete [] WORK; delete [] IPIV;
if(INFO!=0){
WARNING_REPORT;
std::cerr << "Serious trouble happend. INFO = " << INFO << "." << std::endl;
}
return INFO;
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//=============================================================================
/*! calculate eigenvalues and eigenvectors.\n
All of the arguments need not to be initialized.
w is overwitten and become eigenvalues.
This matrix is also overwritten.
if jobz=1, this matrix becomes eigenvectors.
*/
inline long zhematrix::zheev(std::vector<double>& w,
const bool& jobz=0)
{CPPL_VERBOSE_REPORT;
w.resize(n);
char JOBZ, UPLO('l');
if(jobz==0){ JOBZ='n'; } else{ JOBZ='V'; }
long LDA(n), INFO(1), LWORK(-1);
double *RWORK(new double[std::max(1l, 3*n-2)]);
comple *WORK(new comple[1]);
zheev_(JOBZ, UPLO, n, array, LDA, &w[0], WORK, LWORK, RWORK, INFO);
INFO=1;
LWORK = long(std::real(WORK[0]));
delete [] WORK; WORK = new comple[LWORK];
zheev_(JOBZ, UPLO, n, array, LDA, &w[0], WORK, LWORK, RWORK, INFO);
delete [] RWORK; delete [] WORK;
if(INFO!=0){
WARNING_REPORT;
std::cerr << "Serious trouble happend. INFO = " << INFO << "." << std::endl;
}
return INFO;
}
//=============================================================================
/*! calculate eigenvalues and eigenvectors.\n
All of the arguments need not to be initialized.
w and v are overwitten and become
eigenvalues and eigenvectors, respectively.
This matrix is also overwritten.
*/
inline long zhematrix::zheev(std::vector<double>& w,
std::vector<zcovector>& v)
{CPPL_VERBOSE_REPORT;
w.resize(n); v.resize(n);
for(long i=0; i<n; i++){ v[i].resize(n); }
char JOBZ('V'), UPLO('l');
long LDA(n), INFO(1), LWORK(-1);
double *RWORK(new double[std::max(1l, 3*n-2)]);
comple *WORK(new comple[1]);
zheev_(JOBZ, UPLO, n, array, LDA, &w[0], WORK, LWORK, RWORK, INFO);
INFO=1;
LWORK = long(std::real(WORK[0]));
delete [] WORK; WORK = new comple[LWORK];
zheev_(JOBZ, UPLO, n, array, LDA, &w[0], WORK, LWORK, RWORK, INFO);
delete [] RWORK; delete [] WORK;
//// forming ////
for(long i=0; i<n; i++){ for(long j=0; j<n; j++){
v[j](i) = array[i+n*j];
}}
if(INFO!=0){
WARNING_REPORT;
std::cerr << "Serious trouble happend. INFO = " << INFO << "." << std::endl;
}
return INFO;
}
//=============================================================================
/*! calculate eigenvalues and eigenvectors.\n
All of the arguments need not to be initialized.
w and v are overwitten and become
eigenvalues and eigenvectors, respectively.
This matrix is also overwritten.
*/
inline long zhematrix::zheev(std::vector<double>& w,
std::vector<zrovector>& v)
{CPPL_VERBOSE_REPORT;
w.resize(n); v.resize(n);
for(long i=0; i<n; i++){ v[i].resize(n); }
char JOBZ('V'), UPLO('l');
long LDA(n), INFO(1), LWORK(-1);
double *RWORK(new double[std::max(1l, 3*n-2)]);
comple *WORK(new comple[1]);
zheev_(JOBZ, UPLO, n, array, LDA, &w[0], WORK, LWORK, RWORK, INFO);
INFO=1;
LWORK = long(std::real(WORK[0]));
delete [] WORK; WORK = new comple[LWORK];
zheev_(JOBZ, UPLO, n, array, LDA, &w[0], WORK, LWORK, RWORK, INFO);
delete [] RWORK; delete [] WORK;
//// forming ////
for(long i=0; i<n; i++){ for(long j=0; j<n; j++){
v[j](i) = array[i+n*j];
}}
if(INFO!=0){
WARNING_REPORT;
std::cerr << "Serious trouble happend. INFO = " << INFO << "." << std::endl;
}
return INFO;
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.