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 2673, Fri May 20 16:19:18 2011 UTC revision 2901, Tue Sep 10 10:45:25 2013 UTC
# Line 1  Line 1 
1                          /* Sparse matrices in compressed column-oriented form */                          /* Sparse matrices in compressed column-oriented form */
2    
3  #include "Csparse.h"  #include "Csparse.h"
4  #include "Tsparse.h"  #include "Tsparse.h"
5  #include "chm_common.h"  #include "chm_common.h"
# Line 36  Line 37 
37      return Csparse_validate_(x, FALSE);      return Csparse_validate_(x, FALSE);
38  }  }
39    
 SEXP Csparse_validate2(SEXP x, SEXP maybe_modify) {  
     return Csparse_validate_(x, asLogical(maybe_modify));  
 }  
40    
41  SEXP Csparse_validate_(SEXP x, Rboolean maybe_modify)  #define _t_Csparse_validate
42  {  #include "t_Csparse_validate.c"
     /* NB: we do *NOT* check a potential 'x' slot here, at all */  
     SEXP pslot = GET_SLOT(x, Matrix_pSym),  
         islot = GET_SLOT(x, Matrix_iSym);  
     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);  
43    
44      if (length(pslot) != dims[1] + 1)  #define _t_Csparse_sort
45          return mkString(_("slot p must have length = ncol(.) + 1"));  #include "t_Csparse_validate.c"
     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 !!! */  
46    
47              /* Now re-check that row indices are *strictly* increasing  // R: .validateCsparse(x, sort.if.needed = FALSE) :
48               * (and not just increasing) within each column : */  SEXP Csparse_validate2(SEXP x, SEXP maybe_modify) {
49              for (j = 0; j < ncol; j++) {      return Csparse_validate_(x, asLogical(maybe_modify));
                 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"));  
50      }      }
51      return ScalarLogical(1);  
52    // R: Matrix:::.sortCsparse(x) :
53    SEXP Csparse_sort (SEXP x) {
54       int ok = Csparse_sort_2(x, TRUE); // modifying x directly
55       if(!ok) warning(_("Csparse_sort(x): x is not a valid (apart from sorting) CsparseMatrix"));
56       return x;
57  }  }
58    
59  SEXP Rsparse_validate(SEXP x)  SEXP Rsparse_validate(SEXP x)
# Line 188  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 233  Line 191 
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_SP__(x), &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 265  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 565  Line 531 
531      }      }
532      else { /* triangular with diag='N'): now drop the diagonal */      else { /* triangular with diag='N'): now drop the diagonal */
533          /* duplicate, since chx will be modified: */          /* duplicate, since chx will be modified: */
534          CHM_SP chx = AS_CHM_SP__(duplicate(x));          SEXP xx = PROTECT(duplicate(x));
535            CHM_SP chx = AS_CHM_SP__(xx);
536          int uploT = (*uplo_P(x) == 'U') ? 1 : -1,          int uploT = (*uplo_P(x) == 'U') ? 1 : -1,
537              Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;              Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
538          R_CheckStack();          R_CheckStack();
539    
540          chm_diagN2U(chx, uploT, /* do_realloc */ FALSE);          chm_diagN2U(chx, uploT, /* do_realloc */ FALSE);
541    
542          return chm_sparse_to_SEXP(chx, /*dofree*/ 0/* or 1 ?? */,          SEXP ans = 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    
# Line 600  Line 569 
569      if (csize >= 0 && !isInteger(j))      if (csize >= 0 && !isInteger(j))
570          error(_("Index j must be NULL or integer"));          error(_("Index j must be NULL or integer"));
571    
572      if (chx->stype) /* symmetricMatrix */      if (!chx->stype) {/* non-symmetric Matrix */
         /* for now, cholmod_submatrix() only accepts "generalMatrix" */  
         chx = cholmod_copy(chx, /* stype: */ 0, chx->xtype, &c);  
   
573      return chm_sparse_to_SEXP(cholmod_submatrix(chx,      return chm_sparse_to_SEXP(cholmod_submatrix(chx,
574                                  (rsize < 0) ? NULL : INTEGER(i), rsize,                                  (rsize < 0) ? NULL : INTEGER(i), rsize,
575                                  (csize < 0) ? NULL : INTEGER(j), csize,                                  (csize < 0) ? NULL : INTEGER(j), csize,
# Line 611  Line 577 
577                                1, 0, Rkind, "",                                1, 0, Rkind, "",
578                                /* FIXME: drops dimnames */ R_NilValue);                                /* FIXME: drops dimnames */ R_NilValue);
579  }  }
580                                    /* for now, cholmod_submatrix() only accepts "generalMatrix" */
581        CHM_SP tmp = cholmod_copy(chx, /* stype: */ 0, chx->xtype, &c);
582        CHM_SP ans = cholmod_submatrix(tmp,
583                                       (rsize < 0) ? NULL : INTEGER(i), rsize,
584                                       (csize < 0) ? NULL : INTEGER(j), csize,
585                                       TRUE, TRUE, &c);
586        cholmod_free_sparse(&tmp, &c);
587        return chm_sparse_to_SEXP(ans, 1, 0, Rkind, "", R_NilValue);
588    }
589    
590  /**  #define _d_Csp_
591   * Subassignment:  x[i,j]  <- value  #include "t_Csparse_subassign.c"
  *  
  * @param x  
  * @param i_ integer row    index 0-origin vector (as returned from R .ind.prep2())  
  * @param j_ integer column index 0-origin vector  
  * @param value currently must be a dsparseVector {which is recycled if needed}  
  *  
  * @return a Csparse matrix like x, but with the values replaced  
  */  
 SEXP Csparse_subassign(SEXP x, SEXP i_, SEXP j_, SEXP value)  
 {  
     static const char  
         *valid_cM [] = {"dgCMatrix",// the only one, for "the moment", more later  
                         ""},  
         *valid_spv[] = {"dsparseVector",  
                         ""};  
   
     int ctype = Matrix_check_class_etc(x, valid_cM);  
     if (ctype < 0)  
         error(_("invalid class of 'x' in Csparse_subassign()"));  
     // value: assume a  "dsparseVector" for now -- slots: (i, length, x)  
     ctype = Matrix_check_class_etc(value, valid_spv);  
     if (ctype < 0)  
         error(_("invalid class of 'value' in Csparse_subassign()"));  
   
     SEXP ans,  
         pslot = GET_SLOT(x, Matrix_pSym),  
         islot = GET_SLOT(x, Matrix_iSym),  
         i_cp = PROTECT((TYPEOF(i_) == INTSXP) ?  
                        duplicate(i_) : coerceVector(i_, INTSXP)),  
         j_cp = PROTECT((TYPEOF(j_) == INTSXP) ?  
                        duplicate(j_) : coerceVector(j_, INTSXP)),  
         // for d.CMatrix and l.CMatrix  but not n.CMatrix:  
         xslot = GET_SLOT(x, Matrix_xSym);  
   
     int *dims = INTEGER(GET_SLOT(x, Matrix_DimSym)),  
         nrow = dims[0],  
         ncol = dims[1],  
         *xp = INTEGER(pslot),  
         *xi = INTEGER(islot),  
         *ii = INTEGER(i_cp), len_i = LENGTH(i_cp),  
         *jj = INTEGER(j_cp), len_j = LENGTH(j_cp),  
         i, j, k;  
     int    *val_i = INTEGER(GET_SLOT(value, Matrix_iSym));  
     // for dsparseVector only:  
     double *val_x =   REAL (GET_SLOT(value, Matrix_xSym));  
     int len_val = asInteger(GET_SLOT(value, Matrix_lengthSym));  
     int p_last = xp[0];  
   
     // for d.CMatrix only:  
     double *xx = REAL(xslot);  
     double ind; // the index that goes all the way from 1:(len_i * len_j)  
   
     PROTECT(ans = duplicate(x));  
     for(j = 0; j < ncol; j++) {  
 // FIXME  
 // ....  
 // ....  
 // ....  
 // ....  
   
592    
593    #define _l_Csp_
594    #include "t_Csparse_subassign.c"
595    
596    #define _i_Csp_
597    #include "t_Csparse_subassign.c"
598    
599    #define _n_Csp_
600    #include "t_Csparse_subassign.c"
601    
602    #define _z_Csp_
603    #include "t_Csparse_subassign.c"
604    
605    
 // ....  
 // ....  
 // ....  
 // ....  
 // ....  
     }  
     UNPROTECT(3);  
     return ans;  
 }  
606    
607  SEXP Csparse_MatrixMarket(SEXP x, SEXP fname)  SEXP Csparse_MatrixMarket(SEXP x, SEXP fname)
608  {  {
# Line 772  Line 686 
686      case diag_backpermuted:      case diag_backpermuted:
687          for_DIAG(v[i] = x_x[i_from]);          for_DIAG(v[i] = x_x[i_from]);
688    
689          warning(_("resultKind = 'diagBack' (back-permuted) is experimental"));          warning(_("%s = '%s' (back-permuted) is experimental"),
690                    "resultKind", "diagBack");
691          /* now back_permute : */          /* now back_permute : */
692          for(i = 0; i < n; i++) {          for(i = 0; i < n; i++) {
693              double tmp = v[i]; v[i] = v[perm[i]]; v[perm[i]] = tmp;              double tmp = v[i]; v[i] = v[perm[i]]; v[perm[i]] = tmp;

Legend:
Removed from v.2673  
changed lines
  Added in v.2901

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