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 957, Sat Oct 1 20:08:04 2005 UTC revision 1960, Fri Jul 6 16:54:43 2007 UTC
# Line 1  Line 1 
1                          /* Sparse matrices in compress column-oriented form */                          /* Sparse matrices in compressed column-oriented form */
2  #include "Csparse.h"  #include "Csparse.h"
 #ifdef USE_CHOLMOD  
3  #include "chm_common.h"  #include "chm_common.h"
 #endif  /* USE_CHOLMOD */  
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,      Rboolean sorted, strictly;
11        int j, k,
12          *dims = INTEGER(GET_SLOT(x, Matrix_DimSym)),          *dims = INTEGER(GET_SLOT(x, Matrix_DimSym)),
13          nrow, *xp = INTEGER(pslot),          nrow = dims[0],
14            ncol = dims[1],
15            *xp = INTEGER(pslot),
16          *xi = INTEGER(islot);          *xi = INTEGER(islot);
17    
18      nrow = dims[0];      if (length(pslot) != dims[1] + 1)
19      if (length(pslot) <= 0)          return mkString(_("slot p must have length = ncol(.) + 1"));
         return mkString(_("slot p must have length > 0"));  
20      if (xp[0] != 0)      if (xp[0] != 0)
21          return mkString(_("first element of slot p must be zero"));          return mkString(_("first element of slot p must be zero"));
22      if (length(islot) != xp[ncol])      if (length(islot) < xp[ncol]) /* allow larger slots from over-allocation!*/
23          return mkString(_("last element of slot p must match length of slots i and x"));          return
24                mkString(_("last element of slot p must match length of slots i and x"));
25        for (j = 0; j < length(islot); j++) {
26            if (xi[j] < 0 || xi[j] >= nrow)
27                return mkString(_("all row indices must be between 0 and nrow-1"));
28        }
29        sorted = TRUE; strictly = TRUE;
30      for (j = 0; j < ncol; j++) {      for (j = 0; j < ncol; j++) {
31          if (xp[j] > xp[j+1])          if (xp[j] > xp[j+1])
32              return mkString(_("slot p must be non-decreasing"));              return mkString(_("slot p must be non-decreasing"));
33            if(sorted)
34                for (k = xp[j] + 1; k < xp[j + 1]; k++) {
35                    if (xi[k] < xi[k - 1])
36                        sorted = FALSE;
37                    else if (xi[k] == xi[k - 1])
38                        strictly = FALSE;
39      }      }
40      for (j = 0; j < length(islot); j++) {      }
41          if (xi[j] < 0 || xi[j] >= nrow)      if (!sorted) {
42              return mkString(_("all row indices must be between 0 and nrow-1"));          CHM_SP chx = AS_CHM_SP(x);
43            R_CheckStack();
44    
45            cholmod_sort(chx, &c);
46            /* Now re-check that row indices are *strictly* increasing
47             * (and not just increasing) within each column : */
48            for (j = 0; j < ncol; j++) {
49                for (k = xp[j] + 1; k < xp[j + 1]; k++)
50                    if (xi[k] == xi[k - 1])
51                        return mkString(_("slot i is not *strictly* increasing inside a column (even after cholmod_sort)"));
52            }
53    
54        } else if(!strictly) {  /* sorted, but not strictly */
55            return mkString(_("slot i is not *strictly* increasing inside a column"));
56      }      }
57      return ScalarLogical(1);      return ScalarLogical(1);
58  }  }
59    
60  SEXP Csparse_to_Tsparse(SEXP x)  /* Called from ../R/Csparse.R : */
61    /* Can only return [dln]geMatrix (no symm/triang);
62     * FIXME: replace by non-CHOLMOD code ! */
63    SEXP Csparse_to_dense(SEXP x)
64    {
65        CHM_SP chxs = AS_CHM_SP(x);
66        /* This loses the symmetry property, since cholmod_dense has none,
67         * BUT, much worse (FIXME!), it also transforms CHOLMOD_PATTERN ("n") matrices
68         * to numeric (CHOLMOD_REAL) ones : */
69        CHM_DN chxd = cholmod_sparse_to_dense(chxs, &c);
70        int Rkind = (chxs->xtype == CHOLMOD_PATTERN)? -1 : Real_kind(x);
71        R_CheckStack();
72    
73        return chm_dense_to_SEXP(chxd, 1, Rkind, GET_SLOT(x, Matrix_DimNamesSym));
74    }
75    
76    SEXP Csparse_to_nz_pattern(SEXP x, SEXP tri)
77    {
78        CHM_SP chxs = AS_CHM_SP(x);
79        CHM_SP chxcp = cholmod_copy(chxs, chxs->stype, CHOLMOD_PATTERN, &c);
80        int tr = asLogical(tri);
81        R_CheckStack();
82    
83        return chm_sparse_to_SEXP(chxcp, 1/*do_free*/,
84                                  tr ? ((*uplo_P(x) == 'U') ? 1 : -1) : 0,
85                                  0, tr ? diag_P(x) : "",
86                                  GET_SLOT(x, Matrix_DimNamesSym));
87    }
88    
89    SEXP Csparse_to_matrix(SEXP x)
90    {
91        return chm_dense_to_matrix(cholmod_sparse_to_dense(AS_CHM_SP(x), &c),
92                                   1 /*do_free*/, GET_SLOT(x, Matrix_DimNamesSym));
93    }
94    
95    SEXP Csparse_to_Tsparse(SEXP x, SEXP tri)
96  {  {
97  #ifdef USE_CHOLMOD      CHM_SP chxs = AS_CHM_SP(x);
98      cholmod_sparse *chxs = as_cholmod_sparse(x);      CHM_TR chxt = cholmod_sparse_to_triplet(chxs, &c);
99      cholmod_triplet *chxt = cholmod_sparse_to_triplet(chxs, &c);      int tr = asLogical(tri);
100        int Rkind = (chxs->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
101      Free(chxs);      R_CheckStack();
102      return chm_triplet_to_SEXP(chxt, 1);  
103  #else      return chm_triplet_to_SEXP(chxt, 1,
104      error("General conversion requires CHOLMOD");                                 tr ? ((*uplo_P(x) == 'U') ? 1 : -1) : 0,
105      return R_NilValue;          /* -Wall */                                 Rkind, tr ? diag_P(x) : "",
106  #endif  /* USE_CHOLMOD */                                 GET_SLOT(x, Matrix_DimNamesSym));
 }  
   
 SEXP Csparse_transpose(SEXP x)  
 {  
 #ifdef USE_CHOLMOD  
     cholmod_sparse *chx = as_cholmod_sparse(x);  
     cholmod_sparse *chxt = cholmod_transpose(chx, (int) chx->xtype, &c);  
   
     Free(chx);  
     return chm_sparse_to_SEXP(chxt, 1);  
 #else  
     error("General conversion requires CHOLMOD");  
     return R_NilValue;          /* -Wall */  
 #endif  /* USE_CHOLMOD */  
107  }  }
108    
109    /* this used to be called  sCMatrix_to_gCMatrix(..)   [in ./dsCMatrix.c ]: */
110    SEXP Csparse_symmetric_to_general(SEXP x)
111    {
112        CHM_SP chx = AS_CHM_SP(x), chgx;
113        int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
114        R_CheckStack();
115    
116        if (!(chx->stype))
117            error(_("Nonsymmetric matrix in Csparse_symmetric_to_general"));
118        chgx = cholmod_copy(chx, /* stype: */ 0, chx->xtype, &c);
119        /* xtype: pattern, "real", complex or .. */
120        return chm_sparse_to_SEXP(chgx, 1, 0, Rkind, "",
121                                  GET_SLOT(x, Matrix_DimNamesSym));
122    }
123    
124    SEXP Csparse_general_to_symmetric(SEXP x, SEXP uplo)
125    {
126        CHM_SP chx = AS_CHM_SP(x), chgx;
127        int uploT = (*CHAR(asChar(uplo)) == 'U') ? 1 : -1;
128        int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
129        R_CheckStack();
130    
131        chgx = cholmod_copy(chx, /* stype: */ uploT, chx->xtype, &c);
132        /* xtype: pattern, "real", complex or .. */
133        return chm_sparse_to_SEXP(chgx, 1, 0, Rkind, "",
134                                  GET_SLOT(x, Matrix_DimNamesSym));
135    }
136    
137    SEXP Csparse_transpose(SEXP x, SEXP tri)
138    {
139        /* TODO: lgCMatrix & igC* currently go via double prec. cholmod -
140         *       since cholmod (& cs) lacks sparse 'int' matrices */
141        CHM_SP chx = AS_CHM_SP(x);
142        int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
143        CHM_SP chxt = cholmod_transpose(chx, chx->xtype, &c);
144        SEXP dn = PROTECT(duplicate(GET_SLOT(x, Matrix_DimNamesSym))), tmp;
145        int tr = asLogical(tri);
146        R_CheckStack();
147    
148        tmp = VECTOR_ELT(dn, 0);    /* swap the dimnames */
149        SET_VECTOR_ELT(dn, 0, VECTOR_ELT(dn, 1));
150        SET_VECTOR_ELT(dn, 1, tmp);
151        UNPROTECT(1);
152        return chm_sparse_to_SEXP(chxt, 1, /* SWAP 'uplo' for triangular */
153                                  tr ? ((*uplo_P(x) == 'U') ? -1 : 1) : 0,
154                                  Rkind, tr ? diag_P(x) : "", dn);
155    }
156    
157  SEXP Csparse_Csparse_prod(SEXP a, SEXP b)  SEXP Csparse_Csparse_prod(SEXP a, SEXP b)
158  {  {
159  #ifdef USE_CHOLMOD      CHM_SP cha = AS_CHM_SP(a), chb = AS_CHM_SP(b);
160      cholmod_sparse *cha = as_cholmod_sparse(a), *chb = as_cholmod_sparse(b);      CHM_SP chc = cholmod_ssmult(cha, chb, 0, cha->xtype, 1, &c);
161      cholmod_sparse *chc = cholmod_ssmult(cha, chb, 0, (int) cha->xtype, 1, &c);      SEXP dn = allocVector(VECSXP, 2);
162        R_CheckStack();
163      Free(cha); Free(chb);  
164      return chm_sparse_to_SEXP(chc, 1);      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */
165  #else                     duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), 0)));
166      error("General multiplication requires CHOLMOD");      SET_VECTOR_ELT(dn, 1,
167      return R_NilValue;          /* -Wall */                     duplicate(VECTOR_ELT(GET_SLOT(b, Matrix_DimNamesSym), 1)));
168  #endif  /* USE_CHOLMOD */      return chm_sparse_to_SEXP(chc, 1, 0, 0, "", dn);
169    }
170    
171    SEXP Csparse_Csparse_crossprod(SEXP a, SEXP b, SEXP trans)
172    {
173        int tr = asLogical(trans);
174        CHM_SP cha = AS_CHM_SP(a), chb = AS_CHM_SP(b), chTr, chc;
175        SEXP dn = allocVector(VECSXP, 2);
176        R_CheckStack();
177    
178        chTr = cholmod_transpose((tr) ? chb : cha, chb->xtype, &c);
179        chc = cholmod_ssmult((tr) ? cha : chTr, (tr) ? chTr : chb,
180                             0, cha->xtype, 1, &c);
181        cholmod_free_sparse(&chTr, &c);
182    
183        SET_VECTOR_ELT(dn, 0,       /* establish dimnames */
184                       duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), (tr) ? 0 : 1)));
185        SET_VECTOR_ELT(dn, 1,
186                       duplicate(VECTOR_ELT(GET_SLOT(b, Matrix_DimNamesSym), (tr) ? 0 : 1)));
187        return chm_sparse_to_SEXP(chc, 1, 0, 0, "", dn);
188  }  }
189    
190  SEXP Csparse_dense_prod(SEXP a, SEXP b)  SEXP Csparse_dense_prod(SEXP a, SEXP b)
191  {  {
192  #ifdef USE_CHOLMOD      CHM_SP cha = AS_CHM_SP(a);
193      cholmod_sparse *cha = as_cholmod_sparse(a);      SEXP b_M = PROTECT(mMatrix_as_dgeMatrix(b));
194      cholmod_dense *chb = as_cholmod_dense(b);      CHM_DN chb = AS_CHM_DN(b_M);
195      cholmod_dense *chc = cholmod_allocate_dense(cha->nrow, chb->ncol,      CHM_DN chc = cholmod_allocate_dense(cha->nrow, chb->ncol, cha->nrow,
196                                                  cha->nrow, chb->xtype, &c);                                          chb->xtype, &c);
197      double alpha = 1, beta = 0;      SEXP dn = PROTECT(allocVector(VECSXP, 2));
198        double one[] = {1,0}, zero[] = {0,0};
199      cholmod_sdmult(cha, 0, &alpha, &beta, chb, chc, &c);      R_CheckStack();
200      Free(cha); Free(chb);  
201      return chm_dense_to_SEXP(chc, 1);      cholmod_sdmult(cha, 0, one, zero, chb, chc, &c);
202  #else      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */
203      error("General multiplication requires CHOLMOD");                     duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), 0)));
204      return R_NilValue;          /* -Wall */      SET_VECTOR_ELT(dn, 1,
205  #endif  /* USE_CHOLMOD */                     duplicate(VECTOR_ELT(GET_SLOT(b_M, Matrix_DimNamesSym), 1)));
206        UNPROTECT(2);
207        return chm_dense_to_SEXP(chc, 1, 0, dn);
208    }
209    
210    SEXP Csparse_dense_crossprod(SEXP a, SEXP b)
211    {
212        CHM_SP cha = AS_CHM_SP(a);
213        SEXP b_M = PROTECT(mMatrix_as_dgeMatrix(b));
214        CHM_DN chb = AS_CHM_DN(b_M);
215        CHM_DN chc = cholmod_allocate_dense(cha->ncol, chb->ncol, cha->ncol,
216                                            chb->xtype, &c);
217        SEXP dn = PROTECT(allocVector(VECSXP, 2));
218        double one[] = {1,0}, zero[] = {0,0};
219        R_CheckStack();
220    
221        cholmod_sdmult(cha, 1, one, zero, chb, chc, &c);
222        SET_VECTOR_ELT(dn, 0,       /* establish dimnames */
223                       duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), 1)));
224        SET_VECTOR_ELT(dn, 1,
225                       duplicate(VECTOR_ELT(GET_SLOT(b_M, Matrix_DimNamesSym), 1)));
226        UNPROTECT(2);
227        return chm_dense_to_SEXP(chc, 1, 0, dn);
228  }  }
229    
230    /* Computes   x'x  or  x x'  -- see Csparse_Csparse_crossprod above for  x'y and x y' */
231  SEXP Csparse_crossprod(SEXP x, SEXP trans, SEXP triplet)  SEXP Csparse_crossprod(SEXP x, SEXP trans, SEXP triplet)
232  {  {
 #ifdef USE_CHOLMOD  
233      int trip = asLogical(triplet),      int trip = asLogical(triplet),
234          tr   = asLogical(trans); /* gets reversed because _aat is tcrossprod */          tr   = asLogical(trans); /* gets reversed because _aat is tcrossprod */
235      cholmod_triplet      CHM_TR cht = trip ? AS_CHM_TR(x) : (CHM_TR) NULL;
236          *cht = trip ? as_cholmod_triplet(x) : (cholmod_triplet*) NULL;      CHM_SP chcp, chxt,
237      cholmod_sparse *chcp, *chxt,          chx = trip ? cholmod_triplet_to_sparse(cht, cht->nnz, &c) : AS_CHM_SP(x);
238          *chx = trip ? cholmod_triplet_to_sparse(cht, cht->nnz, &c)      SEXP dn = PROTECT(allocVector(VECSXP, 2));
239          : as_cholmod_sparse(x);      R_CheckStack();
240    
241      if (!tr)      if (!tr) chxt = cholmod_transpose(chx, chx->xtype, &c);
         chxt = cholmod_transpose(chx, (int) chx->xtype, &c);  
242      chcp = cholmod_aat((!tr) ? chxt : chx, (int *) NULL, 0, chx->xtype, &c);      chcp = cholmod_aat((!tr) ? chxt : chx, (int *) NULL, 0, chx->xtype, &c);
243      if(!chcp)      if(!chcp) error(_("Csparse_crossprod(): error return from cholmod_aat()"));
244          error("Csparse_crossprod(): error return from cholmod_aat()");      cholmod_band_inplace(0, chcp->ncol, chcp->xtype, chcp, &c);
245        chcp->stype = 1;
246        if (trip) cholmod_free_sparse(&chx, &c);
247        if (!tr) cholmod_free_sparse(&chxt, &c);
248        SET_VECTOR_ELT(dn, 0,       /* establish dimnames */
249                       duplicate(VECTOR_ELT(GET_SLOT(x, Matrix_DimNamesSym),
250                                            (tr) ? 0 : 1)));
251        SET_VECTOR_ELT(dn, 1, duplicate(VECTOR_ELT(dn, 0)));
252        UNPROTECT(1);
253        return chm_sparse_to_SEXP(chcp, 1, 0, 0, "", dn);
254    }
255    
256      if (trip) {  SEXP Csparse_drop(SEXP x, SEXP tol)
257          cholmod_free_sparse(&chx, &c);  {
258          Free(cht);      CHM_SP chx = AS_CHM_SP(x);
259      } else {      CHM_SP ans = cholmod_copy(chx, chx->stype, chx->xtype, &c);
260          Free(chx);      double dtol = asReal(tol);
261        int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
262        R_CheckStack();
263    
264        if(!cholmod_drop(dtol, ans, &c))
265            error(_("cholmod_drop() failed"));
266        return chm_sparse_to_SEXP(ans, 1, 0, Rkind, "",
267                                  GET_SLOT(x, Matrix_DimNamesSym));
268      }      }
269      if (!tr) cholmod_free_sparse(&chxt, &c);  
270      return chm_sparse_to_SEXP(chcp, 1);  SEXP Csparse_horzcat(SEXP x, SEXP y)
271  #else  {
272      error("General crossproduct requires CHOLMOD");      CHM_SP chx = AS_CHM_SP(x), chy = AS_CHM_SP(y);
273      return R_NilValue;          /* -Wall */      int Rkind = 0; /* only for "d" - FIXME */
274  #endif  /* USE_CHOLMOD */      R_CheckStack();
275    
276        /* FIXME: currently drops dimnames */
277        return chm_sparse_to_SEXP(cholmod_horzcat(chx, chy, 1, &c),
278                                  1, 0, Rkind, "", R_NilValue);
279  }  }
280    
281    SEXP Csparse_vertcat(SEXP x, SEXP y)
282    {
283        CHM_SP chx = AS_CHM_SP(x), chy = AS_CHM_SP(y);
284        int Rkind = 0; /* only for "d" - FIXME */
285        R_CheckStack();
286    
287        /* FIXME: currently drops dimnames */
288        return chm_sparse_to_SEXP(cholmod_vertcat(chx, chy, 1, &c),
289                                  1, 0, Rkind, "", R_NilValue);
290    }
291    
292    SEXP Csparse_band(SEXP x, SEXP k1, SEXP k2)
293    {
294        CHM_SP chx = AS_CHM_SP(x);
295        int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
296        CHM_SP ans = cholmod_band(chx, asInteger(k1), asInteger(k2), chx->xtype, &c);
297        R_CheckStack();
298    
299        return chm_sparse_to_SEXP(ans, 1, 0, Rkind, "",
300                                  GET_SLOT(x, Matrix_DimNamesSym));
301    }
302    
303    SEXP Csparse_diagU2N(SEXP x)
304    {
305        if (*diag_P(x) != 'U') {/* "trivially fast" when there's no 'diag' slot at all */
306            return (x);
307        }
308        else {
309            CHM_SP chx = AS_CHM_SP(x);
310            CHM_SP eye = cholmod_speye(chx->nrow, chx->ncol, chx->xtype, &c);
311            double one[] = {1, 0};
312            CHM_SP ans = cholmod_add(chx, eye, one, one, TRUE, TRUE, &c);
313            int uploT = (*uplo_P(x) == 'U') ? 1 : -1;
314            int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
315    
316            R_CheckStack();
317            cholmod_free_sparse(&eye, &c);
318            return chm_sparse_to_SEXP(ans, 1, uploT, Rkind, "N",
319                                      GET_SLOT(x, Matrix_DimNamesSym));
320        }
321    }
322    
323    SEXP Csparse_submatrix(SEXP x, SEXP i, SEXP j)
324    {
325        CHM_SP chx = AS_CHM_SP(x);
326        int rsize = (isNull(i)) ? -1 : LENGTH(i),
327            csize = (isNull(j)) ? -1 : LENGTH(j);
328        int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
329        R_CheckStack();
330    
331        if (rsize >= 0 && !isInteger(i))
332            error(_("Index i must be NULL or integer"));
333        if (csize >= 0 && !isInteger(j))
334            error(_("Index j must be NULL or integer"));
335    
336        return chm_sparse_to_SEXP(cholmod_submatrix(chx, INTEGER(i), rsize,
337                                                    INTEGER(j), csize,
338                                                    TRUE, TRUE, &c),
339                                  1, 0, Rkind, "",
340                                  /* FIXME: drops dimnames */ R_NilValue);
341    }

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

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