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 3076, Mon Mar 30 10:23:42 2015 UTC
# Line 1  Line 1 
1                          /* Sparse matrices in compressed column-oriented form */  /** @file Csparse.c
2     * The "CsparseMatrix" class from R package Matrix:
3     *
4     * Sparse matrices in compressed column-oriented form
5     */
6  #include "Csparse.h"  #include "Csparse.h"
7  #include "Tsparse.h"  #include "Tsparse.h"
8  #include "chm_common.h"  #include "chm_common.h"
# Line 36  Line 40 
40      return Csparse_validate_(x, FALSE);      return Csparse_validate_(x, FALSE);
41  }  }
42    
 SEXP Csparse_validate2(SEXP x, SEXP maybe_modify) {  
     return Csparse_validate_(x, asLogical(maybe_modify));  
 }  
43    
44  SEXP Csparse_validate_(SEXP x, Rboolean maybe_modify)  #define _t_Csparse_validate
45  {  #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);  
46    
47      if (length(pslot) != dims[1] + 1)  #define _t_Csparse_sort
48          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 !!! */  
49    
50              /* Now re-check that row indices are *strictly* increasing  // R: .validateCsparse(x, sort.if.needed = FALSE) :
51               * (and not just increasing) within each column : */  SEXP Csparse_validate2(SEXP x, SEXP maybe_modify) {
52              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"));  
53      }      }
54      return ScalarLogical(1);  
55    // R: Matrix:::.sortCsparse(x) :
56    SEXP Csparse_sort (SEXP x) {
57       int ok = Csparse_sort_2(x, TRUE); // modifying x directly
58       if(!ok) warning(_("Csparse_sort(x): x is not a valid (apart from sorting) CsparseMatrix"));
59       return x;
60  }  }
61    
62  SEXP Rsparse_validate(SEXP x)  SEXP Rsparse_validate(SEXP x)
# Line 136  Line 96 
96              }              }
97      }      }
98      if (!sorted)      if (!sorted)
99          /* cannot easily use cholmod_l_sort(.) ... -> "error out" :*/          /* cannot easily use cholmod_sort(.) ... -> "error out" :*/
100          return mkString(_("slot j is not increasing inside a column"));          return mkString(_("slot j is not increasing inside a column"));
101      else if(!strictly) /* sorted, but not strictly */      else if(!strictly) /* sorted, but not strictly */
102          return mkString(_("slot j is not *strictly* increasing inside a column"));          return mkString(_("slot j is not *strictly* increasing inside a column"));
# Line 144  Line 104 
104      return ScalarLogical(1);      return ScalarLogical(1);
105  }  }
106    
107    /** @brief From a CsparseMatrix, produce a dense one.
108  /* Called from ../R/Csparse.R : */   *
109  /* Can only return [dln]geMatrix (no symm/triang);   * Directly deals with symmetric, triangular and general.
110   * FIXME: replace by non-CHOLMOD code ! */   * Called from ../R/Csparse.R's  C2dense()
111  SEXP Csparse_to_dense(SEXP x)   *
112     * @param x a CsparseMatrix: currently all 9 of  "[dln][gst]CMatrix"
113     * @param symm_or_tri integer (NA, < 0, > 0, = 0) specifying the knowledge of the caller about x:
114     *      NA  : unknown => will be determined
115     *      = 0 : "generalMatrix" (not symm or tri);
116     *      < 0 : "triangularMatrix"
117     *      > 0 : "symmetricMatrix"
118     *
119     * @return a "denseMatrix"
120     */
121    SEXP Csparse_to_dense(SEXP x, SEXP symm_or_tri)
122  {  {
123      CHM_SP chxs = AS_CHM_SP__(x);      Rboolean is_sym, is_tri;
124      /* This loses the symmetry property, since cholmod_dense has none,      int is_sym_or_tri = asInteger(symm_or_tri),
125            ctype = 0; // <- default = "dgC"
126        static const char *valid[] = { MATRIX_VALID_Csparse, ""};
127        if(is_sym_or_tri == NA_INTEGER) { // find if  is(x, "symmetricMatrix") :
128            ctype = Matrix_check_class_etc(x, valid);
129            is_sym = (ctype % 3 == 1);
130            is_tri = (ctype % 3 == 2);
131        } else {
132            is_sym = is_sym_or_tri > 0;
133            is_tri = is_sym_or_tri < 0;
134            // => both are FALSE  iff  is_.. == 0
135            if(is_sym || is_tri)
136                ctype = Matrix_check_class_etc(x, valid);
137        }
138        CHM_SP chxs = AS_CHM_SP__(x);// -> chxs->stype = +- 1 <==> symmetric
139        R_CheckStack();
140        if(is_tri && *diag_P(x) == 'U') { // ==>  x := diagU2N(x), directly for chxs
141            CHM_SP eye = cholmod_speye(chxs->nrow, chxs->ncol, chxs->xtype, &c);
142            double one[] = {1, 0};
143            CHM_SP ans = cholmod_add(chxs, eye, one, one,
144                                     /* values: */ ((ctype / 3) != 2), // TRUE iff not "nMatrix"
145                                     TRUE, &c);
146            cholmod_free_sparse(&eye, &c);
147            chxs = cholmod_copy_sparse(ans, &c);
148            cholmod_free_sparse(&ans, &c);
149        }
150        /* The following loses the symmetry property, since cholmod_dense has none,
151       * BUT, much worse (FIXME!), it also transforms CHOLMOD_PATTERN ("n") matrices       * BUT, much worse (FIXME!), it also transforms CHOLMOD_PATTERN ("n") matrices
152       * to numeric (CHOLMOD_REAL) ones : */       * to numeric (CHOLMOD_REAL) ones {and we "revert" via chm_dense_to_SEXP()}: */
153      CHM_DN chxd = cholmod_l_sparse_to_dense(chxs, &c);      CHM_DN chxd = cholmod_sparse_to_dense(chxs, &c);
154      int Rkind = (chxs->xtype == CHOLMOD_PATTERN)? -1 : Real_kind(x);      int Rkind = (chxs->xtype == CHOLMOD_PATTERN)? -1 : Real_kind(x);
     R_CheckStack();  
155    
156      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),
157                                     /* transp: */ FALSE);
158        // -> a [dln]geMatrix
159        if(is_sym) { // ==> want  [dln]syMatrix
160            const char cl1 = class_P(ans)[0];
161            PROTECT(ans);
162            SEXP aa = PROTECT(NEW_OBJECT(MAKE_CLASS((cl1 == 'd') ? "dsyMatrix" :
163                                                    ((cl1 == 'l') ? "lsyMatrix" : "nsyMatrix"))));
164            // No need to duplicate() as slots of ans are freshly allocated and ans will not be used
165            SET_SLOT(aa, Matrix_xSym,       GET_SLOT(ans, Matrix_xSym));
166            SET_SLOT(aa, Matrix_DimSym,     GET_SLOT(ans, Matrix_DimSym));
167            SET_SLOT(aa, Matrix_DimNamesSym,GET_SLOT(ans, Matrix_DimNamesSym));
168            SET_SLOT(aa, Matrix_uploSym, mkString((chxs->stype > 0) ? "U" : "L"));
169            UNPROTECT(2);
170            return aa;
171        }
172        else if(is_tri) { // ==> want  [dln]trMatrix
173            const char cl1 = class_P(ans)[0];
174            PROTECT(ans);
175            SEXP aa = PROTECT(NEW_OBJECT(MAKE_CLASS((cl1 == 'd') ? "dtrMatrix" :
176                                                    ((cl1 == 'l') ? "ltrMatrix" : "ntrMatrix"))));
177            // No need to duplicate() as slots of ans are freshly allocated and ans will not be used
178            SET_SLOT(aa, Matrix_xSym,       GET_SLOT(ans, Matrix_xSym));
179            SET_SLOT(aa, Matrix_DimSym,     GET_SLOT(ans, Matrix_DimSym));
180            SET_SLOT(aa, Matrix_DimNamesSym,GET_SLOT(ans, Matrix_DimNamesSym));
181            slot_dup(aa, x, Matrix_uploSym);
182            /* already by NEW_OBJECT(..) above:
183               SET_SLOT(aa, Matrix_diagSym, mkString("N")); */
184            UNPROTECT(2);
185            return aa;
186        }
187        else
188            return ans;
189  }  }
190    
191  // 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?
192  SEXP Csparse_to_nz_pattern(SEXP x, SEXP tri)  SEXP Csparse2nz(SEXP x, Rboolean tri)
193  {  {
194      CHM_SP chxs = AS_CHM_SP__(x);      CHM_SP chxs = AS_CHM_SP__(x);
195      CHM_SP chxcp = cholmod_l_copy(chxs, chxs->stype, CHOLMOD_PATTERN, &c);      CHM_SP chxcp = cholmod_copy(chxs, chxs->stype, CHOLMOD_PATTERN, &c);
     int tr = asLogical(tri);  
196      R_CheckStack();      R_CheckStack();
197    
198      return chm_sparse_to_SEXP(chxcp, 1/*do_free*/,      return chm_sparse_to_SEXP(chxcp, 1/*do_free*/,
199                                tr ? ((*uplo_P(x) == 'U') ? 1 : -1) : 0,                                tri ? ((*uplo_P(x) == 'U') ? 1 : -1) : 0,
200                                0, tr ? diag_P(x) : "",                                /* Rkind: pattern */ 0,
201                                  /* diag = */ tri ? diag_P(x) : "",
202                                GET_SLOT(x, Matrix_DimNamesSym));                                GET_SLOT(x, Matrix_DimNamesSym));
203  }  }
204    SEXP Csparse_to_nz_pattern(SEXP x, SEXP tri)
205    {
206        int tr_ = asLogical(tri);
207        if(tr_ == NA_LOGICAL) {
208            warning(_("Csparse_to_nz_pattern(x, tri = NA): 'tri' is taken as TRUE"));
209            tr_ = TRUE;
210        }
211        return Csparse2nz(x, (Rboolean) tr_);
212    }
213    
214  // n.CMatrix --> [dli].CMatrix  (not going through CHM!)  // n.CMatrix --> [dli].CMatrix  (not going through CHM!)
215  SEXP nz_pattern_to_Csparse(SEXP x, SEXP res_kind)  SEXP nz_pattern_to_Csparse(SEXP x, SEXP res_kind)
216  {  {
217      return nz2Csparse(x, asInteger(res_kind));      return nz2Csparse(x, asInteger(res_kind));
218  }  }
219    
220  // n.CMatrix --> [dli].CMatrix  (not going through CHM!)  // n.CMatrix --> [dli].CMatrix  (not going through CHM!)
221    // NOTE: use chm_MOD_xtype(() to change type of  'cholmod_sparse' matrix
222  SEXP nz2Csparse(SEXP x, enum x_slot_kind r_kind)  SEXP nz2Csparse(SEXP x, enum x_slot_kind r_kind)
223  {  {
224      const char *cl_x = class_P(x);      const char *cl_x = class_P(x);
# Line 188  Line 226 
226      if(cl_x[2] != 'C') error(_("not a CsparseMatrix"));      if(cl_x[2] != 'C') error(_("not a CsparseMatrix"));
227      int nnz = LENGTH(GET_SLOT(x, Matrix_iSym));      int nnz = LENGTH(GET_SLOT(x, Matrix_iSym));
228      SEXP ans;      SEXP ans;
229      char *ncl = strdup(cl_x);      char *ncl = alloca(strlen(cl_x) + 1); /* not much memory required */
230        strcpy(ncl, cl_x);
231      double *dx_x; int *ix_x;      double *dx_x; int *ix_x;
232      ncl[0] = (r_kind == x_double ? 'd' :      ncl[0] = (r_kind == x_double ? 'd' :
233                (r_kind == x_logical ? 'l' :                (r_kind == x_logical ? 'l' :
# Line 228  Line 267 
267      return ans;      return ans;
268  }  }
269    
270  SEXP Csparse_to_matrix(SEXP x)  SEXP Csparse_to_matrix(SEXP x, SEXP chk, SEXP symm)
271    {
272        int is_sym = asLogical(symm);
273        if(is_sym == NA_LOGICAL) { // find if  is(x, "symmetricMatrix") :
274            static const char *valid[] = { MATRIX_VALID_Csparse, ""};
275            int ctype = Matrix_check_class_etc(x, valid);
276            is_sym = (ctype % 3 == 1);
277        }
278        return chm_dense_to_matrix(
279            cholmod_sparse_to_dense(AS_CHM_SP2(x, asLogical(chk)), &c),
280            1 /*do_free*/,
281            (is_sym
282             ? symmetric_DimNames(GET_SLOT(x, Matrix_DimNamesSym))
283             :                    GET_SLOT(x, Matrix_DimNamesSym)));
284    }
285    
286    SEXP Csparse_to_vector(SEXP x)
287  {  {
288      return chm_dense_to_matrix(cholmod_l_sparse_to_dense(AS_CHM_SP__(x), &c),      return chm_dense_to_vector(cholmod_sparse_to_dense(AS_CHM_SP__(x), &c), 1);
                                1 /*do_free*/, GET_SLOT(x, Matrix_DimNamesSym));  
289  }  }
290    
291  SEXP Csparse_to_Tsparse(SEXP x, SEXP tri)  SEXP Csparse_to_Tsparse(SEXP x, SEXP tri)
292  {  {
293      CHM_SP chxs = AS_CHM_SP__(x);      CHM_SP chxs = AS_CHM_SP__(x);
294      CHM_TR chxt = cholmod_l_sparse_to_triplet(chxs, &c);      CHM_TR chxt = cholmod_sparse_to_triplet(chxs, &c);
295      int tr = asLogical(tri);      int tr = asLogical(tri);
296      int Rkind = (chxs->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;      int Rkind = (chxs->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
297      R_CheckStack();      R_CheckStack();
# Line 248  Line 302 
302                                 GET_SLOT(x, Matrix_DimNamesSym));                                 GET_SLOT(x, Matrix_DimNamesSym));
303  }  }
304    
305  /* this used to be called  sCMatrix_to_gCMatrix(..)   [in ./dsCMatrix.c ]: */  SEXP Csparse_to_tCsparse(SEXP x, SEXP uplo, SEXP diag)
306    {
307        CHM_SP chxs = AS_CHM_SP__(x);
308        int Rkind = (chxs->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
309        R_CheckStack();
310        return chm_sparse_to_SEXP(chxs, /* dofree = */ 0,
311                                  /* uploT = */ (*CHAR(asChar(uplo)) == 'U')? 1: -1,
312                                   Rkind, /* diag = */ CHAR(STRING_ELT(diag, 0)),
313                                   GET_SLOT(x, Matrix_DimNamesSym));
314    }
315    
316    SEXP Csparse_to_tTsparse(SEXP x, SEXP uplo, SEXP diag)
317    {
318        CHM_SP chxs = AS_CHM_SP__(x);
319        CHM_TR chxt = cholmod_sparse_to_triplet(chxs, &c);
320        int Rkind = (chxs->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
321        R_CheckStack();
322        return chm_triplet_to_SEXP(chxt, 1,
323                                  /* uploT = */ (*CHAR(asChar(uplo)) == 'U')? 1: -1,
324                                   Rkind, /* diag = */ CHAR(STRING_ELT(diag, 0)),
325                                   GET_SLOT(x, Matrix_DimNamesSym));
326    }
327    
328    
329  SEXP Csparse_symmetric_to_general(SEXP x)  SEXP Csparse_symmetric_to_general(SEXP x)
330  {  {
331      CHM_SP chx = AS_CHM_SP__(x), chgx;      CHM_SP chx = AS_CHM_SP__(x), chgx;
# Line 257  Line 334 
334    
335      if (!(chx->stype))      if (!(chx->stype))
336          error(_("Nonsymmetric matrix in Csparse_symmetric_to_general"));          error(_("Nonsymmetric matrix in Csparse_symmetric_to_general"));
337      chgx = cholmod_l_copy(chx, /* stype: */ 0, chx->xtype, &c);      chgx = cholmod_copy(chx, /* stype: */ 0, chx->xtype, &c);
338      /* xtype: pattern, "real", complex or .. */      /* xtype: pattern, "real", complex or .. */
339      return chm_sparse_to_SEXP(chgx, 1, 0, Rkind, "",      return chm_sparse_to_SEXP(chgx, 1, 0, Rkind, "",
340                                GET_SLOT(x, Matrix_DimNamesSym));                                symmetric_DimNames(GET_SLOT(x, Matrix_DimNamesSym)));
341  }  }
342    
343  SEXP Csparse_general_to_symmetric(SEXP x, SEXP uplo)  SEXP Csparse_general_to_symmetric(SEXP x, SEXP uplo, SEXP sym_dmns)
344  {  {
345        int *adims = INTEGER(GET_SLOT(x, Matrix_DimSym)), n = adims[0];
346        if(n != adims[1]) {
347            error(_("Csparse_general_to_symmetric(): matrix is not square!"));
348            return R_NilValue; /* -Wall */
349        }
350      CHM_SP chx = AS_CHM_SP__(x), chgx;      CHM_SP chx = AS_CHM_SP__(x), chgx;
351      int uploT = (*CHAR(STRING_ELT(uplo,0)) == 'U') ? 1 : -1;      int uploT = (*CHAR(asChar(uplo)) == 'U') ? 1 : -1;
352      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
353      R_CheckStack();      R_CheckStack();
354        chgx = cholmod_copy(chx, /* stype: */ uploT, chx->xtype, &c);
355    
356      chgx = cholmod_l_copy(chx, /* stype: */ uploT, chx->xtype, &c);      SEXP dns = GET_SLOT(x, Matrix_DimNamesSym);
357        if(asLogical(sym_dmns))
358            dns = symmetric_DimNames(dns);
359        else if((!isNull(VECTOR_ELT(dns, 0)) &&
360                 !isNull(VECTOR_ELT(dns, 1))) ||
361                !isNull(getAttrib(dns, R_NamesSymbol))) {
362            /* symmetrize them if both are not NULL
363             * or names(dimnames(.)) is asymmetric : */
364            dns = PROTECT(duplicate(dns));
365            if(!equal_string_vectors(VECTOR_ELT(dns, 0),
366                                     VECTOR_ELT(dns, 1))) {
367                if(uploT == 1)
368                    SET_VECTOR_ELT(dns, 0, VECTOR_ELT(dns,1));
369                else
370                    SET_VECTOR_ELT(dns, 1, VECTOR_ELT(dns,0));
371            }
372            SEXP nms_dns = getAttrib(dns, R_NamesSymbol);
373            if(!isNull(nms_dns) &&  // names(dimnames(.)) :
374               !R_compute_identical(STRING_ELT(nms_dns, 0),
375                                    STRING_ELT(nms_dns, 1), 16)) {
376                if(uploT == 1)
377                    SET_STRING_ELT(nms_dns, 0, STRING_ELT(nms_dns,1));
378                else
379                    SET_STRING_ELT(nms_dns, 1, STRING_ELT(nms_dns,0));
380                setAttrib(dns, R_NamesSymbol, nms_dns);
381            }
382            UNPROTECT(1);
383        }
384      /* xtype: pattern, "real", complex or .. */      /* xtype: pattern, "real", complex or .. */
385      return chm_sparse_to_SEXP(chgx, 1, 0, Rkind, "",      return chm_sparse_to_SEXP(chgx, 1, 0, Rkind, "", dns);
                               GET_SLOT(x, Matrix_DimNamesSym));  
386  }  }
387    
388  SEXP Csparse_transpose(SEXP x, SEXP tri)  SEXP Csparse_transpose(SEXP x, SEXP tri)
# Line 282  Line 391 
391       *       since cholmod (& cs) lacks sparse 'int' matrices */       *       since cholmod (& cs) lacks sparse 'int' matrices */
392      CHM_SP chx = AS_CHM_SP__(x);      CHM_SP chx = AS_CHM_SP__(x);
393      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
394      CHM_SP chxt = cholmod_l_transpose(chx, chx->xtype, &c);      CHM_SP chxt = cholmod_transpose(chx, chx->xtype, &c);
395      SEXP dn = PROTECT(duplicate(GET_SLOT(x, Matrix_DimNamesSym))), tmp;      SEXP dn = PROTECT(duplicate(GET_SLOT(x, Matrix_DimNamesSym))), tmp;
396      int tr = asLogical(tri);      int tr = asLogical(tri);
397      R_CheckStack();      R_CheckStack();
# Line 290  Line 399 
399      tmp = VECTOR_ELT(dn, 0);    /* swap the dimnames */      tmp = VECTOR_ELT(dn, 0);    /* swap the dimnames */
400      SET_VECTOR_ELT(dn, 0, VECTOR_ELT(dn, 1));      SET_VECTOR_ELT(dn, 0, VECTOR_ELT(dn, 1));
401      SET_VECTOR_ELT(dn, 1, tmp);      SET_VECTOR_ELT(dn, 1, tmp);
402        if(!isNull(tmp = getAttrib(dn, R_NamesSymbol))) { // swap names(dimnames(.)):
403            SEXP nms_dns = PROTECT(allocVector(VECSXP, 2));
404            SET_VECTOR_ELT(nms_dns, 1, STRING_ELT(tmp, 0));
405            SET_VECTOR_ELT(nms_dns, 0, STRING_ELT(tmp, 1));
406            setAttrib(dn, R_NamesSymbol, nms_dns);
407            UNPROTECT(1);
408        }
409      UNPROTECT(1);      UNPROTECT(1);
410      return chm_sparse_to_SEXP(chxt, 1, /* SWAP 'uplo' for triangular */      return chm_sparse_to_SEXP(chxt, 1, /* SWAP 'uplo' for triangular */
411                                tr ? ((*uplo_P(x) == 'U') ? -1 : 1) : 0,                                tr ? ((*uplo_P(x) == 'U') ? -1 : 1) : 0,
412                                Rkind, tr ? diag_P(x) : "", dn);                                Rkind, tr ? diag_P(x) : "", dn);
413  }  }
414    
415  SEXP Csparse_Csparse_prod(SEXP a, SEXP b)  /** @brief  A %*% B  - for matrices of class CsparseMatrix (R package "Matrix")
416     *
417     * @param a
418     * @param b
419     * @param bool_arith
420     *
421     * @return
422     *
423     * NOTA BENE:  cholmod_ssmult(A,B, ...) ->  ./CHOLMOD/MatrixOps/cholmod_ssmult.c
424     * ---------  computes a patter*n* matrix __always_ when
425     * *one* of A or B is pattern*n*, because of this (line 73-74):
426       ---------------------------------------------------------------------------
427        values = values &&
428            (A->xtype != CHOLMOD_PATTERN) && (B->xtype != CHOLMOD_PATTERN) ;
429       ---------------------------------------------------------------------------
430     * ==> Often need to copy the patter*n* to a *l*ogical matrix first !!!
431     */
432    SEXP Csparse_Csparse_prod(SEXP a, SEXP b, SEXP bool_arith)
433  {  {
434      CHM_SP      CHM_SP
435          cha = AS_CHM_SP(a),          cha = AS_CHM_SP(a),
436          chb = AS_CHM_SP(b),          chb = AS_CHM_SP(b), chc;
         chc = cholmod_l_ssmult(cha, chb, /*out_stype:*/ 0,  
                                /* values:= is_numeric (T/F) */ cha->xtype > 0,  
                                /*out sorted:*/ 1, &c);  
     const char *cl_a = class_P(a), *cl_b = class_P(b);  
     char diag[] = {'\0', '\0'};  
     int uploT = 0;  
     SEXP dn = PROTECT(allocVector(VECSXP, 2));  
437      R_CheckStack();      R_CheckStack();
438        // const char *cl_a = class_P(a), *cl_b = class_P(b);
439        static const char *valid_tri[] = { MATRIX_VALID_tri_Csparse, "" };
440        char diag[] = {'\0', '\0'};
441        int uploT = 0, nprot = 1,
442            do_bool = asLogical(bool_arith); // TRUE / NA / FALSE
443        Rboolean
444            a_is_n = (cha->xtype == CHOLMOD_PATTERN),
445            b_is_n = (chb->xtype == CHOLMOD_PATTERN),
446            force_num = (do_bool == FALSE),
447            maybe_bool= (do_bool == NA_LOGICAL);
448    
449  #ifdef DEBUG_Matrix_verbose  #ifdef DEBUG_Matrix_verbose
450      Rprintf("DBG Csparse_C*_prod(%s, %s)\n", cl_a, cl_b);      Rprintf("DBG Csparse_C*_prod(%s, %s)\n", class_P(a), class_P(b));
451  #endif  #endif
452    
453        if(a_is_n && (force_num || (maybe_bool && !b_is_n))) {
454            /* coerce 'a' to  double;
455             * have no CHOLMOD function (pattern -> logical) --> use "our" code */
456            SEXP da = PROTECT(nz2Csparse(a, x_double)); nprot++;
457            cha = AS_CHM_SP(da);
458            R_CheckStack();
459            a_is_n = FALSE;
460        }
461        else if(b_is_n && (force_num || (maybe_bool && !a_is_n))) {
462            // coerce 'b' to  double
463            SEXP db = PROTECT(nz2Csparse(b, x_double)); nprot++;
464            chb = AS_CHM_SP(db);
465            R_CheckStack();
466            b_is_n = FALSE;
467        }
468        chc = cholmod_ssmult(cha, chb, /*out_stype:*/ 0,
469                             /* values : */ do_bool != TRUE,
470                             /* sorted = TRUE: */ 1, &c);
471    
472      /* Preserve triangularity and even unit-triangularity if appropriate.      /* Preserve triangularity and even unit-triangularity if appropriate.
473       * Note that in that case, the multiplication itself should happen       * Note that in that case, the multiplication itself should happen
474       * faster.  But there's no support for that in CHOLMOD */       * faster.  But there's no support for that in CHOLMOD */
475    
476      /* UGLY hack -- rather should have (fast!) C-level version of      if(Matrix_check_class_etc(a, valid_tri) >= 0 &&
477       *       is(a, "triangularMatrix") etc */         Matrix_check_class_etc(b, valid_tri) >= 0)
     if (cl_a[1] == 't' && cl_b[1] == 't')  
         /* FIXME: fails for "Cholesky","BunchKaufmann"..*/  
478          if(*uplo_P(a) == *uplo_P(b)) { /* both upper, or both lower tri. */          if(*uplo_P(a) == *uplo_P(b)) { /* both upper, or both lower tri. */
479              uploT = (*uplo_P(a) == 'U') ? 1 : -1;              uploT = (*uplo_P(a) == 'U') ? 1 : -1;
480              if(*diag_P(a) == 'U' && *diag_P(b) == 'U') { /* return UNIT-triag. */              if(*diag_P(a) == 'U' && *diag_P(b) == 'U') { /* return UNIT-triag. */
# Line 331  Line 484 
484              }              }
485              else diag[0]= 'N';              else diag[0]= 'N';
486          }          }
487    
488        SEXP dn = PROTECT(allocVector(VECSXP, 2));
489      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */
490                     duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), 0)));                     duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), 0)));
491      SET_VECTOR_ELT(dn, 1,      SET_VECTOR_ELT(dn, 1,
492                     duplicate(VECTOR_ELT(GET_SLOT(b, Matrix_DimNamesSym), 1)));                     duplicate(VECTOR_ELT(GET_SLOT(b, Matrix_DimNamesSym), 1)));
493      UNPROTECT(1);      UNPROTECT(nprot);
494      return chm_sparse_to_SEXP(chc, 1, uploT, /*Rkind*/0, diag, dn);      return chm_sparse_to_SEXP(chc, 1, uploT, /*Rkind*/0, diag, dn);
495  }  }
496    
497  SEXP Csparse_Csparse_crossprod(SEXP a, SEXP b, SEXP trans)  /** @brief [t]crossprod (<Csparse>, <Csparse>)
498     *
499     * @param a a "CsparseMatrix" object
500     * @param b a "CsparseMatrix" object
501     * @param trans trans = FALSE:  crossprod(a,b)
502     *              trans = TRUE : tcrossprod(a,b)
503     * @param bool_arith logical (TRUE / NA / FALSE): Should boolean arithmetic be used.
504     *
505     * @return a CsparseMatrix, the (t)cross product of a and b.
506     */
507    SEXP Csparse_Csparse_crossprod(SEXP a, SEXP b, SEXP trans, SEXP bool_arith)
508  {  {
509      int tr = asLogical(trans);      int tr = asLogical(trans), nprot = 1,
510            do_bool = asLogical(bool_arith); // TRUE / NA / FALSE
511      CHM_SP      CHM_SP
512          cha = AS_CHM_SP(a),          cha = AS_CHM_SP(a),
513          chb = AS_CHM_SP(b),          chb = AS_CHM_SP(b),
514          chTr, chc;          chTr, chc;
515      const char *cl_a = class_P(a), *cl_b = class_P(b);      R_CheckStack();
516        static const char *valid_tri[] = { MATRIX_VALID_tri_Csparse, "" };
517      char diag[] = {'\0', '\0'};      char diag[] = {'\0', '\0'};
518      int uploT = 0;      int uploT = 0;
519      SEXP dn = PROTECT(allocVector(VECSXP, 2));      Rboolean
520      R_CheckStack();          a_is_n = (cha->xtype == CHOLMOD_PATTERN),
521            b_is_n = (chb->xtype == CHOLMOD_PATTERN),
522            force_num = (do_bool == FALSE),
523            maybe_bool= (do_bool == NA_LOGICAL);
524    
525      chTr = cholmod_l_transpose((tr) ? chb : cha, chb->xtype, &c);      if(a_is_n && (force_num || (maybe_bool && !b_is_n))) {
526      chc = cholmod_l_ssmult((tr) ? cha : chTr, (tr) ? chTr : chb,          // coerce 'a' to  double
527                           /*out_stype:*/ 0, cha->xtype, /*out sorted:*/ 1, &c);          SEXP da = PROTECT(nz2Csparse(a, x_double)); nprot++;
528      cholmod_l_free_sparse(&chTr, &c);          cha = AS_CHM_SP(da);
529            R_CheckStack();
530            // a_is_n = FALSE;
531        }
532        else if(b_is_n && (force_num || (maybe_bool && !a_is_n))) {
533            // coerce 'b' to  double
534            SEXP db = PROTECT(nz2Csparse(b, x_double)); nprot++;
535            chb = AS_CHM_SP(db);
536            R_CheckStack();
537            // b_is_n = FALSE;
538        }
539        else if(do_bool == TRUE) { // Want boolean arithmetic: sufficient if *one* is pattern:
540            if(!a_is_n && !b_is_n) {
541                // coerce 'a' to pattern
542                SEXP da = PROTECT(Csparse2nz(a, /* tri = */
543                                             Matrix_check_class_etc(a, valid_tri) >= 0)); nprot++;
544                cha = AS_CHM_SP(da);
545                R_CheckStack();
546                // a_is_n = TRUE;
547            }
548        }
549        chTr = cholmod_transpose((tr) ? chb : cha, chb->xtype, &c);
550        chc = cholmod_ssmult((tr) ? cha : chTr, (tr) ? chTr : chb,
551                             /*out_stype:*/ 0, /* values : */ do_bool != TRUE,
552                             /* sorted = TRUE: */ 1, &c);
553        cholmod_free_sparse(&chTr, &c);
554    
555      /* Preserve triangularity and unit-triangularity if appropriate;      /* Preserve triangularity and unit-triangularity if appropriate;
556       * see Csparse_Csparse_prod() for comments */       * see Csparse_Csparse_prod() for comments */
557      if (cl_a[1] == 't' && cl_b[1] == 't')      if(Matrix_check_class_etc(a, valid_tri) >= 0 &&
558           Matrix_check_class_etc(b, valid_tri) >= 0)
559          if(*uplo_P(a) != *uplo_P(b)) { /* one 'U', the other 'L' */          if(*uplo_P(a) != *uplo_P(b)) { /* one 'U', the other 'L' */
560              uploT = (*uplo_P(b) == 'U') ? 1 : -1;              uploT = (*uplo_P(b) == 'U') ? 1 : -1;
561              if(*diag_P(a) == 'U' && *diag_P(b) == 'U') { /* return UNIT-triag. */              if(*diag_P(a) == 'U' && *diag_P(b) == 'U') { /* return UNIT-triag. */
# Line 368  Line 564 
564              }              }
565              else diag[0]= 'N';              else diag[0]= 'N';
566          }          }
567    
568        SEXP dn = PROTECT(allocVector(VECSXP, 2));
569      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */
570                     duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), (tr) ? 0 : 1)));                     duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym),
571                                            (tr) ? 0 : 1)));
572      SET_VECTOR_ELT(dn, 1,      SET_VECTOR_ELT(dn, 1,
573                     duplicate(VECTOR_ELT(GET_SLOT(b, Matrix_DimNamesSym), (tr) ? 0 : 1)));                     duplicate(VECTOR_ELT(GET_SLOT(b, Matrix_DimNamesSym),
574      UNPROTECT(1);                                          (tr) ? 0 : 1)));
575        UNPROTECT(nprot);
576      return chm_sparse_to_SEXP(chc, 1, uploT, /*Rkind*/0, diag, dn);      return chm_sparse_to_SEXP(chc, 1, uploT, /*Rkind*/0, diag, dn);
577  }  }
578    
579  SEXP Csparse_dense_prod(SEXP a, SEXP b)  /**
580     * All (dense * sparse)  Matrix products and cross products
581     *
582     *   f( f(<Csparse>)  %*%  f(<dense>) )   where  f ()  is either t () [tranpose] or the identity.
583     *
584     * @param a CsparseMatrix  (n x m)
585     * @param b numeric vector, matrix, or denseMatrix (m x k) or (k x m)  if `transp` is '2' or 'B'
586     * @param transp character.
587     *        = " " : nothing transposed {apart from a}
588     *        = "2" : "transpose 2nd arg": use  t(b) instead of b (= 2nd argument)
589     *        = "c" : "transpose c":       Return  t(c) instead of c
590     *        = "B" : "transpose both":    use t(b) and return t(c) instead of c
591     * NB: For "2", "c", "B", need to transpose a *dense* matrix, B or C --> chm_transpose_dense()
592     *
593     * @return a dense matrix, the matrix product c = g(a,b) :
594     *
595     *                                                Condition (R)   Condition (C)
596     *   R notation            Math notation          cross  transp   t.a t.b t.ans
597     *   ~~~~~~~~~~~~~~~~~     ~~~~~~~~~~~~~~~~~~     ~~~~~~~~~~~~~   ~~~~~~~~~~~~~
598     *   c <-   a %*%   b      C :=      A B            .       " "    .   .   .
599     *   c <-   a %*% t(b)     C :=      A B'           .       "2"    .   |   .
600     *   c <- t(a %*%   b)     C := (A B)'  = B'A'      .       "c"    .   .   |
601     *   c <- t(a %*% t(b))    C := (A B')' = B A'      .       "B"    .   |   |
602     *
603     *   c <-   t(a) %*%   b   C :=      A'B           TRUE     " "    |   .   .
604     *   c <-   t(a) %*% t(b)  C :=      A'B'          TRUE     "2"    |   |   .
605     *   c <- t(t(a) %*%   b)  C := (A'B)'  = B'A      TRUE     "c"    |   .   |
606     *   c <- t(t(a) %*% t(b)) C := (A'B')' = B A      TRUE     "B"    |   |   |
607     */
608    SEXP Csp_dense_products(SEXP a, SEXP b,
609                            Rboolean transp_a, Rboolean transp_b, Rboolean transp_ans)
610  {  {
611      CHM_SP cha = AS_CHM_SP(a);      CHM_SP cha = AS_CHM_SP(a);
612      SEXP b_M = PROTECT(mMatrix_as_dgeMatrix(b));      int a_nc = transp_a ? cha->nrow : cha->ncol,
613      CHM_DN chb = AS_CHM_DN(b_M);          a_nr = transp_a ? cha->ncol : cha->nrow;
614      CHM_DN chc = cholmod_l_allocate_dense(cha->nrow, chb->ncol, cha->nrow,      Rboolean
615                                          chb->xtype, &c);          maybe_transp_b = (a_nc == 1),
616      SEXP dn = PROTECT(allocVector(VECSXP, 2));          b_is_vector = FALSE;
617        /* NOTE: trans_b {<--> "use t(b) instead of b" }
618           ----  "interferes" with the  case automatic treatment of *vector* b.
619           In that case,  t(b) or b is used "whatever make more sense",
620           according to the general R philosophy of treating vectors in matrix products.
621        */
622    
623        /* repeating a "cheap part" of  mMatrix_as_dgeMatrix2(b, .)  to see if
624         * we have a vector that we might 'transpose_if_vector' : */
625        static const char *valid[] = {"_NOT_A_CLASS_", MATRIX_VALID_ddense, ""};
626        /* int ctype = Matrix_check_class_etc(b, valid);
627         * if (ctype > 0)   /.* a ddenseMatrix object */
628        if (Matrix_check_class_etc(b, valid) < 0) {
629            // not a ddenseM*:  is.matrix() or vector:
630            b_is_vector = !isMatrix(b);
631        }
632    
633        if(b_is_vector) {
634            /* determine *if* we want/need to transpose at all:
635             * if (length(b) == ncol(A)) have match: use dim = c(n, 1) (<=> do *not* transp);
636             *  otherwise, try to transpose: ok  if (ncol(A) == 1) [see also above]:  */
637            maybe_transp_b = (LENGTH(b) != a_nc);
638            // Here, we transpose already in mMatrix_as_dge*()  ==> don't do it later:
639            transp_b = FALSE;
640        }
641        SEXP b_M = PROTECT(mMatrix_as_dgeMatrix2(b, maybe_transp_b));
642    
643        CHM_DN chb = AS_CHM_DN(b_M), b_t;
644        R_CheckStack();
645        int ncol_b;
646        if(transp_b) { // transpose b:
647            b_t = cholmod_allocate_dense(chb->ncol, chb->nrow, chb->ncol, chb->xtype, &c);
648            chm_transpose_dense(b_t, chb);
649            ncol_b = b_t->ncol;
650        } else
651            ncol_b = chb->ncol;
652        // Result C {with dim() before it may be transposed}:
653        CHM_DN chc = cholmod_allocate_dense(a_nr, ncol_b, a_nr, chb->xtype, &c);
654      double one[] = {1,0}, zero[] = {0,0};      double one[] = {1,0}, zero[] = {0,0};
655      int nprot = 2;      int nprot = 2;
656      R_CheckStack();  
657      /* Tim Davis, please FIXME:  currently (2010-11) *fails* when  a  is a pattern matrix:*/      /* Tim Davis, please FIXME:  currently (2010-11) *fails* when  a  is a pattern matrix:*/
658      if(cha->xtype == CHOLMOD_PATTERN) {      if(cha->xtype == CHOLMOD_PATTERN) {
659          /* warning(_("Csparse_dense_prod(): cholmod_sdmult() not yet implemented for pattern./ ngCMatrix" */          /* warning(_("Csparse_dense_prod(): cholmod_sdmult() not yet implemented for pattern./ ngCMatrix" */
# Line 398  Line 665 
665          SEXP da = PROTECT(nz2Csparse(a, x_double)); nprot++;          SEXP da = PROTECT(nz2Csparse(a, x_double)); nprot++;
666          cha = AS_CHM_SP(da);          cha = AS_CHM_SP(da);
667      }      }
668      cholmod_l_sdmult(cha, 0, one, zero, chb, chc, &c);  
669      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */      /* cholmod_sdmult(A, transp, alpha, beta, X,  Y,  &c): depending on transp == 0 / != 0:
670                     duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), 0)));       *  Y := alpha*(A*X) + beta*Y or alpha*(A'*X) + beta*Y;  here, alpha = 1, beta = 0:
671      SET_VECTOR_ELT(dn, 1,       *  Y := A*X  or  A'*X
672                     duplicate(VECTOR_ELT(GET_SLOT(b_M, Matrix_DimNamesSym), 1)));       *                       NB: always  <sparse> %*% <dense> !
673         */
674        cholmod_sdmult(cha, transp_a, one, zero, (transp_b ? b_t : chb), /* -> */ chc, &c);
675    
676        SEXP dn = PROTECT(allocVector(VECSXP, 2));  /* establish dimnames */
677        SET_VECTOR_ELT(dn, transp_ans ? 1 : 0,
678                       duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), transp_a ? 1 : 0)));
679        SET_VECTOR_ELT(dn, transp_ans ? 0 : 1,
680                       duplicate(VECTOR_ELT(GET_SLOT(b_M, Matrix_DimNamesSym),
681                                            transp_b ? 0 : 1)));
682        if(transp_b) cholmod_free_dense(&b_t, &c);
683      UNPROTECT(nprot);      UNPROTECT(nprot);
684      return chm_dense_to_SEXP(chc, 1, 0, dn);      return chm_dense_to_SEXP(chc, 1, 0, dn, transp_ans);
685  }  }
686    
687  SEXP Csparse_dense_crossprod(SEXP a, SEXP b)  
688    SEXP Csparse_dense_prod(SEXP a, SEXP b, SEXP transp)
689  {  {
690      CHM_SP cha = AS_CHM_SP(a);      return
691      SEXP b_M = PROTECT(mMatrix_as_dgeMatrix(b));          Csp_dense_products(a, b,
692      CHM_DN chb = AS_CHM_DN(b_M);                  /* transp_a = */ FALSE,
693      CHM_DN chc = cholmod_l_allocate_dense(cha->ncol, chb->ncol, cha->ncol,                  /* transp_b   = */ (*CHAR(asChar(transp)) == '2' || *CHAR(asChar(transp)) == 'B'),
694                                          chb->xtype, &c);                  /* transp_ans = */ (*CHAR(asChar(transp)) == 'c' || *CHAR(asChar(transp)) == 'B'));
     SEXP dn = PROTECT(allocVector(VECSXP, 2)); int nprot = 2;  
     double one[] = {1,0}, zero[] = {0,0};  
     R_CheckStack();  
     // -- see Csparse_dense_prod() above :  
     if(cha->xtype == CHOLMOD_PATTERN) {  
         SEXP da = PROTECT(nz2Csparse(a, x_double)); nprot++;  
         cha = AS_CHM_SP(da);  
695      }      }
696      cholmod_l_sdmult(cha, 1, one, zero, chb, chc, &c);  
697      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */  SEXP Csparse_dense_crossprod(SEXP a, SEXP b, SEXP transp)
698                     duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), 1)));  {
699      SET_VECTOR_ELT(dn, 1,      return
700                     duplicate(VECTOR_ELT(GET_SLOT(b_M, Matrix_DimNamesSym), 1)));          Csp_dense_products(a, b,
701      UNPROTECT(nprot);                  /* transp_a = */ TRUE,
702      return chm_dense_to_SEXP(chc, 1, 0, dn);                  /* transp_b   = */ (*CHAR(asChar(transp)) == '2' || *CHAR(asChar(transp)) == 'B'),
703                    /* transp_ans = */ (*CHAR(asChar(transp)) == 'c' || *CHAR(asChar(transp)) == 'B'));
704  }  }
705    
706  /* Computes   x'x  or  x x' -- *also* for Tsparse (triplet = TRUE)  
707     see Csparse_Csparse_crossprod above for  x'y and x y' */  /** @brief Computes   x'x  or  x x' -- *also* for Tsparse (triplet = TRUE)
708  SEXP Csparse_crossprod(SEXP x, SEXP trans, SEXP triplet)      see Csparse_Csparse_crossprod above for  x'y and x y'
709    */
710    SEXP Csparse_crossprod(SEXP x, SEXP trans, SEXP triplet, SEXP bool_arith)
711  {  {
712      int trip = asLogical(triplet),      int tripl = asLogical(triplet),
713          tr   = asLogical(trans); /* gets reversed because _aat is tcrossprod */          tr   = asLogical(trans), /* gets reversed because _aat is tcrossprod */
714            do_bool = asLogical(bool_arith); // TRUE / NA / FALSE
715  #ifdef AS_CHM_DIAGU2N_FIXED_FINALLY  #ifdef AS_CHM_DIAGU2N_FIXED_FINALLY
716      CHM_TR cht = trip ? AS_CHM_TR(x) : (CHM_TR) NULL;      CHM_TR cht = tripl ? AS_CHM_TR(x) : (CHM_TR) NULL;  int nprot = 1;
717  #else /* workaround needed:*/  #else /* workaround needed:*/
718      SEXP xx = PROTECT(Tsparse_diagU2N(x));      SEXP xx = PROTECT(Tsparse_diagU2N(x));
719      CHM_TR cht = trip ? AS_CHM_TR__(xx) : (CHM_TR) NULL;      CHM_TR cht = tripl ? AS_CHM_TR__(xx) : (CHM_TR) NULL; int nprot = 2;
720  #endif  #endif
721      CHM_SP chcp, chxt,      CHM_SP chcp, chxt, chxc,
722          chx = (trip ?          chx = (tripl ?
723                 cholmod_l_triplet_to_sparse(cht, cht->nnz, &c) :                 cholmod_triplet_to_sparse(cht, cht->nnz, &c) :
724                 AS_CHM_SP(x));                 AS_CHM_SP(x));
725      SEXP dn = PROTECT(allocVector(VECSXP, 2));      SEXP dn = PROTECT(allocVector(VECSXP, 2));
726      R_CheckStack();      R_CheckStack();
727        Rboolean
728            x_is_n = (chx->xtype == CHOLMOD_PATTERN),
729            x_is_sym = chx->stype != 0,
730            force_num = (do_bool == FALSE);
731    
732        if(x_is_n && force_num) {
733            // coerce 'x' to  double
734            SEXP dx = PROTECT(nz2Csparse(x, x_double)); nprot++;
735            chx = AS_CHM_SP(dx);
736            R_CheckStack();
737        }
738        else if(do_bool == TRUE && !x_is_n) { // Want boolean arithmetic; need patter[n]
739            // coerce 'x' to pattern
740            static const char *valid_tri[] = { MATRIX_VALID_tri_Csparse, "" };
741            SEXP dx = PROTECT(Csparse2nz(x, /* tri = */
742                                         Matrix_check_class_etc(x, valid_tri) >= 0)); nprot++;
743            chx = AS_CHM_SP(dx);
744            R_CheckStack();
745        }
746    
747      if (!tr) chxt = cholmod_l_transpose(chx, chx->xtype, &c);      if (!tr) chxt = cholmod_transpose(chx, chx->xtype, &c);
748      chcp = cholmod_l_aat((!tr) ? chxt : chx, (int *) NULL, 0, chx->xtype, &c);  
749        if (x_is_sym) // cholmod_aat() does not like symmetric
750            chxc = cholmod_copy(tr ? chx : chxt, /* stype: */ 0,
751                                chx->xtype, &c);
752        // CHOLMOD/Core/cholmod_aat.c :
753        chcp = cholmod_aat(x_is_sym ? chxc : (tr ? chx : chxt),
754                           (int *) NULL, 0, /* mode: */ chx->xtype, &c);
755      if(!chcp) {      if(!chcp) {
756          UNPROTECT(1);          UNPROTECT(1);
757          error(_("Csparse_crossprod(): error return from cholmod_l_aat()"));          error(_("Csparse_crossprod(): error return from cholmod_aat()"));
758      }      }
759      cholmod_l_band_inplace(0, chcp->ncol, chcp->xtype, chcp, &c);      cholmod_band_inplace(0, chcp->ncol, chcp->xtype, chcp, &c);
760      chcp->stype = 1;      chcp->stype = 1; // symmetric
761      if (trip) cholmod_l_free_sparse(&chx, &c);      if (tripl) cholmod_free_sparse(&chx, &c);
762      if (!tr) cholmod_l_free_sparse(&chxt, &c);      if (!tr) cholmod_free_sparse(&chxt, &c);
763      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */
764                     duplicate(VECTOR_ELT(GET_SLOT(x, Matrix_DimNamesSym),                     duplicate(VECTOR_ELT(GET_SLOT(x, Matrix_DimNamesSym),
765                                          (tr) ? 0 : 1)));                                          (tr) ? 0 : 1)));
766      SET_VECTOR_ELT(dn, 1, duplicate(VECTOR_ELT(dn, 0)));      SET_VECTOR_ELT(dn, 1, duplicate(VECTOR_ELT(dn, 0)));
767  #ifdef AS_CHM_DIAGU2N_FIXED_FINALLY      UNPROTECT(nprot);
768      UNPROTECT(1);      // FIXME: uploT for symmetric ?
 #else  
     UNPROTECT(2);  
 #endif  
769      return chm_sparse_to_SEXP(chcp, 1, 0, 0, "", dn);      return chm_sparse_to_SEXP(chcp, 1, 0, 0, "", dn);
770  }  }
771    
772  /* Csparse_drop(x, tol):  drop entries with absolute value < tol, i.e,  /** @brief Csparse_drop(x, tol):  drop entries with absolute value < tol, i.e,
773  *  at least all "explicit" zeros */   *  at least all "explicit" zeros. */
774  SEXP Csparse_drop(SEXP x, SEXP tol)  SEXP Csparse_drop(SEXP x, SEXP tol)
775  {  {
776      const char *cl = class_P(x);      const char *cl = class_P(x);
777      /* dtCMatrix, etc; [1] = the second character =?= 't' for triangular */      /* dtCMatrix, etc; [1] = the second character =?= 't' for triangular */
778      int tr = (cl[1] == 't');      int tr = (cl[1] == 't');
779      CHM_SP chx = AS_CHM_SP__(x);      CHM_SP chx = AS_CHM_SP__(x);
780      CHM_SP ans = cholmod_l_copy(chx, chx->stype, chx->xtype, &c);      CHM_SP ans = cholmod_copy(chx, chx->stype, chx->xtype, &c);
781      double dtol = asReal(tol);      double dtol = asReal(tol);
782      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
783      R_CheckStack();      R_CheckStack();
784    
785      if(!cholmod_l_drop(dtol, ans, &c))      if(!cholmod_drop(dtol, ans, &c))
786          error(_("cholmod_l_drop() failed"));          error(_("cholmod_drop() failed"));
787      return chm_sparse_to_SEXP(ans, 1,      return chm_sparse_to_SEXP(ans, 1,
788                                tr ? ((*uplo_P(x) == 'U') ? 1 : -1) : 0,                                tr ? ((*uplo_P(x) == 'U') ? 1 : -1) : 0,
789                                Rkind, tr ? diag_P(x) : "",                                Rkind, tr ? diag_P(x) : "",
790                                GET_SLOT(x, Matrix_DimNamesSym));                                GET_SLOT(x, Matrix_DimNamesSym));
791  }  }
792    
793    /** @brief Horizontal Concatenation -  cbind( <Csparse>,  <Csparse>)
794     */
795  SEXP Csparse_horzcat(SEXP x, SEXP y)  SEXP Csparse_horzcat(SEXP x, SEXP y)
796  {  {
797      CHM_SP chx = AS_CHM_SP__(x), chy = AS_CHM_SP__(y);  #define CSPARSE_CAT(_KIND_)                                             \
798      int Rk_x = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0,      CHM_SP chx = AS_CHM_SP__(x), chy = AS_CHM_SP__(y);                  \
799          Rk_y = (chy->xtype != CHOLMOD_PATTERN) ? Real_kind(y) : 0,      R_CheckStack();                                                     \
800          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,     \
801      R_CheckStack();          Rk_y = (chy->xtype != CHOLMOD_PATTERN) ? Real_kind(y) : -3, Rkind; \
802        if(Rk_x == -3 || Rk_y == -3) { /* at least one of them is patter"n" */ \
803            if(Rk_x == -3 && Rk_y == -3) { /* fine */                       \
804            } else { /* only one is a patter"n"                             \
805                      * "Bug" in cholmod_horzcat()/vertcat(): returns patter"n" matrix if one of them is */ \
806                Rboolean ok;                                                \
807                if(Rk_x == -3) {                                            \
808                    ok = chm_MOD_xtype(CHOLMOD_REAL, chx, &c); Rk_x = 0;    \
809                } else if(Rk_y == -3) {                                     \
810                    ok = chm_MOD_xtype(CHOLMOD_REAL, chy, &c); Rk_y = 0;    \
811                } else                                                      \
812                    error(_("Impossible Rk_x/Rk_y in Csparse_%s(), please report"), _KIND_); \
813                if(!ok)                                                     \
814                    error(_("chm_MOD_xtype() was not successful in Csparse_%s(), please report"), \
815                          _KIND_);                                          \
816            }                                                               \
817        }                                                                   \
818        Rkind = /* logical if both x and y are */ (Rk_x == 1 && Rk_y == 1) ? 1 : 0
819    
820      /* TODO: currently drops dimnames - and we fix at R level */      CSPARSE_CAT("horzcat");
821      return chm_sparse_to_SEXP(cholmod_l_horzcat(chx, chy, 1, &c),      // TODO: currently drops dimnames - and we fix at R level;
822    
823        return chm_sparse_to_SEXP(cholmod_horzcat(chx, chy, 1, &c),
824                                1, 0, Rkind, "", R_NilValue);                                1, 0, Rkind, "", R_NilValue);
825  }  }
826    
827    /** @brief Vertical Concatenation -  rbind( <Csparse>,  <Csparse>)
828     */
829  SEXP Csparse_vertcat(SEXP x, SEXP y)  SEXP Csparse_vertcat(SEXP x, SEXP y)
830  {  {
831      CHM_SP chx = AS_CHM_SP__(x), chy = AS_CHM_SP__(y);      CSPARSE_CAT("vertcat");
832      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();  
833    
834      /* TODO: currently drops dimnames - and we fix at R level */      return chm_sparse_to_SEXP(cholmod_vertcat(chx, chy, 1, &c),
     return chm_sparse_to_SEXP(cholmod_l_vertcat(chx, chy, 1, &c),  
835                                1, 0, Rkind, "", R_NilValue);                                1, 0, Rkind, "", R_NilValue);
836  }  }
837    
# Line 523  Line 839 
839  {  {
840      CHM_SP chx = AS_CHM_SP__(x);      CHM_SP chx = AS_CHM_SP__(x);
841      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
842      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);
843      R_CheckStack();      R_CheckStack();
844    
845      return chm_sparse_to_SEXP(ans, 1, 0, Rkind, "",      return chm_sparse_to_SEXP(ans, 1, 0, Rkind, "",
# Line 541  Line 857 
857      }      }
858      else { /* unit triangular (diag='U'): "fill the diagonal" & diag:= "N" */      else { /* unit triangular (diag='U'): "fill the diagonal" & diag:= "N" */
859          CHM_SP chx = AS_CHM_SP__(x);          CHM_SP chx = AS_CHM_SP__(x);
860          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);
861          double one[] = {1, 0};          double one[] = {1, 0};
862          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);
863          int uploT = (*uplo_P(x) == 'U') ? 1 : -1;          int uploT = (*uplo_P(x) == 'U') ? 1 : -1;
864          int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;          int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
865    
866          R_CheckStack();          R_CheckStack();
867          cholmod_l_free_sparse(&eye, &c);          cholmod_free_sparse(&eye, &c);
868          return chm_sparse_to_SEXP(ans, 1, uploT, Rkind, "N",          return chm_sparse_to_SEXP(ans, 1, uploT, Rkind, "N",
869                                    GET_SLOT(x, Matrix_DimNamesSym));                                    GET_SLOT(x, Matrix_DimNamesSym));
870      }      }
# Line 565  Line 881 
881      }      }
882      else { /* triangular with diag='N'): now drop the diagonal */      else { /* triangular with diag='N'): now drop the diagonal */
883          /* duplicate, since chx will be modified: */          /* duplicate, since chx will be modified: */
884          CHM_SP chx = AS_CHM_SP__(duplicate(x));          SEXP xx = PROTECT(duplicate(x));
885            CHM_SP chx = AS_CHM_SP__(xx);
886          int uploT = (*uplo_P(x) == 'U') ? 1 : -1,          int uploT = (*uplo_P(x) == 'U') ? 1 : -1,
887              Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;              Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
888          R_CheckStack();          R_CheckStack();
889    
890          chm_diagN2U(chx, uploT, /* do_realloc */ FALSE);          chm_diagN2U(chx, uploT, /* do_realloc */ FALSE);
891    
892          return chm_sparse_to_SEXP(chx, /*dofree*/ 0/* or 1 ?? */,          SEXP ans = chm_sparse_to_SEXP(chx, /*dofree*/ 0/* or 1 ?? */,
893                                    uploT, Rkind, "U",                                    uploT, Rkind, "U",
894                                    GET_SLOT(x, Matrix_DimNamesSym));                                    GET_SLOT(x, Matrix_DimNamesSym));
895            UNPROTECT(1);// only now !
896            return ans;
897      }      }
898  }  }
899    
900  /**  /**
901   * "Indexing" aka subsetting : Compute  x[i,j], also for vectors i and j   * Indexing aka subsetting : Compute  x[i,j], also for vectors i and j
902   * Working via CHOLMOD_submatrix, see ./CHOLMOD/MatrixOps/cholmod_submatrix.c   * Working via CHOLMOD_submatrix, see ./CHOLMOD/MatrixOps/cholmod_submatrix.c
903   * @param x CsparseMatrix   * @param x CsparseMatrix
904   * @param i row     indices (0-origin), or NULL (R's)   * @param i row     indices (0-origin), or NULL (R, not C)
905   * @param j columns indices (0-origin), or NULL   * @param j columns indices (0-origin), or NULL
906   *   *
907   * @return x[i,j]  still CsparseMatrix --- currently, this loses dimnames   * @return x[i,j]  still CsparseMatrix --- currently, this loses dimnames
# Line 600  Line 919 
919      if (csize >= 0 && !isInteger(j))      if (csize >= 0 && !isInteger(j))
920          error(_("Index j must be NULL or integer"));          error(_("Index j must be NULL or integer"));
921    
922      if (chx->stype) /* symmetricMatrix */  #define CHM_SUB(_M_, _i_, _j_)                                  \
923        cholmod_submatrix(_M_,                                      \
924                          (rsize < 0) ? NULL : INTEGER(_i_), rsize, \
925                          (csize < 0) ? NULL : INTEGER(_j_), csize, \
926                          TRUE, TRUE, &c)
927        CHM_SP ans;
928        if (!chx->stype) {/* non-symmetric Matrix */
929            ans = CHM_SUB(chx, i, j);
930        }
931        else {
932          /* for now, cholmod_submatrix() only accepts "generalMatrix" */          /* for now, cholmod_submatrix() only accepts "generalMatrix" */
933          chx = cholmod_l_copy(chx, /* stype: */ 0, chx->xtype, &c);          CHM_SP tmp = cholmod_copy(chx, /* stype: */ 0, chx->xtype, &c);
934            ans = CHM_SUB(tmp, i, j);
935            cholmod_free_sparse(&tmp, &c);
936        }
937    
938      return chm_sparse_to_SEXP(cholmod_l_submatrix(chx,      // "FIXME": currently dropping dimnames, and adding them afterwards in R :
939                                  (rsize < 0) ? NULL : INTEGER(i), rsize,      /* // dimnames: */
940                                  (csize < 0) ? NULL : INTEGER(j), csize,      /* SEXP x_dns = GET_SLOT(x, Matrix_DimNamesSym), */
941                                                    TRUE, TRUE, &c),      /*  dn = PROTECT(allocVector(VECSXP, 2)); */
942                                1, 0, Rkind, "",      return chm_sparse_to_SEXP(ans, 1, 0, Rkind, "", /* dimnames: */ R_NilValue);
                               /* FIXME: drops dimnames */ R_NilValue);  
943  }  }
944    
945    #define _d_Csp_
946    #include "t_Csparse_subassign.c"
947    
948    #define _l_Csp_
949    #include "t_Csparse_subassign.c"
950    
951    #define _i_Csp_
952    #include "t_Csparse_subassign.c"
953    
954    #define _n_Csp_
955    #include "t_Csparse_subassign.c"
956    
957    #define _z_Csp_
958    #include "t_Csparse_subassign.c"
959    
960    
961    
962  SEXP Csparse_MatrixMarket(SEXP x, SEXP fname)  SEXP Csparse_MatrixMarket(SEXP x, SEXP fname)
963  {  {
964      FILE *f = fopen(CHAR(asChar(fname)), "w");      FILE *f = fopen(CHAR(asChar(fname)), "w");
# Line 619  Line 966 
966      if (!f)      if (!f)
967          error(_("failure to open file \"%s\" for writing"),          error(_("failure to open file \"%s\" for writing"),
968                CHAR(asChar(fname)));                CHAR(asChar(fname)));
969      if (!cholmod_l_write_sparse(f, AS_CHM_SP(x),      if (!cholmod_write_sparse(f, AS_CHM_SP(x),
970                                (CHM_SP)NULL, (char*) NULL, &c))                                (CHM_SP)NULL, (char*) NULL, &c))
971          error(_("cholmod_l_write_sparse returned error code"));          error(_("cholmod_write_sparse returned error code"));
972      fclose(f);      fclose(f);
973      return R_NilValue;      return R_NilValue;
974  }  }
# Line 639  Line 986 
986   *   *
987   * @return  a SEXP, either a (double) number or a length n-vector of diagonal entries   * @return  a SEXP, either a (double) number or a length n-vector of diagonal entries
988   */   */
989  SEXP diag_tC_ptr(int n, int *x_p, double *x_x, int *perm, SEXP resultKind)  SEXP diag_tC_ptr(int n, int *x_p, double *x_x, Rboolean is_U, int *perm,
990  /*                                ^^^^^^ FIXME[Generalize] to int / ... */  /*                                ^^^^^^ FIXME[Generalize] to int / ... */
991                     SEXP resultKind)
992  {  {
993      const char* res_ch = CHAR(STRING_ELT(resultKind,0));      const char* res_ch = CHAR(STRING_ELT(resultKind,0));
994      enum diag_kind { diag, diag_backpermuted, trace, prod, sum_log      enum diag_kind { diag, diag_backpermuted, trace, prod, sum_log, min, max, range
995      } res_kind = ((!strcmp(res_ch, "trace")) ? trace :      } res_kind = ((!strcmp(res_ch, "trace")) ? trace :
996                    ((!strcmp(res_ch, "sumLog")) ? sum_log :                    ((!strcmp(res_ch, "sumLog")) ? sum_log :
997                     ((!strcmp(res_ch, "prod")) ? prod :                     ((!strcmp(res_ch, "prod")) ? prod :
998                        ((!strcmp(res_ch, "min")) ? min :
999                         ((!strcmp(res_ch, "max")) ? max :
1000                          ((!strcmp(res_ch, "range")) ? range :
1001                      ((!strcmp(res_ch, "diag")) ? diag :                      ((!strcmp(res_ch, "diag")) ? diag :
1002                       ((!strcmp(res_ch, "diagBack")) ? diag_backpermuted :                       ((!strcmp(res_ch, "diagBack")) ? diag_backpermuted :
1003                        -1)))));                           -1))))))));
1004      int i, n_x, i_from = 0;      int i, n_x, i_from;
1005      SEXP ans = PROTECT(allocVector(REALSXP,      SEXP ans = PROTECT(allocVector(REALSXP,
1006  /*                                 ^^^^  FIXME[Generalize] */  /*                                 ^^^^  FIXME[Generalize] */
1007                                     (res_kind == diag ||                                     (res_kind == diag ||
1008                                      res_kind == diag_backpermuted) ? n : 1));                                      res_kind == diag_backpermuted) ? n :
1009                                       (res_kind == range ? 2 : 1)));
1010      double *v = REAL(ans);      double *v = REAL(ans);
1011  /*  ^^^^^^      ^^^^  FIXME[Generalize] */  /*  ^^^^^^      ^^^^  FIXME[Generalize] */
1012    
1013        i_from = (is_U ? -1 : 0);
1014    
1015  #define for_DIAG(v_ASSIGN)                                              \  #define for_DIAG(v_ASSIGN)                                              \
1016      for(i = 0; i < n; i++, i_from += n_x) {                             \      for(i = 0; i < n; i++) {                                    \
1017          /* looking at i-th column */                                    \          /* looking at i-th column */                                    \
1018          n_x = x_p[i+1] - x_p[i];/* #{entries} in this column */ \          n_x = x_p[i+1] - x_p[i];/* #{entries} in this column */ \
1019            if( is_U) i_from += n_x;                                \
1020          v_ASSIGN;                                                       \          v_ASSIGN;                                                       \
1021            if(!is_U) i_from += n_x;                                \
1022      }      }
1023    
1024      /* NOTA BENE: we assume  -- uplo = "L" i.e. lower triangular matrix      /* NOTA BENE: we assume  -- uplo = "L" i.e. lower triangular matrix
1025       *            for uplo = "U" (makes sense with a "dtCMatrix" !),       *            for uplo = "U" (makes sense with a "dtCMatrix" !),
1026       *            should use  x_x[i_from + (nx - 1)] instead of x_x[i_from],       *            should use  x_x[i_from + (n_x - 1)] instead of x_x[i_from],
1027       *            where nx = (x_p[i+1] - x_p[i])       *            where n_x = (x_p[i+1] - x_p[i])
1028       */       */
1029    
1030      switch(res_kind) {      switch(res_kind) {
1031      case trace:      case trace: // = sum
1032          v[0] = 0.;          v[0] = 0.;
1033          for_DIAG(v[0] += x_x[i_from]);          for_DIAG(v[0] += x_x[i_from]);
1034          break;          break;
# Line 687  Line 1043 
1043          for_DIAG(v[0] *= x_x[i_from]);          for_DIAG(v[0] *= x_x[i_from]);
1044          break;          break;
1045    
1046        case min:
1047            v[0] = R_PosInf;
1048            for_DIAG(if(v[0] > x_x[i_from]) v[0] = x_x[i_from]);
1049            break;
1050    
1051        case max:
1052            v[0] = R_NegInf;
1053            for_DIAG(if(v[0] < x_x[i_from]) v[0] = x_x[i_from]);
1054            break;
1055    
1056        case range:
1057            v[0] = R_PosInf;
1058            v[1] = R_NegInf;
1059            for_DIAG(if(v[0] > x_x[i_from]) v[0] = x_x[i_from];
1060                     if(v[1] < x_x[i_from]) v[1] = x_x[i_from]);
1061            break;
1062    
1063      case diag:      case diag:
1064          for_DIAG(v[i] = x_x[i_from]);          for_DIAG(v[i] = x_x[i_from]);
1065          break;          break;
# Line 694  Line 1067 
1067      case diag_backpermuted:      case diag_backpermuted:
1068          for_DIAG(v[i] = x_x[i_from]);          for_DIAG(v[i] = x_x[i_from]);
1069    
1070          warning(_("resultKind = 'diagBack' (back-permuted) is experimental"));          warning(_("%s = '%s' (back-permuted) is experimental"),
1071                    "resultKind", "diagBack");
1072          /* now back_permute : */          /* now back_permute : */
1073          for(i = 0; i < n; i++) {          for(i = 0; i < n; i++) {
1074              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 715  Line 1089 
1089   * Extract the diagonal entries from *triangular* Csparse matrix  __or__ a   * Extract the diagonal entries from *triangular* Csparse matrix  __or__ a
1090   * cholmod_sparse factor (LDL = TRUE).   * cholmod_sparse factor (LDL = TRUE).
1091   *   *
1092     * @param obj -- now a cholmod_sparse factor or a dtCMatrix
1093   * @param pslot  'p' (column pointer)   slot of Csparse matrix/factor   * @param pslot  'p' (column pointer)   slot of Csparse matrix/factor
1094   * @param xslot  'x' (non-zero entries) slot of Csparse matrix/factor   * @param xslot  'x' (non-zero entries) slot of Csparse matrix/factor
1095   * @param perm_slot  'perm' (= permutation vector) slot of corresponding CHMfactor;   * @param perm_slot  'perm' (= permutation vector) slot of corresponding CHMfactor;
# Line 723  Line 1098 
1098   *   *
1099   * @return  a SEXP, either a (double) number or a length n-vector of diagonal entries   * @return  a SEXP, either a (double) number or a length n-vector of diagonal entries
1100   */   */
1101  SEXP diag_tC(SEXP pslot, SEXP xslot, SEXP perm_slot, SEXP resultKind)  SEXP diag_tC(SEXP obj, SEXP resultKind)
1102  {  {
1103    
1104        SEXP
1105            pslot = GET_SLOT(obj, Matrix_pSym),
1106            xslot = GET_SLOT(obj, Matrix_xSym);
1107        Rboolean is_U = (R_has_slot(obj, Matrix_uploSym) &&
1108                         *CHAR(asChar(GET_SLOT(obj, Matrix_uploSym))) == 'U');
1109      int n = length(pslot) - 1, /* n = ncol(.) = nrow(.) */      int n = length(pslot) - 1, /* n = ncol(.) = nrow(.) */
1110          *x_p  = INTEGER(pslot),          *x_p  = INTEGER(pslot), pp = -1, *perm;
         *perm = INTEGER(perm_slot);  
1111      double *x_x = REAL(xslot);      double *x_x = REAL(xslot);
1112  /*  ^^^^^^        ^^^^ FIXME[Generalize] to INTEGER(.) / LOGICAL(.) / ... xslot !*/  /*  ^^^^^^        ^^^^ FIXME[Generalize] to INTEGER(.) / LOGICAL(.) / ... xslot !*/
1113    
1114      return diag_tC_ptr(n, x_p, x_x, perm, resultKind);      if(R_has_slot(obj, Matrix_permSym))
1115            perm = INTEGER(GET_SLOT(obj, Matrix_permSym));
1116        else perm = &pp;
1117    
1118        return diag_tC_ptr(n, x_p, x_x, is_U, perm, resultKind);
1119  }  }
1120    
1121    
1122  /**  /**
1123   * Create a Csparse matrix object from indices and/or pointers.   * Create a Csparse matrix object from indices and/or pointers.
1124   *   *
# Line 835  Line 1220 
1220      if (cls[1] != 'g')      if (cls[1] != 'g')
1221          error(_("Only 'g'eneral sparse matrix types allowed"));          error(_("Only 'g'eneral sparse matrix types allowed"));
1222                                  /* allocate and populate the triplet */                                  /* allocate and populate the triplet */
1223      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,
1224                                      xtype, &c);                                      xtype, &c);
1225      T->x = x;      T->x = x;
1226      tri = (int*)T->i;      tri = (int*)T->i;
# Line 845  Line 1230 
1230          trj[ii] = j[ii] - ((!mj && index1) ? 1 : 0);          trj[ii] = j[ii] - ((!mj && index1) ? 1 : 0);
1231      }      }
1232                                  /* create the cholmod_sparse structure */                                  /* create the cholmod_sparse structure */
1233      A = cholmod_l_triplet_to_sparse(T, nnz, &c);      A = cholmod_triplet_to_sparse(T, nnz, &c);
1234      cholmod_l_free_triplet(&T, &c);      cholmod_free_triplet(&T, &c);
1235                                  /* copy the information to the SEXP */                                  /* copy the information to the SEXP */
1236      ans = PROTECT(NEW_OBJECT(MAKE_CLASS(cls)));      ans = PROTECT(NEW_OBJECT(MAKE_CLASS(cls)));
1237  /* 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
1238                                  /* allocate and copy common slots */                                  /* allocate and copy common slots */
1239      nnz = cholmod_l_nnz(A, &c);      nnz = cholmod_nnz(A, &c);
1240      dims = INTEGER(ALLOC_SLOT(ans, Matrix_DimSym, INTSXP, 2));      dims = INTEGER(ALLOC_SLOT(ans, Matrix_DimSym, INTSXP, 2));
1241      dims[0] = A->nrow; dims[1] = A->ncol;      dims[0] = A->nrow; dims[1] = A->ncol;
1242      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 1249 
1249          error(_("code not yet written for cls = \"lgCMatrix\""));          error(_("code not yet written for cls = \"lgCMatrix\""));
1250      }      }
1251  /* FIXME: dimnames are *NOT* put there yet (if non-NULL) */  /* FIXME: dimnames are *NOT* put there yet (if non-NULL) */
1252      cholmod_l_free_sparse(&A, &c);      cholmod_free_sparse(&A, &c);
1253      UNPROTECT(1);      UNPROTECT(1);
1254      return ans;      return ans;
1255  }  }

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

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