-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlapack_matrix_routines_wrapper_v3.cc
More file actions
252 lines (209 loc) · 6.39 KB
/
Copy pathlapack_matrix_routines_wrapper_v3.cc
File metadata and controls
252 lines (209 loc) · 6.39 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
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
extern "C" {
#include <mkl.h>
}
//#include <fstream>
#include <stdio.h>
#include <math.h>
//#include <malloc.h>
//#include <string.h>
#include <stdlib.h>
/* // copy this in you .h
extern "C" void dgetrf(int *,int *, double *, int *, int *, int *);
extern "C" void dgetri(int *,double *,int *,int *, double *, int*,int *);
extern "C" void dsyevd(char *,char *,int *, double *, int *,double *,double *, int *, int *,int *, int * );
*/
static int invert_symmetric_matrix_lapack( double **m, int dim_m, double **inv_m){
/*
M is a NON-SINGULAR symmetric matrix of size (dim_m * dim_m).
The program calculates the inverse and returns it in inv_m
written by CM, 20/07/2008
tested by CM, 21/07/2008
*/
int c, i,j,k,l,lwork, info;
double *array_m;
double *work, tmp1, tmp2, worksize;
int *iwork;
int *isupps;
// cout<<"cacca2"<<endl;
array_m = d1t(dim_m*dim_m);
// cout<<"cacca3"<<endl;
printf("copio l'array\n");
c=0;
for(i=0; i< dim_m; i++){
for(j=0; j< dim_m; j++){
// cout<<i<<j<<endl;
// cout<<m[i][j]<<endl;
/* copy matrix in array */
array_m[c]=m[i][j];
c++;
}
}
isupps = i1t(1+dim_m); /* OK for ATLAS and MKL */
printf("dgetrf\n");
dgetrf(&dim_m,&dim_m,array_m,&dim_m,isupps,&info);
if ( info!=0 ) {
// fprintf(stderr,"ERROR in dgetrf. Bailing out.\n");
cout<<"ERROR in dgetrf. Bailing out. Info: "<<info<<endl;
// fprintf(stderr,"\tInfo: %d\n", info);
// exit(1);
return -1;
}
lwork=-1; /*workspace query */
printf("dgetri 1\n");
//lapack function
dgetri(&dim_m,array_m,&dim_m,isupps,&worksize,&lwork,&info);
if ( info!=0 ) {
cout<<"ERROR in workspace query for dgetri. Bailing out."<<endl;
cout<<"Info:"<<info<<endl;
// exit(1);
return -1;
}
lwork=(int) worksize+1;
printf("dgetri info: %d ...allocating workspace for work of size: %d (%lf)\n",info,lwork,worksize+1);
work = d1t(lwork);
printf("dgetri 2\n");
//lapack function
dgetri(&dim_m,array_m,&dim_m,isupps,work,&lwork,&info);
if ( info!=0 ) {
cout<<"ERROR in dgetri. Bailing out.Info: "<<info<<endl;
// exit(1);
return -1;
}
printf("ricopio\n");
c=0;
for(i=0; i< dim_m; i++){
for(j=0; j< dim_m; j++){
/* copy array in inverse matrix */
inv_m[i][j]=array_m[c];
c++;
}
}
printf("...ricopiato!\n");
free_d1t(array_m);
free_d1t(work);
free_i1t(isupps);
printf("finito!\n");
return(0); /* Success */
}
static int multiply_quad_matrix_lapack( double **m_a,double **m_b,int dim_m, double**m_c){
/*
non so cosa cazzo sto facendo
*/
double *A,*B,*C;
char TRANS = 'N';
double ALPHA = 1.0;
double BETA = 0.0;
int k=0;
A = d1t(dim_m*dim_m);
B = d1t(dim_m*dim_m);
C = d1t(dim_m*dim_m);
printf("copio l'array\n");
k=0;
for(int i=0; i< dim_m; i++){
for(int j=0; j< dim_m; j++){
/* copy matrix in array */
A[k]=m_a[i][j];
B[k]=m_b[i][j];
k++;
}
}
dgemm(&TRANS, &TRANS, &dim_m, &dim_m, &dim_m, &ALPHA, A, &dim_m, B, &dim_m, &BETA, C, &dim_m);
printf("ricopio\n");
k=0;
for(int i=0; i< dim_m; i++){
for(int j=0; j< dim_m; j++){
/* copy array in inverse matrix */
m_c[i][j]=C[k];
k++;
}
}
printf("...ricopiato!\n");
free_d1t(A);
free_d1t(B);
free_d1t(C);
printf("finito!\n");
return(0); /* Success */
}
static double multiply_vector_lapack( double *v_a,double *v_b,int dim_v){
/*
non so cosa cazzo sto facendo
*/
int inc=1;
double temp=ddot(&dim_v,v_a,&inc,v_b,&inc);
return temp;
}
static void decompose_symmetric_matrix_lapack( double **m, int dim_m, double *eigenvalues_m, double **eigenvectors){
// cout<<"cacca"<<endl;
/* M is a SINGULAR (or NON-SINGULAR) symmetric matrix of size dim_m * dim_m.
The program calculates the eigenvalues, eigenvectors, the pseudoinverse.
returns the number of zero eigenvalues (i.e. eigenvalues that are less than "tol" in modulus.
*/
int c,i,j,k,l,lwork, info;
double *array_m;
double *work, worksize;
int liwork, *iwork, iworksize, nzeros;
char c1, c2;
// cout<<"cacca"<<endl;
array_m = d1t(dim_m*dim_m);
// cout<<"cacca"<<endl;
c=0;
for(i=0; i< dim_m; i++){
for(j=0; j< dim_m; j++){
/* copy matrix in array */
// cout<<i<<j<<endl;
// cout<<m[i][j]<<endl;
array_m[c]=m[i][j];
c++;
}
}
// printf("...computing size of workspace\n");
c1='v';
c2='u';
lwork=liwork=-1; /* workspace query */
//### lapack function
// printf("dimensione %d\n",dim_m);
dsyevd( &c1, &c2, &dim_m, array_m, &dim_m, eigenvalues_m,&worksize, &lwork, &iworksize, &liwork, &info );
if (info!=0) {printf("Error in dsyevd. Workspace query failed. Info value: %d\n",info); exit(1);}
lwork=(int) (worksize+1);
liwork=iworksize;
c1='v';
c2='u';
// printf("dsyevd info: %d ...allocating workspace for work of size: %d (%lf) and iwork (size: %d )\n",info,lwork,worksize+1,liwork);
work=d1t(lwork);
iwork=i1t(liwork);
// printf("...decomposing matrix\n");
dsyevd( &c1, &c2, &dim_m, array_m, &dim_m, eigenvalues_m,work, &lwork, iwork, &liwork, &info );
// printf("done\n");
if ( info!=0 ) {
fprintf(stderr,"ERROR in dsyevd.\n");
fprintf(stderr,"\tInfo: %d\n", info);
exit(1);
}
/* eigenvalues, in ascending order, are in eigenvalues_m */
/* eigenvectors, in ascending order, are in array. */
/* For a given eigenvector we should have x1,y1,z1, x2,y2,z2, etc...
*/
for( c=0, j=0; j< dim_m; j++)
for(l=0; l< dim_m; l++, c++ ) {
eigenvectors[j][l] = array_m[c];
}
free_d1t(work);
free_i1t(iwork);
free_d1t(array_m);
}
static void spectral_singular_inversion(double **inv_m, int dim_m, double *eigenvalues_m, double **eigenvectors, double tol){
int i, j, k;
for(i=0; i< dim_m; i++){
for(j=i; j< dim_m; j++){
/* copy matrix in array */
inv_m[i][j]=0;
/* check se deve essere >= k 0 no */
for( k = 0;k< dim_m; k++){
/* Run the sum backwards to minimize roundoff errors in the sum of inverse eigenvalues */
if (fabs(eigenvalues_m[k]) < tol) continue;
inv_m[i][j] += 1.0/eigenvalues_m[k]*eigenvectors[k][i]*eigenvectors[k][j];
}
if (i!=j) inv_m[j][i]=inv_m[i][j]; /* symmetry */
}
}
}