-
Notifications
You must be signed in to change notification settings - Fork 5
/
svdutil.c
654 lines (564 loc) · 18.4 KB
/
svdutil.c
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
/*
Copyright © 2002, University of Tennessee Research Foundation.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the University of Tennessee nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <math.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include "svdlib.h"
#include "svdutil.h"
#define BUNZIP2 "bzip2 -d"
#define BZIP2 "bzip2 -1"
#define UNZIP "gzip -d"
#define ZIP "gzip -1"
#define COMPRESS "compress"
#define MAX_FILENAME 512
#define MAX_PIPES 64
static FILE *Pipe[MAX_PIPES];
static int numPipes = 0;
long *svd_longArray(long size, char empty, char *name) {
long *a;
if (empty) a = (long *) calloc(size, sizeof(long));
else a = (long *) malloc(size * sizeof(long));
if (!a) {
perror(name);
/* exit(errno); */
}
return a;
}
double *svd_doubleArray(long size, char empty, char *name) {
double *a;
if (empty) a = (double *) calloc(size, sizeof(double));
else a = (double *) malloc(size * sizeof(double));
if (!a) {
perror(name);
/* exit(errno); */
}
return a;
}
void svd_beep(void) {
fputc('\a', stderr);
fflush(stderr);
}
void svd_debug(char *fmt, ...) {
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
}
void svd_error(char *fmt, ...) {
va_list args;
va_start(args, fmt);
svd_beep();
fprintf(stderr, "ERROR: ");
vfprintf(stderr, fmt, args);
fprintf(stderr, "\n");
va_end(args);
}
void svd_fatalError(char *fmt, ...) {
va_list args;
va_start(args, fmt);
svd_beep();
fprintf(stderr, "ERROR: ");
vfprintf(stderr, fmt, args);
fprintf(stderr, "\a\n");
va_end(args);
exit(1);
}
static void registerPipe(FILE *p) {
if (numPipes >= MAX_PIPES) svd_error("Too many pipes open");
Pipe[numPipes++] = p;
}
static char isPipe(FILE *p) {
int i;
for (i = 0; i < numPipes && Pipe[i] != p; i++);
if (i == numPipes) return FALSE;
Pipe[i] = Pipe[--numPipes];
return TRUE;
}
static FILE *openPipe(char *pipeName, char *mode) {
FILE *pipe;
fflush(stdout);
if ((pipe = popen(pipeName, mode))) registerPipe(pipe);
return pipe;
}
static FILE *readZippedFile(char *command, char *fileName) {
char buf[MAX_FILENAME];
sprintf(buf, "%s < %s 2>/dev/null", command, fileName);
return openPipe(buf, "r");
}
FILE *svd_fatalReadFile(char *filename) {
FILE *file;
if (!(file = svd_readFile(filename)))
svd_fatalError("couldn't read the file %s", filename);
return file;
}
static int stringEndsIn(char *s, char *t) {
int ls = strlen(s);
int lt = strlen(t);
if (ls < lt) return FALSE;
return (strcmp(s + ls - lt, t)) ? FALSE : TRUE;
}
/* Will silently return NULL if file couldn't be opened */
FILE *svd_readFile(char *fileName) {
char fileBuf[MAX_FILENAME];
struct stat statbuf;
/* Special file name */
if (!strcmp(fileName, "-"))
return stdin;
/* If it is a pipe */
if (fileName[0] == '|')
return openPipe(fileName + 1, "r");
/* Check if already ends in .gz or .Z and assume compressed */
if (stringEndsIn(fileName, ".gz") || stringEndsIn(fileName, ".Z")) {
if (!stat(fileName, &statbuf))
return readZippedFile(UNZIP, fileName);
return NULL;
}
/* Check if already ends in .bz or .bz2 and assume compressed */
if (stringEndsIn(fileName, ".bz") || stringEndsIn(fileName, ".bz2")) {
if (!stat(fileName, &statbuf))
return readZippedFile(BUNZIP2, fileName);
return NULL;
}
/* Try just opening normally */
if (!stat(fileName, &statbuf))
return fopen(fileName, "r");
/* Try adding .gz */
sprintf(fileBuf, "%s.gz", fileName);
if (!stat(fileBuf, &statbuf))
return readZippedFile(UNZIP, fileBuf);
/* Try adding .Z */
sprintf(fileBuf, "%s.Z", fileName);
if (!stat(fileBuf, &statbuf))
return readZippedFile(UNZIP, fileBuf);
/* Try adding .bz2 */
sprintf(fileBuf, "%s.bz2", fileName);
if (!stat(fileBuf, &statbuf))
return readZippedFile(BUNZIP2, fileBuf);
/* Try adding .bz */
sprintf(fileBuf, "%s.bz", fileName);
if (!stat(fileBuf, &statbuf))
return readZippedFile(BUNZIP2, fileBuf);
return NULL;
}
static FILE *writeZippedFile(char *fileName, char append) {
char buf[MAX_FILENAME];
const char *op = (append) ? ">>" : ">";
if (stringEndsIn(fileName, ".bz2") || stringEndsIn(fileName, ".bz"))
sprintf(buf, "%s %s \"%s\"", BZIP2, op, fileName);
else if (stringEndsIn(fileName, ".Z"))
sprintf(buf, "%s %s \"%s\"", COMPRESS, op, fileName);
else
sprintf(buf, "%s %s \"%s\"", ZIP, op, fileName);
return openPipe(buf, "w");
}
FILE *svd_writeFile(char *fileName, char append) {
/* Special file name */
if (!strcmp(fileName, "-"))
return stdout;
/* If it is a pipe */
if (fileName[0] == '|')
return openPipe(fileName + 1, "w");
/* Check if ends in .gz, .Z, .bz, .bz2 */
if (stringEndsIn(fileName, ".gz") || stringEndsIn(fileName, ".Z") ||
stringEndsIn(fileName, ".bz") || stringEndsIn(fileName, ".bz2"))
return writeZippedFile(fileName, append);
return (append) ? fopen(fileName, "a") : fopen(fileName, "w");
}
/* Could be a file or a stream. */
void svd_closeFile(FILE *file) {
if (file == stdin || file == stdout) return;
if (isPipe(file)) pclose(file);
else fclose(file);
}
char svd_readBinInt(FILE *file, int *val) {
int x;
if (fread(&x, sizeof(int), 1, file) == 1) {
*val = ntohl(x);
return FALSE;
}
return TRUE;
}
/* This reads a float in network order and converts to a real in host order. */
char svd_readBinFloat(FILE *file, float *val) {
int x;
float y;
if (fread(&x, sizeof(int), 1, file) == 1) {
x = ntohl(x);
y = *((float *) &x);
*val = y;
return FALSE;
}
return TRUE;
}
char svd_writeBinInt(FILE *file, int x) {
int y = htonl(x);
if (fwrite(&y, sizeof(int), 1, file) != 1) return TRUE;
return FALSE;
}
/* This takes a real in host order and writes a float in network order. */
char svd_writeBinFloat(FILE *file, float r) {
int y = htonl(*((int *) &r));
if (fwrite(&y, sizeof(int), 1, file) != 1) return TRUE;
return FALSE;
}
/**************************************************************
* returns |a| if b is positive; else fsign returns -|a| *
**************************************************************/
double svd_fsign(double a, double b) {
if ((a>=0.0 && b>=0.0) || (a<0.0 && b<0.0))return(a);
else return -a;
}
/**************************************************************
* returns the larger of two double precision numbers *
**************************************************************/
double svd_dmax(double a, double b) {
return (a > b) ? a : b;
}
/**************************************************************
* returns the smaller of two double precision numbers *
**************************************************************/
double svd_dmin(double a, double b) {
return (a < b) ? a : b;
}
/**************************************************************
* returns the larger of two integers *
**************************************************************/
long svd_imax(long a, long b) {
return (a > b) ? a : b;
}
/**************************************************************
* returns the smaller of two integers *
**************************************************************/
long svd_imin(long a, long b) {
return (a < b) ? a : b;
}
/**************************************************************
* Function scales a vector by a constant. *
* Based on Fortran-77 routine from Linpack by J. Dongarra *
**************************************************************/
void svd_dscal(long n, double da, double *dx, long incx) {
long i;
if (n <= 0 || incx == 0) return;
if (incx < 0) dx += (-n+1) * incx;
for (i=0; i < n; i++) {
*dx *= da;
dx += incx;
}
return;
}
/**************************************************************
* function scales a vector by a constant. *
* Based on Fortran-77 routine from Linpack by J. Dongarra *
**************************************************************/
void svd_datx(long n, double da, double *dx, long incx, double *dy, long incy) {
long i;
if (n <= 0 || incx == 0 || incy == 0 || da == 0.0) return;
if (incx == 1 && incy == 1)
for (i=0; i < n; i++) *dy++ = da * (*dx++);
else {
if (incx < 0) dx += (-n+1) * incx;
if (incy < 0) dy += (-n+1) * incy;
for (i=0; i < n; i++) {
*dy = da * (*dx);
dx += incx;
dy += incy;
}
}
return;
}
/**************************************************************
* Function copies a vector x to a vector y *
* Based on Fortran-77 routine from Linpack by J. Dongarra *
**************************************************************/
void svd_dcopy(long n, double *dx, long incx, double *dy, long incy) {
long i;
if (n <= 0 || incx == 0 || incy == 0) return;
if (incx == 1 && incy == 1)
for (i=0; i < n; i++) *dy++ = *dx++;
else {
if (incx < 0) dx += (-n+1) * incx;
if (incy < 0) dy += (-n+1) * incy;
for (i=0; i < n; i++) {
*dy = *dx;
dx += incx;
dy += incy;
}
}
return;
}
/**************************************************************
* Function forms the dot product of two vectors. *
* Based on Fortran-77 routine from Linpack by J. Dongarra *
**************************************************************/
double svd_ddot(long n, double *dx, long incx, double *dy, long incy) {
long i;
double dot_product;
if (n <= 0 || incx == 0 || incy == 0) return(0.0);
dot_product = 0.0;
if (incx == 1 && incy == 1)
for (i=0; i < n; i++) dot_product += (*dx++) * (*dy++);
else {
if (incx < 0) dx += (-n+1) * incx;
if (incy < 0) dy += (-n+1) * incy;
for (i=0; i < n; i++) {
dot_product += (*dx) * (*dy);
dx += incx;
dy += incy;
}
}
return(dot_product);
}
/**************************************************************
* Constant times a vector plus a vector *
* Based on Fortran-77 routine from Linpack by J. Dongarra *
**************************************************************/
void svd_daxpy (long n, double da, double *dx, long incx, double *dy, long incy) {
long i;
if (n <= 0 || incx == 0 || incy == 0 || da == 0.0) return;
if (incx == 1 && incy == 1)
for (i=0; i < n; i++) {
*dy += da * (*dx++);
dy++;
}
else {
if (incx < 0) dx += (-n+1) * incx;
if (incy < 0) dy += (-n+1) * incy;
for (i=0; i < n; i++) {
*dy += da * (*dx);
dx += incx;
dy += incy;
}
}
return;
}
/*********************************************************************
* Function sorts array1 and array2 into increasing order for array1 *
*********************************************************************/
void svd_dsort2(long igap, long n, double *array1, double *array2) {
double temp;
long i, j, index;
if (!igap) return;
else {
for (i = igap; i < n; i++) {
j = i - igap;
index = i;
while (j >= 0 && array1[j] > array1[index]) {
temp = array1[j];
array1[j] = array1[index];
array1[index] = temp;
temp = array2[j];
array2[j] = array2[index];
array2[index] = temp;
j -= igap;
index = j + igap;
}
}
}
svd_dsort2(igap/2,n,array1,array2);
}
/**************************************************************
* Function interchanges two vectors *
* Based on Fortran-77 routine from Linpack by J. Dongarra *
**************************************************************/
void svd_dswap(long n, double *dx, long incx, double *dy, long incy) {
long i;
double dtemp;
if (n <= 0 || incx == 0 || incy == 0) return;
if (incx == 1 && incy == 1) {
for (i=0; i < n; i++) {
dtemp = *dy;
*dy++ = *dx;
*dx++ = dtemp;
}
}
else {
if (incx < 0) dx += (-n+1) * incx;
if (incy < 0) dy += (-n+1) * incy;
for (i=0; i < n; i++) {
dtemp = *dy;
*dy = *dx;
*dx = dtemp;
dx += incx;
dy += incy;
}
}
}
/*****************************************************************
* Function finds the index of element having max. absolute value*
* based on FORTRAN 77 routine from Linpack by J. Dongarra *
*****************************************************************/
long svd_idamax(long n, double *dx, long incx) {
long ix,i,imax;
double dtemp, dmax;
if (n < 1) return(-1);
if (n == 1) return(0);
if (incx == 0) return(-1);
if (incx < 0) ix = (-n+1) * incx;
else ix = 0;
imax = ix;
dx += ix;
dmax = fabs(*dx);
for (i=1; i < n; i++) {
ix += incx;
dx += incx;
dtemp = fabs(*dx);
if (dtemp > dmax) {
dmax = dtemp;
imax = ix;
}
}
return(imax);
}
/**************************************************************
* multiplication of matrix B by vector x, where B = A'A, *
* and A is nrow by ncol (nrow >> ncol). Hence, B is of order *
* n = ncol (y stores product vector). *
**************************************************************/
void svd_opb(SMat A, double *x, double *y, double *temp) {
long i, j, end;
long *pointr = A->pointr, *rowind = A->rowind;
double *value = A->value;
long n = A->cols;
SVDCount[SVD_MXV] += 2;
memset(y, 0, n * sizeof(double));
for (i = 0; i < A->rows; i++) temp[i] = 0.0;
for (i = 0; i < A->cols; i++) {
end = pointr[i+1];
for (j = pointr[i]; j < end; j++)
temp[rowind[j]] += value[j] * (*x);
x++;
}
for (i = 0; i < A->cols; i++) {
end = pointr[i+1];
for (j = pointr[i]; j < end; j++)
*y += value[j] * temp[rowind[j]];
y++;
}
return;
}
/***********************************************************
* multiplication of matrix A by vector x, where A is *
* nrow by ncol (nrow >> ncol). y stores product vector. *
***********************************************************/
void svd_opa(SMat A, double *x, double *y) {
long end, i, j;
long *pointr = A->pointr, *rowind = A->rowind;
double *value = A->value;
SVDCount[SVD_MXV]++;
memset(y, 0, A->rows * sizeof(double));
for (i = 0; i < A->cols; i++) {
end = pointr[i+1];
for (j = pointr[i]; j < end; j++)
y[rowind[j]] += value[j] * x[i];
}
return;
}
/***********************************************************************
* *
* random() *
* (double precision) *
***********************************************************************/
/***********************************************************************
Description
-----------
This is a translation of a Fortran-77 uniform random number
generator. The code is based on theory and suggestions given in
D. E. Knuth (1969), vol 2. The argument to the function should
be initialized to an arbitrary integer prior to the first call to
random. The calling program should not alter the value of the
argument between subsequent calls to random. Random returns values
within the interval (0,1).
Arguments
---------
(input)
iy an integer seed whose value must not be altered by the caller
between subsequent calls
(output)
random a double precision random number between (0,1)
***********************************************************************/
double svd_random2(long *iy) {
static long m2 = 0;
static long ia, ic, mic;
static double halfm, s;
/* If first entry, compute (max int) / 2 */
if (!m2) {
m2 = 1 << (8 * (int)sizeof(int) - 2);
halfm = m2;
/* compute multiplier and increment for linear congruential
* method */
ia = 8 * (long)(halfm * atan(1.0) / 8.0) + 5;
ic = 2 * (long)(halfm * (0.5 - sqrt(3.0)/6.0)) + 1;
mic = (m2-ic) + m2;
/* s is the scale factor for converting to floating point */
s = 0.5 / halfm;
}
/* compute next random number */
*iy = *iy * ia;
/* for computers which do not allow integer overflow on addition */
if (*iy > mic) *iy = (*iy - m2) - m2;
*iy = *iy + ic;
/* for computers whose word length for addition is greater than
* for multiplication */
if (*iy / 2 > m2) *iy = (*iy - m2) - m2;
/* for computers whose integer overflow affects the sign bit */
if (*iy < 0) *iy = (*iy + m2) + m2;
return((double)(*iy) * s);
}
/**************************************************************
* *
* Function finds sqrt(a^2 + b^2) without overflow or *
* destructive underflow. *
* *
**************************************************************/
/**************************************************************
Funtions used
-------------
UTILITY dmax, dmin
**************************************************************/
double svd_pythag(double a, double b) {
double p, r, s, t, u, temp;
p = svd_dmax(fabs(a), fabs(b));
if (p != 0.0) {
temp = svd_dmin(fabs(a), fabs(b)) / p;
r = temp * temp;
t = 4.0 + r;
while (t != 4.0) {
s = r / t;
u = 1.0 + 2.0 * s;
p *= u;
temp = s / u;
r *= temp * temp;
t = 4.0 + r;
}
}
return(p);
}