line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#include |
2
|
|
|
|
|
|
|
/* |
3
|
|
|
|
|
|
|
Copyright © 2002, University of Tennessee Research Foundation. |
4
|
|
|
|
|
|
|
All rights reserved. |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without |
7
|
|
|
|
|
|
|
modification, are permitted provided that the following conditions are met: |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright notice, this |
10
|
|
|
|
|
|
|
list of conditions and the following disclaimer. |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
Redistributions in binary form must reproduce the above copyright notice, |
13
|
|
|
|
|
|
|
this list of conditions and the following disclaimer in the documentation |
14
|
|
|
|
|
|
|
and/or other materials provided with the distribution. |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
* Neither the name of the University of Tennessee nor the names of its |
17
|
|
|
|
|
|
|
contributors may be used to endorse or promote products derived from this |
18
|
|
|
|
|
|
|
software without specific prior written permission. |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
21
|
|
|
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
22
|
|
|
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
23
|
|
|
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
24
|
|
|
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
25
|
|
|
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
26
|
|
|
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
27
|
|
|
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
28
|
|
|
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
29
|
|
|
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
30
|
|
|
|
|
|
|
POSSIBILITY OF SUCH DAMAGE. |
31
|
|
|
|
|
|
|
*/ |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
#include |
34
|
|
|
|
|
|
|
#include |
35
|
|
|
|
|
|
|
#include "svdlib.h" |
36
|
|
|
|
|
|
|
#include "svdutil.h" |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
char *SVDVersion = "1.4"; |
39
|
|
|
|
|
|
|
__SVDLIBC_LONG SVDVerbosity = 1; |
40
|
|
|
|
|
|
|
__SVDLIBC_LONG SVDCount[SVD_COUNTERS]; |
41
|
|
|
|
|
|
|
|
42
|
10
|
|
|
|
|
|
void svdResetCounters(void) { |
43
|
|
|
|
|
|
|
int i; |
44
|
20
|
100
|
|
|
|
|
for (i = 0; i < SVD_COUNTERS; i++) |
45
|
10
|
|
|
|
|
|
SVDCount[i] = 0; |
46
|
10
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
/********************************* Allocation ********************************/ |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
/* Row major order. Rows are vectors that are consecutive in memory. Matrix |
51
|
|
|
|
|
|
|
is initialized to empty. */ |
52
|
20
|
|
|
|
|
|
DMat svdNewDMat(int rows, int cols) { |
53
|
|
|
|
|
|
|
int i; |
54
|
20
|
|
|
|
|
|
DMat D = (DMat) malloc(sizeof(struct dmat)); |
55
|
20
|
50
|
|
|
|
|
if (!D) {perror("svdNewDMat"); return NULL;} |
56
|
20
|
|
|
|
|
|
D->rows = rows; |
57
|
20
|
|
|
|
|
|
D->cols = cols; |
58
|
|
|
|
|
|
|
|
59
|
20
|
|
|
|
|
|
D->value = (double **) malloc(rows * sizeof(double *)); |
60
|
20
|
50
|
|
|
|
|
if (!D->value) {SAFE_FREE(D); return NULL;} |
|
|
0
|
|
|
|
|
|
61
|
|
|
|
|
|
|
|
62
|
20
|
|
|
|
|
|
D->value[0] = (double *) calloc(rows * cols, sizeof(double)); |
63
|
20
|
50
|
|
|
|
|
if (!D->value[0]) {SAFE_FREE(D->value); SAFE_FREE(D); return NULL;} |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
64
|
|
|
|
|
|
|
|
65
|
114
|
100
|
|
|
|
|
for (i = 1; i < rows; i++) D->value[i] = D->value[i-1] + cols; |
66
|
20
|
|
|
|
|
|
return D; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
20
|
|
|
|
|
|
void svdFreeDMat(DMat D) { |
70
|
20
|
50
|
|
|
|
|
if (!D) return; |
71
|
20
|
50
|
|
|
|
|
SAFE_FREE(D->value[0]); |
72
|
20
|
50
|
|
|
|
|
SAFE_FREE(D->value); |
73
|
20
|
|
|
|
|
|
free(D); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
|
77
|
3
|
|
|
|
|
|
SMat svdNewSMat(int rows, int cols, int vals) { |
78
|
3
|
|
|
|
|
|
SMat S = (SMat) calloc(1, sizeof(struct smat)); |
79
|
3
|
50
|
|
|
|
|
if (!S) {perror("svdNewSMat"); return NULL;} |
80
|
3
|
|
|
|
|
|
S->rows = rows; |
81
|
3
|
|
|
|
|
|
S->cols = cols; |
82
|
3
|
|
|
|
|
|
S->vals = vals; |
83
|
3
|
|
|
|
|
|
S->pointr = svd_longArray(cols + 1, TRUE, "svdNewSMat: pointr"); |
84
|
3
|
50
|
|
|
|
|
if (!S->pointr) {svdFreeSMat(S); return NULL;} |
85
|
3
|
|
|
|
|
|
S->rowind = svd_longArray(vals, FALSE, "svdNewSMat: rowind"); |
86
|
3
|
50
|
|
|
|
|
if (!S->rowind) {svdFreeSMat(S); return NULL;} |
87
|
3
|
|
|
|
|
|
S->value = svd_doubleArray(vals, FALSE, "svdNewSMat: value"); |
88
|
3
|
50
|
|
|
|
|
if (!S->value) {svdFreeSMat(S); return NULL;} |
89
|
3
|
|
|
|
|
|
return S; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
3
|
|
|
|
|
|
void svdFreeSMat(SMat S) { |
93
|
3
|
50
|
|
|
|
|
if (!S) return; |
94
|
3
|
50
|
|
|
|
|
SAFE_FREE(S->pointr); |
95
|
3
|
50
|
|
|
|
|
SAFE_FREE(S->rowind); |
96
|
3
|
50
|
|
|
|
|
SAFE_FREE(S->value); |
97
|
3
|
|
|
|
|
|
free(S); |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
/* Creates an empty SVD record */ |
102
|
10
|
|
|
|
|
|
SVDRec svdNewSVDRec(void) { |
103
|
10
|
|
|
|
|
|
SVDRec R = (SVDRec) calloc(1, sizeof(struct svdrec)); |
104
|
10
|
50
|
|
|
|
|
if (!R) {perror("svdNewSVDRec"); return NULL;} |
105
|
10
|
|
|
|
|
|
return R; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
/* Frees an svd rec and all its contents. */ |
109
|
10
|
|
|
|
|
|
void svdFreeSVDRec(SVDRec R) { |
110
|
10
|
50
|
|
|
|
|
if (!R) return; |
111
|
10
|
50
|
|
|
|
|
if (R->Ut) svdFreeDMat(R->Ut); |
112
|
10
|
50
|
|
|
|
|
if (R->S) SAFE_FREE(R->S); |
|
|
50
|
|
|
|
|
|
113
|
10
|
50
|
|
|
|
|
if (R->Vt) svdFreeDMat(R->Vt); |
114
|
10
|
|
|
|
|
|
free(R); |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
/**************************** Conversion *************************************/ |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
/* Converts a sparse matrix to a dense one (without affecting the former) */ |
121
|
0
|
|
|
|
|
|
DMat svdConvertStoD(SMat S) { |
122
|
|
|
|
|
|
|
int i, c; |
123
|
0
|
|
|
|
|
|
DMat D = svdNewDMat(S->rows, S->cols); |
124
|
0
|
0
|
|
|
|
|
if (!D) { |
125
|
0
|
|
|
|
|
|
svd_error("svdConvertStoD: failed to allocate D"); |
126
|
0
|
|
|
|
|
|
return NULL; |
127
|
|
|
|
|
|
|
} |
128
|
0
|
0
|
|
|
|
|
for (i = 0, c = 0; i < S->vals; i++) { |
129
|
0
|
0
|
|
|
|
|
while (S->pointr[c + 1] <= i) c++; |
130
|
0
|
|
|
|
|
|
D->value[S->rowind[i]][c] = S->value[i]; |
131
|
|
|
|
|
|
|
} |
132
|
0
|
|
|
|
|
|
return D; |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
/* Converts a dense matrix to a sparse one (without affecting the dense one) */ |
136
|
3
|
|
|
|
|
|
SMat svdConvertDtoS(DMat D) { |
137
|
|
|
|
|
|
|
SMat S; |
138
|
|
|
|
|
|
|
int i, j, n; |
139
|
21
|
100
|
|
|
|
|
for (i = 0, n = 0; i < D->rows; i++) |
140
|
144
|
100
|
|
|
|
|
for (j = 0; j < D->cols; j++) |
141
|
126
|
100
|
|
|
|
|
if (D->value[i][j] != 0) n++; |
142
|
|
|
|
|
|
|
|
143
|
3
|
|
|
|
|
|
S = svdNewSMat(D->rows, D->cols, n); |
144
|
3
|
50
|
|
|
|
|
if (!S) { |
145
|
0
|
|
|
|
|
|
svd_error("svdConvertDtoS: failed to allocate S"); |
146
|
0
|
|
|
|
|
|
return NULL; |
147
|
|
|
|
|
|
|
} |
148
|
24
|
100
|
|
|
|
|
for (j = 0, n = 0; j < D->cols; j++) { |
149
|
21
|
|
|
|
|
|
S->pointr[j] = n; |
150
|
147
|
100
|
|
|
|
|
for (i = 0; i < D->rows; i++) |
151
|
126
|
100
|
|
|
|
|
if (D->value[i][j] != 0) { |
152
|
66
|
|
|
|
|
|
S->rowind[n] = i; |
153
|
66
|
|
|
|
|
|
S->value[n] = D->value[i][j]; |
154
|
66
|
|
|
|
|
|
n++; |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
} |
157
|
3
|
|
|
|
|
|
S->pointr[S->cols] = S->vals; |
158
|
3
|
|
|
|
|
|
return S; |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
/* Transposes a dense matrix. */ |
162
|
0
|
|
|
|
|
|
DMat svdTransposeD(DMat D) { |
163
|
|
|
|
|
|
|
int r, c; |
164
|
0
|
|
|
|
|
|
DMat N = svdNewDMat(D->cols, D->rows); |
165
|
0
|
0
|
|
|
|
|
for (r = 0; r < D->rows; r++) |
166
|
0
|
0
|
|
|
|
|
for (c = 0; c < D->cols; c++) |
167
|
0
|
|
|
|
|
|
N->value[c][r] = D->value[r][c]; |
168
|
0
|
|
|
|
|
|
return N; |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
/* Efficiently transposes a sparse matrix. */ |
172
|
0
|
|
|
|
|
|
SMat svdTransposeS(SMat S) { |
173
|
|
|
|
|
|
|
int r, c, i, j; |
174
|
0
|
|
|
|
|
|
SMat N = svdNewSMat(S->cols, S->rows, S->vals); |
175
|
|
|
|
|
|
|
/* Count number nz in each row. */ |
176
|
0
|
0
|
|
|
|
|
for (i = 0; i < S->vals; i++) |
177
|
0
|
|
|
|
|
|
N->pointr[S->rowind[i]]++; |
178
|
|
|
|
|
|
|
/* Fill each cell with the starting point of the previous row. */ |
179
|
0
|
|
|
|
|
|
N->pointr[S->rows] = S->vals - N->pointr[S->rows - 1]; |
180
|
0
|
0
|
|
|
|
|
for (r = S->rows - 1; r > 0; r--) |
181
|
0
|
|
|
|
|
|
N->pointr[r] = N->pointr[r+1] - N->pointr[r-1]; |
182
|
0
|
|
|
|
|
|
N->pointr[0] = 0; |
183
|
|
|
|
|
|
|
/* Assign the new columns and values. */ |
184
|
0
|
0
|
|
|
|
|
for (c = 0, i = 0; c < S->cols; c++) { |
185
|
0
|
0
|
|
|
|
|
for (; i < S->pointr[c+1]; i++) { |
186
|
0
|
|
|
|
|
|
r = S->rowind[i]; |
187
|
0
|
|
|
|
|
|
j = N->pointr[r+1]++; |
188
|
0
|
|
|
|
|
|
N->rowind[j] = c; |
189
|
0
|
|
|
|
|
|
N->value[j] = S->value[i]; |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
} |
192
|
0
|
|
|
|
|
|
return N; |
193
|
|
|
|
|
|
|
} |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
/**************************** Input/Output ***********************************/ |
197
|
|
|
|
|
|
|
|
198
|
0
|
|
|
|
|
|
void svdWriteDenseArray(double *a, int n, char *filename, char binary) { |
199
|
|
|
|
|
|
|
int i; |
200
|
0
|
|
|
|
|
|
FILE *file = svd_writeFile(filename, FALSE); |
201
|
0
|
0
|
|
|
|
|
if (!file) |
202
|
0
|
|
|
|
|
|
return svd_error("svdWriteDenseArray: failed to write %s", filename); |
203
|
0
|
0
|
|
|
|
|
if (binary) { |
204
|
0
|
|
|
|
|
|
svd_writeBinInt(file, n); |
205
|
0
|
0
|
|
|
|
|
for (i = 0; i < n; i++) |
206
|
0
|
|
|
|
|
|
svd_writeBinFloat(file, (float) a[i]); |
207
|
|
|
|
|
|
|
} else { |
208
|
0
|
|
|
|
|
|
fprintf(file, "%d\n", n); |
209
|
0
|
0
|
|
|
|
|
for (i = 0; i < n; i++) |
210
|
0
|
|
|
|
|
|
fprintf(file, "%g\n", a[i]); |
211
|
|
|
|
|
|
|
} |
212
|
0
|
|
|
|
|
|
svd_closeFile(file); |
213
|
|
|
|
|
|
|
} |
214
|
|
|
|
|
|
|
|
215
|
0
|
|
|
|
|
|
double *svdLoadDenseArray(char *filename, int *np, char binary) { |
216
|
|
|
|
|
|
|
int i, n; |
217
|
|
|
|
|
|
|
double *a; |
218
|
|
|
|
|
|
|
|
219
|
0
|
|
|
|
|
|
FILE *file = svd_readFile(filename); |
220
|
0
|
0
|
|
|
|
|
if (!file) { |
221
|
0
|
|
|
|
|
|
svd_error("svdLoadDenseArray: failed to read %s", filename); |
222
|
0
|
|
|
|
|
|
return NULL; |
223
|
|
|
|
|
|
|
} |
224
|
0
|
0
|
|
|
|
|
if (binary) { |
225
|
0
|
|
|
|
|
|
svd_readBinInt(file, np); |
226
|
0
|
0
|
|
|
|
|
} else if (fscanf(file, " %d", np) != 1) { |
227
|
0
|
|
|
|
|
|
svd_error("svdLoadDenseArray: error reading %s", filename); |
228
|
0
|
|
|
|
|
|
svd_closeFile(file); |
229
|
0
|
|
|
|
|
|
return NULL; |
230
|
|
|
|
|
|
|
} |
231
|
0
|
|
|
|
|
|
n = *np; |
232
|
0
|
|
|
|
|
|
a = svd_doubleArray(n, FALSE, "svdLoadDenseArray: a"); |
233
|
0
|
0
|
|
|
|
|
if (!a) return NULL; |
234
|
0
|
0
|
|
|
|
|
if (binary) { |
235
|
|
|
|
|
|
|
float f; |
236
|
0
|
0
|
|
|
|
|
for (i = 0; i < n; i++) { |
237
|
0
|
|
|
|
|
|
svd_readBinFloat(file, &f); |
238
|
0
|
|
|
|
|
|
a[i] = f; |
239
|
|
|
|
|
|
|
} |
240
|
|
|
|
|
|
|
} else { |
241
|
0
|
0
|
|
|
|
|
for (i = 0; i < n; i++) { |
242
|
0
|
0
|
|
|
|
|
if (fscanf(file, " %lf\n", a + i) != 1) { |
243
|
0
|
|
|
|
|
|
svd_error("svdLoadDenseArray: error reading %s", filename); |
244
|
0
|
|
|
|
|
|
break; |
245
|
|
|
|
|
|
|
} |
246
|
|
|
|
|
|
|
} |
247
|
|
|
|
|
|
|
} |
248
|
0
|
|
|
|
|
|
svd_closeFile(file); |
249
|
0
|
|
|
|
|
|
return a; |
250
|
|
|
|
|
|
|
} |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
/* File format has a funny header, then first entry index per column, then the |
254
|
|
|
|
|
|
|
row for each entry, then the value for each entry. Indices count from 1. |
255
|
|
|
|
|
|
|
Assumes A is initialized. */ |
256
|
0
|
|
|
|
|
|
static SMat svdLoadSparseTextHBFile(FILE *file) { |
257
|
|
|
|
|
|
|
char line[128]; |
258
|
|
|
|
|
|
|
__SVDLIBC_LONG i, x, rows, cols, vals, num_mat; |
259
|
|
|
|
|
|
|
SMat S; |
260
|
|
|
|
|
|
|
/* Skip the header line: */ |
261
|
0
|
|
|
|
|
|
if (!fgets(line, 128, file)); |
262
|
|
|
|
|
|
|
/* Skip the line giving the number of lines in this file: */ |
263
|
0
|
|
|
|
|
|
if (!fgets(line, 128, file)); |
264
|
|
|
|
|
|
|
/* Read the line with useful dimensions: */ |
265
|
0
|
0
|
|
|
|
|
if (fscanf(file, "%*s%ld%ld%ld%ld\n", |
266
|
|
|
|
|
|
|
&rows, &cols, &vals, &num_mat) != 4) { |
267
|
0
|
|
|
|
|
|
svd_error("svdLoadSparseTextHBFile: bad file format on line 3"); |
268
|
0
|
|
|
|
|
|
return NULL; |
269
|
|
|
|
|
|
|
} |
270
|
0
|
0
|
|
|
|
|
if (num_mat != 0) { |
271
|
0
|
|
|
|
|
|
svd_error("svdLoadSparseTextHBFile: I don't know how to handle a file " |
272
|
|
|
|
|
|
|
"with elemental matrices (last entry on header line 3)"); |
273
|
0
|
|
|
|
|
|
return NULL; |
274
|
|
|
|
|
|
|
} |
275
|
|
|
|
|
|
|
/* Skip the line giving the formats: */ |
276
|
0
|
|
|
|
|
|
if (!fgets(line, 128, file)); |
277
|
|
|
|
|
|
|
|
278
|
0
|
|
|
|
|
|
S = svdNewSMat(rows, cols, vals); |
279
|
0
|
0
|
|
|
|
|
if (!S) return NULL; |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
/* Read column pointers. */ |
282
|
0
|
0
|
|
|
|
|
for (i = 0; i <= S->cols; i++) { |
283
|
0
|
0
|
|
|
|
|
if (fscanf(file, " %ld", &x) != 1) { |
284
|
0
|
|
|
|
|
|
svd_error("svdLoadSparseTextHBFile: error reading pointr %d", i); |
285
|
0
|
|
|
|
|
|
return NULL; |
286
|
|
|
|
|
|
|
} |
287
|
0
|
|
|
|
|
|
S->pointr[i] = x - 1; |
288
|
|
|
|
|
|
|
} |
289
|
0
|
|
|
|
|
|
S->pointr[S->cols] = S->vals; |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
/* Read row indices. */ |
292
|
0
|
0
|
|
|
|
|
for (i = 0; i < S->vals; i++) { |
293
|
0
|
0
|
|
|
|
|
if (fscanf(file, " %ld", &x) != 1) { |
294
|
0
|
|
|
|
|
|
svd_error("svdLoadSparseTextHBFile: error reading rowind %d", i); |
295
|
0
|
|
|
|
|
|
return NULL; |
296
|
|
|
|
|
|
|
} |
297
|
0
|
|
|
|
|
|
S->rowind[i] = x - 1; |
298
|
|
|
|
|
|
|
} |
299
|
0
|
0
|
|
|
|
|
for (i = 0; i < S->vals; i++) |
300
|
0
|
0
|
|
|
|
|
if (fscanf(file, " %lf", S->value + i) != 1) { |
301
|
0
|
|
|
|
|
|
svd_error("svdLoadSparseTextHBFile: error reading value %d", i); |
302
|
0
|
|
|
|
|
|
return NULL; |
303
|
|
|
|
|
|
|
} |
304
|
0
|
|
|
|
|
|
return S; |
305
|
|
|
|
|
|
|
} |
306
|
|
|
|
|
|
|
|
307
|
0
|
|
|
|
|
|
static void svdWriteSparseTextHBFile(SMat S, FILE *file) { |
308
|
|
|
|
|
|
|
int i; |
309
|
0
|
0
|
|
|
|
|
__SVDLIBC_LONG col_lines = ((S->cols + 1) / 8) + (((S->cols + 1) % 8) ? 1 : 0); |
310
|
0
|
0
|
|
|
|
|
__SVDLIBC_LONG row_lines = (S->vals / 8) + ((S->vals % 8) ? 1 : 0); |
311
|
0
|
|
|
|
|
|
__SVDLIBC_LONG total_lines = col_lines + 2 * row_lines; |
312
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
char title[32]; |
314
|
0
|
|
|
|
|
|
sprintf(title, "SVDLIBC v. %s", SVDVersion); |
315
|
0
|
|
|
|
|
|
fprintf(file, "%-72s%-8s\n", title, ""); |
316
|
0
|
|
|
|
|
|
fprintf(file, "%14ld%14ld%14ld%14ld%14d\n", total_lines, col_lines, |
317
|
|
|
|
|
|
|
row_lines, row_lines, 0); |
318
|
0
|
|
|
|
|
|
fprintf(file, "%-14s%14ld%14ld%14ld%14d\n", "rra", S->rows, S->cols, |
319
|
|
|
|
|
|
|
S->vals, 0); |
320
|
0
|
|
|
|
|
|
fprintf(file, "%16s%16s%16s%16s\n", "(8i)", "(8i)", "(8e)", "(8e)"); |
321
|
|
|
|
|
|
|
|
322
|
0
|
0
|
|
|
|
|
for (i = 0; i <= S->cols; i++) |
323
|
0
|
0
|
|
|
|
|
fprintf(file, "%ld%s", S->pointr[i] + 1, (((i+1) % 8) == 0) ? "\n" : " "); |
324
|
0
|
|
|
|
|
|
fprintf(file, "\n"); |
325
|
0
|
0
|
|
|
|
|
for (i = 0; i < S->vals; i++) |
326
|
0
|
0
|
|
|
|
|
fprintf(file, "%ld%s", S->rowind[i] + 1, (((i+1) % 8) == 0) ? "\n" : " "); |
327
|
0
|
|
|
|
|
|
fprintf(file, "\n"); |
328
|
0
|
0
|
|
|
|
|
for (i = 0; i < S->vals; i++) |
329
|
0
|
0
|
|
|
|
|
fprintf(file, "%g%s", S->value[i], (((i+1) % 8) == 0) ? "\n" : " "); |
330
|
0
|
|
|
|
|
|
fprintf(file, "\n"); |
331
|
0
|
|
|
|
|
|
} |
332
|
|
|
|
|
|
|
|
333
|
|
|
|
|
|
|
|
334
|
0
|
|
|
|
|
|
static SMat svdLoadSparseTextFile(FILE *file) { |
335
|
|
|
|
|
|
|
__SVDLIBC_LONG c, i, n, v, rows, cols, vals; |
336
|
|
|
|
|
|
|
SMat S; |
337
|
0
|
0
|
|
|
|
|
if (fscanf(file, " %ld %ld %ld", &rows, &cols, &vals) != 3) { |
338
|
0
|
|
|
|
|
|
svd_error("svdLoadSparseTextFile: bad file format"); |
339
|
0
|
|
|
|
|
|
return NULL; |
340
|
|
|
|
|
|
|
} |
341
|
|
|
|
|
|
|
|
342
|
0
|
|
|
|
|
|
S = svdNewSMat(rows, cols, vals); |
343
|
0
|
0
|
|
|
|
|
if (!S) return NULL; |
344
|
|
|
|
|
|
|
|
345
|
0
|
0
|
|
|
|
|
for (c = 0, v = 0; c < cols; c++) { |
346
|
0
|
0
|
|
|
|
|
if (fscanf(file, " %ld", &n) != 1) { |
347
|
0
|
|
|
|
|
|
svd_error("svdLoadSparseTextFile: bad file format"); |
348
|
0
|
|
|
|
|
|
return NULL; |
349
|
|
|
|
|
|
|
} |
350
|
0
|
|
|
|
|
|
S->pointr[c] = v; |
351
|
0
|
0
|
|
|
|
|
for (i = 0; i < n; i++, v++) { |
352
|
0
|
0
|
|
|
|
|
if (fscanf(file, " %ld %lf", S->rowind + v, S->value + v) != 2) { |
353
|
0
|
|
|
|
|
|
svd_error("svdLoadSparseTextFile: bad file format"); |
354
|
0
|
|
|
|
|
|
return NULL; |
355
|
|
|
|
|
|
|
} |
356
|
|
|
|
|
|
|
} |
357
|
|
|
|
|
|
|
} |
358
|
0
|
|
|
|
|
|
S->pointr[cols] = vals; |
359
|
0
|
|
|
|
|
|
return S; |
360
|
|
|
|
|
|
|
} |
361
|
|
|
|
|
|
|
|
362
|
0
|
|
|
|
|
|
static void svdWriteSparseTextFile(SMat S, FILE *file) { |
363
|
|
|
|
|
|
|
int c, v; |
364
|
0
|
|
|
|
|
|
fprintf(file, "%ld %ld %ld\n", S->rows, S->cols, S->vals); |
365
|
0
|
0
|
|
|
|
|
for (c = 0, v = 0; c < S->cols; c++) { |
366
|
0
|
|
|
|
|
|
fprintf(file, "%ld\n", S->pointr[c + 1] - S->pointr[c]); |
367
|
0
|
0
|
|
|
|
|
for (; v < S->pointr[c+1]; v++) |
368
|
0
|
|
|
|
|
|
fprintf(file, "%ld %g\n", S->rowind[v], S->value[v]); |
369
|
|
|
|
|
|
|
} |
370
|
0
|
|
|
|
|
|
} |
371
|
|
|
|
|
|
|
|
372
|
|
|
|
|
|
|
|
373
|
0
|
|
|
|
|
|
static SMat svdLoadSparseBinaryFile(FILE *file) { |
374
|
0
|
|
|
|
|
|
int rows, cols, vals, n, c, i, v, r, e = 0; |
375
|
|
|
|
|
|
|
float f; |
376
|
|
|
|
|
|
|
SMat S; |
377
|
0
|
|
|
|
|
|
e += svd_readBinInt(file, &rows); |
378
|
0
|
|
|
|
|
|
e += svd_readBinInt(file, &cols); |
379
|
0
|
|
|
|
|
|
e += svd_readBinInt(file, &vals); |
380
|
0
|
0
|
|
|
|
|
if (e) { |
381
|
0
|
|
|
|
|
|
svd_error("svdLoadSparseBinaryFile: bad file format"); |
382
|
0
|
|
|
|
|
|
return NULL; |
383
|
|
|
|
|
|
|
} |
384
|
|
|
|
|
|
|
|
385
|
0
|
|
|
|
|
|
S = svdNewSMat(rows, cols, vals); |
386
|
0
|
0
|
|
|
|
|
if (!S) return NULL; |
387
|
|
|
|
|
|
|
|
388
|
0
|
0
|
|
|
|
|
for (c = 0, v = 0; c < cols; c++) { |
389
|
0
|
0
|
|
|
|
|
if (svd_readBinInt(file, &n)) { |
390
|
0
|
|
|
|
|
|
svd_error("svdLoadSparseBinaryFile: bad file format"); |
391
|
0
|
|
|
|
|
|
return NULL; |
392
|
|
|
|
|
|
|
} |
393
|
0
|
|
|
|
|
|
S->pointr[c] = v; |
394
|
0
|
0
|
|
|
|
|
for (i = 0; i < n; i++, v++) { |
395
|
0
|
|
|
|
|
|
e += svd_readBinInt(file, &r); |
396
|
0
|
|
|
|
|
|
e += svd_readBinFloat(file, &f); |
397
|
0
|
0
|
|
|
|
|
if (e) { |
398
|
0
|
|
|
|
|
|
svd_error("svdLoadSparseBinaryFile: bad file format"); |
399
|
0
|
|
|
|
|
|
return NULL; |
400
|
|
|
|
|
|
|
} |
401
|
0
|
|
|
|
|
|
S->rowind[v] = r; |
402
|
0
|
|
|
|
|
|
S->value[v] = f; |
403
|
|
|
|
|
|
|
} |
404
|
|
|
|
|
|
|
} |
405
|
0
|
|
|
|
|
|
S->pointr[cols] = vals; |
406
|
0
|
|
|
|
|
|
return S; |
407
|
|
|
|
|
|
|
} |
408
|
|
|
|
|
|
|
|
409
|
0
|
|
|
|
|
|
static void svdWriteSparseBinaryFile(SMat S, FILE *file) { |
410
|
|
|
|
|
|
|
int c, v; |
411
|
0
|
|
|
|
|
|
svd_writeBinInt(file, (int) S->rows); |
412
|
0
|
|
|
|
|
|
svd_writeBinInt(file, (int) S->cols); |
413
|
0
|
|
|
|
|
|
svd_writeBinInt(file, (int) S->vals); |
414
|
0
|
0
|
|
|
|
|
for (c = 0, v = 0; c < S->cols; c++) { |
415
|
0
|
|
|
|
|
|
svd_writeBinInt(file, (int) (S->pointr[c + 1] - S->pointr[c])); |
416
|
0
|
0
|
|
|
|
|
for (; v < S->pointr[c+1]; v++) { |
417
|
0
|
|
|
|
|
|
svd_writeBinInt(file, (int) S->rowind[v]); |
418
|
0
|
|
|
|
|
|
svd_writeBinFloat(file, (float) S->value[v]); |
419
|
|
|
|
|
|
|
} |
420
|
|
|
|
|
|
|
} |
421
|
0
|
|
|
|
|
|
} |
422
|
|
|
|
|
|
|
|
423
|
|
|
|
|
|
|
|
424
|
0
|
|
|
|
|
|
static DMat svdLoadDenseTextFile(FILE *file) { |
425
|
|
|
|
|
|
|
__SVDLIBC_LONG rows, cols, i, j; |
426
|
|
|
|
|
|
|
DMat D; |
427
|
0
|
0
|
|
|
|
|
if (fscanf(file, " %ld %ld", &rows, &cols) != 2) { |
428
|
0
|
|
|
|
|
|
svd_error("svdLoadDenseTextFile: bad file format"); |
429
|
0
|
|
|
|
|
|
return NULL; |
430
|
|
|
|
|
|
|
} |
431
|
|
|
|
|
|
|
|
432
|
0
|
|
|
|
|
|
D = svdNewDMat(rows, cols); |
433
|
0
|
0
|
|
|
|
|
if (!D) return NULL; |
434
|
|
|
|
|
|
|
|
435
|
0
|
0
|
|
|
|
|
for (i = 0; i < rows; i++) |
436
|
0
|
0
|
|
|
|
|
for (j = 0; j < cols; j++) { |
437
|
0
|
0
|
|
|
|
|
if (fscanf(file, " %lf", &(D->value[i][j])) != 1) { |
438
|
0
|
|
|
|
|
|
svd_error("svdLoadDenseTextFile: bad file format"); |
439
|
0
|
|
|
|
|
|
return NULL; |
440
|
|
|
|
|
|
|
} |
441
|
|
|
|
|
|
|
} |
442
|
0
|
|
|
|
|
|
return D; |
443
|
|
|
|
|
|
|
} |
444
|
|
|
|
|
|
|
|
445
|
0
|
|
|
|
|
|
static void svdWriteDenseTextFile(DMat D, FILE *file) { |
446
|
|
|
|
|
|
|
int i, j; |
447
|
0
|
|
|
|
|
|
fprintf(file, "%ld %ld\n", D->rows, D->cols); |
448
|
0
|
0
|
|
|
|
|
for (i = 0; i < D->rows; i++) |
449
|
0
|
0
|
|
|
|
|
for (j = 0; j < D->cols; j++) |
450
|
0
|
0
|
|
|
|
|
fprintf(file, "%g%c", D->value[i][j], (j == D->cols - 1) ? '\n' : ' '); |
451
|
0
|
|
|
|
|
|
} |
452
|
|
|
|
|
|
|
|
453
|
|
|
|
|
|
|
|
454
|
0
|
|
|
|
|
|
static DMat svdLoadDenseBinaryFile(FILE *file) { |
455
|
0
|
|
|
|
|
|
int rows, cols, i, j, e = 0; |
456
|
|
|
|
|
|
|
float f; |
457
|
|
|
|
|
|
|
DMat D; |
458
|
0
|
|
|
|
|
|
e += svd_readBinInt(file, &rows); |
459
|
0
|
|
|
|
|
|
e += svd_readBinInt(file, &cols); |
460
|
0
|
0
|
|
|
|
|
if (e) { |
461
|
0
|
|
|
|
|
|
svd_error("svdLoadDenseBinaryFile: bad file format"); |
462
|
0
|
|
|
|
|
|
return NULL; |
463
|
|
|
|
|
|
|
} |
464
|
|
|
|
|
|
|
|
465
|
0
|
|
|
|
|
|
D = svdNewDMat(rows, cols); |
466
|
0
|
0
|
|
|
|
|
if (!D) return NULL; |
467
|
|
|
|
|
|
|
|
468
|
0
|
0
|
|
|
|
|
for (i = 0; i < rows; i++) |
469
|
0
|
0
|
|
|
|
|
for (j = 0; j < cols; j++) { |
470
|
0
|
0
|
|
|
|
|
if (svd_readBinFloat(file, &f)) { |
471
|
0
|
|
|
|
|
|
svd_error("svdLoadDenseBinaryFile: bad file format"); |
472
|
0
|
|
|
|
|
|
return NULL; |
473
|
|
|
|
|
|
|
} |
474
|
0
|
|
|
|
|
|
D->value[i][j] = f; |
475
|
|
|
|
|
|
|
} |
476
|
0
|
|
|
|
|
|
return D; |
477
|
|
|
|
|
|
|
} |
478
|
|
|
|
|
|
|
|
479
|
0
|
|
|
|
|
|
static void svdWriteDenseBinaryFile(DMat D, FILE *file) { |
480
|
|
|
|
|
|
|
int i, j; |
481
|
0
|
|
|
|
|
|
svd_writeBinInt(file, (int) D->rows); |
482
|
0
|
|
|
|
|
|
svd_writeBinInt(file, (int) D->cols); |
483
|
0
|
0
|
|
|
|
|
for (i = 0; i < D->rows; i++) |
484
|
0
|
0
|
|
|
|
|
for (j = 0; j < D->cols; j++) |
485
|
0
|
|
|
|
|
|
svd_writeBinFloat(file, (float) D->value[i][j]); |
486
|
0
|
|
|
|
|
|
} |
487
|
|
|
|
|
|
|
|
488
|
|
|
|
|
|
|
|
489
|
0
|
|
|
|
|
|
SMat svdLoadSparseMatrix(char *filename, int format) { |
490
|
0
|
|
|
|
|
|
SMat S = NULL; |
491
|
0
|
|
|
|
|
|
DMat D = NULL; |
492
|
0
|
|
|
|
|
|
FILE *file = svd_fatalReadFile(filename); |
493
|
0
|
|
|
|
|
|
switch (format) { |
494
|
|
|
|
|
|
|
case SVD_F_STH: |
495
|
0
|
|
|
|
|
|
S = svdLoadSparseTextHBFile(file); |
496
|
0
|
|
|
|
|
|
break; |
497
|
|
|
|
|
|
|
case SVD_F_ST: |
498
|
0
|
|
|
|
|
|
S = svdLoadSparseTextFile(file); |
499
|
0
|
|
|
|
|
|
break; |
500
|
|
|
|
|
|
|
case SVD_F_SB: |
501
|
0
|
|
|
|
|
|
S = svdLoadSparseBinaryFile(file); |
502
|
0
|
|
|
|
|
|
break; |
503
|
|
|
|
|
|
|
case SVD_F_DT: |
504
|
0
|
|
|
|
|
|
D = svdLoadDenseTextFile(file); |
505
|
0
|
|
|
|
|
|
break; |
506
|
|
|
|
|
|
|
case SVD_F_DB: |
507
|
0
|
|
|
|
|
|
D = svdLoadDenseBinaryFile(file); |
508
|
0
|
|
|
|
|
|
break; |
509
|
0
|
|
|
|
|
|
default: svd_error("svdLoadSparseMatrix: unknown format %d", format); |
510
|
|
|
|
|
|
|
} |
511
|
0
|
|
|
|
|
|
svd_closeFile(file); |
512
|
0
|
0
|
|
|
|
|
if (D) { |
513
|
0
|
|
|
|
|
|
S = svdConvertDtoS(D); |
514
|
0
|
|
|
|
|
|
svdFreeDMat(D); |
515
|
|
|
|
|
|
|
} |
516
|
0
|
|
|
|
|
|
return S; |
517
|
|
|
|
|
|
|
} |
518
|
|
|
|
|
|
|
|
519
|
0
|
|
|
|
|
|
DMat svdLoadDenseMatrix(char *filename, int format) { |
520
|
0
|
|
|
|
|
|
SMat S = NULL; |
521
|
0
|
|
|
|
|
|
DMat D = NULL; |
522
|
0
|
|
|
|
|
|
FILE *file = svd_fatalReadFile(filename); |
523
|
0
|
|
|
|
|
|
switch (format) { |
524
|
|
|
|
|
|
|
case SVD_F_STH: |
525
|
0
|
|
|
|
|
|
S = svdLoadSparseTextHBFile(file); |
526
|
0
|
|
|
|
|
|
break; |
527
|
|
|
|
|
|
|
case SVD_F_ST: |
528
|
0
|
|
|
|
|
|
S = svdLoadSparseTextFile(file); |
529
|
0
|
|
|
|
|
|
break; |
530
|
|
|
|
|
|
|
case SVD_F_SB: |
531
|
0
|
|
|
|
|
|
S = svdLoadSparseBinaryFile(file); |
532
|
0
|
|
|
|
|
|
break; |
533
|
|
|
|
|
|
|
case SVD_F_DT: |
534
|
0
|
|
|
|
|
|
D = svdLoadDenseTextFile(file); |
535
|
0
|
|
|
|
|
|
break; |
536
|
|
|
|
|
|
|
case SVD_F_DB: |
537
|
0
|
|
|
|
|
|
D = svdLoadDenseBinaryFile(file); |
538
|
0
|
|
|
|
|
|
break; |
539
|
0
|
|
|
|
|
|
default: svd_error("svdLoadSparseMatrix: unknown format %d", format); |
540
|
|
|
|
|
|
|
} |
541
|
0
|
|
|
|
|
|
svd_closeFile(file); |
542
|
0
|
0
|
|
|
|
|
if (S) { |
543
|
0
|
|
|
|
|
|
D = svdConvertStoD(S); |
544
|
0
|
|
|
|
|
|
svdFreeSMat(S); |
545
|
|
|
|
|
|
|
} |
546
|
0
|
|
|
|
|
|
return D; |
547
|
|
|
|
|
|
|
} |
548
|
|
|
|
|
|
|
|
549
|
0
|
|
|
|
|
|
void svdWriteSparseMatrix(SMat S, char *filename, int format) { |
550
|
0
|
|
|
|
|
|
DMat D = NULL; |
551
|
0
|
|
|
|
|
|
FILE *file = svd_writeFile(filename, FALSE); |
552
|
0
|
0
|
|
|
|
|
if (!file) { |
553
|
0
|
|
|
|
|
|
svd_error("svdWriteSparseMatrix: failed to write file %s\n", filename); |
554
|
0
|
|
|
|
|
|
return; |
555
|
|
|
|
|
|
|
} |
556
|
0
|
|
|
|
|
|
switch (format) { |
557
|
|
|
|
|
|
|
case SVD_F_STH: |
558
|
0
|
|
|
|
|
|
svdWriteSparseTextHBFile(S, file); |
559
|
0
|
|
|
|
|
|
break; |
560
|
|
|
|
|
|
|
case SVD_F_ST: |
561
|
0
|
|
|
|
|
|
svdWriteSparseTextFile(S, file); |
562
|
0
|
|
|
|
|
|
break; |
563
|
|
|
|
|
|
|
case SVD_F_SB: |
564
|
0
|
|
|
|
|
|
svdWriteSparseBinaryFile(S, file); |
565
|
0
|
|
|
|
|
|
break; |
566
|
|
|
|
|
|
|
case SVD_F_DT: |
567
|
0
|
|
|
|
|
|
D = svdConvertStoD(S); |
568
|
0
|
|
|
|
|
|
svdWriteDenseTextFile(D, file); |
569
|
0
|
|
|
|
|
|
break; |
570
|
|
|
|
|
|
|
case SVD_F_DB: |
571
|
0
|
|
|
|
|
|
D = svdConvertStoD(S); |
572
|
0
|
|
|
|
|
|
svdWriteDenseBinaryFile(D, file); |
573
|
0
|
|
|
|
|
|
break; |
574
|
0
|
|
|
|
|
|
default: svd_error("svdLoadSparseMatrix: unknown format %d", format); |
575
|
|
|
|
|
|
|
} |
576
|
0
|
|
|
|
|
|
svd_closeFile(file); |
577
|
0
|
0
|
|
|
|
|
if (D) svdFreeDMat(D); |
578
|
|
|
|
|
|
|
} |
579
|
|
|
|
|
|
|
|
580
|
0
|
|
|
|
|
|
void svdWriteDenseMatrix(DMat D, char *filename, int format) { |
581
|
0
|
|
|
|
|
|
SMat S = NULL; |
582
|
0
|
|
|
|
|
|
FILE *file = svd_writeFile(filename, FALSE); |
583
|
0
|
0
|
|
|
|
|
if (!file) { |
584
|
0
|
|
|
|
|
|
svd_error("svdWriteDenseMatrix: failed to write file %s\n", filename); |
585
|
0
|
|
|
|
|
|
return; |
586
|
|
|
|
|
|
|
} |
587
|
0
|
|
|
|
|
|
switch (format) { |
588
|
|
|
|
|
|
|
case SVD_F_STH: |
589
|
0
|
|
|
|
|
|
S = svdConvertDtoS(D); |
590
|
0
|
|
|
|
|
|
svdWriteSparseTextHBFile(S, file); |
591
|
0
|
|
|
|
|
|
break; |
592
|
|
|
|
|
|
|
case SVD_F_ST: |
593
|
0
|
|
|
|
|
|
S = svdConvertDtoS(D); |
594
|
0
|
|
|
|
|
|
svdWriteSparseTextFile(S, file); |
595
|
0
|
|
|
|
|
|
break; |
596
|
|
|
|
|
|
|
case SVD_F_SB: |
597
|
0
|
|
|
|
|
|
S = svdConvertDtoS(D); |
598
|
0
|
|
|
|
|
|
svdWriteSparseBinaryFile(S, file); |
599
|
0
|
|
|
|
|
|
break; |
600
|
|
|
|
|
|
|
case SVD_F_DT: |
601
|
0
|
|
|
|
|
|
svdWriteDenseTextFile(D, file); |
602
|
0
|
|
|
|
|
|
break; |
603
|
|
|
|
|
|
|
case SVD_F_DB: |
604
|
0
|
|
|
|
|
|
svdWriteDenseBinaryFile(D, file); |
605
|
0
|
|
|
|
|
|
break; |
606
|
0
|
|
|
|
|
|
default: svd_error("svdLoadSparseMatrix: unknown format %d", format); |
607
|
|
|
|
|
|
|
} |
608
|
0
|
|
|
|
|
|
svd_closeFile(file); |
609
|
0
|
0
|
|
|
|
|
if (S) svdFreeSMat(S); |
610
|
|
|
|
|
|
|
} |