SCM

SCM Repository

[matrix] Diff of /pkg/Matrix/src/Csparse.c
ViewVC logotype

Diff of /pkg/Matrix/src/Csparse.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2784, Fri Jun 8 17:01:25 2012 UTC revision 2958, Mon Jan 20 16:22:40 2014 UTC
# Line 37  Line 37 
37      return Csparse_validate_(x, FALSE);      return Csparse_validate_(x, FALSE);
38  }  }
39    
40    
41    #define _t_Csparse_validate
42    #include "t_Csparse_validate.c"
43    
44    #define _t_Csparse_sort
45    #include "t_Csparse_validate.c"
46    
47    // R: .validateCsparse(x, sort.if.needed = FALSE) :
48  SEXP Csparse_validate2(SEXP x, SEXP maybe_modify) {  SEXP Csparse_validate2(SEXP x, SEXP maybe_modify) {
49      return Csparse_validate_(x, asLogical(maybe_modify));      return Csparse_validate_(x, asLogical(maybe_modify));
50  }  }
51    
52  SEXP Csparse_validate_(SEXP x, Rboolean maybe_modify)  // R: Matrix:::.sortCsparse(x) :
53  {  SEXP Csparse_sort (SEXP x) {
54      /* NB: we do *NOT* check a potential 'x' slot here, at all */     int ok = Csparse_sort_2(x, TRUE); // modifying x directly
55      SEXP pslot = GET_SLOT(x, Matrix_pSym),     if(!ok) warning(_("Csparse_sort(x): x is not a valid (apart from sorting) CsparseMatrix"));
56          islot = GET_SLOT(x, Matrix_iSym);     return x;
     Rboolean sorted, strictly;  
     int j, k,  
         *dims = INTEGER(GET_SLOT(x, Matrix_DimSym)),  
         nrow = dims[0],  
         ncol = dims[1],  
         *xp = INTEGER(pslot),  
         *xi = INTEGER(islot);  
   
     if (length(pslot) != dims[1] + 1)  
         return mkString(_("slot p must have length = ncol(.) + 1"));  
     if (xp[0] != 0)  
         return mkString(_("first element of slot p must be zero"));  
     if (length(islot) < xp[ncol]) /* allow larger slots from over-allocation!*/  
         return  
             mkString(_("last element of slot p must match length of slots i and x"));  
     for (j = 0; j < xp[ncol]; j++) {  
         if (xi[j] < 0 || xi[j] >= nrow)  
             return mkString(_("all row indices must be between 0 and nrow-1"));  
     }  
     sorted = TRUE; strictly = TRUE;  
     for (j = 0; j < ncol; j++) {  
         if (xp[j] > xp[j + 1])  
             return mkString(_("slot p must be non-decreasing"));  
         if(sorted) /* only act if >= 2 entries in column j : */  
             for (k = xp[j] + 1; k < xp[j + 1]; k++) {  
                 if (xi[k] < xi[k - 1])  
                     sorted = FALSE;  
                 else if (xi[k] == xi[k - 1])  
                     strictly = FALSE;  
             }  
     }  
     if (!sorted) {  
         if(maybe_modify) {  
             CHM_SP chx = (CHM_SP) alloca(sizeof(cholmod_sparse));  
             R_CheckStack();  
             as_cholmod_sparse(chx, x, FALSE, TRUE);/*-> cholmod_l_sort() ! */  
             /* as chx = AS_CHM_SP__(x)  but  ^^^^ sorting x in_place !!! */  
   
             /* Now re-check that row indices are *strictly* increasing  
              * (and not just increasing) within each column : */  
             for (j = 0; j < ncol; j++) {  
                 for (k = xp[j] + 1; k < xp[j + 1]; k++)  
                     if (xi[k] == xi[k - 1])  
                         return mkString(_("slot i is not *strictly* increasing inside a column (even after cholmod_l_sort)"));  
             }  
         } else { /* no modifying sorting : */  
             return mkString(_("row indices are not sorted within columns"));  
         }  
     } else if(!strictly) {  /* sorted, but not strictly */  
         return mkString(_("slot i is not *strictly* increasing inside a column"));  
     }  
     return ScalarLogical(1);  
57  }  }
58    
59  SEXP Rsparse_validate(SEXP x)  SEXP Rsparse_validate(SEXP x)
# Line 189  Line 145 
145      if(cl_x[2] != 'C') error(_("not a CsparseMatrix"));      if(cl_x[2] != 'C') error(_("not a CsparseMatrix"));
146      int nnz = LENGTH(GET_SLOT(x, Matrix_iSym));      int nnz = LENGTH(GET_SLOT(x, Matrix_iSym));
147      SEXP ans;      SEXP ans;
148      char *ncl = strdup(cl_x);      char *ncl = alloca(strlen(cl_x) + 1); /* not much memory required */
149        strcpy(ncl, cl_x);
150      double *dx_x; int *ix_x;      double *dx_x; int *ix_x;
151      ncl[0] = (r_kind == x_double ? 'd' :      ncl[0] = (r_kind == x_double ? 'd' :
152                (r_kind == x_logical ? 'l' :                (r_kind == x_logical ? 'l' :
# Line 229  Line 186 
186      return ans;      return ans;
187  }  }
188    
189  SEXP Csparse_to_matrix(SEXP x)  SEXP Csparse_to_matrix(SEXP x, SEXP chk)
190  {  {
191      return chm_dense_to_matrix(cholmod_sparse_to_dense(AS_CHM_SP__(x), &c),      return chm_dense_to_matrix(cholmod_sparse_to_dense(AS_CHM_SP2(x, asLogical(chk)), &c),
192                                 1 /*do_free*/, GET_SLOT(x, Matrix_DimNamesSym));                                 1 /*do_free*/, GET_SLOT(x, Matrix_DimNamesSym));
193  }  }
194    SEXP Csparse_to_vector(SEXP x)
195    {
196        return chm_dense_to_vector(cholmod_sparse_to_dense(AS_CHM_SP__(x), &c), 1);
197    }
198    
199  SEXP Csparse_to_Tsparse(SEXP x, SEXP tri)  SEXP Csparse_to_Tsparse(SEXP x, SEXP tri)
200  {  {
# Line 266  Line 227 
227    
228  SEXP Csparse_general_to_symmetric(SEXP x, SEXP uplo)  SEXP Csparse_general_to_symmetric(SEXP x, SEXP uplo)
229  {  {
230        int *adims = INTEGER(GET_SLOT(x, Matrix_DimSym)), n = adims[0];
231        if(n != adims[1]) {
232            error(_("Csparse_general_to_symmetric(): matrix is not square!"));
233            return R_NilValue; /* -Wall */
234        }
235      CHM_SP chx = AS_CHM_SP__(x), chgx;      CHM_SP chx = AS_CHM_SP__(x), chgx;
236      int uploT = (*CHAR(STRING_ELT(uplo,0)) == 'U') ? 1 : -1;      int uploT = (*CHAR(STRING_ELT(uplo,0)) == 'U') ? 1 : -1;
237      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
238      R_CheckStack();      R_CheckStack();
   
239      chgx = cholmod_copy(chx, /* stype: */ uploT, chx->xtype, &c);      chgx = cholmod_copy(chx, /* stype: */ uploT, chx->xtype, &c);
240      /* xtype: pattern, "real", complex or .. */      /* xtype: pattern, "real", complex or .. */
241      return chm_sparse_to_SEXP(chgx, 1, 0, Rkind, "",      return chm_sparse_to_SEXP(chgx, 1, 0, Rkind, "",
# Line 574  Line 539 
539    
540          chm_diagN2U(chx, uploT, /* do_realloc */ FALSE);          chm_diagN2U(chx, uploT, /* do_realloc */ FALSE);
541    
542          UNPROTECT(1);          SEXP ans = chm_sparse_to_SEXP(chx, /*dofree*/ 0/* or 1 ?? */,
         return chm_sparse_to_SEXP(chx, /*dofree*/ 0/* or 1 ?? */,  
543                                    uploT, Rkind, "U",                                    uploT, Rkind, "U",
544                                    GET_SLOT(x, Matrix_DimNamesSym));                                    GET_SLOT(x, Matrix_DimNamesSym));
545            UNPROTECT(1);// only now !
546            return ans;
547      }      }
548  }  }
549    

Legend:
Removed from v.2784  
changed lines
  Added in v.2958

root@r-forge.r-project.org
ViewVC Help
Powered by ViewVC 1.0.0  
Thanks to:
Vienna University of Economics and Business Powered By FusionForge