[go: up one dir, main page]

Menu

[r299]: / libpetey / full_util.cc  Maximize  Restore  History

Download this file

384 lines (313 with data), 13.7 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
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#include <stdint.h>
#include <math.h>
#include <full_util.h>
template <class real, class integer>
real ** allocate_matrix(integer m, integer n) {
real **mat;
mat=new real *[m];
mat[0]=new real[m*n];
for (integer i=1; i<m; i++) {
mat[i]=mat[0]+n*i;
}
return mat;
}
template <class real, class integer>
void zero_matrix(real ** mat, integer m, integer n) {
for (integer i=0; i<m*n; i++) mat[0][i]=0;
}
template <class real, class integer>
real ** zero_matrix(integer m, integer n) {
real **result;
result=allocate_matrix<real, integer>(m, n);
zero_matrix(result, m, n);
return result;
}
template <class real, class integer>
void identity_matrix(real ** mat, integer m, integer n) {
integer dmin;
zero_matrix(mat, m, n);
if (m < n) dmin=m; else dmin=n;
for (integer i=0; i<dmin; i++) mat[i][i]=1;
}
template <class real, class integer>
real ** identity_matrix(integer m, integer n) {
real **result;
result=allocate_matrix<real, integer>(m, n);
identity_matrix(result, m, n);
return result;
}
template <class real>
void delete_matrix(real ** mat) {
if (mat==NULL) return;
delete[] mat[0];
delete[] mat;
}
template <class real, class integer>
void copy_matrix(real **m1, real **m2, integer m, integer n) {
for (integer i=0; i<m; i++) {
for (integer j=0; j<n; j++) {
m2[i][j]=m1[i][j];
}
}
}
template <class real, class integer>
real ** copy_matrix(real **mat, integer m, integer n) {
real **result;
//this is really fucking annoying, I have to add all these fucking
//template parameters in order for it to compile:
result=allocate_matrix<real, integer>(m, n);
copy_matrix(mat, result, m, n);
return result;
}
//should be more efficient:
template <class real, class integer>
void matrix_mult_t(real **plier, real **cand, real **result, integer m, integer p, integer n) {
for (integer i=0; i<m; i++) {
for (integer j=0; j<n; j++) {
result[i][j]=0;
for (integer k=0; k<p; k++) {
result[i][j]+=plier[k][i]*cand[k][j];
}
}
}
}
template <class real, class integer>
void matrix_mult(real **plier, real **cand, real **result, integer m, integer p, integer n) {
for (integer i=0; i<m; i++) {
for (integer j=0; j<n; j++) {
result[i][j]=0;
for (integer k=0; k<p; k++) {
result[i][j]+=plier[i][k]*cand[k][j];
}
}
}
}
template <class real, class integer>
real ** matrix_mult(real **plier, real **cand, integer m, integer p, integer n) {
real **result;
result=allocate_matrix<real, integer>(m, n);
matrix_mult(plier, cand, result, m, p, n);
return result;
}
template <class real, class integer>
void vector_mult(real **plier, real *cand, real *result, integer m, integer n) {
for (integer i=0; i<m; i++) {
result[i]=0;
for (integer j=0; j<n; j++) result[i]+=plier[i][j]*cand[j];
}
}
template <class real, class integer>
real * vector_mult(real **plier, real *cand, integer m, integer n) {
real *result;
result=new real[n];
vector_mult(plier, cand, result, m, n);
return result;
}
template <class real, class integer>
void left_vec_mult(real *plier, real **cand, real *result, integer m, integer n) {
for (integer i=0; i<n; i++) {
result[i]=0;
for (integer j=0; j<m; j++) result[i]+=plier[j]*cand[j][i];
}
}
template <class real, class integer>
real * left_vec_mult(real *plier, real **cand, integer m, integer n) {
real *result;
result=new real[n];
left_vec_mult<real, integer>(plier, cand, result, m, n);
return result;
}
template <class real, class integer>
void matrix_add(real **mat1, real ** mat2, integer m, integer n) {
for (integer i=0; i<m; i++) {
for (integer j=0; j<n; j++) {
mat1[i][j]=mat1[i][j]+mat2[i][j];
}
}
}
//for square matrices (inplace):
template <class real, class integer>
void matrix_transpose(real **mat, integer m) {
real swap;
for (integer i=0; i<m; i++) {
for (integer j=0; j<i; j++) {
swap=mat[j][i];
mat[j][i]=mat[i][j];
mat[i][j]=swap;
}
}
}
#define MAXLL 5000
template <class real, class integer>
real ** matrix_transpose(real **mat, integer m, integer n) {
real **matnew;
//fuuuuuuuuuuuu
matnew=allocate_matrix<real, integer>(n, m);
for (integer i=0; i<m; i++) {
for (integer j=0; j<n; j++) {
matnew[j][i]=mat[i][j];
}
}
return matnew;
}
#define MAXLL 5000
template <class real, class integer>
real ** scan_matrix(FILE *fptr, integer &m, integer &n) {
char line[MAXLL];
real **mat;
int line_pos=0;
int lc;
if (fgets(line, MAXLL, fptr)==NULL) return NULL;
sscanf(line, "%d %d\n", &m, &n);
//fuuuuuu fufufuuuuuuuuuu..... cccccck
mat=allocate_matrix<real, integer>(m, n);
for (integer i=0; i<m; i++) {
if (fgets(line, MAXLL, fptr)==NULL) {
fprintf(stderr, "scan_matrix: file ended before finished scanning\n");
break;
}
for (integer j=0; j<n; j++) {
sscanf(line+line_pos, "%f%n", &mat[i][j], &lc);
line_pos+=lc;
}
}
return mat;
}
template <class real, class integer>
void print_matrix(FILE *fptr, real **mat, integer m, integer n) {
fprintf(fptr, "%d %d\n", m, n);
for (integer i=0; i<m; i++) {
for (integer j=0; j<n; j++) {
fprintf(fptr, "%f ", mat[i][j]);
}
fprintf(fptr, "\n");
}
}
//getting too long to be inlined...
template <class real, class integer>
real ** read_matrix(FILE *fptr, integer &m, integer &n) {
real **mat;
long check;
fread(&n, 1, sizeof(n), fptr);
check=ftell(fptr);
//not a seekable stream--use a linked list to get the data:
if (check < 0) {
linked_list<real_a> data;
real_a *data2;
real_a val=0;
long ntot=0;
while (fread(&val, sizeof(val), 1, fs)==1) data.add(val);
data2=data.make_array(ntot);
//assert(ntot % nvar==0);
ntrain=ntot/nvar;
train=new real_a *[ntrain];
train[0]=data2;
for (nel_ta i=0; i<ntrain; i++) train[i]=train[0]+i*nvar;
if (ntot % n != 0) m=-1;
} else {
fseek(fptr, 0, SEEK_END);
m=(ftell(fptr)-sizeof(n))/sizeof(real)/n;
fseek(fptr, sizeof(n), SEEK_SET);
//printf("read_matrix: reading a [%dx%d] matrix\n", m, n);
mat=allocate_matrix<real, integer>(m, n);
fread(mat[0], sizeof(real), m*n, fptr);
}
return mat;
}
template <class real, class integer>
size_t write_matrix(FILE *fptr, real **mat, integer m, integer n) {
size_t nwrit;
nwrit+=fwrite(&n, 1, sizeof(n), fptr);
nwrit+=fwrite(mat[0], sizeof(real), m*n, fptr);
return nwrit;
}
template <class real, class integer>
real matrix_norm(real **mat, integer m, integer n) {
real result=0;
for (integer i=0; i<m*n; i++) result+=mat[0][i]*mat[0][i];
return sqrt(result);
}
template float ** zero_matrix<float, int16_t>(int16_t, int16_t);
template float ** zero_matrix<float, int32_t>(int32_t, int32_t);
template float ** zero_matrix<float, int64_t>(int16_t, int64_t);
template double ** zero_matrix<double, int16_t>(int16_t, int16_t);
template double ** zero_matrix<double, int32_t>(int32_t, int32_t);
template double ** zero_matrix<double, int64_t>(int16_t, int64_t);
template float ** identity_matrix<float, int16_t>(int16_t, int16_t);
template float ** identity_matrix<float, int32_t>(int32_t, int32_t);
template float ** identity_matrix<float, int64_t>(int16_t, int64_t);
template double ** identity_matrix<double, int16_t>(int16_t, int16_t);
template double ** identity_matrix<double, int32_t>(int32_t, int32_t);
template double ** identity_matrix<double, int64_t>(int16_t, int64_t);
template void delete_matrix<float>(float **);
template void delete_matrix<double>(double **);
template float ** copy_matrix<float, int16_t>(float **, int16_t, int16_t);
template float ** copy_matrix<float, int32_t>(float **, int32_t, int32_t);
template float ** copy_matrix<float, int64_t>(float **, int16_t, int64_t);
template double ** copy_matrix<double, int16_t>(double **, int16_t, int16_t);
template double ** copy_matrix<double, int32_t>(double **, int32_t, int32_t);
template double ** copy_matrix<double, int64_t>(double **, int64_t);
template float ** matrix_mult<float, int16_t>(float **, float **, int16_t, int16_t, int16_t);
template float ** matrix_mult<float, int32_t>(float **, float **, int32_t, int32_t, int32_t);
template float ** matrix_mult<float, int64_t>(float **, float **, int64_t, int64_t, int64_t);
template double ** matrix_mult<double, int16_t>(double **, double **, int16_t, int16_t, int16_t);
template double ** matrix_mult<double, int32_t>(double **, double **, int32_t, int32_t, int32_t);
template double ** matrix_mult<double, int64_t>(double **, double **, int64_t, int64_t, int64_t);
template void matrix_mult_t<float, int16_t>(float **, float **, float **, int16_t, int16_t, int16_t);
template void matrix_mult_t<float, int32_t>(float **, float **, float **, int32_t, int32_t, int32_t);
template void matrix_mult_t<float, int64_t>(float **, float **, float **, int64_t, int64_t, int64_t);
template void matrix_mult_t<double, int16_t>(double **, double **, double **,
int16_t, int16_t, int16_t);
template void matrix_mult_t<double, int32_t>(double **, double **, double **,
int32_t, int32_t, int32_t);
template void matrix_mult_t<double, int64_t>(double **, double **, double **,
int64_t, int64_t, int64_t);
template float * vector_mult<float, int16_t>(float **, float *, int16_t, int16_t);
template float * vector_mult<float, int32_t>(float **, float *, int32_t, int32_t);
template float * vector_mult<float, int64_t>(float **, float *, int64_t, int64_t);
template double * vector_mult<double, int16_t>(double **, double *, int16_t, int16_t);
template double * vector_mult<double, int32_t>(double **, double *, int32_t, int32_t);
template double * vector_mult<double, int64_t>(double **, double *, int64_t, int64_t);
template float * left_vec_mult<float, int16_t>(float *, float **, int16_t, int16_t);
template float * left_vec_mult<float, int32_t>(float *, float **, int32_t, int32_t);
template float * left_vec_mult<float, int64_t>(float *, float **, int64_t, int64_t);
template double * left_vec_mult<double, int16_t>(double *, double **, int16_t, int16_t);
template double * left_vec_mult<double, int32_t>(double *, double **, int32_t, int32_t);
template double * left_vec_mult<double, int64_t>(double *, double **, int64_t, int64_t);
//fuck, half of this file is just gonna be template instantiations...
template float ** matrix_transpose<float, int16_t>(float **, int16_t, int16_t);
template float ** matrix_transpose<float, int32_t>(float **, int32_t, int32_t);
template float ** matrix_transpose<float, int64_t>(float **, int64_t, int64_t);
template double ** matrix_transpose<double, int16_t>(double **, int16_t, int16_t);
template double ** matrix_transpose<double, int32_t>(double **, int32_t, int32_t);
template double ** matrix_transpose<double, int64_t>(double **, int64_t, int64_t);
template float ** scan_matrix<float, int16_t>(FILE *fs, int16_t &, int16_t &);
template float ** scan_matrix<float, int32_t>(FILE *fs, int32_t &, int32_t &);
template float ** scan_matrix<float, int64_t>(FILE *fs, int64_t &, int64_t &);
template double ** scan_matrix<double, int16_t>(FILE *fs, int16_t, int16_t);
template double ** scan_matrix<double, int32_t>(FILE *fs, int32_t, int32_t);
template double ** scan_matrix<double, int64_t>(FILE *fs, int64_t, int64_t);
template float ** read_matrix<float, int16_t>(FILE *fs, int16_t &, int16_t &);
template float ** read_matrix<float, int32_t>(FILE *fs, int32_t &, int32_t &);
template float ** read_matrix<float, int64_t>(FILE *fs, int64_t &, int64_t &);
template double ** read_matrix<double, int16_t>(FILE *fs, int16_t, int16_t);
template double ** read_matrix<double, int32_t>(FILE *fs, int32_t, int32_t);
template double ** read_matrix<double, int64_t>(FILE *fs, int64_t, int64_t);
template void print_matrix<float, int16_t>(FILE *fs, float **, int16_t &, int16_t &);
template void print_matrix<float, int32_t>(FILE *fs, float **, int32_t &, int32_t &);
template void print_matrix<float, int64_t>(FILE *fs, int64_t &, int64_t &);
template void print_matrix<double, int16_t>(FILE *fs, double **, int16_t, int16_t);
template void print_matrix<double, int32_t>(FILE *fs, double **, int32_t, int32_t);
template void print_matrix<double, int64_t>(FILE *fs, double **, int64_t, int64_t);
template size_t write_matrix<float, int16_t>(FILE *fs, float **, int16_t &, int16_t &);
template size_t write_matrix<float, int32_t>(FILE *fs, float **, int32_t &, int32_t &);
template size_t write_matrix<float, int64_t>(FILE *fs, int64_t &, int64_t &);
template size_t write_matrix<double, int16_t>(FILE *fs, double **, int16_t, int16_t);
template size_t write_matrix<double, int32_t>(FILE *fs, double **, int32_t, int32_t);
template size_t write_matrix<double, int64_t>(FILE *fs, double **, int64_t, int64_t);
template float matrix_norm<float, int16_t>(float **, int16_t, int16_t);
template float matrix_norm<float, int32_t>(float **, int32_t, int32_t);
template float matrix_norm<float, int64_t>(float **, int64_t, int64_t);
template double matrix_norm<double, int16_t>(double **, int16_t, int16_t);
template double matrix_norm<double, int32_t>(double **, int32_t, int32_t);
template double matrix_norm<double, int64_t>(double **, int64_t, int64_t);