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 2646, Mon Feb 21 10:57:49 2011 UTC revision 2889, Thu Aug 8 21:06:22 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 136  Line 93 
93              }              }
94      }      }
95      if (!sorted)      if (!sorted)
96          /* cannot easily use cholmod_l_sort(.) ... -> "error out" :*/          /* cannot easily use cholmod_sort(.) ... -> "error out" :*/
97          return mkString(_("slot j is not increasing inside a column"));          return mkString(_("slot j is not increasing inside a column"));
98      else if(!strictly) /* sorted, but not strictly */      else if(!strictly) /* sorted, but not strictly */
99          return mkString(_("slot j is not *strictly* increasing inside a column"));          return mkString(_("slot j is not *strictly* increasing inside a column"));
# Line 154  Line 111 
111      /* This loses the symmetry property, since cholmod_dense has none,      /* This loses the symmetry property, since cholmod_dense has none,
112       * BUT, much worse (FIXME!), it also transforms CHOLMOD_PATTERN ("n") matrices       * BUT, much worse (FIXME!), it also transforms CHOLMOD_PATTERN ("n") matrices
113       * to numeric (CHOLMOD_REAL) ones : */       * to numeric (CHOLMOD_REAL) ones : */
114      CHM_DN chxd = cholmod_l_sparse_to_dense(chxs, &c);      CHM_DN chxd = cholmod_sparse_to_dense(chxs, &c);
115      int Rkind = (chxs->xtype == CHOLMOD_PATTERN)? -1 : Real_kind(x);      int Rkind = (chxs->xtype == CHOLMOD_PATTERN)? -1 : Real_kind(x);
116      R_CheckStack();      R_CheckStack();
117    
# Line 165  Line 122 
122  SEXP Csparse_to_nz_pattern(SEXP x, SEXP tri)  SEXP Csparse_to_nz_pattern(SEXP x, SEXP tri)
123  {  {
124      CHM_SP chxs = AS_CHM_SP__(x);      CHM_SP chxs = AS_CHM_SP__(x);
125      CHM_SP chxcp = cholmod_l_copy(chxs, chxs->stype, CHOLMOD_PATTERN, &c);      CHM_SP chxcp = cholmod_copy(chxs, chxs->stype, CHOLMOD_PATTERN, &c);
126      int tr = asLogical(tri);      int tr = asLogical(tri);
127      R_CheckStack();      R_CheckStack();
128    
# 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 230  Line 188 
188    
189  SEXP Csparse_to_matrix(SEXP x)  SEXP Csparse_to_matrix(SEXP x)
190  {  {
191      return chm_dense_to_matrix(cholmod_l_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    
195  SEXP Csparse_to_Tsparse(SEXP x, SEXP tri)  SEXP Csparse_to_Tsparse(SEXP x, SEXP tri)
196  {  {
197      CHM_SP chxs = AS_CHM_SP__(x);      CHM_SP chxs = AS_CHM_SP__(x);
198      CHM_TR chxt = cholmod_l_sparse_to_triplet(chxs, &c);      CHM_TR chxt = cholmod_sparse_to_triplet(chxs, &c);
199      int tr = asLogical(tri);      int tr = asLogical(tri);
200      int Rkind = (chxs->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;      int Rkind = (chxs->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
201      R_CheckStack();      R_CheckStack();
# Line 257  Line 215 
215    
216      if (!(chx->stype))      if (!(chx->stype))
217          error(_("Nonsymmetric matrix in Csparse_symmetric_to_general"));          error(_("Nonsymmetric matrix in Csparse_symmetric_to_general"));
218      chgx = cholmod_l_copy(chx, /* stype: */ 0, chx->xtype, &c);      chgx = cholmod_copy(chx, /* stype: */ 0, chx->xtype, &c);
219      /* xtype: pattern, "real", complex or .. */      /* xtype: pattern, "real", complex or .. */
220      return chm_sparse_to_SEXP(chgx, 1, 0, Rkind, "",      return chm_sparse_to_SEXP(chgx, 1, 0, Rkind, "",
221                                GET_SLOT(x, Matrix_DimNamesSym));                                GET_SLOT(x, Matrix_DimNamesSym));
# Line 265  Line 223 
223    
224  SEXP Csparse_general_to_symmetric(SEXP x, SEXP uplo)  SEXP Csparse_general_to_symmetric(SEXP x, SEXP uplo)
225  {  {
226        int *adims = INTEGER(GET_SLOT(x, Matrix_DimSym)), n = adims[0];
227        if(n != adims[1]) {
228            error(_("Csparse_general_to_symmetric(): matrix is not square!"));
229            return R_NilValue; /* -Wall */
230        }
231      CHM_SP chx = AS_CHM_SP__(x), chgx;      CHM_SP chx = AS_CHM_SP__(x), chgx;
232      int uploT = (*CHAR(STRING_ELT(uplo,0)) == 'U') ? 1 : -1;      int uploT = (*CHAR(STRING_ELT(uplo,0)) == 'U') ? 1 : -1;
233      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
234      R_CheckStack();      R_CheckStack();
235        chgx = cholmod_copy(chx, /* stype: */ uploT, chx->xtype, &c);
     chgx = cholmod_l_copy(chx, /* stype: */ uploT, chx->xtype, &c);  
236      /* xtype: pattern, "real", complex or .. */      /* xtype: pattern, "real", complex or .. */
237      return chm_sparse_to_SEXP(chgx, 1, 0, Rkind, "",      return chm_sparse_to_SEXP(chgx, 1, 0, Rkind, "",
238                                GET_SLOT(x, Matrix_DimNamesSym));                                GET_SLOT(x, Matrix_DimNamesSym));
# Line 282  Line 244 
244       *       since cholmod (& cs) lacks sparse 'int' matrices */       *       since cholmod (& cs) lacks sparse 'int' matrices */
245      CHM_SP chx = AS_CHM_SP__(x);      CHM_SP chx = AS_CHM_SP__(x);
246      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
247      CHM_SP chxt = cholmod_l_transpose(chx, chx->xtype, &c);      CHM_SP chxt = cholmod_transpose(chx, chx->xtype, &c);
248      SEXP dn = PROTECT(duplicate(GET_SLOT(x, Matrix_DimNamesSym))), tmp;      SEXP dn = PROTECT(duplicate(GET_SLOT(x, Matrix_DimNamesSym))), tmp;
249      int tr = asLogical(tri);      int tr = asLogical(tri);
250      R_CheckStack();      R_CheckStack();
# Line 301  Line 263 
263      CHM_SP      CHM_SP
264          cha = AS_CHM_SP(a),          cha = AS_CHM_SP(a),
265          chb = AS_CHM_SP(b),          chb = AS_CHM_SP(b),
266          chc = cholmod_l_ssmult(cha, chb, /*out_stype:*/ 0,          chc = cholmod_ssmult(cha, chb, /*out_stype:*/ 0,
267                                 /* values:= is_numeric (T/F) */ cha->xtype > 0,                                 /* values:= is_numeric (T/F) */ cha->xtype > 0,
268                                 /*out sorted:*/ 1, &c);                                 /*out sorted:*/ 1, &c);
269      const char *cl_a = class_P(a), *cl_b = class_P(b);      const char *cl_a = class_P(a), *cl_b = class_P(b);
# Line 352  Line 314 
314      SEXP dn = PROTECT(allocVector(VECSXP, 2));      SEXP dn = PROTECT(allocVector(VECSXP, 2));
315      R_CheckStack();      R_CheckStack();
316    
317      chTr = cholmod_l_transpose((tr) ? chb : cha, chb->xtype, &c);      chTr = cholmod_transpose((tr) ? chb : cha, chb->xtype, &c);
318      chc = cholmod_l_ssmult((tr) ? cha : chTr, (tr) ? chTr : chb,      chc = cholmod_ssmult((tr) ? cha : chTr, (tr) ? chTr : chb,
319                           /*out_stype:*/ 0, cha->xtype, /*out sorted:*/ 1, &c);                           /*out_stype:*/ 0, cha->xtype, /*out sorted:*/ 1, &c);
320      cholmod_l_free_sparse(&chTr, &c);      cholmod_free_sparse(&chTr, &c);
321    
322      /* Preserve triangularity and unit-triangularity if appropriate;      /* Preserve triangularity and unit-triangularity if appropriate;
323       * see Csparse_Csparse_prod() for comments */       * see Csparse_Csparse_prod() for comments */
# Line 381  Line 343 
343      CHM_SP cha = AS_CHM_SP(a);      CHM_SP cha = AS_CHM_SP(a);
344      SEXP b_M = PROTECT(mMatrix_as_dgeMatrix(b));      SEXP b_M = PROTECT(mMatrix_as_dgeMatrix(b));
345      CHM_DN chb = AS_CHM_DN(b_M);      CHM_DN chb = AS_CHM_DN(b_M);
346      CHM_DN chc = cholmod_l_allocate_dense(cha->nrow, chb->ncol, cha->nrow,      CHM_DN chc = cholmod_allocate_dense(cha->nrow, chb->ncol, cha->nrow,
347                                          chb->xtype, &c);                                          chb->xtype, &c);
348      SEXP dn = PROTECT(allocVector(VECSXP, 2));      SEXP dn = PROTECT(allocVector(VECSXP, 2));
349      double one[] = {1,0}, zero[] = {0,0};      double one[] = {1,0}, zero[] = {0,0};
# Line 398  Line 360 
360          SEXP da = PROTECT(nz2Csparse(a, x_double)); nprot++;          SEXP da = PROTECT(nz2Csparse(a, x_double)); nprot++;
361          cha = AS_CHM_SP(da);          cha = AS_CHM_SP(da);
362      }      }
363      cholmod_l_sdmult(cha, 0, one, zero, chb, chc, &c);      cholmod_sdmult(cha, 0, one, zero, chb, chc, &c);
364      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */
365                     duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), 0)));                     duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), 0)));
366      SET_VECTOR_ELT(dn, 1,      SET_VECTOR_ELT(dn, 1,
# Line 412  Line 374 
374      CHM_SP cha = AS_CHM_SP(a);      CHM_SP cha = AS_CHM_SP(a);
375      SEXP b_M = PROTECT(mMatrix_as_dgeMatrix(b));      SEXP b_M = PROTECT(mMatrix_as_dgeMatrix(b));
376      CHM_DN chb = AS_CHM_DN(b_M);      CHM_DN chb = AS_CHM_DN(b_M);
377      CHM_DN chc = cholmod_l_allocate_dense(cha->ncol, chb->ncol, cha->ncol,      CHM_DN chc = cholmod_allocate_dense(cha->ncol, chb->ncol, cha->ncol,
378                                          chb->xtype, &c);                                          chb->xtype, &c);
379      SEXP dn = PROTECT(allocVector(VECSXP, 2)); int nprot = 2;      SEXP dn = PROTECT(allocVector(VECSXP, 2)); int nprot = 2;
380      double one[] = {1,0}, zero[] = {0,0};      double one[] = {1,0}, zero[] = {0,0};
# Line 422  Line 384 
384          SEXP da = PROTECT(nz2Csparse(a, x_double)); nprot++;          SEXP da = PROTECT(nz2Csparse(a, x_double)); nprot++;
385          cha = AS_CHM_SP(da);          cha = AS_CHM_SP(da);
386      }      }
387      cholmod_l_sdmult(cha, 1, one, zero, chb, chc, &c);      cholmod_sdmult(cha, 1, one, zero, chb, chc, &c);
388      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */
389                     duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), 1)));                     duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), 1)));
390      SET_VECTOR_ELT(dn, 1,      SET_VECTOR_ELT(dn, 1,
# Line 445  Line 407 
407  #endif  #endif
408      CHM_SP chcp, chxt,      CHM_SP chcp, chxt,
409          chx = (trip ?          chx = (trip ?
410                 cholmod_l_triplet_to_sparse(cht, cht->nnz, &c) :                 cholmod_triplet_to_sparse(cht, cht->nnz, &c) :
411                 AS_CHM_SP(x));                 AS_CHM_SP(x));
412      SEXP dn = PROTECT(allocVector(VECSXP, 2));      SEXP dn = PROTECT(allocVector(VECSXP, 2));
413      R_CheckStack();      R_CheckStack();
414    
415      if (!tr) chxt = cholmod_l_transpose(chx, chx->xtype, &c);      if (!tr) chxt = cholmod_transpose(chx, chx->xtype, &c);
416      chcp = cholmod_l_aat((!tr) ? chxt : chx, (int *) NULL, 0, chx->xtype, &c);      chcp = cholmod_aat((!tr) ? chxt : chx, (int *) NULL, 0, chx->xtype, &c);
417      if(!chcp) {      if(!chcp) {
418          UNPROTECT(1);          UNPROTECT(1);
419          error(_("Csparse_crossprod(): error return from cholmod_l_aat()"));          error(_("Csparse_crossprod(): error return from cholmod_aat()"));
420      }      }
421      cholmod_l_band_inplace(0, chcp->ncol, chcp->xtype, chcp, &c);      cholmod_band_inplace(0, chcp->ncol, chcp->xtype, chcp, &c);
422      chcp->stype = 1;      chcp->stype = 1;
423      if (trip) cholmod_l_free_sparse(&chx, &c);      if (trip) cholmod_free_sparse(&chx, &c);
424      if (!tr) cholmod_l_free_sparse(&chxt, &c);      if (!tr) cholmod_free_sparse(&chxt, &c);
425      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */
426                     duplicate(VECTOR_ELT(GET_SLOT(x, Matrix_DimNamesSym),                     duplicate(VECTOR_ELT(GET_SLOT(x, Matrix_DimNamesSym),
427                                          (tr) ? 0 : 1)));                                          (tr) ? 0 : 1)));
# Line 480  Line 442 
442      /* dtCMatrix, etc; [1] = the second character =?= 't' for triangular */      /* dtCMatrix, etc; [1] = the second character =?= 't' for triangular */
443      int tr = (cl[1] == 't');      int tr = (cl[1] == 't');
444      CHM_SP chx = AS_CHM_SP__(x);      CHM_SP chx = AS_CHM_SP__(x);
445      CHM_SP ans = cholmod_l_copy(chx, chx->stype, chx->xtype, &c);      CHM_SP ans = cholmod_copy(chx, chx->stype, chx->xtype, &c);
446      double dtol = asReal(tol);      double dtol = asReal(tol);
447      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
448      R_CheckStack();      R_CheckStack();
449    
450      if(!cholmod_l_drop(dtol, ans, &c))      if(!cholmod_drop(dtol, ans, &c))
451          error(_("cholmod_l_drop() failed"));          error(_("cholmod_drop() failed"));
452      return chm_sparse_to_SEXP(ans, 1,      return chm_sparse_to_SEXP(ans, 1,
453                                tr ? ((*uplo_P(x) == 'U') ? 1 : -1) : 0,                                tr ? ((*uplo_P(x) == 'U') ? 1 : -1) : 0,
454                                Rkind, tr ? diag_P(x) : "",                                Rkind, tr ? diag_P(x) : "",
# Line 502  Line 464 
464      R_CheckStack();      R_CheckStack();
465    
466      /* TODO: currently drops dimnames - and we fix at R level */      /* TODO: currently drops dimnames - and we fix at R level */
467      return chm_sparse_to_SEXP(cholmod_l_horzcat(chx, chy, 1, &c),      return chm_sparse_to_SEXP(cholmod_horzcat(chx, chy, 1, &c),
468                                1, 0, Rkind, "", R_NilValue);                                1, 0, Rkind, "", R_NilValue);
469  }  }
470    
# Line 515  Line 477 
477      R_CheckStack();      R_CheckStack();
478    
479      /* TODO: currently drops dimnames - and we fix at R level */      /* TODO: currently drops dimnames - and we fix at R level */
480      return chm_sparse_to_SEXP(cholmod_l_vertcat(chx, chy, 1, &c),      return chm_sparse_to_SEXP(cholmod_vertcat(chx, chy, 1, &c),
481                                1, 0, Rkind, "", R_NilValue);                                1, 0, Rkind, "", R_NilValue);
482  }  }
483    
# Line 523  Line 485 
485  {  {
486      CHM_SP chx = AS_CHM_SP__(x);      CHM_SP chx = AS_CHM_SP__(x);
487      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
488      CHM_SP ans = cholmod_l_band(chx, asInteger(k1), asInteger(k2), chx->xtype, &c);      CHM_SP ans = cholmod_band(chx, asInteger(k1), asInteger(k2), chx->xtype, &c);
489      R_CheckStack();      R_CheckStack();
490    
491      return chm_sparse_to_SEXP(ans, 1, 0, Rkind, "",      return chm_sparse_to_SEXP(ans, 1, 0, Rkind, "",
# Line 541  Line 503 
503      }      }
504      else { /* unit triangular (diag='U'): "fill the diagonal" & diag:= "N" */      else { /* unit triangular (diag='U'): "fill the diagonal" & diag:= "N" */
505          CHM_SP chx = AS_CHM_SP__(x);          CHM_SP chx = AS_CHM_SP__(x);
506          CHM_SP eye = cholmod_l_speye(chx->nrow, chx->ncol, chx->xtype, &c);          CHM_SP eye = cholmod_speye(chx->nrow, chx->ncol, chx->xtype, &c);
507          double one[] = {1, 0};          double one[] = {1, 0};
508          CHM_SP ans = cholmod_l_add(chx, eye, one, one, TRUE, TRUE, &c);          CHM_SP ans = cholmod_add(chx, eye, one, one, TRUE, TRUE, &c);
509          int uploT = (*uplo_P(x) == 'U') ? 1 : -1;          int uploT = (*uplo_P(x) == 'U') ? 1 : -1;
510          int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;          int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
511    
512          R_CheckStack();          R_CheckStack();
513          cholmod_l_free_sparse(&eye, &c);          cholmod_free_sparse(&eye, &c);
514          return chm_sparse_to_SEXP(ans, 1, uploT, Rkind, "N",          return chm_sparse_to_SEXP(ans, 1, uploT, Rkind, "N",
515                                    GET_SLOT(x, Matrix_DimNamesSym));                                    GET_SLOT(x, Matrix_DimNamesSym));
516      }      }
# Line 565  Line 527 
527      }      }
528      else { /* triangular with diag='N'): now drop the diagonal */      else { /* triangular with diag='N'): now drop the diagonal */
529          /* duplicate, since chx will be modified: */          /* duplicate, since chx will be modified: */
530          CHM_SP chx = AS_CHM_SP__(duplicate(x));          SEXP xx = PROTECT(duplicate(x));
531            CHM_SP chx = AS_CHM_SP__(xx);
532          int uploT = (*uplo_P(x) == 'U') ? 1 : -1,          int uploT = (*uplo_P(x) == 'U') ? 1 : -1,
533              Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;              Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
534          R_CheckStack();          R_CheckStack();
535    
536          chm_diagN2U(chx, uploT, /* do_realloc */ FALSE);          chm_diagN2U(chx, uploT, /* do_realloc */ FALSE);
537    
538          return chm_sparse_to_SEXP(chx, /*dofree*/ 0/* or 1 ?? */,          SEXP ans = chm_sparse_to_SEXP(chx, /*dofree*/ 0/* or 1 ?? */,
539                                    uploT, Rkind, "U",                                    uploT, Rkind, "U",
540                                    GET_SLOT(x, Matrix_DimNamesSym));                                    GET_SLOT(x, Matrix_DimNamesSym));
541            UNPROTECT(1);// only now !
542            return ans;
543      }      }
544  }  }
545    
# Line 600  Line 565 
565      if (csize >= 0 && !isInteger(j))      if (csize >= 0 && !isInteger(j))
566          error(_("Index j must be NULL or integer"));          error(_("Index j must be NULL or integer"));
567    
568      if (chx->stype) /* symmetricMatrix */      if (!chx->stype) {/* non-symmetric Matrix */
569          /* for now, cholmod_submatrix() only accepts "generalMatrix" */          return chm_sparse_to_SEXP(cholmod_submatrix(chx,
         chx = cholmod_l_copy(chx, /* stype: */ 0, chx->xtype, &c);  
   
     return chm_sparse_to_SEXP(cholmod_l_submatrix(chx,  
570                                  (rsize < 0) ? NULL : INTEGER(i), rsize,                                  (rsize < 0) ? NULL : INTEGER(i), rsize,
571                                  (csize < 0) ? NULL : INTEGER(j), csize,                                  (csize < 0) ? NULL : INTEGER(j), csize,
572                                                    TRUE, TRUE, &c),                                                    TRUE, TRUE, &c),
573                                1, 0, Rkind, "",                                1, 0, Rkind, "",
574                                /* FIXME: drops dimnames */ R_NilValue);                                /* FIXME: drops dimnames */ R_NilValue);
575  }  }
576                                    /* for now, cholmod_submatrix() only accepts "generalMatrix" */
577        CHM_SP tmp = cholmod_copy(chx, /* stype: */ 0, chx->xtype, &c);
578        CHM_SP ans = cholmod_submatrix(tmp,
579                                       (rsize < 0) ? NULL : INTEGER(i), rsize,
580                                       (csize < 0) ? NULL : INTEGER(j), csize,
581                                       TRUE, TRUE, &c);
582        cholmod_free_sparse(&tmp, &c);
583        return chm_sparse_to_SEXP(ans, 1, 0, Rkind, "", R_NilValue);
584    }
585    
586    #define _d_Csp_
587    #include "t_Csparse_subassign.c"
588    
589    #define _l_Csp_
590    #include "t_Csparse_subassign.c"
591    
592    #define _i_Csp_
593    #include "t_Csparse_subassign.c"
594    
595    #define _n_Csp_
596    #include "t_Csparse_subassign.c"
597    
598    #define _z_Csp_
599    #include "t_Csparse_subassign.c"
600    
601    
602    
603  SEXP Csparse_MatrixMarket(SEXP x, SEXP fname)  SEXP Csparse_MatrixMarket(SEXP x, SEXP fname)
604  {  {
# Line 619  Line 607 
607      if (!f)      if (!f)
608          error(_("failure to open file \"%s\" for writing"),          error(_("failure to open file \"%s\" for writing"),
609                CHAR(asChar(fname)));                CHAR(asChar(fname)));
610      if (!cholmod_l_write_sparse(f, AS_CHM_SP(x),      if (!cholmod_write_sparse(f, AS_CHM_SP(x),
611                                (CHM_SP)NULL, (char*) NULL, &c))                                (CHM_SP)NULL, (char*) NULL, &c))
612          error(_("cholmod_l_write_sparse returned error code"));          error(_("cholmod_write_sparse returned error code"));
613      fclose(f);      fclose(f);
614      return R_NilValue;      return R_NilValue;
615  }  }
# Line 694  Line 682 
682      case diag_backpermuted:      case diag_backpermuted:
683          for_DIAG(v[i] = x_x[i_from]);          for_DIAG(v[i] = x_x[i_from]);
684    
685          warning(_("resultKind = 'diagBack' (back-permuted) is experimental"));          warning(_("%s = '%s' (back-permuted) is experimental"),
686                    "resultKind", "diagBack");
687          /* now back_permute : */          /* now back_permute : */
688          for(i = 0; i < n; i++) {          for(i = 0; i < n; i++) {
689              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;
# Line 835  Line 824 
824      if (cls[1] != 'g')      if (cls[1] != 'g')
825          error(_("Only 'g'eneral sparse matrix types allowed"));          error(_("Only 'g'eneral sparse matrix types allowed"));
826                                  /* allocate and populate the triplet */                                  /* allocate and populate the triplet */
827      T = cholmod_l_allocate_triplet((size_t)nrow, (size_t)ncol, (size_t)nnz, 0,      T = cholmod_allocate_triplet((size_t)nrow, (size_t)ncol, (size_t)nnz, 0,
828                                      xtype, &c);                                      xtype, &c);
829      T->x = x;      T->x = x;
830      tri = (int*)T->i;      tri = (int*)T->i;
# Line 845  Line 834 
834          trj[ii] = j[ii] - ((!mj && index1) ? 1 : 0);          trj[ii] = j[ii] - ((!mj && index1) ? 1 : 0);
835      }      }
836                                  /* create the cholmod_sparse structure */                                  /* create the cholmod_sparse structure */
837      A = cholmod_l_triplet_to_sparse(T, nnz, &c);      A = cholmod_triplet_to_sparse(T, nnz, &c);
838      cholmod_l_free_triplet(&T, &c);      cholmod_free_triplet(&T, &c);
839                                  /* copy the information to the SEXP */                                  /* copy the information to the SEXP */
840      ans = PROTECT(NEW_OBJECT(MAKE_CLASS(cls)));      ans = PROTECT(NEW_OBJECT(MAKE_CLASS(cls)));
841  /* FIXME: This has been copied from chm_sparse_to_SEXP in chm_common.c */  /* FIXME: This has been copied from chm_sparse_to_SEXP in chm_common.c */
842                                  /* allocate and copy common slots */                                  /* allocate and copy common slots */
843      nnz = cholmod_l_nnz(A, &c);      nnz = cholmod_nnz(A, &c);
844      dims = INTEGER(ALLOC_SLOT(ans, Matrix_DimSym, INTSXP, 2));      dims = INTEGER(ALLOC_SLOT(ans, Matrix_DimSym, INTSXP, 2));
845      dims[0] = A->nrow; dims[1] = A->ncol;      dims[0] = A->nrow; dims[1] = A->ncol;
846      Memcpy(INTEGER(ALLOC_SLOT(ans, Matrix_pSym, INTSXP, A->ncol + 1)), (int*)A->p, A->ncol + 1);      Memcpy(INTEGER(ALLOC_SLOT(ans, Matrix_pSym, INTSXP, A->ncol + 1)), (int*)A->p, A->ncol + 1);
# Line 864  Line 853 
853          error(_("code not yet written for cls = \"lgCMatrix\""));          error(_("code not yet written for cls = \"lgCMatrix\""));
854      }      }
855  /* FIXME: dimnames are *NOT* put there yet (if non-NULL) */  /* FIXME: dimnames are *NOT* put there yet (if non-NULL) */
856      cholmod_l_free_sparse(&A, &c);      cholmod_free_sparse(&A, &c);
857      UNPROTECT(1);      UNPROTECT(1);
858      return ans;      return ans;
859  }  }

Legend:
Removed from v.2646  
changed lines
  Added in v.2889

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