-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.h
executable file
·368 lines (334 loc) · 9.31 KB
/
matrix.h
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
* matrix.h
*
* Created on: Aug 22, 2011
* Author: wsttiger
*/
#include <vector>
#include <complex>
using std::vector;
using std::complex;
#ifndef MATRIX_H_
#define MATRIX_H_
extern "C" void dsyev_(char *jobz, char *uplo, int *n, double *a,
int *lda, double *w, double *work, int *lwork,
int *info);
extern "C" void dgetri(int* n, double* a, int *lda, int *ipiv,
double* work, int* lwork, int* info);
extern "C" void dgetrf(int* m, int* n, double* a, int* lda, int* ipiv, int* info);
extern "C" void zgetri(int* n, complex<double>* a, int *lda, int *ipiv,
complex<double>* work, int* lwork, int* info);
extern "C" void zgetrf(int* m, int* n, complex<double>* a, int* lda, int* ipiv, int* info);
//*****************************************************************************
long fact(int n)
{
long prod = 1;
for (long i = 1; i <= n; i++)
{
prod *= i;
}
return prod;
}
//*****************************************************************************
//*****************************************************************************
int nchoosek(int n, int k)
{
long rval = 1;
for (long i = k+1; i <= n; i++)
{
rval *= i;
}
return (rval/fact(n-k));
}
//*****************************************************************************
//*****************************************************************************
double dotvec(const vector<double>& v1, const vector<double>& v2)
{
double rval = 0.0;
for (unsigned int iv = 0; iv < v1.size(); iv++)
{
rval += v1[iv]*v2[iv];
}
return rval;
}
//*****************************************************************************
//r = a*x + b*y
//*****************************************************************************
vector<double> wst_daxpy(const double& a, const vector<double>& x,
const double& b, const vector<double>& y)
{
vector<double> rv(x.size(),0.0);
for (unsigned int i = 0; i < rv.size(); i++)
{
rv[i] = a*x[i]+b*y[i];
}
return rv;
}
//*****************************************************************************
// in-place version of daxpy
// x = a*x + b*y
//*****************************************************************************
void wst_daxpy_inplace(const double& a, vector<double>& x,
const double& b, const vector<double>& y)
{
for (unsigned int i = 0; i < x.size(); i++)
{
x[i] = a*x[i]+b*y[i];
}
}
//*****************************************************************************
//*****************************************************************************
double norm2(const vector<double>& v)
{
double rnorm = 0.0;
for (unsigned int iv = 0; iv < v.size(); iv++)
{
rnorm += v[iv]*v[iv];
}
return sqrt(rnorm);
}
//*****************************************************************************
//*****************************************************************************
vector<double> matrix_vector_mult(vector<double> mat, vector<double> v)
{
unsigned int szmat = mat.size();
unsigned int sz2 = v.size();
unsigned int sz1 = szmat / sz1;
vector<double> rv(sz2,0.0);
for (unsigned int i = 0; i < sz2; i++)
{
for (unsigned int j = 0; j < sz1; j++)
{
rv[i] += mat[i*sz1+j];
}
}
return rv;
}
//*****************************************************************************
//*****************************************************************************
void normalize(vector<double>& v)
{
double rnorm = 0.0;
for (unsigned int iv = 0; iv < v.size(); iv++)
{
rnorm += v[iv]*v[iv];
}
rnorm = sqrt(rnorm);
for (unsigned int iv = 0; iv < v.size(); iv++)
{
v[iv] /= rnorm;
}
}
//*****************************************************************************
//*****************************************************************************
void print_vector(const vector<double>& v)
{
for (unsigned int i = 0; i < v.size(); i++)
{
printf(" %10.5f\n",v[i]);
}
}
//*****************************************************************************
//*****************************************************************************
void print_matrix(const vector<double>& mat, int m, int n)
{
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
printf("%8.2f",mat[i*n+j]);
}
printf("\n");
}
}
//*****************************************************************************
//*****************************************************************************
void print_matrix(const vector< complex<double> >& mat, int m, int n)
{
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
printf(" (%8.4f,%8.4f)",mat[i*n+j].real(),mat[i*n+j].imag());
}
printf("\n");
}
}
//*****************************************************************************
//void diag_matrix2(const vector<double>& mat, int n,
// vector<double>& e, vector <double>& evec)
//{
// MatrixXcf A = MatrixXcf::Zero(n,n);
// for (int i = 0; i < n; i++)
// {
// for (int j = 0; j < n; j++)
// {
// A(i,j) = complex<double>(mat[i*n+j],0.0);
// }
// }
//
// ComplexEigenSolver<MatrixXcf> ces;
// ces.compute(A);
// VectorXcf et = ces.eigenvalues();
// MatrixXcf evt = ces.eigenvectors();
//
// for (int i = 0; i < n; i++)
// {
// e[i] = real(et(i));
// for (int j = 0; j < n; j++)
// {
// evec[i*n+j] = real(evt(i,j));
// }
// }
//
//}
//*****************************************************************************
vector<double> get_col_from_matrix(const vector<double> mat, int col, int m, int n)
{
vector<double> rv(m,0.0);
for (int i = 0; i < m; i++)
{
rv[i] = mat[i*n+col];
}
return rv;
}
//*****************************************************************************
//*****************************************************************************
void diag_matrix(const vector<double>& mat, int n,
vector<double>& e, vector <double>& evec)
{
char jobz = 'V';
char uplo = 'U';
int info;
double* a = new double[n*n];
int lda = n;
int lwork = 3*n-1;
double *work = new double[lwork];
double *et = new double[n];
for (int i = 0; i < n*n; i++) a[i] = mat[i];
dsyev_(&jobz, &uplo, &n, a, &lda, et, work, &lwork, &info);
if (info != 0)
{
printf("[[Error:]] lapack::dsyev failed --- info = %d\n\n", info);
}
else
{
//printf("Eigenvalues:\n");
for (int i = 0; i < n; i++)
{
e[i] = et[i];
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
evec[j*n+i] = a[i*n+j];
}
}
}
delete a;
delete work;
delete et;
}
//*****************************************************************************
//*****************************************************************************
vector<double> invert_matrix(const vector<double>& mat, int n)
{
vector<double> rv(n*n,0.0);
int info;
double* a = new double[n*n];
int lda = n;
int lwork = 3*n-1;
int* ipiv = new int[n];
for (int i = 0; i < n; i++) ipiv[i] = 0;
double *work = new double[lwork];
for (int i = 0; i < n*n; i++) a[i] = mat[i];
dgetrf(&n, &n, a, &lda, ipiv, &info);
if (info != 0)
{
printf("[[Error:]] lapack::dgetrf failed --- info = %d\n\n", info);
}
dgetri(&n, a, &lda, ipiv, work, &lwork, &info);
if (info != 0)
{
printf("[[Error:]] lapack::dgetri failed --- info = %d\n\n", info);
}
else
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
rv[i*n+j] = a[i*n+j];
}
}
}
delete a;
delete work;
delete ipiv;
return rv;
}
//*****************************************************************************
//*****************************************************************************
vector< complex<double> > invert_matrix(const vector< complex<double> >& mat, int n)
{
vector< complex<double> > rv(n*n,0.0);
int info;
complex<double>* a = new complex<double>[n*n];
int lda = n;
int lwork = 3*n-1;
int* ipiv = new int[n];
for (int i = 0; i < n; i++) ipiv[i] = 0;
complex<double>* work = new complex<double>[lwork];
for (int i = 0; i < n*n; i++) a[i] = mat[i];
zgetrf(&n, &n, a, &lda, ipiv, &info);
if (info != 0)
{
printf("[[Error:]] lapack::zgetrf failed --- info = %d\n\n", info);
}
zgetri(&n, a, &lda, ipiv, work, &lwork, &info);
if (info != 0)
{
printf("[[Error:]] lapack::zgetri failed --- info = %d\n\n", info);
}
else
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
rv[i*n+j] = a[i*n+j];
}
}
}
delete a;
delete work;
delete ipiv;
return rv;
}
//*****************************************************************************
//*****************************************************************************
vector<double> random_vector(const int& nsize)
{
vector<double> v(nsize,0.0);
for (int i = 0; i < nsize; i++)
{
int i1 = rand();
double t1 = (i1 % 100000)/100000.0;
v[i] = t1;
}
normalize(v);
return v;
}
//*****************************************************************************
//*****************************************************************************
bool is_equals(const vector<double>& a, const vector<double>& b, double tol = 1e-10)
{
for (unsigned int i = 0; i < a.size(); i++)
{
if (fabs(a[i]-b[i]) > tol) return false;
}
return true;
}
//*****************************************************************************
#endif /* MATRIX_H_ */