void svdWriteDenseMatrix(DMat D, char *filename, int format) { SMat S = NULL; FILE *file = svd_writeFile(filename, FALSE); if (!file) { svd_error("svdWriteDenseMatrix: failed to write file %s\n", filename); return; } switch (format) { case SVD_F_STH: S = svdConvertDtoS(D); svdWriteSparseTextHBFile(S, file); break; case SVD_F_ST: S = svdConvertDtoS(D); svdWriteSparseTextFile(S, file); break; case SVD_F_SB: S = svdConvertDtoS(D); svdWriteSparseBinaryFile(S, file); break; case SVD_F_DT: svdWriteDenseTextFile(D, file); break; case SVD_F_DB: svdWriteDenseBinaryFile(D, file); break; default: svd_error("svdLoadSparseMatrix: unknown format %d", format); } svd_closeFile(file); if (S) svdFreeSMat(S); }
static SMat svdLoadSparseBinaryFile(FILE *file) { int rows, cols, vals, n, c, i, v, r, e = 0; float f; SMat S; e += svd_readBinInt(file, &rows); e += svd_readBinInt(file, &cols); e += svd_readBinInt(file, &vals); if (e) { svd_error("svdLoadSparseBinaryFile: bad file format"); return NULL; } S = svdNewSMat(rows, cols, vals); if (!S) return NULL; for (c = 0, v = 0; c < cols; c++) { if (svd_readBinInt(file, &n)) { svd_error("svdLoadSparseBinaryFile: bad file format"); return NULL; } S->pointr[c] = v; for (i = 0; i < n; i++, v++) { e += svd_readBinInt(file, &r); e += svd_readBinFloat(file, &f); if (e) { svd_error("svdLoadSparseBinaryFile: bad file format"); return NULL; } S->rowind[v] = r; S->value[v] = f; } } S->pointr[cols] = vals; return S; }
static SMat svdLoadSparseTextFile(FILE *file) { long c, i, n, v, rows, cols, vals; SMat S; if (fscanf(file, " %ld %ld %ld", &rows, &cols, &vals) != 3) { svd_error("svdLoadSparseTextFile: bad file format"); return NULL; } S = svdNewSMat(rows, cols, vals); if (!S) return NULL; for (c = 0, v = 0; c < cols; c++) { if (fscanf(file, " %ld", &n) != 1) { svd_error("svdLoadSparseTextFile: bad file format"); return NULL; } S->pointr[c] = v; for (i = 0; i < n; i++, v++) { if (fscanf(file, " %ld %lf", S->rowind + v, S->value + v) != 2) { svd_error("svdLoadSparseTextFile: bad file format"); return NULL; } } } S->pointr[cols] = vals; return S; }
double *svdLoadDenseArray(const char *filename, int *np, char binary) { int i, n; double *a; int nfound; FILE *file = svd_readFile(filename); if (!file) { svd_error("svdLoadDenseArray: failed to read %s", filename); return NULL; } if (binary) svd_readBinInt(file, np); else { nfound = fscanf(file, " %d", np); if (nfound == 0) svd_error("svdLoadDenseArray: didn't get np"); } n = *np; a = svd_doubleArray(n, FALSE, "svdLoadDenseArray: a"); if (!a) return NULL; if (binary) { float f; for (i = 0; i < n; i++) { svd_readBinFloat(file, &f); a[i] = f; } } else { for (i = 0; i < n; i++) { nfound = fscanf(file, " %lf\n", a + i); if (nfound == 0) svd_error("svdLoadDenseArray: didn't get value"); } } svd_closeFile(file); return a; }
/* File format has a funny header, then first entry index per column, then the row for each entry, then the value for each entry. Indices count from 1. Assumes A is initialized. */ static SMat svdLoadSparseTextHBFile(FILE *file) { char line[128]; long i, x, rows, cols, vals, num_mat; SMat S; /* Skip the header line: */ if (!fgets(line, 128, file)) ; /* Skip the line giving the number of lines in this file: */ if (!fgets(line, 128, file)) ; /* Read the line with useful dimensions: */ if (fscanf(file, "%*s%ld%ld%ld%ld\n", &rows, &cols, &vals, &num_mat) != 4) { svd_error("svdLoadSparseTextHBFile: bad file format on line 3"); return NULL; } if (num_mat != 0) { svd_error("svdLoadSparseTextHBFile: I don't know how to handle a file " "with elemental matrices (last entry on header line 3)"); return NULL; } /* Skip the line giving the formats: */ if (!fgets(line, 128, file)) ; S = svdNewSMat(rows, cols, vals); if (!S) return NULL; /* Read column pointers. */ for (i = 0; i <= S->cols; i++) { if (fscanf(file, " %ld", &x) != 1) { svd_error("svdLoadSparseTextHBFile: error reading pointr %d", i); return NULL; } S->pointr[i] = x - 1; } S->pointr[S->cols] = S->vals; /* Read row indices. */ for (i = 0; i < S->vals; i++) { if (fscanf(file, " %ld", &x) != 1) { svd_error("svdLoadSparseTextHBFile: error reading rowind %d", i); return NULL; } S->rowind[i] = x - 1; } for (i = 0; i < S->vals; i++) if (fscanf(file, " %lf", S->value + i) != 1) { svd_error("svdLoadSparseTextHBFile: error reading value %d", i); return NULL; } return S; }
DMat svdLoadDenseMatrix(char *filename, int format) { SMat S = NULL; DMat D = NULL; FILE *file = svd_fatalReadFile(filename); switch (format) { case SVD_F_STH: S = svdLoadSparseTextHBFile(file); break; case SVD_F_ST: S = svdLoadSparseTextFile(file); break; case SVD_F_SB: S = svdLoadSparseBinaryFile(file); break; case SVD_F_DT: D = svdLoadDenseTextFile(file); break; case SVD_F_DB: D = svdLoadDenseBinaryFile(file); break; default: svd_error("svdLoadSparseMatrix: unknown format %d", format); } svd_closeFile(file); if (S) { D = svdConvertStoD(S); svdFreeSMat(S); } return D; }
static DMat svdLoadDenseTextFile(FILE *file) { long rows, cols, i, j; DMat D; if (fscanf(file, " %ld %ld", &rows, &cols) != 2) { svd_error("svdLoadDenseTextFile: bad file format"); return NULL; } D = svdNewDMat(rows, cols); if (!D) return NULL; for (i = 0; i < rows; i++) for (j = 0; j < cols; j++) { if (fscanf(file, " %lf", &(D->value[i][j])) != 1) { svd_error("svdLoadDenseTextFile: bad file format"); return NULL; } } return D; }
/* Converts a sparse matrix to a dense one (without affecting the former) */ DMat svdConvertStoD(SMat S) { int i, c; DMat D = svdNewDMat(S->rows, S->cols); if (!D) { svd_error("svdConvertStoD: failed to allocate D"); return NULL; } for (i = 0, c = 0; i < S->vals; i++) { while (S->pointr[c + 1] <= i) c++; D->value[S->rowind[i]][c] = S->value[i]; } return D; }
static DMat svdLoadDenseBinaryFile(FILE *file) { int rows, cols, i, j, e = 0; float f; DMat D; e += svd_readBinInt(file, &rows); e += svd_readBinInt(file, &cols); if (e) { svd_error("svdLoadDenseBinaryFile: bad file format"); return NULL; } D = svdNewDMat(rows, cols); if (!D) return NULL; for (i = 0; i < rows; i++) for (j = 0; j < cols; j++) { if (svd_readBinFloat(file, &f)) { svd_error("svdLoadDenseBinaryFile: bad file format"); return NULL; } D->value[i][j] = f; } return D; }
double *svdLoadDenseArray(char *filename, int *np, char binary) { int i, n; double *a; FILE *file = svd_readFile(filename); if (!file) { svd_error("svdLoadDenseArray: failed to read %s", filename); return NULL; } if (binary) { svd_readBinInt(file, np); } else if (fscanf(file, " %d", np) != 1) { svd_error("svdLoadDenseArray: error reading %s", filename); svd_closeFile(file); return NULL; } n = *np; a = svd_doubleArray(n, FALSE, "svdLoadDenseArray: a"); if (!a) return NULL; if (binary) { float f; for (i = 0; i < n; i++) { svd_readBinFloat(file, &f); a[i] = f; } } else { for (i = 0; i < n; i++) { if (fscanf(file, " %lf\n", a + i) != 1) { svd_error("svdLoadDenseArray: error reading %s", filename); break; } } } svd_closeFile(file); return a; }
void svdWriteDenseArray(double *a, int n, char *filename, char binary) { int i; FILE *file = svd_writeFile(filename, FALSE); if (!file) return svd_error("svdWriteDenseArray: failed to write %s", filename); if (binary) { svd_writeBinInt(file, n); for (i = 0; i < n; i++) svd_writeBinFloat(file, (float) a[i]); } else { fprintf(file, "%d\n", n); for (i = 0; i < n; i++) fprintf(file, "%g\n", a[i]); } svd_closeFile(file); }
/* Converts a dense matrix to a sparse one (without affecting the dense one) */ SMat svdConvertDtoS(DMat D) { SMat S; int i, j, n; for (i = 0, n = 0; i < D->rows; i++) for (j = 0; j < D->cols; j++) if (D->value[i][j] != 0) n++; S = svdNewSMat(D->rows, D->cols, n); if (!S) { svd_error("svdConvertDtoS: failed to allocate S"); return NULL; } for (j = 0, n = 0; j < D->cols; j++) { S->pointr[j] = n; for (i = 0; i < D->rows; i++) if (D->value[i][j] != 0) { S->rowind[n] = i; S->value[n] = D->value[i][j]; n++; } } S->pointr[S->cols] = S->vals; return S; }
static void registerPipe(FILE *p) { if (numPipes >= MAX_PIPES) svd_error("Too many pipes open"); Pipe[numPipes++] = p; }