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 3023, Sat Dec 20 22:29:49 2014 UTC revision 3055, Sat Mar 14 21:12:03 2015 UTC
# Line 101  Line 101 
101      return ScalarLogical(1);      return ScalarLogical(1);
102  }  }
103    
104    /**
105  /* Called from ../R/Csparse.R : */   * From a CsparseMatrix, produce a dense one.
106  /* Can only return [dln]geMatrix (no symm/triang);   * Directly deals with symmetric, triangular and general.
107   * FIXME: replace by non-CHOLMOD code ! */   * Called from ../R/Csparse.R's  C2dense()
108  SEXP Csparse_to_dense(SEXP x)   *
109     * @param x a CsparseMatrix: currently all 9 of  "[dln][gst]CMatrix"
110     * @param symm_or_tri integer (NA, < 0, > 0, = 0) specifying the knowledge of the caller about x:
111     *      NA  : unknown => will be determined
112     *      = 0 : "generalMatrix" (not symm or tri);
113     *      < 0 : "triangularMatrix"
114     *      > 0 : "symmetricMatrix"
115     *
116     * @return a "denseMatrix"
117     */
118    SEXP Csparse_to_dense(SEXP x, SEXP symm_or_tri)
119  {  {
120      CHM_SP chxs = AS_CHM_SP__(x);      Rboolean is_sym, is_tri;
121      /* This loses the symmetry property, since cholmod_dense has none,      int is_sym_or_tri = asInteger(symm_or_tri),
122            ctype = 0; // <- default = "dgC"
123        static const char *valid[] = { MATRIX_VALID_Csparse, ""};
124        if(is_sym_or_tri == NA_INTEGER) { // find if  is(x, "symmetricMatrix") :
125            ctype = Matrix_check_class_etc(x, valid);
126            is_sym = (ctype % 3 == 1);
127            is_tri = (ctype % 3 == 2);
128        } else {
129            is_sym = is_sym_or_tri > 0;
130            is_tri = is_sym_or_tri < 0;
131            // => both are FALSE  iff  is_.. == 0
132            if(is_sym || is_tri)
133                ctype = Matrix_check_class_etc(x, valid);
134        }
135        CHM_SP chxs = AS_CHM_SP__(x);// -> chxs->stype = +- 1 <==> symmetric
136        R_CheckStack();
137        if(is_tri && *diag_P(x) == 'U') { // ==>  x := diagU2N(x), directly for chxs
138            CHM_SP eye = cholmod_speye(chxs->nrow, chxs->ncol, chxs->xtype, &c);
139            double one[] = {1, 0};
140            CHM_SP ans = cholmod_add(chxs, eye, one, one,
141                                     /* values: */ ((ctype / 3) != 2), // TRUE iff not "nMatrix"
142                                     TRUE, &c);
143            cholmod_free_sparse(&eye, &c);
144            chxs = cholmod_copy_sparse(ans, &c);
145            cholmod_free_sparse(&ans, &c);
146        }
147        /* The following loses the symmetry property, since cholmod_dense has none,
148       * BUT, much worse (FIXME!), it also transforms CHOLMOD_PATTERN ("n") matrices       * BUT, much worse (FIXME!), it also transforms CHOLMOD_PATTERN ("n") matrices
149       * to numeric (CHOLMOD_REAL) ones : */       * to numeric (CHOLMOD_REAL) ones {and we "revert" via chm_dense_to_SEXP()}: */
150      CHM_DN chxd = cholmod_sparse_to_dense(chxs, &c);      CHM_DN chxd = cholmod_sparse_to_dense(chxs, &c);
151      int Rkind = (chxs->xtype == CHOLMOD_PATTERN)? -1 : Real_kind(x);      int Rkind = (chxs->xtype == CHOLMOD_PATTERN)? -1 : Real_kind(x);
     R_CheckStack();  
152    
153      return chm_dense_to_SEXP(chxd, 1, Rkind, GET_SLOT(x, Matrix_DimNamesSym));      SEXP ans = chm_dense_to_SEXP(chxd, 1, Rkind,GET_SLOT(x, Matrix_DimNamesSym));
154        // -> a [dln]geMatrix
155        if(is_sym) { // ==> want  [dln]syMatrix
156            const char cl1 = class_P(ans)[0];
157            PROTECT(ans);
158            SEXP aa = PROTECT(NEW_OBJECT(MAKE_CLASS((cl1 == 'd') ? "dsyMatrix" :
159                                                    ((cl1 == 'l') ? "lsyMatrix" : "nsyMatrix"))));
160            // No need to duplicate() as slots of ans are freshly allocated and ans will not be used
161            SET_SLOT(aa, Matrix_xSym,       GET_SLOT(ans, Matrix_xSym));
162            SET_SLOT(aa, Matrix_DimSym,     GET_SLOT(ans, Matrix_DimSym));
163            SET_SLOT(aa, Matrix_DimNamesSym,GET_SLOT(ans, Matrix_DimNamesSym));
164            SET_SLOT(aa, Matrix_uploSym, mkString((chxs->stype > 0) ? "U" : "L"));
165            UNPROTECT(2);
166            return aa;
167        }
168        else if(is_tri) { // ==> want  [dln]trMatrix
169            const char cl1 = class_P(ans)[0];
170            PROTECT(ans);
171            SEXP aa = PROTECT(NEW_OBJECT(MAKE_CLASS((cl1 == 'd') ? "dtrMatrix" :
172                                                    ((cl1 == 'l') ? "ltrMatrix" : "ntrMatrix"))));
173            // No need to duplicate() as slots of ans are freshly allocated and ans will not be used
174            SET_SLOT(aa, Matrix_xSym,       GET_SLOT(ans, Matrix_xSym));
175            SET_SLOT(aa, Matrix_DimSym,     GET_SLOT(ans, Matrix_DimSym));
176            SET_SLOT(aa, Matrix_DimNamesSym,GET_SLOT(ans, Matrix_DimNamesSym));
177            slot_dup(aa, x, Matrix_uploSym);
178            /* already by NEW_OBJECT(..) above:
179               SET_SLOT(aa, Matrix_diagSym, mkString("N")); */
180            UNPROTECT(2);
181            return aa;
182        }
183        else
184            return ans;
185  }  }
186    
187  // FIXME: do not go via CHM (should not be too hard, to just *drop* the x-slot, right?  // FIXME: do not go via CHM (should not be too hard, to just *drop* the x-slot, right?
# Line 137  Line 203 
203  {  {
204      return nz2Csparse(x, asInteger(res_kind));      return nz2Csparse(x, asInteger(res_kind));
205  }  }
206    
207  // n.CMatrix --> [dli].CMatrix  (not going through CHM!)  // n.CMatrix --> [dli].CMatrix  (not going through CHM!)
208    // NOTE: use chm_MOD_xtype(() to change type of  'cholmod_sparse' matrix
209  SEXP nz2Csparse(SEXP x, enum x_slot_kind r_kind)  SEXP nz2Csparse(SEXP x, enum x_slot_kind r_kind)
210  {  {
211      const char *cl_x = class_P(x);      const char *cl_x = class_P(x);
# Line 186  Line 254 
254      return ans;      return ans;
255  }  }
256    
257  SEXP Csparse_to_matrix(SEXP x, SEXP chk)  SEXP Csparse_to_matrix(SEXP x, SEXP chk, SEXP symm)
258  {  {
259      return chm_dense_to_matrix(cholmod_sparse_to_dense(AS_CHM_SP2(x, asLogical(chk)), &c),      int is_sym = asLogical(symm);
260                                 1 /*do_free*/, GET_SLOT(x, Matrix_DimNamesSym));      if(is_sym == NA_LOGICAL) { // find if  is(x, "symmetricMatrix") :
261            static const char *valid[] = { MATRIX_VALID_Csparse, ""};
262            int ctype = Matrix_check_class_etc(x, valid);
263            is_sym = (ctype % 3 == 1);
264        }
265        return chm_dense_to_matrix(
266            cholmod_sparse_to_dense(AS_CHM_SP2(x, asLogical(chk)), &c),
267            1 /*do_free*/,
268            (is_sym
269             ? symmetric_DimNames(GET_SLOT(x, Matrix_DimNamesSym))
270             :                    GET_SLOT(x, Matrix_DimNamesSym)));
271  }  }
272    
273  SEXP Csparse_to_vector(SEXP x)  SEXP Csparse_to_vector(SEXP x)
274  {  {
275      return chm_dense_to_vector(cholmod_sparse_to_dense(AS_CHM_SP__(x), &c), 1);      return chm_dense_to_vector(cholmod_sparse_to_dense(AS_CHM_SP__(x), &c), 1);
# Line 234  Line 313 
313  }  }
314    
315    
 /* this used to be called  sCMatrix_to_gCMatrix(..)   [in ./dsCMatrix.c ]: */  
316  SEXP Csparse_symmetric_to_general(SEXP x)  SEXP Csparse_symmetric_to_general(SEXP x)
317  {  {
318      CHM_SP chx = AS_CHM_SP__(x), chgx;      CHM_SP chx = AS_CHM_SP__(x), chgx;
# Line 246  Line 324 
324      chgx = cholmod_copy(chx, /* stype: */ 0, chx->xtype, &c);      chgx = cholmod_copy(chx, /* stype: */ 0, chx->xtype, &c);
325      /* xtype: pattern, "real", complex or .. */      /* xtype: pattern, "real", complex or .. */
326      return chm_sparse_to_SEXP(chgx, 1, 0, Rkind, "",      return chm_sparse_to_SEXP(chgx, 1, 0, Rkind, "",
327                                GET_SLOT(x, Matrix_DimNamesSym));                                symmetric_DimNames(GET_SLOT(x, Matrix_DimNamesSym)));
328  }  }
329    
330  SEXP Csparse_general_to_symmetric(SEXP x, SEXP uplo)  SEXP Csparse_general_to_symmetric(SEXP x, SEXP uplo, SEXP sym_dmns)
331  {  {
332      int *adims = INTEGER(GET_SLOT(x, Matrix_DimSym)), n = adims[0];      int *adims = INTEGER(GET_SLOT(x, Matrix_DimSym)), n = adims[0];
333      if(n != adims[1]) {      if(n != adims[1]) {
# Line 262  Line 340 
340      R_CheckStack();      R_CheckStack();
341      chgx = cholmod_copy(chx, /* stype: */ uploT, chx->xtype, &c);      chgx = cholmod_copy(chx, /* stype: */ uploT, chx->xtype, &c);
342    
343      /* need _symmetric_ dimnames */      SEXP dns = GET_SLOT(x, Matrix_DimNamesSym);
344      SEXP dns = PROTECT(duplicate(GET_SLOT(x, Matrix_DimNamesSym))),      if(asLogical(sym_dmns))
345          nms_dns = getAttrib(dns, R_NamesSymbol);          dns = symmetric_DimNames(dns);
346        else if((!isNull(VECTOR_ELT(dns, 0)) &&
347                 !isNull(VECTOR_ELT(dns, 1))) ||
348                !isNull(getAttrib(dns, R_NamesSymbol))) {
349            /* symmetrize them if both are not NULL
350             * or names(dimnames(.)) is asymmetric : */
351            dns = PROTECT(duplicate(dns));
352      if(!equal_string_vectors(VECTOR_ELT(dns, 0),      if(!equal_string_vectors(VECTOR_ELT(dns, 0),
353                               VECTOR_ELT(dns, 1))) {                               VECTOR_ELT(dns, 1))) {
354          if(uploT == 1)          if(uploT == 1)
# Line 272  Line 356 
356          else          else
357              SET_VECTOR_ELT(dns, 1, VECTOR_ELT(dns,0));              SET_VECTOR_ELT(dns, 1, VECTOR_ELT(dns,0));
358      }      }
359            SEXP nms_dns = getAttrib(dns, R_NamesSymbol);
360      if(!isNull(nms_dns) &&  // names(dimnames(.)) :      if(!isNull(nms_dns) &&  // names(dimnames(.)) :
361         !R_compute_identical(STRING_ELT(nms_dns, 0),         !R_compute_identical(STRING_ELT(nms_dns, 0),
362                              STRING_ELT(nms_dns, 1), 15)) {                                  STRING_ELT(nms_dns, 1), 16)) {
363          if(uploT == 1)          if(uploT == 1)
364              SET_STRING_ELT(nms_dns, 0, STRING_ELT(nms_dns,1));              SET_STRING_ELT(nms_dns, 0, STRING_ELT(nms_dns,1));
365          else          else
366              SET_STRING_ELT(nms_dns, 1, STRING_ELT(nms_dns,0));              SET_STRING_ELT(nms_dns, 1, STRING_ELT(nms_dns,0));
367          setAttrib(dns, R_NamesSymbol, nms_dns);          setAttrib(dns, R_NamesSymbol, nms_dns);
368      }      }
   
369      UNPROTECT(1);      UNPROTECT(1);
370        }
371      /* xtype: pattern, "real", complex or .. */      /* xtype: pattern, "real", complex or .. */
372      return chm_sparse_to_SEXP(chgx, 1, 0, Rkind, "", dns);      return chm_sparse_to_SEXP(chgx, 1, 0, Rkind, "", dns);
373  }  }
# Line 515  Line 600 
600    
601  SEXP Csparse_horzcat(SEXP x, SEXP y)  SEXP Csparse_horzcat(SEXP x, SEXP y)
602  {  {
603      CHM_SP chx = AS_CHM_SP__(x), chy = AS_CHM_SP__(y);  #define CSPARSE_CAT(_KIND_)                                             \
604      int Rk_x = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0,      CHM_SP chx = AS_CHM_SP__(x), chy = AS_CHM_SP__(y);                  \
605          Rk_y = (chy->xtype != CHOLMOD_PATTERN) ? Real_kind(y) : 0,      R_CheckStack();                                                     \
606          Rkind = /* logical if both x and y are */ (Rk_x == 1 && Rk_y == 1) ? 1 : 0;      int Rk_x = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : -3,     \
607      R_CheckStack();          Rk_y = (chy->xtype != CHOLMOD_PATTERN) ? Real_kind(y) : -3, Rkind; \
608        if(Rk_x == -3 || Rk_y == -3) { /* at least one of them is patter"n" */ \
609            if(Rk_x == -3 && Rk_y == -3) { /* fine */                       \
610            } else { /* only one is a patter"n"                             \
611                      * "Bug" in cholmod_horzcat()/vertcat(): returns patter"n" matrix if one of them is */ \
612                Rboolean ok;                                                \
613                if(Rk_x == -3) {                                            \
614                    ok = chm_MOD_xtype(CHOLMOD_REAL, chx, &c); Rk_x = 0;    \
615                } else if(Rk_y == -3) {                                     \
616                    ok = chm_MOD_xtype(CHOLMOD_REAL, chy, &c); Rk_y = 0;    \
617                } else                                                      \
618                    error(_("Impossible Rk_x/Rk_y in Csparse_%s(), please report"), _KIND_); \
619                if(!ok)                                                     \
620                    error(_("chm_MOD_xtype() was not successful in Csparse_%s(), please report"), \
621                          _KIND_);                                          \
622            }                                                               \
623        }                                                                   \
624        Rkind = /* logical if both x and y are */ (Rk_x == 1 && Rk_y == 1) ? 1 : 0
625    
626        CSPARSE_CAT("horzcat");
627        // TODO: currently drops dimnames - and we fix at R level;
628    
     /* TODO: currently drops dimnames - and we fix at R level */  
629      return chm_sparse_to_SEXP(cholmod_horzcat(chx, chy, 1, &c),      return chm_sparse_to_SEXP(cholmod_horzcat(chx, chy, 1, &c),
630                                1, 0, Rkind, "", R_NilValue);                                1, 0, Rkind, "", R_NilValue);
631  }  }
632    
633  SEXP Csparse_vertcat(SEXP x, SEXP y)  SEXP Csparse_vertcat(SEXP x, SEXP y)
634  {  {
635      CHM_SP chx = AS_CHM_SP__(x), chy = AS_CHM_SP__(y);      CSPARSE_CAT("vertcat");
636      int Rk_x = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0,      // TODO: currently drops dimnames - and we fix at R level;
         Rk_y = (chy->xtype != CHOLMOD_PATTERN) ? Real_kind(y) : 0,  
         Rkind = /* logical if both x and y are */ (Rk_x == 1 && Rk_y == 1) ? 1 : 0;  
     R_CheckStack();  
637    
     /* TODO: currently drops dimnames - and we fix at R level */  
638      return chm_sparse_to_SEXP(cholmod_vertcat(chx, chy, 1, &c),      return chm_sparse_to_SEXP(cholmod_vertcat(chx, chy, 1, &c),
639                                1, 0, Rkind, "", R_NilValue);                                1, 0, Rkind, "", R_NilValue);
640  }  }
# Line 938  Line 1038 
1038      cholmod_free_triplet(&T, &c);      cholmod_free_triplet(&T, &c);
1039      /* copy the information to the SEXP */      /* copy the information to the SEXP */
1040      ans = PROTECT(NEW_OBJECT(MAKE_CLASS(cls)));      ans = PROTECT(NEW_OBJECT(MAKE_CLASS(cls)));
1041  /* 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
1042      /* allocate and copy common slots */      /* allocate and copy common slots */
1043      nnz = cholmod_nnz(A, &c);      nnz = cholmod_nnz(A, &c);
1044      dims = INTEGER(ALLOC_SLOT(ans, Matrix_DimSym, INTSXP, 2));      dims = INTEGER(ALLOC_SLOT(ans, Matrix_DimSym, INTSXP, 2));

Legend:
Removed from v.3023  
changed lines
  Added in v.3055

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