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 2817, Sat Aug 11 23:41:46 2012 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 188  Line 189 
189      if(cl_x[2] != 'C') error(_("not a CsparseMatrix"));      if(cl_x[2] != 'C') error(_("not a CsparseMatrix"));
190      int nnz = LENGTH(GET_SLOT(x, Matrix_iSym));      int nnz = LENGTH(GET_SLOT(x, Matrix_iSym));
191      SEXP ans;      SEXP ans;
192      char *ncl = strdup(cl_x);      char *ncl = alloca(strlen(cl_x) + 1); /* not much memory required */
193        strcpy(ncl, cl_x);
194      double *dx_x; int *ix_x;      double *dx_x; int *ix_x;
195      ncl[0] = (r_kind == x_double ? 'd' :      ncl[0] = (r_kind == x_double ? 'd' :
196                (r_kind == x_logical ? 'l' :                (r_kind == x_logical ? 'l' :
# Line 265  Line 267 
267    
268  SEXP Csparse_general_to_symmetric(SEXP x, SEXP uplo)  SEXP Csparse_general_to_symmetric(SEXP x, SEXP uplo)
269  {  {
270        int *adims = INTEGER(GET_SLOT(x, Matrix_DimSym)), n = adims[0];
271        if(n != adims[1]) {
272            error(_("Csparse_general_to_symmetric(): matrix is not square!"));
273            return R_NilValue; /* -Wall */
274        }
275      CHM_SP chx = AS_CHM_SP__(x), chgx;      CHM_SP chx = AS_CHM_SP__(x), chgx;
276      int uploT = (*CHAR(STRING_ELT(uplo,0)) == 'U') ? 1 : -1;      int uploT = (*CHAR(STRING_ELT(uplo,0)) == 'U') ? 1 : -1;
277      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
278      R_CheckStack();      R_CheckStack();
   
279      chgx = cholmod_copy(chx, /* stype: */ uploT, chx->xtype, &c);      chgx = cholmod_copy(chx, /* stype: */ uploT, chx->xtype, &c);
280      /* xtype: pattern, "real", complex or .. */      /* xtype: pattern, "real", complex or .. */
281      return chm_sparse_to_SEXP(chgx, 1, 0, Rkind, "",      return chm_sparse_to_SEXP(chgx, 1, 0, Rkind, "",
# Line 565  Line 571 
571      }      }
572      else { /* triangular with diag='N'): now drop the diagonal */      else { /* triangular with diag='N'): now drop the diagonal */
573          /* duplicate, since chx will be modified: */          /* duplicate, since chx will be modified: */
574          CHM_SP chx = AS_CHM_SP__(duplicate(x));          SEXP xx = PROTECT(duplicate(x));
575            CHM_SP chx = AS_CHM_SP__(xx);
576          int uploT = (*uplo_P(x) == 'U') ? 1 : -1,          int uploT = (*uplo_P(x) == 'U') ? 1 : -1,
577              Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;              Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
578          R_CheckStack();          R_CheckStack();
579    
580          chm_diagN2U(chx, uploT, /* do_realloc */ FALSE);          chm_diagN2U(chx, uploT, /* do_realloc */ FALSE);
581    
582            UNPROTECT(1);
583          return chm_sparse_to_SEXP(chx, /*dofree*/ 0/* or 1 ?? */,          return chm_sparse_to_SEXP(chx, /*dofree*/ 0/* or 1 ?? */,
584                                    uploT, Rkind, "U",                                    uploT, Rkind, "U",
585                                    GET_SLOT(x, Matrix_DimNamesSym));                                    GET_SLOT(x, Matrix_DimNamesSym));
# Line 600  Line 608 
608      if (csize >= 0 && !isInteger(j))      if (csize >= 0 && !isInteger(j))
609          error(_("Index j must be NULL or integer"));          error(_("Index j must be NULL or integer"));
610    
611      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);  
   
612      return chm_sparse_to_SEXP(cholmod_submatrix(chx,      return chm_sparse_to_SEXP(cholmod_submatrix(chx,
613                                  (rsize < 0) ? NULL : INTEGER(i), rsize,                                  (rsize < 0) ? NULL : INTEGER(i), rsize,
614                                  (csize < 0) ? NULL : INTEGER(j), csize,                                  (csize < 0) ? NULL : INTEGER(j), csize,
# Line 611  Line 616 
616                                1, 0, Rkind, "",                                1, 0, Rkind, "",
617                                /* FIXME: drops dimnames */ R_NilValue);                                /* FIXME: drops dimnames */ R_NilValue);
618  }  }
619                                    /* for now, cholmod_submatrix() only accepts "generalMatrix" */
620        CHM_SP tmp = cholmod_copy(chx, /* stype: */ 0, chx->xtype, &c);
621        CHM_SP ans = cholmod_submatrix(tmp,
622                                       (rsize < 0) ? NULL : INTEGER(i), rsize,
623                                       (csize < 0) ? NULL : INTEGER(j), csize,
624                                       TRUE, TRUE, &c);
625        cholmod_free_sparse(&tmp, &c);
626        return chm_sparse_to_SEXP(ans, 1, 0, Rkind, "", R_NilValue);
627    }
628    
629  /**  #define _d_Csp_
630   * 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  
 // ....  
 // ....  
 // ....  
 // ....  
   
631    
632    #define _l_Csp_
633    #include "t_Csparse_subassign.c"
634    
635    #define _i_Csp_
636    #include "t_Csparse_subassign.c"
637    
638    #define _n_Csp_
639    #include "t_Csparse_subassign.c"
640    
641    #define _z_Csp_
642    #include "t_Csparse_subassign.c"
643    
644    
 // ....  
 // ....  
 // ....  
 // ....  
 // ....  
     }  
     UNPROTECT(3);  
     return ans;  
 }  
645    
646  SEXP Csparse_MatrixMarket(SEXP x, SEXP fname)  SEXP Csparse_MatrixMarket(SEXP x, SEXP fname)
647  {  {
# Line 772  Line 725 
725      case diag_backpermuted:      case diag_backpermuted:
726          for_DIAG(v[i] = x_x[i_from]);          for_DIAG(v[i] = x_x[i_from]);
727    
728          warning(_("resultKind = 'diagBack' (back-permuted) is experimental"));          warning(_("%s = '%s' (back-permuted) is experimental"),
729                    "resultKind", "diagBack");
730          /* now back_permute : */          /* now back_permute : */
731          for(i = 0; i < n; i++) {          for(i = 0; i < n; i++) {
732              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.2817

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