svdlib.h

Go to the documentation of this file.
00001 #ifndef SVDLIB_H
00002 #define SVDLIB_H
00003 
00004 #ifndef FALSE
00005 #  define FALSE 0
00006 #endif
00007 #ifndef TRUE
00008 #  define TRUE  1
00009 #endif
00010 
00011 /******************************** Structures *********************************/
00012 typedef struct smat *SMat;
00013 typedef struct dmat *DMat;
00014 typedef struct svdrec *SVDRec;
00015 
00016 /* Harwell-Boeing sparse matrix. */
00017 struct smat {
00018   long rows;
00019   long cols;
00020   long vals;     /* Total non-zero entries. */
00021   long *pointr;  /* For each col (plus 1), index of first non-zero entry. */
00022   long *rowind;  /* For each nz entry, the row index. */
00023   double *value; /* For each nz entry, the value. */
00024 };
00025 
00026 /* Row-major dense matrix.  Rows are consecutive vectors. */
00027 struct dmat {
00028   long rows;
00029   long cols;
00030   double **value; /* Accessed by [row][col]. Free value[0] and value to free.*/
00031 };
00032 
00033 struct svdrec {
00034   int d;      /* Dimensionality (rank) */
00035   DMat Ut;    /* Transpose of left singular vectors. (d by m)
00036                  The vectors are the rows of Ut. */
00037   double *S;  /* Array of singular values. (length d) */
00038   DMat Vt;    /* Transpose of right singular vectors. (d by n)
00039                  The vectors are the rows of Vt. */
00040 };
00041 
00042 
00043 /******************************** Variables **********************************/
00044 
00045 /* Version info */
00046 extern char *SVDVersion;
00047 
00048 /* How verbose is the package: 0, 1 (default), 2 */
00049 extern long SVDVerbosity;
00050 
00051 /* Counter(s) used to track how much work is done in computing the SVD. */
00052 enum svdCounters {SVD_MXV, SVD_COUNTERS};
00053 extern long SVDCount[SVD_COUNTERS];
00054 extern void svdResetCounters(void);
00055 
00056 enum svdFileFormats {SVD_F_STH, SVD_F_ST, SVD_F_SB, SVD_F_DT, SVD_F_DB};
00057 /*
00058 File formats:
00059 SVD_F_STH: sparse text, SVDPACK-style
00060 SVD_F_ST:  sparse text, SVDLIB-style
00061 SVD_F_DT:  dense text
00062 SVD_F_SB:  sparse binary
00063 SVD_F_DB:  dense binary
00064 */
00065 
00066 /* True if a file format is sparse: */
00067 #define SVD_IS_SPARSE(format) ((format >= SVD_F_STH) && (format <= SVD_F_SB))
00068 
00069 
00070 /******************************** Functions **********************************/
00071 
00072 /* Creates an empty dense matrix. */
00073 extern DMat svdNewDMat(int rows, int cols);
00074 /* Frees a dense matrix. */
00075 extern void svdFreeDMat(DMat D);
00076 
00077 /* Creates an empty sparse matrix. */
00078 SMat svdNewSMat(int rows, int cols, int vals);
00079 /* Frees a sparse matrix. */
00080 void svdFreeSMat(SMat S);
00081 
00082 /* Creates an empty SVD record. */
00083 SVDRec svdNewSVDRec(void);
00084 /* Frees an svd rec and all its contents. */
00085 void svdFreeSVDRec(SVDRec R);
00086 
00087 /* Converts a sparse matrix to a dense one (without affecting former) */
00088 DMat svdConvertStoD(SMat S);
00089 /* Converts a dense matrix to a sparse one (without affecting former) */
00090 SMat svdConvertDtoS(DMat D);
00091 
00092 /* Transposes a dense matrix (returning a new one) */
00093 DMat svdTransposeD(DMat D);
00094 /* Transposes a sparse matrix (returning a new one) */
00095 SMat svdTransposeS(SMat S);
00096 
00097 /* Writes an array to a file. */
00098 extern void svdWriteDenseArray(double *a, int n, char *filename, char binary);
00099 /* Reads an array from a file, storing its size in *np. */
00100 extern double *svdLoadDenseArray(char *filename, int *np, char binary);
00101 
00102 /* Loads a matrix file (in various formats) into a sparse matrix. */
00103 extern SMat svdLoadSparseMatrix(char *filename, int format);
00104 /* Loads a matrix file (in various formats) into a dense matrix. */
00105 extern DMat svdLoadDenseMatrix(char *filename, int format);
00106 
00107 /* Writes a dense matrix to a file in a given format. */
00108 extern void svdWriteDenseMatrix(DMat A, char *filename, int format);
00109 /* Writes a sparse matrix to a file in a given format. */
00110 extern void svdWriteSparseMatrix(SMat A, char *filename, int format);
00111 
00112 
00113 /* Performs the las2 SVD algorithm and returns the resulting Ut, S, and Vt. */
00114 extern SVDRec svdLAS2(SMat A, long dimensions, long iterations, double end[2], 
00115                       double kappa);
00116 /* Chooses default parameter values.  Set dimensions to 0 for all dimensions: */
00117 extern SVDRec svdLAS2A(SMat A, long dimensions);
00118 
00119 #endif /* SVDLIB_H */