-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmatops.cpp
More file actions
337 lines (304 loc) · 7.3 KB
/
Copy pathmatops.cpp
File metadata and controls
337 lines (304 loc) · 7.3 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
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
// kron.cpp : Defines the entry point for the console application.
#include <string>
#include <iostream>
#include <cstdlib>
#include <vector>
#include <valarray>
#include <algorithm>
#include <cassert>
#ifdef _WIN32
#include <mkl.h>
#define F77_FUNC(func) (func)
#else
#include "fortran.h"
#define F77_FUNC(func) func##_
#endif
#include "matrix.h"
#include "matops.h"
using namespace std;
matrix reshape(const valarray<double> & x, size_t rows,size_t cols)
{
if (x.size() != rows*cols)
throw 1;
matrix temp(cols,rows);
*temp.data=x;
temp=transpose(temp);
return(temp);
}
valarray<double> unroll(const matrix &X)
{
int N=X.data[0].size(),c=X.getCol(),r=X.getRow();
valarray<double> temp(N),col(r);
for (int i=0;i < c;i++)
{
col=X.data[0][slice(i,r,c)];
temp[slice(i*r,r,1)]=col;
}
return(temp);
}
matrix element_mult(const matrix &A,const matrix &B)
{
size_t M1=A.getRow(),M2=B.getRow(),N1=A.getCol(),N2=B.getCol();
if ((M1 != M2) || (N1 != N2))
{
cout << "element_mult: Matrices are of wrong dimensions. Fix that! " << endl;
exit(0);
}
matrix temp(M1,N1);
(*temp.data) = (*A.data) * (*B.data);
return(temp);
}
void zero(matrix &X)
{
int N = X.data[0].size();
for (int i=0;i<N;i++)
X.data[0][i]=0.;
}
valarray<double> diag(const matrix &A)
{
size_t M=A.getRow(),N=A.getCol();
size_t i;
valarray<double> temp(M);
if (M != N)
{
cout << "diag: Matrix must be square" << endl;
exit(0);
}
for (i=0;i<M;i++)
temp[i]=A[i][i];
return(temp);
}
double rcond(const matrix &X)
{
int M=X.getRow();
int N=X.getCol();
int i,j;
int ipiv[M],info,iwork[M];
matrix A(M,N);
//const char NORM="1";
double anorm,result,work[4*N];
valarray<double> temprow(N);
A=X;
for (i=0;i<M;i++)
temprow += abs(A.extract_row(i));
anorm = temprow.max();
A=transpose(A);
dgetrf_(&M,&N,&A[0][0],&N,ipiv,&info);
assert(info==0);
dgecon_("1",&M,&A[0][0],&M,&anorm,&result,work,iwork,&info);
assert(info==0);
return(result);
}
matrix vertcat(bool TransposeA, bool TransposeB, const matrix &A, const matrix &B)
{
matrix tempA,tempB;
int M1=A.getRow();
int M2=B.getRow();
int N1=A.getCol();
int N2=B.getCol();
size_t lengths1[2],lengths2[2],strides1[2],strides2[2];
if (TransposeA)
{
lengths1[0]=N1;
lengths1[1]=M1;
strides1[0]=1;
strides1[1]=N1;
swap(M1,N1);
}
else
{
lengths1[0]=M1;
lengths1[1]=N1;
strides1[0]=N1;
strides1[1]=1;
}
if (TransposeB)
{
lengths2[0]=N2;
lengths2[1]=M2;
strides2[0]=1;
strides2[1]=N2;
swap(M2,N2);
}
else
{
lengths2[0]=M2;
lengths2[1]=N2;
strides2[0]=N2;
strides2[1]=1;
}
if (N1 != N2)
{
cout << "vertcat: dimensions do not match" << endl;
exit(0);
}
matrix temp(M1+M2,N2);
gslice A_slice(0,valarray<size_t>(lengths1,2),valarray<size_t>(strides1,2));
gslice B_slice(0,valarray<size_t>(lengths2,2),valarray<size_t>(strides2,2));
slice A_part(0,M1*N1,1);
slice B_part(M1*N1,M2*N2,1);
temp.data[0][A_part]=A.data[0][A_slice];
temp.data[0][B_part]=B.data[0][B_slice];
return(temp);
}
matrix horzcat(const matrix &A, const matrix &B)
{
return(transpose(vertcat(true,true,A,B)));
}
matrix kron(const matrix &A,const matrix & B)
{
int rows, cols,*f,*g,*h,*e;
int i,j;
int ra,rb,ca,cb;
ra = A.getRow();
rb = B.getRow();
ca = A.getCol();
cb = B.getCol();
rows = ra*rb;
cols = ca * cb;
matrix temp(rows,cols);
// vector< valarray<double> > vals;
f = new int[rows];
g = new int[cols];
e = new int[rows];
h = new int[cols];
for (i=0;i<rb;i++)
for (j=0;j<ra;j++)
{
f[i+rb*j]=j;
e[i+rb*j]=i;
}
for (i=0;i<cb;i++)
for (j=0;j<ca;j++)
{
g[i+cb*j] = j;
h[i+cb*j]=i;
}
for (i=0;i<rows;i++)
for (j=0;j<cols;j++)
temp[i][j]=A[f[i]][g[j]]*B[e[i]][h[j]];
//for (i=0;i<ra;i++)
// for (j=0;j<ca;j++)
// vals.push_back(A[i][j]*B.data[0]);
return(temp);
}
/*vector<int> generate_rand( int range_min, int range_max, int n )
{
// Generate random numbers in the half-closed interval
// [range_min, range_max). In other words,
// range_min <= random number < range_max
int i;
vector<int> temp;
for ( i = 0; i < n; i++ )
temp.push_back((double)rand() / (RAND_MAX + 1) * (range_max - range_min)
+ range_min);
return(temp);
} */
//void get_correct(vector<int> & number,string guess)
//{
// int i,n = number.size();
// bool match=false;
//}
//void get_correct_position(vector<int> & number,string guess)
//{
//}
//void process(vector<int> & number, string guess)
//{
// int n,i;
// n=guess.length();
// get_correct(number,guess);
// get_correct_position(number,guess);
//}
vector<double> pack(const matrix &X)
{
int i,j;
vector<double> temp;
for (i=1;i<=X.getRow();i++)
for (j=0;j<i;j++)
temp.push_back(X(i-1,j));
return(temp);
}
matrix transpose(const matrix &X)
{
matrix temp(X.getCol(),X.getRow());
int i,j;
for (i=0;i<X.getCol();i++)
for (j=0;j<X.getRow();j++)
temp(i,j)=X(j,i);
return(temp);
}
matrix chol(const matrix &A)
{
int c,d,info,i,j;
matrix C;
C=transpose(A);
c=C.getRow();
d=C.getCol();
if (c != d)
throw 1;
for (j=1;j<=d;j++)
for (i=c-1;i>=j;i--)
C(i,j-1)=0.;
F77_FUNC(dpotrf)("L",&c,&C(0,0),&c,&info);
if (info != 0)
cout << "Not positive definite...you better check yourself! Minor " << info << " is not working for me" << endl;
return(C);
}
matrix shrink_matrix(size_t rows, size_t cols,const vector<int> dependent_rows)
{
size_t column_index = 0,nlin_index=0;
matrix T(rows-dependent_rows.size(),cols); //Create transformation matrix
for (size_t row_index=0;row_index< T.getRow();row_index++)
{
while (column_index == dependent_rows[nlin_index])
{
column_index++;
nlin_index++;
}
T[row_index][column_index++]=1.;
}
return(T);
}
matrix qr(const matrix &A)
{
matrix C=transpose(A);
int lwork = -1,info,r,c,tau_size;
vector<double> work(1);
r=A.getRow();
c=A.getCol();
tau_size=min(r,c);
vector<double> tau(tau_size);
F77_FUNC(dgeqrf)(&r,&c,&C(0,0),&r,&tau[0],&work[0],&lwork,&info); //workspace query
if (info == 0)
lwork=(int)work[0];
else
cout << "Still some problems in QR factorization" << endl;
work.resize(lwork);
F77_FUNC(dgeqrf)(&r,&c,&C(0,0),&r,&tau[0],&work[0],&lwork,&info);
C=transpose(C);
return(C);
}
SVDRECORD svd(const matrix &A)
{
SVDRECORD results;
int M,N,K;
int info,lwork=-1;
double wkopt;
M=A.getRow();
N=A.getCol();
K=min(M,N);
valarray<double> S(K);
matrix temp(M,N),U(M,M),VT(N,N);
temp=A;
dgesvd_("A","A",&M,&N,&temp[0][0],&M,&S[0],&U[0][0],&M,&VT[0][0],&N,&wkopt,&lwork,&info);
lwork = (int)wkopt;
double work[lwork];
dgesvd_("A","A",&M,&N,&temp[0][0],&M,&S[0],&U[0][0],&M,&VT[0][0],&N,work,&lwork,&info);
results.U=matrix(M,M);
results.V=matrix(N,N);
results.S=valarray<double>(K);
results.U=U;
results.V=VT;
results.S=S;
return(results);
}