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 1657, Wed Nov 1 16:29:53 2006 UTC revision 1751, Tue Jan 30 17:41:02 2007 UTC
# Line 7  Line 7 
7      /* NB: we do *NOT* check a potential 'x' slot here, at all */      /* 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, k, ncol, nrow, sorted,      int j, k, sorted,
11          *dims = INTEGER(GET_SLOT(x, Matrix_DimSym)),          *dims = INTEGER(GET_SLOT(x, Matrix_DimSym)),
12            nrow = dims[0],
13            ncol = dims[1],
14          *xp = INTEGER(pslot),          *xp = INTEGER(pslot),
15          *xi = INTEGER(islot);          *xi = INTEGER(islot);
16    
     nrow = dims[0];  
     ncol = dims[1];  
17      if (length(pslot) != dims[1] + 1)      if (length(pslot) != dims[1] + 1)
18          return mkString(_("slot p must have length = ncol(.) + 1"));          return mkString(_("slot p must have length = ncol(.) + 1"));
19      if (xp[0] != 0)      if (xp[0] != 0)
# Line 40  Line 40 
40      return ScalarLogical(1);      return ScalarLogical(1);
41  }  }
42    
43    /* Called from ../R/Csparse.R : */
44    /* Can only return [dln]geMatrix (no symm/triang);
45     * FIXME: replace by non-CHOLMOD code ! */
46  SEXP Csparse_to_dense(SEXP x)  SEXP Csparse_to_dense(SEXP x)
47  {  {
48      cholmod_sparse *chxs = as_cholmod_sparse(x);      cholmod_sparse *chxs = as_cholmod_sparse(x);
49        /* This loses the symmetry property, since cholmod_dense has none,
50         * BUT, much worse (FIXME!), it also transforms CHOLMOD_PATTERN ("n") matrices
51         * to numeric (CHOLMOD_REAL) ones : */
52      cholmod_dense *chxd = cholmod_sparse_to_dense(chxs, &c);      cholmod_dense *chxd = cholmod_sparse_to_dense(chxs, &c);
53        int Rkind = (chxs->xtype == CHOLMOD_PATTERN)? -1 : Real_kind(x);
54    
55      Free(chxs);      Free(chxs);
56      return chm_dense_to_SEXP(chxd, 1, Real_kind(x));      return chm_dense_to_SEXP(chxd, 1, Rkind, GET_SLOT(x, Matrix_DimNamesSym));
57  }  }
58    
59  SEXP Csparse_to_nz_pattern(SEXP x, SEXP tri)  SEXP Csparse_to_nz_pattern(SEXP x, SEXP tri)
# Line 58  Line 65 
65    
66      Free(chxs);      Free(chxs);
67      if (asLogical(tri)) {       /* triangular sparse matrices */      if (asLogical(tri)) {       /* triangular sparse matrices */
68          uploT = (strcmp(CHAR(asChar(GET_SLOT(x, Matrix_uploSym))), "U")) ?          uploT = (*uplo_P(x) == 'U') ? 1 : -1;
             -1 : 1;  
69          diag = CHAR(asChar(GET_SLOT(x, Matrix_diagSym)));          diag = CHAR(asChar(GET_SLOT(x, Matrix_diagSym)));
70      }      }
71      return chm_sparse_to_SEXP(chxcp, 1, uploT, 0, diag,      return chm_sparse_to_SEXP(chxcp, 1, uploT, 0, diag,
# Line 82  Line 88 
88      cholmod_triplet *chxt = cholmod_sparse_to_triplet(chxs, &c);      cholmod_triplet *chxt = cholmod_sparse_to_triplet(chxs, &c);
89      int uploT = 0;      int uploT = 0;
90      char *diag = "";      char *diag = "";
91      int Rkind = (chxs->xtype == CHOLMOD_REAL) ? Real_kind(x) : 0;      int Rkind = (chxs->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
92    
93      Free(chxs);      Free(chxs);
94      if (asLogical(tri)) {       /* triangular sparse matrices */      if (asLogical(tri)) {       /* triangular sparse matrices */
95          uploT = (*uplo_P(x) == 'U') ? -1 : 1;          uploT = (*uplo_P(x) == 'U') ? 1 : -1;
96          diag = diag_P(x);          diag = diag_P(x);
97      }      }
98      return chm_triplet_to_SEXP(chxt, 1, uploT, Rkind, diag,      return chm_triplet_to_SEXP(chxt, 1, uploT, Rkind, diag,
# Line 97  Line 103 
103  SEXP Csparse_symmetric_to_general(SEXP x)  SEXP Csparse_symmetric_to_general(SEXP x)
104  {  {
105      cholmod_sparse *chx = as_cholmod_sparse(x), *chgx;      cholmod_sparse *chx = as_cholmod_sparse(x), *chgx;
106      int Rkind = (chx->xtype == CHOLMOD_REAL) ? Real_kind(x) : 0;      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
107    
108      if (!(chx->stype))      if (!(chx->stype))
109          error(_("Nonsymmetric matrix in Csparse_symmetric_to_general"));          error(_("Nonsymmetric matrix in Csparse_symmetric_to_general"));
# Line 111  Line 117 
117  SEXP Csparse_general_to_symmetric(SEXP x, SEXP uplo)  SEXP Csparse_general_to_symmetric(SEXP x, SEXP uplo)
118  {  {
119      cholmod_sparse *chx = as_cholmod_sparse(x), *chgx;      cholmod_sparse *chx = as_cholmod_sparse(x), *chgx;
120      int uploT = (*CHAR(asChar(uplo)) == 'U') ? -1 : 1;      int uploT = (*CHAR(asChar(uplo)) == 'U') ? 1 : -1;
121      int Rkind = (chx->xtype == CHOLMOD_REAL) ? Real_kind(x) : 0;      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
122    
123      chgx = cholmod_copy(chx, /* stype: */ uploT, chx->xtype, &c);      chgx = cholmod_copy(chx, /* stype: */ uploT, chx->xtype, &c);
124      /* xtype: pattern, "real", complex or .. */      /* xtype: pattern, "real", complex or .. */
# Line 124  Line 130 
130  SEXP Csparse_transpose(SEXP x, SEXP tri)  SEXP Csparse_transpose(SEXP x, SEXP tri)
131  {  {
132      cholmod_sparse *chx = as_cholmod_sparse(x);      cholmod_sparse *chx = as_cholmod_sparse(x);
133      int Rkind = (chx->xtype == CHOLMOD_REAL) ? Real_kind(x) : 0;      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
134      cholmod_sparse *chxt = cholmod_transpose(chx, (int) chx->xtype, &c);      cholmod_sparse *chxt = cholmod_transpose(chx, (int) chx->xtype, &c);
135      SEXP dn = PROTECT(duplicate(GET_SLOT(x, Matrix_DimNamesSym))), tmp;      SEXP dn = PROTECT(duplicate(GET_SLOT(x, Matrix_DimNamesSym))), tmp;
136      int uploT = 0; char *diag = "";      int uploT = 0; char *diag = "";
# Line 134  Line 140 
140      SET_VECTOR_ELT(dn, 0, VECTOR_ELT(dn, 1));      SET_VECTOR_ELT(dn, 0, VECTOR_ELT(dn, 1));
141      SET_VECTOR_ELT(dn, 1, tmp);      SET_VECTOR_ELT(dn, 1, tmp);
142      UNPROTECT(1);      UNPROTECT(1);
143      if (asLogical(tri)) {       /* triangular sparse matrices */      if (asLogical(tri)) { /* triangular sparse matrices : SWAP 'uplo' */
144          uploT = (*uplo_P(x) == 'U') ? -1 : 1;          uploT = (*uplo_P(x) == 'U') ? -1 : 1;
145          diag = diag_P(x);          diag = diag_P(x);
146      }      }
# Line 143  Line 149 
149    
150  SEXP Csparse_Csparse_prod(SEXP a, SEXP b)  SEXP Csparse_Csparse_prod(SEXP a, SEXP b)
151  {  {
152      cholmod_sparse *cha = as_cholmod_sparse(a),      cholmod_sparse
153            *cha = as_cholmod_sparse(a),
154          *chb = as_cholmod_sparse(b);          *chb = as_cholmod_sparse(b);
155      cholmod_sparse *chc = cholmod_ssmult(cha, chb, 0, cha->xtype, 1, &c);      cholmod_sparse *chc = cholmod_ssmult(cha, chb, 0, cha->xtype, 1, &c);
156      SEXP dn = allocVector(VECSXP, 2);      SEXP dn = allocVector(VECSXP, 2);
# Line 156  Line 163 
163      return chm_sparse_to_SEXP(chc, 1, 0, 0, "", dn);      return chm_sparse_to_SEXP(chc, 1, 0, 0, "", dn);
164  }  }
165    
166  SEXP Csparse_Csparse_crossprod(SEXP a, SEXP b)  SEXP Csparse_Csparse_crossprod(SEXP a, SEXP b, SEXP trans)
167  {  {
168      cholmod_sparse *cha = as_cholmod_sparse(a),      int tr = asLogical(trans);
169        cholmod_sparse
170            *cha = as_cholmod_sparse(a),
171          *chb = as_cholmod_sparse(b);          *chb = as_cholmod_sparse(b);
172      cholmod_sparse *chta = cholmod_transpose(cha, 1, &c);      cholmod_sparse *chTr, *chc;
     cholmod_sparse *chc = cholmod_ssmult(chta, chb, 0, cha->xtype, 1, &c);  
173      SEXP dn = allocVector(VECSXP, 2);      SEXP dn = allocVector(VECSXP, 2);
174    
175      Free(cha); Free(chb); cholmod_free_sparse(&chta, &c);  /*     cholmod_sparse *chTr = cholmod_transpose(cha, 1, &c); */
176    /*     cholmod_sparse *chc = cholmod_ssmult(chTr, chb, 0, cha->xtype, 1, &c); */
177    
178        if (tr)
179            chTr = cholmod_transpose(chb, chb->xtype, &c);
180        else
181            chTr = cholmod_transpose(cha, cha->xtype, &c);
182        chc = cholmod_ssmult((tr) ? cha : chTr, (tr) ? chTr : chb,
183                             0, cha->xtype, 1, &c);
184    
185        Free(cha); Free(chb); cholmod_free_sparse(&chTr, &c);
186    
187      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */
188                     duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), 1)));                     duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), (tr) ? 0 : 1)));
189      SET_VECTOR_ELT(dn, 1,      SET_VECTOR_ELT(dn, 1,
190                     duplicate(VECTOR_ELT(GET_SLOT(b, Matrix_DimNamesSym), 1)));                     duplicate(VECTOR_ELT(GET_SLOT(b, Matrix_DimNamesSym), (tr) ? 0 : 1)));
191      return chm_sparse_to_SEXP(chc, 1, 0, 0, "", dn);      return chm_sparse_to_SEXP(chc, 1, 0, 0, "", dn);
192  }  }
193    
194  SEXP Csparse_dense_prod(SEXP a, SEXP b)  SEXP Csparse_dense_prod(SEXP a, SEXP b)
195  {  {
196      cholmod_sparse *cha = as_cholmod_sparse(a);      cholmod_sparse *cha = as_cholmod_sparse(a);
197      cholmod_dense *chb = as_cholmod_dense(PROTECT(mMatrix_as_dgeMatrix(b)));      SEXP b_M = PROTECT(mMatrix_as_dgeMatrix(b));
198        cholmod_dense *chb = as_cholmod_dense(b_M);
199      cholmod_dense *chc =      cholmod_dense *chc =
200          cholmod_allocate_dense(cha->nrow, chb->ncol, cha->nrow, chb->xtype, &c);          cholmod_allocate_dense(cha->nrow, chb->ncol, cha->nrow, chb->xtype, &c);
201        SEXP dn = allocVector(VECSXP, 2);
202      double alpha[] = {1,0}, beta[] = {0,0};      double alpha[] = {1,0}, beta[] = {0,0};
203    
204      cholmod_sdmult(cha, 0, alpha, beta, chb, chc, &c);      cholmod_sdmult(cha, 0, alpha, beta, chb, chc, &c);
205      Free(cha); Free(chb);      Free(cha); Free(chb);
206      UNPROTECT(1);      UNPROTECT(1);
207      return chm_dense_to_SEXP(chc, 1, 0);      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */
208                       duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), 0)));
209        SET_VECTOR_ELT(dn, 1,
210                       duplicate(VECTOR_ELT(GET_SLOT(b_M, Matrix_DimNamesSym), 1)));
211        return chm_dense_to_SEXP(chc, 1, 0, dn);
212  }  }
213    
214  SEXP Csparse_dense_crossprod(SEXP a, SEXP b)  SEXP Csparse_dense_crossprod(SEXP a, SEXP b)
215  {  {
216      cholmod_sparse *cha = as_cholmod_sparse(a);      cholmod_sparse *cha = as_cholmod_sparse(a);
217      cholmod_dense *chb = as_cholmod_dense(PROTECT(mMatrix_as_dgeMatrix(b)));      SEXP b_M = PROTECT(mMatrix_as_dgeMatrix(b));
218        cholmod_dense *chb = as_cholmod_dense(b_M);
219      cholmod_dense *chc =      cholmod_dense *chc =
220          cholmod_allocate_dense(cha->ncol, chb->ncol, cha->ncol, chb->xtype, &c);          cholmod_allocate_dense(cha->ncol, chb->ncol, cha->ncol, chb->xtype, &c);
221        SEXP dn = allocVector(VECSXP, 2);
222      double alpha[] = {1,0}, beta[] = {0,0};      double alpha[] = {1,0}, beta[] = {0,0};
223    
224      cholmod_sdmult(cha, 1, alpha, beta, chb, chc, &c);      cholmod_sdmult(cha, 1, alpha, beta, chb, chc, &c);
225      Free(cha); Free(chb);      Free(cha); Free(chb);
226      UNPROTECT(1);      UNPROTECT(1);
227      return chm_dense_to_SEXP(chc, 1, 0);      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */
228                       duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), 1)));
229        SET_VECTOR_ELT(dn, 1,
230                       duplicate(VECTOR_ELT(GET_SLOT(b_M, Matrix_DimNamesSym), 1)));
231        return chm_dense_to_SEXP(chc, 1, 0, dn);
232  }  }
233    
234    /* Computes   x'x  or  x x'  -- see Csparse_Csparse_crossprod above for  x'y and x y' */
235  SEXP Csparse_crossprod(SEXP x, SEXP trans, SEXP triplet)  SEXP Csparse_crossprod(SEXP x, SEXP trans, SEXP triplet)
236  {  {
237      int trip = asLogical(triplet),      int trip = asLogical(triplet),
# Line 228  Line 260 
260                                  /* create dimnames */                                  /* create dimnames */
261      SET_VECTOR_ELT(dn, 0,      SET_VECTOR_ELT(dn, 0,
262                     duplicate(VECTOR_ELT(GET_SLOT(x, Matrix_DimNamesSym),                     duplicate(VECTOR_ELT(GET_SLOT(x, Matrix_DimNamesSym),
263                                          (tr) ? 1 : 0)));                                          (tr) ? 0 : 1)));
264      SET_VECTOR_ELT(dn, 1, duplicate(VECTOR_ELT(dn, 0)));      SET_VECTOR_ELT(dn, 1, duplicate(VECTOR_ELT(dn, 0)));
265      UNPROTECT(1);      UNPROTECT(1);
266      return chm_sparse_to_SEXP(chcp, 1, 0, 0, "", dn);      return chm_sparse_to_SEXP(chcp, 1, 0, 0, "", dn);
# Line 239  Line 271 
271      cholmod_sparse *chx = as_cholmod_sparse(x),      cholmod_sparse *chx = as_cholmod_sparse(x),
272          *ans = cholmod_copy(chx, chx->stype, chx->xtype, &c);          *ans = cholmod_copy(chx, chx->stype, chx->xtype, &c);
273      double dtol = asReal(tol);      double dtol = asReal(tol);
274      int Rkind = (chx->xtype == CHOLMOD_REAL) ? Real_kind(x) : 0;      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
275    
276      if(!cholmod_drop(dtol, ans, &c))      if(!cholmod_drop(dtol, ans, &c))
277          error(_("cholmod_drop() failed"));          error(_("cholmod_drop() failed"));
278      Free(chx);      Free(chx);
279      /* FIXME: currently drops dimnames */      return chm_sparse_to_SEXP(ans, 1, 0, Rkind, "",
280      return chm_sparse_to_SEXP(ans, 1, 0, Rkind, "", R_NilValue);                                GET_SLOT(x, Matrix_DimNamesSym));
281  }  }
282    
283    
# Line 276  Line 308 
308  SEXP Csparse_band(SEXP x, SEXP k1, SEXP k2)  SEXP Csparse_band(SEXP x, SEXP k1, SEXP k2)
309  {  {
310      cholmod_sparse *chx = as_cholmod_sparse(x), *ans;      cholmod_sparse *chx = as_cholmod_sparse(x), *ans;
311      int Rkind = (chx->xtype == CHOLMOD_REAL) ? Real_kind(x) : 0;      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
312    
313      ans = cholmod_band(chx, asInteger(k1), asInteger(k2), chx->xtype, &c);      ans = cholmod_band(chx, asInteger(k1), asInteger(k2), chx->xtype, &c);
314      Free(chx);      Free(chx);
315      return chm_sparse_to_SEXP(ans, 1, 0, Rkind, "", R_NilValue);      return chm_sparse_to_SEXP(ans, 1, 0, Rkind, "",
316                                  GET_SLOT(x, Matrix_DimNamesSym));
317  }  }
318    
319  SEXP Csparse_diagU2N(SEXP x)  SEXP Csparse_diagU2N(SEXP x)
320  {  {
321        if (*diag_P(x) != 'U') {/* "trivially fast" when there's no 'diag' slot at all */
322            return (x);
323        }
324        else {
325      cholmod_sparse *chx = as_cholmod_sparse(x);      cholmod_sparse *chx = as_cholmod_sparse(x);
326      cholmod_sparse *eye = cholmod_speye(chx->nrow, chx->ncol, chx->xtype, &c);      cholmod_sparse *eye = cholmod_speye(chx->nrow, chx->ncol, chx->xtype, &c);
327      double one[] = {1, 0};      double one[] = {1, 0};
328      cholmod_sparse *ans = cholmod_add(chx, eye, one, one, TRUE, TRUE, &c);      cholmod_sparse *ans = cholmod_add(chx, eye, one, one, TRUE, TRUE, &c);
329      int uploT = (strcmp(CHAR(asChar(GET_SLOT(x, Matrix_uploSym))), "U")) ?          int uploT = (*uplo_P(x) == 'U') ? 1 : -1;
330          -1 : 1;          int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
     int Rkind = (chx->xtype == CHOLMOD_REAL) ? Real_kind(x) : 0;  
331    
332      Free(chx); cholmod_free_sparse(&eye, &c);      Free(chx); cholmod_free_sparse(&eye, &c);
333      return chm_sparse_to_SEXP(ans, 1, uploT, Rkind, "N",      return chm_sparse_to_SEXP(ans, 1, uploT, Rkind, "N",
334                                duplicate(GET_SLOT(x, Matrix_DimNamesSym)));                                    GET_SLOT(x, Matrix_DimNamesSym));
335        }
336  }  }
337    
338  SEXP Csparse_submatrix(SEXP x, SEXP i, SEXP j)  SEXP Csparse_submatrix(SEXP x, SEXP i, SEXP j)
# Line 303  Line 340 
340      cholmod_sparse *chx = as_cholmod_sparse(x);      cholmod_sparse *chx = as_cholmod_sparse(x);
341      int rsize = (isNull(i)) ? -1 : LENGTH(i),      int rsize = (isNull(i)) ? -1 : LENGTH(i),
342          csize = (isNull(j)) ? -1 : LENGTH(j);          csize = (isNull(j)) ? -1 : LENGTH(j);
343      int Rkind = (chx->xtype == CHOLMOD_REAL) ? Real_kind(x) : 0;      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
344    
345      if (rsize >= 0 && !isInteger(i))      if (rsize >= 0 && !isInteger(i))
346          error(_("Index i must be NULL or integer"));          error(_("Index i must be NULL or integer"));
347      if (csize >= 0 && !isInteger(j))      if (csize >= 0 && !isInteger(j))
348          error(_("Index j must be NULL or integer"));          error(_("Index j must be NULL or integer"));
349    
350      return chm_sparse_to_SEXP(cholmod_submatrix(chx, INTEGER(i), rsize,      return chm_sparse_to_SEXP(cholmod_submatrix(chx, INTEGER(i), rsize,
351                                                  INTEGER(j), csize,                                                  INTEGER(j), csize,
352                                                  TRUE, TRUE, &c),                                                  TRUE, TRUE, &c),
353                                1, 0, Rkind, "", R_NilValue);                                1, 0, Rkind, "",
354                                  /* FIXME: drops dimnames */ R_NilValue);
355  }  }

Legend:
Removed from v.1657  
changed lines
  Added in v.1751

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