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 1548, Mon Sep 11 22:13:07 2006 UTC revision 1659, Wed Nov 1 17:50:15 2006 UTC
# Line 4  Line 4 
4    
5  SEXP Csparse_validate(SEXP x)  SEXP Csparse_validate(SEXP x)
6  {  {
7        /* NB: we do *NOT* check a potential 'x' slot here, at all */
8      SEXP pslot = GET_SLOT(x, Matrix_pSym),      SEXP pslot = GET_SLOT(x, Matrix_pSym),
9          islot = GET_SLOT(x, Matrix_iSym);          islot = GET_SLOT(x, Matrix_iSym);
10      int j, ncol = length(pslot) - 1,      int j, k, ncol, nrow, sorted,
11          *dims = INTEGER(GET_SLOT(x, Matrix_DimSym)),          *dims = INTEGER(GET_SLOT(x, Matrix_DimSym)),
12          nrow, *xp = INTEGER(pslot),          *xp = INTEGER(pslot),
13          *xi = INTEGER(islot);          *xi = INTEGER(islot);
14    
15      nrow = dims[0];      nrow = dims[0];
16      if (length(pslot) <= 0)      ncol = dims[1];
17          return mkString(_("slot p must have length > 0"));      if (length(pslot) != dims[1] + 1)
18            return mkString(_("slot p must have length = ncol(.) + 1"));
19      if (xp[0] != 0)      if (xp[0] != 0)
20          return mkString(_("first element of slot p must be zero"));          return mkString(_("first element of slot p must be zero"));
21      if (length(islot) != xp[ncol])      if (length(islot) != xp[ncol])
22          return mkString(_("last element of slot p must match length of slots i and x"));          return
23                mkString(_("last element of slot p must match length of slots i and x"));
24        for (j = 0; j < length(islot); j++) {
25            if (xi[j] < 0 || xi[j] >= nrow)
26                return mkString(_("all row indices must be between 0 and nrow-1"));
27        }
28        sorted = TRUE;
29      for (j = 0; j < ncol; j++) {      for (j = 0; j < ncol; j++) {
30          if (xp[j] > xp[j+1])          if (xp[j] > xp[j+1])
31              return mkString(_("slot p must be non-decreasing"));              return mkString(_("slot p must be non-decreasing"));
32            for (k = xp[j] + 1; k < xp[j + 1]; k++)
33                if (xi[k] < xi[k - 1]) sorted = FALSE;
34      }      }
35      for (j = 0; j < length(islot); j++) {      if (!sorted) {
36          if (xi[j] < 0 || xi[j] >= nrow)          cholmod_sparse *chx = as_cholmod_sparse(x);
37              return mkString(_("all row indices must be between 0 and nrow-1"));          cholmod_sort(chx, &c);
38            Free(chx);
39      }      }
40      return ScalarLogical(1);      return ScalarLogical(1);
41  }  }
# Line 97  Line 108 
108                                GET_SLOT(x, Matrix_DimNamesSym));                                GET_SLOT(x, Matrix_DimNamesSym));
109  }  }
110    
111    SEXP Csparse_general_to_symmetric(SEXP x, SEXP uplo)
112    {
113        cholmod_sparse *chx = as_cholmod_sparse(x), *chgx;
114        int uploT = (*CHAR(asChar(uplo)) == 'U') ? -1 : 1;
115        int Rkind = (chx->xtype == CHOLMOD_REAL) ? Real_kind(x) : 0;
116    
117        chgx = cholmod_copy(chx, /* stype: */ uploT, chx->xtype, &c);
118        /* xtype: pattern, "real", complex or .. */
119        Free(chx);
120        return chm_sparse_to_SEXP(chgx, 1, 0, Rkind, "",
121                                  GET_SLOT(x, Matrix_DimNamesSym));
122    }
123    
124  SEXP Csparse_transpose(SEXP x, SEXP tri)  SEXP Csparse_transpose(SEXP x, SEXP tri)
125  {  {
126      cholmod_sparse *chx = as_cholmod_sparse(x);      cholmod_sparse *chx = as_cholmod_sparse(x);
127        int Rkind = (chx->xtype == CHOLMOD_REAL) ? Real_kind(x) : 0;
128      cholmod_sparse *chxt = cholmod_transpose(chx, (int) chx->xtype, &c);      cholmod_sparse *chxt = cholmod_transpose(chx, (int) chx->xtype, &c);
129      SEXP dn = PROTECT(duplicate(GET_SLOT(x, Matrix_DimNamesSym))), tmp;      SEXP dn = PROTECT(duplicate(GET_SLOT(x, Matrix_DimNamesSym))), tmp;
130      int uploT = 0; char *diag = "";      int uploT = 0; char *diag = "";
     int Rkind = (chx->xtype == CHOLMOD_REAL) ? Real_kind(x) : 0;  
131    
132      Free(chx);      Free(chx);
133      tmp = VECTOR_ELT(dn, 0);    /* swap the dimnames */      tmp = VECTOR_ELT(dn, 0);    /* swap the dimnames */
# Line 119  Line 143 
143    
144  SEXP Csparse_Csparse_prod(SEXP a, SEXP b)  SEXP Csparse_Csparse_prod(SEXP a, SEXP b)
145  {  {
146      cholmod_sparse *cha = as_cholmod_sparse(a),      cholmod_sparse
147            *cha = as_cholmod_sparse(a),
148          *chb = as_cholmod_sparse(b);          *chb = as_cholmod_sparse(b);
149      cholmod_sparse *chc = cholmod_ssmult(cha, chb, 0, cha->xtype, 1, &c);      cholmod_sparse *chc = cholmod_ssmult(cha, chb, 0, cha->xtype, 1, &c);
150      SEXP dn = allocVector(VECSXP, 2);      SEXP dn = allocVector(VECSXP, 2);
# Line 132  Line 157 
157      return chm_sparse_to_SEXP(chc, 1, 0, 0, "", dn);      return chm_sparse_to_SEXP(chc, 1, 0, 0, "", dn);
158  }  }
159    
160    SEXP Csparse_Csparse_crossprod(SEXP a, SEXP b, SEXP trans)
161    {
162        int tr = asLogical(trans);
163        cholmod_sparse
164            *cha = as_cholmod_sparse(a),
165            *chb = as_cholmod_sparse(b);
166        cholmod_sparse *chTr, *chc;
167        SEXP dn = allocVector(VECSXP, 2);
168    
169    /*     cholmod_sparse *chTr = cholmod_transpose(cha, 1, &c); */
170    /*     cholmod_sparse *chc = cholmod_ssmult(chTr, chb, 0, cha->xtype, 1, &c); */
171    
172        if (tr)
173            chTr = cholmod_transpose(chb, chb->xtype, &c);
174        else
175            chTr = cholmod_transpose(cha, cha->xtype, &c);
176        chc = cholmod_ssmult((tr) ? cha : chTr, (tr) ? chTr : chb,
177                             0, cha->xtype, 1, &c);
178    
179        Free(cha); Free(chb); cholmod_free_sparse(&chTr, &c);
180    
181        SET_VECTOR_ELT(dn, 0,       /* establish dimnames */
182                       duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), (tr) ? 0 : 1)));
183        SET_VECTOR_ELT(dn, 1,
184                       duplicate(VECTOR_ELT(GET_SLOT(b, Matrix_DimNamesSym), (tr) ? 0 : 1)));
185        return chm_sparse_to_SEXP(chc, 1, 0, 0, "", dn);
186    }
187    
188  SEXP Csparse_dense_prod(SEXP a, SEXP b)  SEXP Csparse_dense_prod(SEXP a, SEXP b)
189  {  {
190      cholmod_sparse *cha = as_cholmod_sparse(a);      cholmod_sparse *cha = as_cholmod_sparse(a);
# Line 160  Line 213 
213      return chm_dense_to_SEXP(chc, 1, 0);      return chm_dense_to_SEXP(chc, 1, 0);
214  }  }
215    
216    /* Computes   x'x  or  x x'  -- see Csparse_Csparse_crossprod above for  x'y and x y' */
217  SEXP Csparse_crossprod(SEXP x, SEXP trans, SEXP triplet)  SEXP Csparse_crossprod(SEXP x, SEXP trans, SEXP triplet)
218  {  {
219      int trip = asLogical(triplet),      int trip = asLogical(triplet),
# Line 175  Line 229 
229          chxt = cholmod_transpose(chx, chx->xtype, &c);          chxt = cholmod_transpose(chx, chx->xtype, &c);
230      chcp = cholmod_aat((!tr) ? chxt : chx, (int *) NULL, 0, chx->xtype, &c);      chcp = cholmod_aat((!tr) ? chxt : chx, (int *) NULL, 0, chx->xtype, &c);
231      if(!chcp)      if(!chcp)
232          error("Csparse_crossprod(): error return from cholmod_aat()");          error(_("Csparse_crossprod(): error return from cholmod_aat()"));
233      cholmod_band_inplace(0, chcp->ncol, chcp->xtype, chcp, &c);      cholmod_band_inplace(0, chcp->ncol, chcp->xtype, chcp, &c);
234      chcp->stype = 1;      chcp->stype = 1;
235      if (trip) {      if (trip) {
# Line 194  Line 248 
248      return chm_sparse_to_SEXP(chcp, 1, 0, 0, "", dn);      return chm_sparse_to_SEXP(chcp, 1, 0, 0, "", dn);
249  }  }
250    
251    SEXP Csparse_drop(SEXP x, SEXP tol)
252    {
253        cholmod_sparse *chx = as_cholmod_sparse(x),
254            *ans = cholmod_copy(chx, chx->stype, chx->xtype, &c);
255        double dtol = asReal(tol);
256        int Rkind = (chx->xtype == CHOLMOD_REAL) ? Real_kind(x) : 0;
257    
258        if(!cholmod_drop(dtol, ans, &c))
259            error(_("cholmod_drop() failed"));
260        Free(chx);
261        /* FIXME: currently drops dimnames */
262        return chm_sparse_to_SEXP(ans, 1, 0, Rkind, "", R_NilValue);
263    }
264    
265    
266  SEXP Csparse_horzcat(SEXP x, SEXP y)  SEXP Csparse_horzcat(SEXP x, SEXP y)
267  {  {
268      cholmod_sparse *chx = as_cholmod_sparse(x),      cholmod_sparse *chx = as_cholmod_sparse(x),

Legend:
Removed from v.1548  
changed lines
  Added in v.1659

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