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 3066, Thu Mar 19 14:27:05 2015 UTC revision 3069, Thu Mar 26 10:00:49 2015 UTC
# Line 150  Line 150 
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);
152    
153      SEXP ans = 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                                     /* transp: */ FALSE);
155      // -> a [dln]geMatrix      // -> a [dln]geMatrix
156      if(is_sym) { // ==> want  [dln]syMatrix      if(is_sym) { // ==> want  [dln]syMatrix
157          const char cl1 = class_P(ans)[0];          const char cl1 = class_P(ans)[0];
# Line 194  Line 195 
195    
196      return chm_sparse_to_SEXP(chxcp, 1/*do_free*/,      return chm_sparse_to_SEXP(chxcp, 1/*do_free*/,
197                                tr ? ((*uplo_P(x) == 'U') ? 1 : -1) : 0,                                tr ? ((*uplo_P(x) == 'U') ? 1 : -1) : 0,
198                                0, tr ? diag_P(x) : "",                                /* Rkind: pattern */ 0,
199                                  /* diag = */ tr ? diag_P(x) : "",
200                                GET_SLOT(x, Matrix_DimNamesSym));                                GET_SLOT(x, Matrix_DimNamesSym));
201  }  }
202    
# Line 399  Line 401 
401                                Rkind, tr ? diag_P(x) : "", dn);                                Rkind, tr ? diag_P(x) : "", dn);
402  }  }
403    
404  SEXP Csparse_Csparse_prod(SEXP a, SEXP b)  /* NOTA BENE:  cholmod_ssmult(A,B, ...) ->  ./CHOLMOD/MatrixOps/cholmod_ssmult.c
405     * ---------  computes a patter*n* matrix __always_ when
406     * *one* of A or B is pattern*n*, because of this (line 73-74):
407       ---------------------------------------------------------------------------
408        values = values &&
409            (A->xtype != CHOLMOD_PATTERN) && (B->xtype != CHOLMOD_PATTERN) ;
410       ---------------------------------------------------------------------------
411     * ==> Often need to copy the patter*n* to a *l*ogical matrix first !!!
412     */
413    
414    SEXP Csparse_Csparse_prod(SEXP a, SEXP b, SEXP bool_arith)
415  {  {
416      CHM_SP      CHM_SP
417          cha = AS_CHM_SP(a),          cha = AS_CHM_SP(a),
418          chb = AS_CHM_SP(b),          chb = AS_CHM_SP(b), chc;
         chc = cholmod_ssmult(cha, chb, /*out_stype:*/ 0,  
                              // values:= is_numeric (T/F) if _one_ is numeric:  
                              cha->xtype > 0 || chb->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));  
419      R_CheckStack();      R_CheckStack();
420        // const char *cl_a = class_P(a), *cl_b = class_P(b);
421        static const char *valid_tri[] = { MATRIX_VALID_tri_Csparse, "" };
422        char diag[] = {'\0', '\0'};
423        int uploT = 0, nprot = 1,
424            do_bool = asLogical(bool_arith); // TRUE / NA / FALSE
425        Rboolean
426            a_is_n = (cha->xtype == CHOLMOD_PATTERN),
427            b_is_n = (chb->xtype == CHOLMOD_PATTERN),
428            force_num = (do_bool == FALSE),
429            maybe_bool= (do_bool == NA_LOGICAL);
430    
431  #ifdef DEBUG_Matrix_verbose  #ifdef DEBUG_Matrix_verbose
432      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));
433  #endif  #endif
434    
435        if(a_is_n && (force_num || (maybe_bool && !b_is_n))) {
436            /* coerce 'a' to  double;
437             * have no CHOLMOD function (pattern -> logical) --> use "our" code */
438            SEXP da = PROTECT(nz2Csparse(a, x_double)); nprot++;
439            cha = AS_CHM_SP(da);
440            R_CheckStack();
441            a_is_n = FALSE;
442        }
443        else if(b_is_n && (force_num || (maybe_bool && !a_is_n))) {
444            // coerce 'b' to  double
445            SEXP db = PROTECT(nz2Csparse(b, x_double)); nprot++;
446            chb = AS_CHM_SP(db);
447            R_CheckStack();
448            b_is_n = FALSE;
449        }
450        chc = cholmod_ssmult(cha, chb, /*out_stype:*/ 0,
451                             /* values : */ do_bool != TRUE,
452                             /* sorted = TRUE: */ 1, &c);
453    
454      /* Preserve triangularity and even unit-triangularity if appropriate.      /* Preserve triangularity and even unit-triangularity if appropriate.
455       * Note that in that case, the multiplication itself should happen       * Note that in that case, the multiplication itself should happen
456       * faster.  But there's no support for that in CHOLMOD */       * faster.  But there's no support for that in CHOLMOD */
457    
458      /* UGLY hack -- rather should have (fast!) C-level version of      if(Matrix_check_class_etc(a, valid_tri) >= 0 &&
459       *       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"..*/  
460          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. */
461              uploT = (*uplo_P(a) == 'U') ? 1 : -1;              uploT = (*uplo_P(a) == 'U') ? 1 : -1;
462              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 435  Line 466 
466              }              }
467              else diag[0]= 'N';              else diag[0]= 'N';
468          }          }
469    
470        SEXP dn = PROTECT(allocVector(VECSXP, 2));
471      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */
472                     duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), 0)));                     duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), 0)));
473      SET_VECTOR_ELT(dn, 1,      SET_VECTOR_ELT(dn, 1,
474                     duplicate(VECTOR_ELT(GET_SLOT(b, Matrix_DimNamesSym), 1)));                     duplicate(VECTOR_ELT(GET_SLOT(b, Matrix_DimNamesSym), 1)));
475      UNPROTECT(1);      UNPROTECT(nprot);
476      return chm_sparse_to_SEXP(chc, 1, uploT, /*Rkind*/0, diag, dn);      return chm_sparse_to_SEXP(chc, 1, uploT, /*Rkind*/0, diag, dn);
477  }  }
478    
479  SEXP Csparse_Csparse_crossprod(SEXP a, SEXP b, SEXP trans)  /* trans = FALSE:  crossprod(a,b)
480     * trans = TRUE : tcrossprod(a,b) */
481    SEXP Csparse_Csparse_crossprod(SEXP a, SEXP b, SEXP trans, SEXP bool_arith)
482  {  {
483      int tr = asLogical(trans);      int tr = asLogical(trans), nprot = 1,
484            do_bool = asLogical(bool_arith); // TRUE / NA / FALSE
485      CHM_SP      CHM_SP
486          cha = AS_CHM_SP(a),          cha = AS_CHM_SP(a),
487          chb = AS_CHM_SP(b),          chb = AS_CHM_SP(b),
488          chTr, chc;          chTr, chc;
489      const char *cl_a = class_P(a), *cl_b = class_P(b);      R_CheckStack();
490        // const char *cl_a = class_P(a), *cl_b = class_P(b);
491        static const char *valid_tri[] = { MATRIX_VALID_tri_Csparse, "" };
492      char diag[] = {'\0', '\0'};      char diag[] = {'\0', '\0'};
493      int uploT = 0;      int uploT = 0;
494      SEXP dn = PROTECT(allocVector(VECSXP, 2));      Rboolean
495            a_is_n = (cha->xtype == CHOLMOD_PATTERN),
496            b_is_n = (chb->xtype == CHOLMOD_PATTERN),
497            force_num = (do_bool == FALSE),
498            maybe_bool= (do_bool == NA_LOGICAL);
499    
500        if(a_is_n && (force_num || (maybe_bool && !b_is_n))) {
501            // coerce 'a' to  double
502            SEXP da = PROTECT(nz2Csparse(a, x_double)); nprot++;
503            cha = AS_CHM_SP(da);
504      R_CheckStack();      R_CheckStack();
505            a_is_n = FALSE;
506        }
507        else if(b_is_n && (force_num || (maybe_bool && !a_is_n))) {
508            // coerce 'b' to  double
509            SEXP db = PROTECT(nz2Csparse(b, x_double)); nprot++;
510            chb = AS_CHM_SP(db);
511            R_CheckStack();
512            b_is_n = FALSE;
513        }
514    
515      chTr = cholmod_transpose((tr) ? chb : cha, chb->xtype, &c);      chTr = cholmod_transpose((tr) ? chb : cha, chb->xtype, &c);
516      chc = cholmod_ssmult((tr) ? cha : chTr, (tr) ? chTr : chb,      chc = cholmod_ssmult((tr) ? cha : chTr, (tr) ? chTr : chb,
517                           /*out_stype:*/ 0, cha->xtype, /*out sorted:*/ 1, &c);                           /*out_stype:*/ 0, /* values : */ do_bool != TRUE,
518                             /* sorted = TRUE: */ 1, &c);
519      cholmod_free_sparse(&chTr, &c);      cholmod_free_sparse(&chTr, &c);
520    
521      /* Preserve triangularity and unit-triangularity if appropriate;      /* Preserve triangularity and unit-triangularity if appropriate;
522       * see Csparse_Csparse_prod() for comments */       * see Csparse_Csparse_prod() for comments */
523      if (cl_a[1] == 't' && cl_b[1] == 't')      if(Matrix_check_class_etc(a, valid_tri) >= 0 &&
524           Matrix_check_class_etc(b, valid_tri) >= 0)
525          if(*uplo_P(a) != *uplo_P(b)) { /* one 'U', the other 'L' */          if(*uplo_P(a) != *uplo_P(b)) { /* one 'U', the other 'L' */
526              uploT = (*uplo_P(b) == 'U') ? 1 : -1;              uploT = (*uplo_P(b) == 'U') ? 1 : -1;
527              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 472  Line 530 
530              }              }
531              else diag[0]= 'N';              else diag[0]= 'N';
532          }          }
533    
534        SEXP dn = PROTECT(allocVector(VECSXP, 2));
535      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */
536                     duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), (tr) ? 0 : 1)));                     duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym),
537                                            (tr) ? 0 : 1)));
538      SET_VECTOR_ELT(dn, 1,      SET_VECTOR_ELT(dn, 1,
539                     duplicate(VECTOR_ELT(GET_SLOT(b, Matrix_DimNamesSym), (tr) ? 0 : 1)));                     duplicate(VECTOR_ELT(GET_SLOT(b, Matrix_DimNamesSym),
540      UNPROTECT(1);                                          (tr) ? 0 : 1)));
541        UNPROTECT(nprot);
542      return chm_sparse_to_SEXP(chc, 1, uploT, /*Rkind*/0, diag, dn);      return chm_sparse_to_SEXP(chc, 1, uploT, /*Rkind*/0, diag, dn);
543  }  }
544    
545  SEXP Csparse_dense_prod(SEXP a, SEXP b)  /**
546     * All (dense * sparse)  Matrix products and cross products
547     *
548     *   f( f(<Csparse>)  %*%  f(<dense>) )   for  f in {t(), identity()}
549     *
550     * @param a CsparseMatrix  (n x m)
551     * @param b numeric vector, matrix, or denseMatrix (m x k) or (k x m)  if `transp` is '2' or 'B'
552     * @param transp character.
553     *        = " " : nothing transposed {apart from a}
554     *        = "2" : "transpose 2nd arg": use  t(b) instead of b (= 2nd argument)
555     *        = "c" : "transpose c":       Return  t(c) instead of c
556     *        = "B" : "transpose both":    use t(b) and return t(c) instead of c
557     * NB: For "2", "c", "B", need to transpose a *dense* matrix, B or C --> chm_transpose_dense()
558     *
559     * @return a dense matrix, the matrix product c = g(a,b) :
560     *
561     *                                                Condition (R)   Condition (C)
562     *   R notation            Math notation          cross  transp   t.a t.b t.ans
563     *   ~~~~~~~~~~~~~~~~~     ~~~~~~~~~~~~~~~~~~     ~~~~~~~~~~~~~   ~~~~~~~~~~~~~
564     *   c <-   a %*%   b      C :=      A B            .       " "    .   .   .
565     *   c <-   a %*% t(b)     C :=      A B'           .       "2"    .   |   .
566     *   c <- t(a %*%   b)     C := (A B)'  = B'A'      .       "c"    .   .   |
567     *   c <- t(a %*% t(b))    C := (A B')' = B A'      .       "B"    .   |   |
568     *
569     *   c <-   t(a) %*%   b   C :=      A'B           TRUE     " "    |   .   .
570     *   c <-   t(a) %*% t(b)  C :=      A'B'          TRUE     "2"    |   |   .
571     *   c <- t(t(a) %*%   b)  C := (A'B)'  = B'A      TRUE     "c"    |   .   |
572     *   c <- t(t(a) %*% t(b)) C := (A'B')' = B A      TRUE     "B"    |   |   |
573     */
574    SEXP Csp_dense_products(SEXP a, SEXP b,
575                            Rboolean transp_a, Rboolean transp_b, Rboolean transp_ans)
576  {  {
577      CHM_SP cha = AS_CHM_SP(a);      CHM_SP cha = AS_CHM_SP(a);
578      SEXP b_M = PROTECT(mMatrix_as_dgeMatrix2(b, // transpose_if_vector =      int a_nc = transp_a ? cha->nrow : cha->ncol,
579                                               cha->ncol == 1));          a_nr = transp_a ? cha->ncol : cha->nrow;
580      CHM_DN chb = AS_CHM_DN(b_M);      Rboolean
581      CHM_DN chc = cholmod_allocate_dense(cha->nrow, chb->ncol, cha->nrow,          maybe_transp_b = (a_nc == 1),
582                                          chb->xtype, &c);          b_is_vector = FALSE;
583      SEXP dn = PROTECT(allocVector(VECSXP, 2));      /* NOTE: trans_b {<--> "use t(b) instead of b" }
584           ----  "interferes" with the  case automatic treatment of *vector* b.
585           In that case,  t(b) or b is used "whatever make more sense",
586           according to the general R philosophy of treating vectors in matrix products.
587        */
588    
589        /* repeating a "cheap part" of  mMatrix_as_dgeMatrix2(b, .)  to see if
590         * we have a vector that we might 'transpose_if_vector' : */
591        static const char *valid[] = {"_NOT_A_CLASS_", MATRIX_VALID_ddense, ""};
592        /* int ctype = Matrix_check_class_etc(b, valid);
593         * if (ctype > 0)   /.* a ddenseMatrix object */
594        if (Matrix_check_class_etc(b, valid) < 0) {
595            // not a ddenseM*:  is.matrix() or vector:
596            b_is_vector = !isMatrix(b);
597        }
598    
599        if(b_is_vector) {
600            /* determine *if* we want/need to transpose at all:
601             * if (length(b) == ncol(A)) have match: use dim = c(n, 1) (<=> do *not* transp);
602             *  otherwise, try to transpose: ok  if (ncol(A) == 1) [see also above]:  */
603            maybe_transp_b = (LENGTH(b) != a_nc);
604            // Here, we transpose already in mMatrix_as_dge*()  ==> don't do it later:
605            transp_b = FALSE;
606        }
607        SEXP b_M = PROTECT(mMatrix_as_dgeMatrix2(b, maybe_transp_b));
608    
609        CHM_DN chb = AS_CHM_DN(b_M), b_t;
610        R_CheckStack();
611        int ncol_b;
612        if(transp_b) { // transpose b:
613            b_t = cholmod_allocate_dense(chb->ncol, chb->nrow, chb->ncol, chb->xtype, &c);
614            chm_transpose_dense(b_t, chb);
615            ncol_b = b_t->ncol;
616        } else
617            ncol_b = chb->ncol;
618        // Result C {with dim() before it may be transposed}:
619        CHM_DN chc = cholmod_allocate_dense(a_nr, ncol_b, a_nr, chb->xtype, &c);
620      double one[] = {1,0}, zero[] = {0,0};      double one[] = {1,0}, zero[] = {0,0};
621      int nprot = 2;      int nprot = 2;
622      R_CheckStack();  
623      /* 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:*/
624      if(cha->xtype == CHOLMOD_PATTERN) {      if(cha->xtype == CHOLMOD_PATTERN) {
625          /* 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 503  Line 631 
631          SEXP da = PROTECT(nz2Csparse(a, x_double)); nprot++;          SEXP da = PROTECT(nz2Csparse(a, x_double)); nprot++;
632          cha = AS_CHM_SP(da);          cha = AS_CHM_SP(da);
633      }      }
634      cholmod_sdmult(cha, 0, one, zero, chb, chc, &c);  
635      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */      /* cholmod_sdmult(A, transp, alpha, beta, X,  Y,  &c): depending on transp == 0 / != 0:
636                     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:
637      SET_VECTOR_ELT(dn, 1,       *  Y := A*X  or  A'*X
638                     duplicate(VECTOR_ELT(GET_SLOT(b_M, Matrix_DimNamesSym), 1)));       *                       NB: always  <sparse> %*% <dense> !
639         */
640        cholmod_sdmult(cha, transp_a, one, zero, (transp_b ? b_t : chb), /* -> */ chc, &c);
641    
642        SEXP dn = PROTECT(allocVector(VECSXP, 2));  /* establish dimnames */
643        SET_VECTOR_ELT(dn, transp_ans ? 1 : 0,
644                       duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), transp_a ? 1 : 0)));
645        SET_VECTOR_ELT(dn, transp_ans ? 0 : 1,
646                       duplicate(VECTOR_ELT(GET_SLOT(b_M, Matrix_DimNamesSym),
647                                            transp_b ? 0 : 1)));
648        if(transp_b) cholmod_free_dense(&b_t, &c);
649      UNPROTECT(nprot);      UNPROTECT(nprot);
650      return chm_dense_to_SEXP(chc, 1, 0, dn);      return chm_dense_to_SEXP(chc, 1, 0, dn, transp_ans);
651  }  }
652    
653  SEXP Csparse_dense_crossprod(SEXP a, SEXP b)  
654    SEXP Csparse_dense_prod(SEXP a, SEXP b, SEXP transp)
655  {  {
656      CHM_SP cha = AS_CHM_SP(a);      return
657      SEXP b_M = PROTECT(mMatrix_as_dgeMatrix2(b, // transpose_if_vector =          Csp_dense_products(a, b,
658                                               cha->nrow == 1));                  /* transp_a = */ FALSE,
659      CHM_DN chb = AS_CHM_DN(b_M);                  /* transp_b   = */ (*CHAR(asChar(transp)) == '2' || *CHAR(asChar(transp)) == 'B'),
660      CHM_DN chc = cholmod_allocate_dense(cha->ncol, chb->ncol, cha->ncol,                  /* transp_ans = */ (*CHAR(asChar(transp)) == 'c' || *CHAR(asChar(transp)) == 'B'));
                                         chb->xtype, &c);  
     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);  
661      }      }
662      cholmod_sdmult(cha, 1, one, zero, chb, chc, &c);  
663      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */  SEXP Csparse_dense_crossprod(SEXP a, SEXP b, SEXP transp)
664                     duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), 1)));  {
665      SET_VECTOR_ELT(dn, 1,      return
666                     duplicate(VECTOR_ELT(GET_SLOT(b_M, Matrix_DimNamesSym), 1)));          Csp_dense_products(a, b,
667      UNPROTECT(nprot);                  /* transp_a = */ TRUE,
668      return chm_dense_to_SEXP(chc, 1, 0, dn);                  /* transp_b   = */ (*CHAR(asChar(transp)) == '2' || *CHAR(asChar(transp)) == 'B'),
669                    /* transp_ans = */ (*CHAR(asChar(transp)) == 'c' || *CHAR(asChar(transp)) == 'B'));
670  }  }
671    
672    
673  /* Computes   x'x  or  x x' -- *also* for Tsparse (triplet = TRUE)  /* Computes   x'x  or  x x' -- *also* for Tsparse (triplet = TRUE)
674     see Csparse_Csparse_crossprod above for  x'y and x y' */     see Csparse_Csparse_crossprod above for  x'y and x y' */
675  SEXP Csparse_crossprod(SEXP x, SEXP trans, SEXP triplet)  SEXP Csparse_crossprod(SEXP x, SEXP trans, SEXP triplet, SEXP bool_arith)
676  {  {
677      int trip = asLogical(triplet),      int tripl = asLogical(triplet),
678          tr   = asLogical(trans); /* gets reversed because _aat is tcrossprod */          tr   = asLogical(trans), /* gets reversed because _aat is tcrossprod */
679            do_bool = asLogical(bool_arith); // TRUE / NA / FALSE
680  #ifdef AS_CHM_DIAGU2N_FIXED_FINALLY  #ifdef AS_CHM_DIAGU2N_FIXED_FINALLY
681      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;
682  #else /* workaround needed:*/  #else /* workaround needed:*/
683      SEXP xx = PROTECT(Tsparse_diagU2N(x));      SEXP xx = PROTECT(Tsparse_diagU2N(x));
684      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;
685  #endif  #endif
686      CHM_SP chcp, chxt,      CHM_SP chcp, chxt,
687          chx = (trip ?          chx = (tripl ?
688                 cholmod_triplet_to_sparse(cht, cht->nnz, &c) :                 cholmod_triplet_to_sparse(cht, cht->nnz, &c) :
689                 AS_CHM_SP(x));                 AS_CHM_SP(x));
690      SEXP dn = PROTECT(allocVector(VECSXP, 2));      SEXP dn = PROTECT(allocVector(VECSXP, 2));
691      R_CheckStack();      R_CheckStack();
692        Rboolean
693            x_is_n = (chx->xtype == CHOLMOD_PATTERN),
694            force_num = (do_bool == FALSE);
695    
696        if(x_is_n && force_num) {
697            // coerce 'x' to  double
698            SEXP dx = PROTECT(nz2Csparse(x, x_double)); nprot++;
699            chx = AS_CHM_SP(dx);
700            R_CheckStack();
701        }
702    
703      if (!tr) chxt = cholmod_transpose(chx, chx->xtype, &c);      if (!tr) chxt = cholmod_transpose(chx, chx->xtype, &c);
704      chcp = cholmod_aat((!tr) ? chxt : chx, (int *) NULL, 0, chx->xtype, &c);  
705        chcp = cholmod_aat((!tr) ? chxt : chx, (int *) NULL, 0,
706                           /* mode: */ chx->xtype, &c);
707      if(!chcp) {      if(!chcp) {
708          UNPROTECT(1);          UNPROTECT(1);
709          error(_("Csparse_crossprod(): error return from cholmod_aat()"));          error(_("Csparse_crossprod(): error return from cholmod_aat()"));
710      }      }
711      cholmod_band_inplace(0, chcp->ncol, chcp->xtype, chcp, &c);      cholmod_band_inplace(0, chcp->ncol, chcp->xtype, chcp, &c);
712      chcp->stype = 1;      chcp->stype = 1; // symmetric
713      if (trip) cholmod_free_sparse(&chx, &c);      if (tripl) cholmod_free_sparse(&chx, &c);
714      if (!tr) cholmod_free_sparse(&chxt, &c);      if (!tr) cholmod_free_sparse(&chxt, &c);
715      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */
716                     duplicate(VECTOR_ELT(GET_SLOT(x, Matrix_DimNamesSym),                     duplicate(VECTOR_ELT(GET_SLOT(x, Matrix_DimNamesSym),
717                                          (tr) ? 0 : 1)));                                          (tr) ? 0 : 1)));
718      SET_VECTOR_ELT(dn, 1, duplicate(VECTOR_ELT(dn, 0)));      SET_VECTOR_ELT(dn, 1, duplicate(VECTOR_ELT(dn, 0)));
719  #ifdef AS_CHM_DIAGU2N_FIXED_FINALLY      UNPROTECT(nprot);
720      UNPROTECT(1);      // FIXME: uploT for symmetric ?
 #else  
     UNPROTECT(2);  
 #endif  
721      return chm_sparse_to_SEXP(chcp, 1, 0, 0, "", dn);      return chm_sparse_to_SEXP(chcp, 1, 0, 0, "", dn);
722  }  }
723    

Legend:
Removed from v.3066  
changed lines
  Added in v.3069

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