Exemple #1
0
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;
}
Exemple #2
0
/* Efficiently transposes a sparse matrix. */
SMat svdTransposeS(SMat S) {
  int r, c, i, j;
  SMat N = svdNewSMat(S->h.cols, S->h.rows, S->h.vals);
  /* Count number nz in each row. */
  for (i = 0; i < S->h.vals; i++)
    N->pointr[S->rowind[i]]++;
  /* Fill each cell with the starting point of the previous row. */
  N->pointr[S->h.rows] = S->h.vals - N->pointr[S->h.rows - 1];
  for (r = S->h.rows - 1; r > 0; r--)
    N->pointr[r] = N->pointr[r+1] - N->pointr[r-1];
  N->pointr[0] = 0;
  /* Assign the new columns and values. */
  for (c = 0, i = 0; c < S->h.cols; c++) {
    for (; i < S->pointr[c+1]; i++) {
      r = S->rowind[i];
      j = N->pointr[r+1]++;
      N->rowind[j] = c;
      N->value[j] = S->value[i];
    }
  }
  /* Transpose the row and column offsets also. */
  if (S->offset_for_col)
    N->offset_for_row = copyVector(S->offset_for_col, S->h.cols, "svdTransposeS: offset_for_row");
  if (S->offset_for_row)
    N->offset_for_col = copyVector(S->offset_for_row, S->h.rows, "svdTransposeS: offset_for_col");

  return N;
}
Exemple #3
0
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;
}
Exemple #4
0
SMat  sparseFromArray(long rows, long columns, double * array) {
    long count = 0;

    //I am going to work with the idl convention that in the one
    for(unsigned i = 0 ; i < columns ; i++) {
        for(unsigned j = 0 ; j < rows ; j++) {
            if(array[i + j * rows] == 0) continue;
            count++;
        }
    }
    long vals = count;

    SMat S = svdNewSMat(rows, columns, vals);
    count = 0;
    for(unsigned i = 0 ; i < columns ; i++) {
        S->pointr[i] = count;
        for(unsigned j = 0 ; j < rows ; j++) {
            if(array[i+j*rows] ==0) continue;
            S->rowind[count] = j;
            S->value[count] = array[i + j* rows];
            count++;
        }
    }
    S->pointr[columns] = vals;
    return S;
};
Exemple #5
0
/* 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;
}
Exemple #6
0
//#define SVD_KAPPA 1e-4
//#define SVD_MAX_ITER 100
//
JNIEXPORT jint JNICALL Java_utils_SVD_svd__III_3I_3I_3D_3D_3D_3D
    (JNIEnv *env, jclass obj, jint _N, jint _M, jint _R, 
     jintArray _x, jintArray _y, jdoubleArray _z,
     jdoubleArray _S, jdoubleArray _Ut, jdoubleArray _Vt)
{
    SVDVerbosity = 0;
    int* x = env->GetIntArrayElements(_x, 0);
    int* y = env->GetIntArrayElements(_y, 0);
    double* z = env->GetDoubleArrayElements(_z, 0);
    jsize K = env->GetArrayLength(_x);
    int rank = 0;

    SMat smat = svdNewSMat(_N, _M, K);
    if (smat) {
        for (int i = 0, p = 0; i < _M; ++i) {
            smat->pointr[i] = p;
            while (p < K && y[p] == i) {
                smat->rowind[p] = x[p];
                smat->value[p] = z[p];
                ++p;
            }
        }
        smat->pointr[_M] = K;

        SVDRec rec = svdLAS2A(smat, _R);
        if (rec) {
            double* S = env->GetDoubleArrayElements(_S, 0);
            double* Ut = env->GetDoubleArrayElements(_Ut, 0);
            double* Vt = env->GetDoubleArrayElements(_Vt, 0);

            rank = rec->d;
            for (int i = 0; i < rank; ++i) S[i] = rec->S[i];
            for (int i = 0; i < rank; ++i)
                for (int j = 0; j < _N; ++j) Ut[i*_N+j] = rec->Ut->value[i][j];
            for (int i = 0; i < rank; ++i)
                for (int j = 0; j < _M; ++j) Vt[i*_M+j] = rec->Vt->value[i][j];

            env->ReleaseDoubleArrayElements(_S, S, 0);
            env->ReleaseDoubleArrayElements(_Ut, Ut, 0);
            env->ReleaseDoubleArrayElements(_Vt, Vt, 0);
        }
        svdFreeSMat(smat);
        svdFreeSVDRec(rec);
    }

    env->ReleaseIntArrayElements(_x, x, 0);
    env->ReleaseIntArrayElements(_y, y, 0);
    env->ReleaseDoubleArrayElements(_z, z, 0);

    return rank;
}
Exemple #7
0
/* Efficiently transposes a sparse matrix. */
SMat svdTransposeS(SMat S) {
  int r, c, i, j;
  SMat N = svdNewSMat(S->cols, S->rows, S->vals);
  /* Count number nz in each row. */
  for (i = 0; i < S->vals; i++)
    N->pointr[S->rowind[i]]++;
  /* Fill each cell with the starting point of the previous row. */
  N->pointr[S->rows] = S->vals - N->pointr[S->rows - 1];
  for (r = S->rows - 1; r > 0; r--)
    N->pointr[r] = N->pointr[r+1] - N->pointr[r-1];
  N->pointr[0] = 0;
  /* Assign the new columns and values. */
  for (c = 0, i = 0; c < S->cols; c++) {
    for (; i < S->pointr[c+1]; i++) {
      r = S->rowind[i];
      j = N->pointr[r+1]++;
      N->rowind[j] = c;
      N->value[j] = S->value[i];
    }
  }
  return N;
}
Exemple #8
0
/* 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;
}
Exemple #9
0
void getSVDscore(unsigned int *split_taxa, int sze)
{  

  int j, jj;
  int num_unique_rows, num_unique_cols;
  long int* firstnz; 

  // initialize score
  double score=0;

  SMat S = NULL;
  SVDRec R = NULL;

  /* initialize split_taxa vector */
  for (j=0; j<nints; j++) {
    split_taxaR[j] = 0;
    split_taxaC[j] = 0;
  }

  /* for counting number of SVs returned */ 
  int numberFound=0;

  /* setting a tolerance for checking when the sum of the squares
     of the requested number of singular values equals the sum of 
     the squares of all of the singular values (the Frobenius norm).
     If these two values are equal to within tol, then the score
     is set to zero.                                                */
  double tol=10e-12;
  int flag=1;

  // transfer split 
  for (j=0; j<nints; j++) split_taxaR[j] = split_taxa[j];   
  // complementary mask 
  for (j=0; j<nints; j++) split_taxaC[j] =~ split_taxaR[j]; 

  /* sort on rows, to get row indices, 
     then on columns, to get sparse encoding of flattening */

  /* sort for rows */
  qsort(*ppBase_u_bin, num_unique_bin, (nints+2)*sizeof(unsigned int), bin_compR);
  
  /* Enter row numbers into array */
  jj=0;  // initialize row number
  ppBase_u_bin[0][nints+1]=0;  // this is always in first row
  for (j=1; j<num_unique_bin; j++)
    {	   	  
      if (bin_compR(ppBase_u_bin[j],ppBase_u_bin[j-1])==1) jj++;
      ppBase_u_bin[j][nints+1]=jj; // record row number
    }
  num_unique_rows=jj+1;

  /* sort for columns */
  qsort(*ppBase_u_bin, num_unique_bin, (nints+2)*sizeof(unsigned int), bin_compC);
  
  /* allocate space for sparse matrix encoding */
  long int* rowindex = (long int*) calloc(num_unique_bin,sizeof(long int));
  double* values  = (double*) calloc(num_unique_bin,sizeof(double));

  /*  create vector encoding column numbers */
  jj=0; // first column number
  // copy value of first non-zero for SVD
  values[0] = (double)ppBase_u_bin[0][nints]/(double)num_no_gaps;

  // copy row index of first entry for SVD
  rowindex[0]=ppBase_u_bin[0][nints+1]; 
  ppBase_u_bin[0][nints+1]=0;  // first entry is always in first column

  /* Note: we're reusing this space to store column info as we remove row info */
  for (j=1; j<num_unique_bin; j++)
    { 
      // copy non-zeros for SVD
      values[j] = (double)(ppBase_u_bin[j][nints])/(double)num_no_gaps;
      rowindex[j]=ppBase_u_bin[j][nints+1]; // copy row indices for SVD
      if (bin_compC(ppBase_u_bin[j],ppBase_u_bin[j-1])==1)  // if new column...
	{ 
          jj++;      
	  ppBase_u_bin[jj][nints+1]=j; // save column coding here temporarily 
	}
    }
  num_unique_cols=jj+1;

  /* allocate final space for sparse matrix encoding */
  firstnz = (long int*) calloc( num_unique_cols+1 , sizeof(long int));

  for (j=0; j<num_unique_cols; j++)
    {
      firstnz[j]=ppBase_u_bin[j][nints+1];
    }

  /* copy over column coding for SVD */
  firstnz[j]=num_unique_bin;/* sparse format requires this too */
  
  /* Create Smat object to use in SVD routines */
  S = svdNewSMat(num_unique_rows,num_unique_cols,num_unique_bin);
  R = svdNewSVDRec();
  
  S->pointr = firstnz;
  S->rowind = rowindex;
  S->value = values;
  
  /* Compute SVD */
  extern long SVDVerbosity;

  SVDVerbosity=0; // print nothing from SVD routine

  R = svdLAS2A(S,rank);

  /** Scores computations **/

  // count number of singular values requested are found
  numberFound = R->d;

  if (numberFound < rank)
    {
      score = NAN;
      flag = 0;
    }
  
  // only compute score if the requested number of singular values was returned

  if (flag) 
    {
      for (j=0; j<rank; j++)   score = score+pow(R->S[j],2);

      /* sometimes sum of squares of r singular values > fnorm2 due to numerical error, 
         i.e. in the 10e-16 decimal place.  Test how close these values are and
	 reset score to 0 if difference less than tolerance.                         */

      if (fabs(score-fnorm2)>tol)  
	{
	  score = sqrt(1-score/fnorm2);
	}
      else
	{
	  score=0;
	}
    }

  fprintf(scoresfile, "%1.15f\n",score); 

  fflush(0);
  
  free(firstnz);    
  free(rowindex);   
  free(values);
  
  S->pointr = NULL;
  S->rowind = NULL;
  S->value = NULL;
  
  svdFreeSMat(S);
  svdFreeSVDRec(R);
  
  num_unique_rows=0;
  num_unique_cols=0;

}