SCM

SCM Repository

[matrix] Diff of /pkg/src/Csparse.c
ViewVC logotype

Diff of /pkg/src/Csparse.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2312, Sat Jan 10 14:01:26 2009 UTC revision 2490, Sat Oct 24 20:40:09 2009 UTC
# Line 248  Line 248 
248          cha = AS_CHM_SP(a),          cha = AS_CHM_SP(a),
249          chb = AS_CHM_SP(b),          chb = AS_CHM_SP(b),
250          chc = cholmod_l_ssmult(cha, chb, /*out_stype:*/ 0,          chc = cholmod_l_ssmult(cha, chb, /*out_stype:*/ 0,
251                               cha->xtype, /*out sorted:*/ 1, &c);                                 /* values:= is_numeric (T/F) */ cha->xtype > 0,
252                                   /*out sorted:*/ 1, &c);
253      const char *cl_a = class_P(a), *cl_b = class_P(b);      const char *cl_a = class_P(a), *cl_b = class_P(b);
254      char diag[] = {'\0', '\0'};      char diag[] = {'\0', '\0'};
255      int uploT = 0;      int uploT = 0;
256      SEXP dn = allocVector(VECSXP, 2);      SEXP dn = allocVector(VECSXP, 2);
257      R_CheckStack();      R_CheckStack();
258    
259    #ifdef DEBUG_Matrix
260        Rprintf("DBG Csparse_C*_prod(%s, %s)\n", cl_a, cl_b);
261    #endif
262    
263      /* Preserve triangularity and even unit-triangularity if appropriate.      /* Preserve triangularity and even unit-triangularity if appropriate.
264       * Note that in that case, the multiplication itself should happen       * Note that in that case, the multiplication itself should happen
265       * faster.  But there's no support for that in CHOLMOD */       * faster.  But there's no support for that in CHOLMOD */
# Line 363  Line 368 
368      int trip = asLogical(triplet),      int trip = asLogical(triplet),
369          tr   = asLogical(trans); /* gets reversed because _aat is tcrossprod */          tr   = asLogical(trans); /* gets reversed because _aat is tcrossprod */
370      CHM_TR cht = trip ? AS_CHM_TR(x) : (CHM_TR) NULL;      CHM_TR cht = trip ? AS_CHM_TR(x) : (CHM_TR) NULL;
371    /* workaround originally:
372        CHM_TR cht = trip ? AS_CHM_TR__(Tsparse_diagU2N(x)) : (CHM_TR) NULL;
373    */
374    
375      CHM_SP chcp, chxt,      CHM_SP chcp, chxt,
376          chx = (trip ?          chx = (trip ?
377                 cholmod_l_triplet_to_sparse(cht, cht->nnz, &c) :                 cholmod_l_triplet_to_sparse(cht, cht->nnz, &c) :
# Line 599  Line 608 
608          break;          break;
609    
610      default: /* -1 from above */      default: /* -1 from above */
611          error("diag_tC(): invalid 'resultKind'");          error(_("diag_tC(): invalid 'resultKind'"));
612          /* Wall: */ ans = R_NilValue; v = REAL(ans);          /* Wall: */ ans = R_NilValue; v = REAL(ans);
613      }      }
614    
# Line 629  Line 638 
638    
639      return diag_tC_ptr(n, x_p, x_x, perm, resultKind);      return diag_tC_ptr(n, x_p, x_x, perm, resultKind);
640  }  }
641    
642    /**
643     * Create a Csparse matrix object from indices and/or pointers.
644     *
645     * @param cls name of actual class of object to create
646     * @param i optional integer vector of length nnz of row indices
647     * @param j optional integer vector of length nnz of column indices
648     * @param p optional integer vector of length np of row or column pointers
649     * @param np length of integer vector p.  Must be zero if p == (int*)NULL
650     * @param x optional vector of values
651     * @param nnz length of vectors i, j and/or x, whichever is to be used
652     * @param dims optional integer vector of length 2 to be used as
653     *     dimensions.  If dims == (int*)NULL then the maximum row and column
654     *     index are used as the dimensions.
655     * @param dimnames optional list of length 2 to be used as dimnames
656     * @param index1 indicator of 1-based indices
657     *
658     * @return an SEXP of class cls inheriting from CsparseMatrix.
659     */
660    SEXP create_Csparse(char* cls, int* i, int* j, int* p, int np,
661                        void* x, int nnz, int* dims, SEXP dimnames,
662                        int index1)
663    {
664        SEXP ans;
665        int *ij = (int*)NULL, *tri, *trj,
666            mi, mj, mp, nrow = -1, ncol = -1;
667        int xtype = -1;             /* -Wall */
668        CHM_TR T;
669        CHM_SP A;
670    
671        if (np < 0 || nnz < 0)
672            error(_("negative vector lengths not allowed: np = %d, nnz = %d"),
673                  np, nnz);
674        if (1 != ((mi = (i == (int*)NULL)) +
675                  (mj = (j == (int*)NULL)) +
676                  (mp = (p == (int*)NULL))))
677            error(_("exactly 1 of 'i', 'j' or 'p' must be NULL"));
678        if (mp) {
679            if (np) error(_("np = %d, must be zero when p is NULL"), np);
680        } else {
681            if (np) {               /* Expand p to form i or j */
682                if (!(p[0])) error(_("p[0] = %d, should be zero"), p[0]);
683                for (int ii = 0; ii < np; ii++)
684                    if (p[ii] > p[ii + 1])
685                        error(_("p must be non-decreasing"));
686                if (p[np] != nnz)
687                    error("p[np] = %d != nnz = %d", p[np], nnz);
688                ij = Calloc(nnz, int);
689                if (mi) {
690                    i = ij;
691                    nrow = np;
692                } else {
693                    j = ij;
694                    ncol = np;
695                }
696                                    /* Expand p to 0-based indices */
697                for (int ii = 0; ii < np; ii++)
698                    for (int jj = p[ii]; jj < p[ii + 1]; jj++) ij[jj] = ii;
699            } else {
700                if (nnz)
701                    error(_("Inconsistent dimensions: np = 0 and nnz = %d"),
702                          nnz);
703            }
704        }
705                                    /* calculate nrow and ncol */
706        if (nrow < 0) {
707            for (int ii = 0; ii < nnz; ii++) {
708                int i1 = i[ii] + (index1 ? 0 : 1); /* 1-based index */
709                if (i1 < 1) error(_("invalid row index at position %d"), ii);
710                if (i1 > nrow) nrow = i1;
711            }
712        }
713        if (ncol < 0) {
714            for (int jj = 0; jj < nnz; jj++) {
715                int j1 = j[jj] + (index1 ? 0 : 1);
716                if (j1 < 1) error(_("invalid column index at position %d"), jj);
717                if (j1 > ncol) ncol = j1;
718            }
719        }
720        if (dims != (int*)NULL) {
721            if (dims[0] > nrow) nrow = dims[0];
722            if (dims[1] > ncol) ncol = dims[1];
723        }
724                                    /* check the class name */
725        if (strlen(cls) != 8)
726            error(_("strlen of cls argument = %d, should be 8"), strlen(cls));
727        if (!strcmp(cls + 2, "CMatrix"))
728            error(_("cls = \"%s\" does not end in \"CMatrix\""), cls);
729        switch(cls[0]) {
730        case 'd':
731        case 'l':
732               xtype = CHOLMOD_REAL;
733               break;
734        case 'n':
735               xtype = CHOLMOD_PATTERN;
736               break;
737        default:
738               error(_("cls = \"%s\" must begin with 'd', 'l' or 'n'"), cls);
739        }
740        if (cls[1] != 'g')
741            error(_("Only 'g'eneral sparse matrix types allowed"));
742                                    /* allocate and populate the triplet */
743        T = cholmod_l_allocate_triplet((size_t)nrow, (size_t)ncol, (size_t)nnz, 0,
744                                        xtype, &c);
745        T->x = x;
746        tri = (int*)T->i;
747        trj = (int*)T->j;
748        for (int ii = 0; ii < nnz; ii++) {
749            tri[ii] = i[ii] - ((!mi && index1) ? 1 : 0);
750            trj[ii] = j[ii] - ((!mj && index1) ? 1 : 0);
751        }
752                                    /* create the cholmod_sparse structure */
753        A = cholmod_l_triplet_to_sparse(T, nnz, &c);
754        cholmod_l_free_triplet(&T, &c);
755                                    /* copy the information to the SEXP */
756        ans = PROTECT(NEW_OBJECT(MAKE_CLASS(cls)));
757    /* FIXME: This has been copied from chm_sparse_to_SEXP in chm_common.c */
758                                    /* allocate and copy common slots */
759        nnz = cholmod_l_nnz(A, &c);
760        dims = INTEGER(ALLOC_SLOT(ans, Matrix_DimSym, INTSXP, 2));
761        dims[0] = A->nrow; dims[1] = A->ncol;
762        Memcpy(INTEGER(ALLOC_SLOT(ans, Matrix_pSym, INTSXP, A->ncol + 1)), (int*)A->p, A->ncol + 1);
763        Memcpy(INTEGER(ALLOC_SLOT(ans, Matrix_iSym, INTSXP, nnz)), (int*)A->i, nnz);
764        switch(cls[1]) {
765        case 'd':
766            Memcpy(REAL(ALLOC_SLOT(ans, Matrix_xSym, REALSXP, nnz)), (double*)A->x, nnz);
767            break;
768        case 'l':
769            error(_("code not yet written for cls = \"lgCMatrix\""));
770        }
771        cholmod_l_free_sparse(&A, &c);
772        UNPROTECT(1);
773        return ans;
774    }

Legend:
Removed from v.2312  
changed lines
  Added in v.2490

root@r-forge.r-project.org
ViewVC Help
Powered by ViewVC 1.0.0  
Thanks to:
Vienna University of Economics and Business Powered By FusionForge