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 1708, Fri Dec 22 19:53:37 2006 UTC revision 1867, Mon Jun 4 17:13:02 2007 UTC
# 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));
                              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 55  Line 61 
61      cholmod_sparse *chxs = as_cholmod_sparse(x);      cholmod_sparse *chxs = as_cholmod_sparse(x);
62      cholmod_sparse      cholmod_sparse
63          *chxcp = cholmod_copy(chxs, chxs->stype, CHOLMOD_PATTERN, &c);          *chxcp = cholmod_copy(chxs, chxs->stype, CHOLMOD_PATTERN, &c);
64      int uploT = 0; char *diag = "";      int tr = asLogical(tri);
65    
66      Free(chxs);      Free(chxs);
67      if (asLogical(tri)) {       /* triangular sparse matrices */      return chm_sparse_to_SEXP(chxcp, 1,
68          uploT = (strcmp(CHAR(asChar(GET_SLOT(x, Matrix_uploSym))), "U")) ?                                tr ? ((*uplo_P(x) == 'U') ? 1 : -1) : 0,
69              -1 : 1;                                0, tr ? diag_P(x) : "",
         diag = CHAR(asChar(GET_SLOT(x, Matrix_diagSym)));  
     }  
     return chm_sparse_to_SEXP(chxcp, 1, uploT, 0, diag,  
70                                GET_SLOT(x, Matrix_DimNamesSym));                                GET_SLOT(x, Matrix_DimNamesSym));
71  }  }
72    
# Line 81  Line 84 
84  {  {
85      cholmod_sparse *chxs = as_cholmod_sparse(x);      cholmod_sparse *chxs = as_cholmod_sparse(x);
86      cholmod_triplet *chxt = cholmod_sparse_to_triplet(chxs, &c);      cholmod_triplet *chxt = cholmod_sparse_to_triplet(chxs, &c);
87      int uploT = 0;      int tr = asLogical(tri);
88      char *diag = "";      int Rkind = (chxs->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
     int Rkind = (chxs->xtype == CHOLMOD_REAL) ? Real_kind(x) : 0;  
89    
90      Free(chxs);      Free(chxs);
91      if (asLogical(tri)) {       /* triangular sparse matrices */      return chm_triplet_to_SEXP(chxt, 1,
92          uploT = (*uplo_P(x) == 'U') ? -1 : 1;                                 tr ? ((*uplo_P(x) == 'U') ? 1 : -1) : 0,
93          diag = diag_P(x);                                 Rkind, tr ? diag_P(x) : "",
     }  
     return chm_triplet_to_SEXP(chxt, 1, uploT, Rkind, diag,  
94                                 GET_SLOT(x, Matrix_DimNamesSym));                                 GET_SLOT(x, Matrix_DimNamesSym));
95  }  }
96    
# Line 98  Line 98 
98  SEXP Csparse_symmetric_to_general(SEXP x)  SEXP Csparse_symmetric_to_general(SEXP x)
99  {  {
100      cholmod_sparse *chx = as_cholmod_sparse(x), *chgx;      cholmod_sparse *chx = as_cholmod_sparse(x), *chgx;
101      int Rkind = (chx->xtype == CHOLMOD_REAL) ? Real_kind(x) : 0;      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
102    
103      if (!(chx->stype))      if (!(chx->stype))
104          error(_("Nonsymmetric matrix in Csparse_symmetric_to_general"));          error(_("Nonsymmetric matrix in Csparse_symmetric_to_general"));
# Line 112  Line 112 
112  SEXP Csparse_general_to_symmetric(SEXP x, SEXP uplo)  SEXP Csparse_general_to_symmetric(SEXP x, SEXP uplo)
113  {  {
114      cholmod_sparse *chx = as_cholmod_sparse(x), *chgx;      cholmod_sparse *chx = as_cholmod_sparse(x), *chgx;
115      int uploT = (*CHAR(asChar(uplo)) == 'U') ? -1 : 1;      int uploT = (*CHAR(asChar(uplo)) == 'U') ? 1 : -1;
116      int Rkind = (chx->xtype == CHOLMOD_REAL) ? Real_kind(x) : 0;      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
117    
118      chgx = cholmod_copy(chx, /* stype: */ uploT, chx->xtype, &c);      chgx = cholmod_copy(chx, /* stype: */ uploT, chx->xtype, &c);
119      /* xtype: pattern, "real", complex or .. */      /* xtype: pattern, "real", complex or .. */
# Line 125  Line 125 
125  SEXP Csparse_transpose(SEXP x, SEXP tri)  SEXP Csparse_transpose(SEXP x, SEXP tri)
126  {  {
127      cholmod_sparse *chx = as_cholmod_sparse(x);      cholmod_sparse *chx = as_cholmod_sparse(x);
128      int Rkind = (chx->xtype == CHOLMOD_REAL) ? Real_kind(x) : 0;      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
129      cholmod_sparse *chxt = cholmod_transpose(chx, (int) chx->xtype, &c);      cholmod_sparse *chxt = cholmod_transpose(chx, (int) chx->xtype, &c);
130      SEXP dn = PROTECT(duplicate(GET_SLOT(x, Matrix_DimNamesSym))), tmp;      SEXP dn = PROTECT(duplicate(GET_SLOT(x, Matrix_DimNamesSym))), tmp;
131      int uploT = 0; char *diag = "";      int tr = asLogical(tri);
132    
133      Free(chx);      Free(chx);
134      tmp = VECTOR_ELT(dn, 0);    /* swap the dimnames */      tmp = VECTOR_ELT(dn, 0);    /* swap the dimnames */
135      SET_VECTOR_ELT(dn, 0, VECTOR_ELT(dn, 1));      SET_VECTOR_ELT(dn, 0, VECTOR_ELT(dn, 1));
136      SET_VECTOR_ELT(dn, 1, tmp);      SET_VECTOR_ELT(dn, 1, tmp);
137      UNPROTECT(1);      UNPROTECT(1);
138      if (asLogical(tri)) {       /* triangular sparse matrices */      return chm_sparse_to_SEXP(chxt, 1, /* SWAP 'uplo' for triangular */
139          uploT = (*uplo_P(x) == 'U') ? -1 : 1;                                tr ? ((*uplo_P(x) == 'U') ? -1 : 1) : 0,
140          diag = diag_P(x);                                Rkind, tr ? diag_P(x) : "", dn);
     }  
     return chm_sparse_to_SEXP(chxt, 1, uploT, Rkind, diag, dn);  
141  }  }
142    
143  SEXP Csparse_Csparse_prod(SEXP a, SEXP b)  SEXP Csparse_Csparse_prod(SEXP a, SEXP b)
# Line 266  Line 264 
264      cholmod_sparse *chx = as_cholmod_sparse(x),      cholmod_sparse *chx = as_cholmod_sparse(x),
265          *ans = cholmod_copy(chx, chx->stype, chx->xtype, &c);          *ans = cholmod_copy(chx, chx->stype, chx->xtype, &c);
266      double dtol = asReal(tol);      double dtol = asReal(tol);
267      int Rkind = (chx->xtype == CHOLMOD_REAL) ? Real_kind(x) : 0;      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
268    
269      if(!cholmod_drop(dtol, ans, &c))      if(!cholmod_drop(dtol, ans, &c))
270          error(_("cholmod_drop() failed"));          error(_("cholmod_drop() failed"));
271      Free(chx);      Free(chx);
272      /* FIXME: currently drops dimnames */      return chm_sparse_to_SEXP(ans, 1, 0, Rkind, "",
273      return chm_sparse_to_SEXP(ans, 1, 0, Rkind, "", R_NilValue);                                GET_SLOT(x, Matrix_DimNamesSym));
274  }  }
275    
276    
# Line 303  Line 301 
301  SEXP Csparse_band(SEXP x, SEXP k1, SEXP k2)  SEXP Csparse_band(SEXP x, SEXP k1, SEXP k2)
302  {  {
303      cholmod_sparse *chx = as_cholmod_sparse(x), *ans;      cholmod_sparse *chx = as_cholmod_sparse(x), *ans;
304      int Rkind = (chx->xtype == CHOLMOD_REAL) ? Real_kind(x) : 0;      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
305    
306      ans = cholmod_band(chx, asInteger(k1), asInteger(k2), chx->xtype, &c);      ans = cholmod_band(chx, asInteger(k1), asInteger(k2), chx->xtype, &c);
307      Free(chx);      Free(chx);
308      return chm_sparse_to_SEXP(ans, 1, 0, Rkind, "", R_NilValue);      return chm_sparse_to_SEXP(ans, 1, 0, Rkind, "",
309                                  GET_SLOT(x, Matrix_DimNamesSym));
310  }  }
311    
312  SEXP Csparse_diagU2N(SEXP x)  SEXP Csparse_diagU2N(SEXP x)
# Line 320  Line 319 
319          cholmod_sparse *eye = cholmod_speye(chx->nrow, chx->ncol, chx->xtype, &c);          cholmod_sparse *eye = cholmod_speye(chx->nrow, chx->ncol, chx->xtype, &c);
320          double one[] = {1, 0};          double one[] = {1, 0};
321          cholmod_sparse *ans = cholmod_add(chx, eye, one, one, TRUE, TRUE, &c);          cholmod_sparse *ans = cholmod_add(chx, eye, one, one, TRUE, TRUE, &c);
322          int uploT = (strcmp(CHAR(asChar(GET_SLOT(x, Matrix_uploSym))), "U")) ?          int uploT = (*uplo_P(x) == 'U') ? 1 : -1;
323              -1 : 1;          int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
         int Rkind = (chx->xtype == CHOLMOD_REAL) ? Real_kind(x) : 0;  
324    
325          Free(chx); cholmod_free_sparse(&eye, &c);          Free(chx); cholmod_free_sparse(&eye, &c);
326          return chm_sparse_to_SEXP(ans, 1, uploT, Rkind, "N",          return chm_sparse_to_SEXP(ans, 1, uploT, Rkind, "N",
327                                    duplicate(GET_SLOT(x, Matrix_DimNamesSym)));                                    GET_SLOT(x, Matrix_DimNamesSym));
328      }      }
329  }  }
330    
# Line 335  Line 333 
333      cholmod_sparse *chx = as_cholmod_sparse(x);      cholmod_sparse *chx = as_cholmod_sparse(x);
334      int rsize = (isNull(i)) ? -1 : LENGTH(i),      int rsize = (isNull(i)) ? -1 : LENGTH(i),
335          csize = (isNull(j)) ? -1 : LENGTH(j);          csize = (isNull(j)) ? -1 : LENGTH(j);
336      int Rkind = (chx->xtype == CHOLMOD_REAL) ? Real_kind(x) : 0;      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
337    
338      if (rsize >= 0 && !isInteger(i))      if (rsize >= 0 && !isInteger(i))
339          error(_("Index i must be NULL or integer"));          error(_("Index i must be NULL or integer"));
340      if (csize >= 0 && !isInteger(j))      if (csize >= 0 && !isInteger(j))
341          error(_("Index j must be NULL or integer"));          error(_("Index j must be NULL or integer"));
342    
343      return chm_sparse_to_SEXP(cholmod_submatrix(chx, INTEGER(i), rsize,      return chm_sparse_to_SEXP(cholmod_submatrix(chx, INTEGER(i), rsize,
344                                                  INTEGER(j), csize,                                                  INTEGER(j), csize,
345                                                  TRUE, TRUE, &c),                                                  TRUE, TRUE, &c),
346                                1, 0, Rkind, "", R_NilValue);                                1, 0, Rkind, "",
347                                  /* FIXME: drops dimnames */ R_NilValue);
348  }  }

Legend:
Removed from v.1708  
changed lines
  Added in v.1867

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