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 1960, Fri Jul 6 16:54:43 2007 UTC revision 2120, Tue Mar 4 21:44:41 2008 UTC
# Line 1  Line 1 
1                          /* Sparse matrices in compressed column-oriented form */                          /* Sparse matrices in compressed column-oriented form */
2  #include "Csparse.h"  #include "Csparse.h"
3    #include "Tsparse.h"
4  #include "chm_common.h"  #include "chm_common.h"
5    
6  SEXP Csparse_validate(SEXP x)  SEXP Csparse_validate(SEXP x)
# Line 57  Line 58 
58      return ScalarLogical(1);      return ScalarLogical(1);
59  }  }
60    
61    SEXP Rsparse_validate(SEXP x)
62    {
63        /* NB: we do *NOT* check a potential 'x' slot here, at all */
64        SEXP pslot = GET_SLOT(x, Matrix_pSym),
65            jslot = GET_SLOT(x, Matrix_jSym);
66        Rboolean sorted, strictly;
67        int i, k,
68            *dims = INTEGER(GET_SLOT(x, Matrix_DimSym)),
69            nrow = dims[0],
70            ncol = dims[1],
71            *xp = INTEGER(pslot),
72            *xj = INTEGER(jslot);
73    
74        if (length(pslot) != dims[0] + 1)
75            return mkString(_("slot p must have length = nrow(.) + 1"));
76        if (xp[0] != 0)
77            return mkString(_("first element of slot p must be zero"));
78        if (length(jslot) < xp[nrow]) /* allow larger slots from over-allocation!*/
79            return
80                mkString(_("last element of slot p must match length of slots j and x"));
81        for (i = 0; i < length(jslot); i++) {
82            if (xj[i] < 0 || xj[i] >= ncol)
83                return mkString(_("all column indices must be between 0 and ncol-1"));
84        }
85        sorted = TRUE; strictly = TRUE;
86        for (i = 0; i < nrow; i++) {
87            if (xp[i] > xp[i+1])
88                return mkString(_("slot p must be non-decreasing"));
89            if(sorted)
90                for (k = xp[i] + 1; k < xp[i + 1]; k++) {
91                    if (xj[k] < xj[k - 1])
92                        sorted = FALSE;
93                    else if (xj[k] == xj[k - 1])
94                        strictly = FALSE;
95                }
96        }
97        if (!sorted)
98            /* cannot easily use cholmod_sort(.) ... -> "error out" :*/
99            return mkString(_("slot j is not increasing inside a column"));
100        else if(!strictly) /* sorted, but not strictly */
101            return mkString(_("slot j is not *strictly* increasing inside a column"));
102    
103        return ScalarLogical(1);
104    }
105    
106    
107  /* Called from ../R/Csparse.R : */  /* Called from ../R/Csparse.R : */
108  /* Can only return [dln]geMatrix (no symm/triang);  /* Can only return [dln]geMatrix (no symm/triang);
109   * FIXME: replace by non-CHOLMOD code ! */   * FIXME: replace by non-CHOLMOD code ! */
# Line 124  Line 171 
171  SEXP Csparse_general_to_symmetric(SEXP x, SEXP uplo)  SEXP Csparse_general_to_symmetric(SEXP x, SEXP uplo)
172  {  {
173      CHM_SP chx = AS_CHM_SP(x), chgx;      CHM_SP chx = AS_CHM_SP(x), chgx;
174      int uploT = (*CHAR(asChar(uplo)) == 'U') ? 1 : -1;      int uploT = (*CHAR(STRING_ELT(uplo,0)) == 'U') ? 1 : -1;
175      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
176      R_CheckStack();      R_CheckStack();
177    
# Line 156  Line 203 
203    
204  SEXP Csparse_Csparse_prod(SEXP a, SEXP b)  SEXP Csparse_Csparse_prod(SEXP a, SEXP b)
205  {  {
206      CHM_SP cha = AS_CHM_SP(a), chb = AS_CHM_SP(b);      CHM_SP
207      CHM_SP chc = cholmod_ssmult(cha, chb, 0, cha->xtype, 1, &c);          cha = AS_CHM_SP(Csparse_diagU2N(a)),
208            chb = AS_CHM_SP(Csparse_diagU2N(b)),
209            chc = cholmod_ssmult(cha, chb, 0, cha->xtype, 1, &c);
210      SEXP dn = allocVector(VECSXP, 2);      SEXP dn = allocVector(VECSXP, 2);
211      R_CheckStack();      R_CheckStack();
212    
# Line 171  Line 220 
220  SEXP Csparse_Csparse_crossprod(SEXP a, SEXP b, SEXP trans)  SEXP Csparse_Csparse_crossprod(SEXP a, SEXP b, SEXP trans)
221  {  {
222      int tr = asLogical(trans);      int tr = asLogical(trans);
223      CHM_SP cha = AS_CHM_SP(a), chb = AS_CHM_SP(b), chTr, chc;      CHM_SP
224            cha = AS_CHM_SP(Csparse_diagU2N(a)),
225            chb = AS_CHM_SP(Csparse_diagU2N(b)),
226            chTr, chc;
227      SEXP dn = allocVector(VECSXP, 2);      SEXP dn = allocVector(VECSXP, 2);
228      R_CheckStack();      R_CheckStack();
229    
# Line 189  Line 241 
241    
242  SEXP Csparse_dense_prod(SEXP a, SEXP b)  SEXP Csparse_dense_prod(SEXP a, SEXP b)
243  {  {
244      CHM_SP cha = AS_CHM_SP(a);      CHM_SP cha = AS_CHM_SP(Csparse_diagU2N(a));
245      SEXP b_M = PROTECT(mMatrix_as_dgeMatrix(b));      SEXP b_M = PROTECT(mMatrix_as_dgeMatrix(b));
246      CHM_DN chb = AS_CHM_DN(b_M);      CHM_DN chb = AS_CHM_DN(b_M);
247      CHM_DN chc = cholmod_allocate_dense(cha->nrow, chb->ncol, cha->nrow,      CHM_DN chc = cholmod_allocate_dense(cha->nrow, chb->ncol, cha->nrow,
# Line 209  Line 261 
261    
262  SEXP Csparse_dense_crossprod(SEXP a, SEXP b)  SEXP Csparse_dense_crossprod(SEXP a, SEXP b)
263  {  {
264      CHM_SP cha = AS_CHM_SP(a);      CHM_SP cha = AS_CHM_SP(Csparse_diagU2N(a));
265      SEXP b_M = PROTECT(mMatrix_as_dgeMatrix(b));      SEXP b_M = PROTECT(mMatrix_as_dgeMatrix(b));
266      CHM_DN chb = AS_CHM_DN(b_M);      CHM_DN chb = AS_CHM_DN(b_M);
267      CHM_DN chc = cholmod_allocate_dense(cha->ncol, chb->ncol, cha->ncol,      CHM_DN chc = cholmod_allocate_dense(cha->ncol, chb->ncol, cha->ncol,
# Line 232  Line 284 
284  {  {
285      int trip = asLogical(triplet),      int trip = asLogical(triplet),
286          tr   = asLogical(trans); /* gets reversed because _aat is tcrossprod */          tr   = asLogical(trans); /* gets reversed because _aat is tcrossprod */
287      CHM_TR cht = trip ? AS_CHM_TR(x) : (CHM_TR) NULL;      CHM_TR cht = trip ? AS_CHM_TR(Tsparse_diagU2N(x)) : (CHM_TR) NULL;
288      CHM_SP chcp, chxt,      CHM_SP chcp, chxt,
289          chx = trip ? cholmod_triplet_to_sparse(cht, cht->nnz, &c) : AS_CHM_SP(x);          chx = (trip ?
290                   cholmod_triplet_to_sparse(cht, cht->nnz, &c) :
291                   AS_CHM_SP(Csparse_diagU2N(x)));
292      SEXP dn = PROTECT(allocVector(VECSXP, 2));      SEXP dn = PROTECT(allocVector(VECSXP, 2));
293      R_CheckStack();      R_CheckStack();
294    
295      if (!tr) chxt = cholmod_transpose(chx, chx->xtype, &c);      if (!tr) chxt = cholmod_transpose(chx, chx->xtype, &c);
296      chcp = cholmod_aat((!tr) ? chxt : chx, (int *) NULL, 0, chx->xtype, &c);      chcp = cholmod_aat((!tr) ? chxt : chx, (int *) NULL, 0, chx->xtype, &c);
297      if(!chcp) error(_("Csparse_crossprod(): error return from cholmod_aat()"));      if(!chcp) {
298            UNPROTECT(1);
299            error(_("Csparse_crossprod(): error return from cholmod_aat()"));
300        }
301      cholmod_band_inplace(0, chcp->ncol, chcp->xtype, chcp, &c);      cholmod_band_inplace(0, chcp->ncol, chcp->xtype, chcp, &c);
302      chcp->stype = 1;      chcp->stype = 1;
303      if (trip) cholmod_free_sparse(&chx, &c);      if (trip) cholmod_free_sparse(&chx, &c);
# Line 302  Line 359 
359    
360  SEXP Csparse_diagU2N(SEXP x)  SEXP Csparse_diagU2N(SEXP x)
361  {  {
362      if (*diag_P(x) != 'U') {/* "trivially fast" when there's no 'diag' slot at all */      const char *cl = class_P(x);
363        /* dtCMatrix, etc; [1] = the second character =?= 't' for triangular */
364        if (cl[1] != 't' || *diag_P(x) != 'U') {
365            /* "trivially fast" when not triangular (<==> no 'diag' slot), or not *unit* triangular */
366          return (x);          return (x);
367      }      }
368      else {      else {
# Line 339  Line 399 
399                                1, 0, Rkind, "",                                1, 0, Rkind, "",
400                                /* FIXME: drops dimnames */ R_NilValue);                                /* FIXME: drops dimnames */ R_NilValue);
401  }  }
402    
403    SEXP Csparse_MatrixMarket(SEXP x, SEXP fname)
404    {
405        FILE *f = fopen(CHAR(asChar(fname)), "w");
406    
407        if (!f)
408            error(_("failure to open file \"%s\" for writing"),
409                  CHAR(asChar(fname)));
410        if (!cholmod_write_sparse(f, AS_CHM_SP(Csparse_diagU2N(x)),
411                                  (CHM_SP)NULL, (char*) NULL, &c))
412            error(_("cholmod_write_sparse returned error code"));
413        fclose(f);
414        return R_NilValue;
415    }

Legend:
Removed from v.1960  
changed lines
  Added in v.2120

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