SCM

SCM Repository

[matrix] Annotation of /pkg/src/Csparse.c
ViewVC logotype

Annotation of /pkg/src/Csparse.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1960 - (view) (download) (as text)

1 : bates 1218 /* Sparse matrices in compressed column-oriented form */
2 : bates 922 #include "Csparse.h"
3 :     #include "chm_common.h"
4 :    
5 :     SEXP Csparse_validate(SEXP x)
6 :     {
7 : maechler 1575 /* NB: we do *NOT* check a potential 'x' slot here, at all */
8 : bates 922 SEXP pslot = GET_SLOT(x, Matrix_pSym),
9 :     islot = GET_SLOT(x, Matrix_iSym);
10 : maechler 1893 Rboolean sorted, strictly;
11 :     int j, k,
12 : bates 922 *dims = INTEGER(GET_SLOT(x, Matrix_DimSym)),
13 : maechler 1660 nrow = dims[0],
14 :     ncol = dims[1],
15 : maechler 1654 *xp = INTEGER(pslot),
16 : bates 922 *xi = INTEGER(islot);
17 :    
18 : maechler 1654 if (length(pslot) != dims[1] + 1)
19 :     return mkString(_("slot p must have length = ncol(.) + 1"));
20 : bates 922 if (xp[0] != 0)
21 :     return mkString(_("first element of slot p must be zero"));
22 : maechler 1893 if (length(islot) < xp[ncol]) /* allow larger slots from over-allocation!*/
23 : bates 1555 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 : maechler 1893 sorted = TRUE; strictly = TRUE;
30 : bates 922 for (j = 0; j < ncol; j++) {
31 :     if (xp[j] > xp[j+1])
32 :     return mkString(_("slot p must be non-decreasing"));
33 : maechler 1893 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 : bates 922 }
41 : maechler 1654 if (!sorted) {
42 : maechler 1960 CHM_SP chx = AS_CHM_SP(x);
43 :     R_CheckStack();
44 :    
45 : maechler 1654 cholmod_sort(chx, &c);
46 : maechler 1893 /* 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 : maechler 1654 }
57 : bates 922 return ScalarLogical(1);
58 :     }
59 :    
60 : maechler 1751 /* Called from ../R/Csparse.R : */
61 :     /* Can only return [dln]geMatrix (no symm/triang);
62 :     * FIXME: replace by non-CHOLMOD code ! */
63 : bates 1059 SEXP Csparse_to_dense(SEXP x)
64 :     {
65 : maechler 1960 CHM_SP chxs = AS_CHM_SP(x);
66 : maechler 1751 /* 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 : maechler 1960 CHM_DN chxd = cholmod_sparse_to_dense(chxs, &c);
70 : maechler 1751 int Rkind = (chxs->xtype == CHOLMOD_PATTERN)? -1 : Real_kind(x);
71 : maechler 1960 R_CheckStack();
72 : bates 1059
73 : maechler 1736 return chm_dense_to_SEXP(chxd, 1, Rkind, GET_SLOT(x, Matrix_DimNamesSym));
74 : bates 1059 }
75 :    
76 : maechler 1548 SEXP Csparse_to_nz_pattern(SEXP x, SEXP tri)
77 : bates 1371 {
78 : maechler 1960 CHM_SP chxs = AS_CHM_SP(x);
79 :     CHM_SP chxcp = cholmod_copy(chxs, chxs->stype, CHOLMOD_PATTERN, &c);
80 : bates 1867 int tr = asLogical(tri);
81 : maechler 1960 R_CheckStack();
82 : bates 1371
83 : maechler 1960 return chm_sparse_to_SEXP(chxcp, 1/*do_free*/,
84 : bates 1867 tr ? ((*uplo_P(x) == 'U') ? 1 : -1) : 0,
85 :     0, tr ? diag_P(x) : "",
86 : maechler 1548 GET_SLOT(x, Matrix_DimNamesSym));
87 : bates 1371 }
88 :    
89 : bates 1366 SEXP Csparse_to_matrix(SEXP x)
90 : bates 922 {
91 : maechler 1960 return chm_dense_to_matrix(cholmod_sparse_to_dense(AS_CHM_SP(x), &c),
92 :     1 /*do_free*/, GET_SLOT(x, Matrix_DimNamesSym));
93 : bates 1366 }
94 :    
95 :     SEXP Csparse_to_Tsparse(SEXP x, SEXP tri)
96 :     {
97 : maechler 1960 CHM_SP chxs = AS_CHM_SP(x);
98 :     CHM_TR chxt = cholmod_sparse_to_triplet(chxs, &c);
99 : bates 1867 int tr = asLogical(tri);
100 : maechler 1736 int Rkind = (chxs->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
101 : maechler 1960 R_CheckStack();
102 : bates 922
103 : bates 1867 return chm_triplet_to_SEXP(chxt, 1,
104 :     tr ? ((*uplo_P(x) == 'U') ? 1 : -1) : 0,
105 :     Rkind, tr ? diag_P(x) : "",
106 : bates 1371 GET_SLOT(x, Matrix_DimNamesSym));
107 : bates 922 }
108 :    
109 : bates 1448 /* this used to be called sCMatrix_to_gCMatrix(..) [in ./dsCMatrix.c ]: */
110 : bates 1371 SEXP Csparse_symmetric_to_general(SEXP x)
111 :     {
112 : maechler 1960 CHM_SP chx = AS_CHM_SP(x), chgx;
113 : maechler 1736 int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
114 : maechler 1960 R_CheckStack();
115 : bates 1371
116 :     if (!(chx->stype))
117 : maechler 1548 error(_("Nonsymmetric matrix in Csparse_symmetric_to_general"));
118 : maechler 1375 chgx = cholmod_copy(chx, /* stype: */ 0, chx->xtype, &c);
119 :     /* xtype: pattern, "real", complex or .. */
120 : maechler 1548 return chm_sparse_to_SEXP(chgx, 1, 0, Rkind, "",
121 : bates 1371 GET_SLOT(x, Matrix_DimNamesSym));
122 :     }
123 :    
124 : maechler 1618 SEXP Csparse_general_to_symmetric(SEXP x, SEXP uplo)
125 : maechler 1598 {
126 : maechler 1960 CHM_SP chx = AS_CHM_SP(x), chgx;
127 : maechler 1710 int uploT = (*CHAR(asChar(uplo)) == 'U') ? 1 : -1;
128 : maechler 1736 int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
129 : maechler 1960 R_CheckStack();
130 : maechler 1598
131 : maechler 1618 chgx = cholmod_copy(chx, /* stype: */ uploT, chx->xtype, &c);
132 : maechler 1598 /* xtype: pattern, "real", complex or .. */
133 :     return chm_sparse_to_SEXP(chgx, 1, 0, Rkind, "",
134 :     GET_SLOT(x, Matrix_DimNamesSym));
135 :     }
136 :    
137 : bates 1369 SEXP Csparse_transpose(SEXP x, SEXP tri)
138 : bates 922 {
139 : maechler 1921 /* TODO: lgCMatrix & igC* currently go via double prec. cholmod -
140 :     * since cholmod (& cs) lacks sparse 'int' matrices */
141 : maechler 1960 CHM_SP chx = AS_CHM_SP(x);
142 : maechler 1736 int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
143 : maechler 1960 CHM_SP chxt = cholmod_transpose(chx, chx->xtype, &c);
144 : bates 1366 SEXP dn = PROTECT(duplicate(GET_SLOT(x, Matrix_DimNamesSym))), tmp;
145 : bates 1867 int tr = asLogical(tri);
146 : maechler 1960 R_CheckStack();
147 : bates 1369
148 : bates 1366 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 : bates 1867 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 : bates 922 }
156 :    
157 :     SEXP Csparse_Csparse_prod(SEXP a, SEXP b)
158 :     {
159 : maechler 1960 CHM_SP cha = AS_CHM_SP(a), chb = AS_CHM_SP(b);
160 :     CHM_SP chc = cholmod_ssmult(cha, chb, 0, cha->xtype, 1, &c);
161 : bates 1366 SEXP dn = allocVector(VECSXP, 2);
162 : maechler 1960 R_CheckStack();
163 : bates 922
164 : bates 1366 SET_VECTOR_ELT(dn, 0, /* establish dimnames */
165 :     duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), 0)));
166 :     SET_VECTOR_ELT(dn, 1,
167 :     duplicate(VECTOR_ELT(GET_SLOT(b, Matrix_DimNamesSym), 1)));
168 : maechler 1548 return chm_sparse_to_SEXP(chc, 1, 0, 0, "", dn);
169 : bates 922 }
170 :    
171 : maechler 1659 SEXP Csparse_Csparse_crossprod(SEXP a, SEXP b, SEXP trans)
172 : bates 1657 {
173 : maechler 1659 int tr = asLogical(trans);
174 : maechler 1960 CHM_SP cha = AS_CHM_SP(a), chb = AS_CHM_SP(b), chTr, chc;
175 : bates 1657 SEXP dn = allocVector(VECSXP, 2);
176 : maechler 1960 R_CheckStack();
177 : bates 1657
178 : maechler 1960 chTr = cholmod_transpose((tr) ? chb : cha, chb->xtype, &c);
179 : maechler 1659 chc = cholmod_ssmult((tr) ? cha : chTr, (tr) ? chTr : chb,
180 :     0, cha->xtype, 1, &c);
181 : maechler 1960 cholmod_free_sparse(&chTr, &c);
182 : maechler 1659
183 : bates 1657 SET_VECTOR_ELT(dn, 0, /* establish dimnames */
184 : maechler 1659 duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), (tr) ? 0 : 1)));
185 : bates 1657 SET_VECTOR_ELT(dn, 1,
186 : maechler 1659 duplicate(VECTOR_ELT(GET_SLOT(b, Matrix_DimNamesSym), (tr) ? 0 : 1)));
187 : bates 1657 return chm_sparse_to_SEXP(chc, 1, 0, 0, "", dn);
188 :     }
189 :    
190 : bates 922 SEXP Csparse_dense_prod(SEXP a, SEXP b)
191 :     {
192 : maechler 1960 CHM_SP cha = AS_CHM_SP(a);
193 : maechler 1660 SEXP b_M = PROTECT(mMatrix_as_dgeMatrix(b));
194 : maechler 1960 CHM_DN chb = AS_CHM_DN(b_M);
195 :     CHM_DN chc = cholmod_allocate_dense(cha->nrow, chb->ncol, cha->nrow,
196 :     chb->xtype, &c);
197 :     SEXP dn = PROTECT(allocVector(VECSXP, 2));
198 :     double one[] = {1,0}, zero[] = {0,0};
199 :     R_CheckStack();
200 : bates 922
201 : maechler 1960 cholmod_sdmult(cha, 0, one, zero, chb, chc, &c);
202 : maechler 1660 SET_VECTOR_ELT(dn, 0, /* establish dimnames */
203 :     duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), 0)));
204 :     SET_VECTOR_ELT(dn, 1,
205 :     duplicate(VECTOR_ELT(GET_SLOT(b_M, Matrix_DimNamesSym), 1)));
206 : maechler 1960 UNPROTECT(2);
207 : maechler 1660 return chm_dense_to_SEXP(chc, 1, 0, dn);
208 : bates 922 }
209 : maechler 925
210 : bates 1067 SEXP Csparse_dense_crossprod(SEXP a, SEXP b)
211 :     {
212 : maechler 1960 CHM_SP cha = AS_CHM_SP(a);
213 : maechler 1660 SEXP b_M = PROTECT(mMatrix_as_dgeMatrix(b));
214 : maechler 1960 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 : bates 1067
221 : maechler 1960 cholmod_sdmult(cha, 1, one, zero, chb, chc, &c);
222 : maechler 1660 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 : maechler 1960 UNPROTECT(2);
227 : maechler 1660 return chm_dense_to_SEXP(chc, 1, 0, dn);
228 : bates 1067 }
229 :    
230 : maechler 1659 /* Computes x'x or x x' -- see Csparse_Csparse_crossprod above for x'y and x y' */
231 : bates 928 SEXP Csparse_crossprod(SEXP x, SEXP trans, SEXP triplet)
232 : bates 922 {
233 : maechler 957 int trip = asLogical(triplet),
234 :     tr = asLogical(trans); /* gets reversed because _aat is tcrossprod */
235 : maechler 1960 CHM_TR cht = trip ? AS_CHM_TR(x) : (CHM_TR) NULL;
236 :     CHM_SP chcp, chxt,
237 :     chx = trip ? cholmod_triplet_to_sparse(cht, cht->nnz, &c) : AS_CHM_SP(x);
238 : bates 1366 SEXP dn = PROTECT(allocVector(VECSXP, 2));
239 : maechler 1960 R_CheckStack();
240 : bates 922
241 : maechler 1960 if (!tr) chxt = cholmod_transpose(chx, chx->xtype, &c);
242 : bates 928 chcp = cholmod_aat((!tr) ? chxt : chx, (int *) NULL, 0, chx->xtype, &c);
243 : maechler 1960 if(!chcp) error(_("Csparse_crossprod(): error return from cholmod_aat()"));
244 : bates 1360 cholmod_band_inplace(0, chcp->ncol, chcp->xtype, chcp, &c);
245 :     chcp->stype = 1;
246 : maechler 1960 if (trip) cholmod_free_sparse(&chx, &c);
247 : bates 923 if (!tr) cholmod_free_sparse(&chxt, &c);
248 : maechler 1960 SET_VECTOR_ELT(dn, 0, /* establish dimnames */
249 : bates 1366 duplicate(VECTOR_ELT(GET_SLOT(x, Matrix_DimNamesSym),
250 : maechler 1660 (tr) ? 0 : 1)));
251 : bates 1366 SET_VECTOR_ELT(dn, 1, duplicate(VECTOR_ELT(dn, 0)));
252 :     UNPROTECT(1);
253 : maechler 1548 return chm_sparse_to_SEXP(chcp, 1, 0, 0, "", dn);
254 : bates 922 }
255 : bates 923
256 : maechler 1618 SEXP Csparse_drop(SEXP x, SEXP tol)
257 :     {
258 : maechler 1960 CHM_SP chx = AS_CHM_SP(x);
259 :     CHM_SP ans = cholmod_copy(chx, chx->stype, chx->xtype, &c);
260 : maechler 1618 double dtol = asReal(tol);
261 : maechler 1736 int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
262 : maechler 1960 R_CheckStack();
263 : maechler 1618
264 :     if(!cholmod_drop(dtol, ans, &c))
265 :     error(_("cholmod_drop() failed"));
266 : maechler 1736 return chm_sparse_to_SEXP(ans, 1, 0, Rkind, "",
267 :     GET_SLOT(x, Matrix_DimNamesSym));
268 : maechler 1618 }
269 :    
270 : bates 1218 SEXP Csparse_horzcat(SEXP x, SEXP y)
271 :     {
272 : maechler 1960 CHM_SP chx = AS_CHM_SP(x), chy = AS_CHM_SP(y);
273 : maechler 1548 int Rkind = 0; /* only for "d" - FIXME */
274 : maechler 1960 R_CheckStack();
275 : maechler 1375
276 : bates 1366 /* FIXME: currently drops dimnames */
277 : maechler 1960 return chm_sparse_to_SEXP(cholmod_horzcat(chx, chy, 1, &c),
278 :     1, 0, Rkind, "", R_NilValue);
279 : bates 1218 }
280 :    
281 :     SEXP Csparse_vertcat(SEXP x, SEXP y)
282 :     {
283 : maechler 1960 CHM_SP chx = AS_CHM_SP(x), chy = AS_CHM_SP(y);
284 : maechler 1548 int Rkind = 0; /* only for "d" - FIXME */
285 : maechler 1960 R_CheckStack();
286 : maechler 1375
287 : bates 1366 /* FIXME: currently drops dimnames */
288 : maechler 1960 return chm_sparse_to_SEXP(cholmod_vertcat(chx, chy, 1, &c),
289 :     1, 0, Rkind, "", R_NilValue);
290 : bates 1218 }
291 : bates 1265
292 :     SEXP Csparse_band(SEXP x, SEXP k1, SEXP k2)
293 :     {
294 : maechler 1960 CHM_SP chx = AS_CHM_SP(x);
295 : maechler 1736 int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
296 : maechler 1960 CHM_SP ans = cholmod_band(chx, asInteger(k1), asInteger(k2), chx->xtype, &c);
297 :     R_CheckStack();
298 : bates 1265
299 : maechler 1736 return chm_sparse_to_SEXP(ans, 1, 0, Rkind, "",
300 :     GET_SLOT(x, Matrix_DimNamesSym));
301 : bates 1265 }
302 : bates 1366
303 :     SEXP Csparse_diagU2N(SEXP x)
304 :     {
305 : maechler 1708 if (*diag_P(x) != 'U') {/* "trivially fast" when there's no 'diag' slot at all */
306 :     return (x);
307 :     }
308 :     else {
309 : maechler 1960 CHM_SP chx = AS_CHM_SP(x);
310 :     CHM_SP eye = cholmod_speye(chx->nrow, chx->ncol, chx->xtype, &c);
311 : maechler 1708 double one[] = {1, 0};
312 : maechler 1960 CHM_SP ans = cholmod_add(chx, eye, one, one, TRUE, TRUE, &c);
313 : maechler 1710 int uploT = (*uplo_P(x) == 'U') ? 1 : -1;
314 : maechler 1736 int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
315 : bates 1366
316 : maechler 1960 R_CheckStack();
317 :     cholmod_free_sparse(&eye, &c);
318 : maechler 1708 return chm_sparse_to_SEXP(ans, 1, uploT, Rkind, "N",
319 : maechler 1736 GET_SLOT(x, Matrix_DimNamesSym));
320 : maechler 1708 }
321 : bates 1366 }
322 :    
323 :     SEXP Csparse_submatrix(SEXP x, SEXP i, SEXP j)
324 :     {
325 : maechler 1960 CHM_SP chx = AS_CHM_SP(x);
326 : bates 1366 int rsize = (isNull(i)) ? -1 : LENGTH(i),
327 :     csize = (isNull(j)) ? -1 : LENGTH(j);
328 : maechler 1736 int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
329 : maechler 1960 R_CheckStack();
330 : bates 1366
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 : maechler 1736
336 : bates 1366 return chm_sparse_to_SEXP(cholmod_submatrix(chx, INTEGER(i), rsize,
337 : maechler 1375 INTEGER(j), csize,
338 : bates 1366 TRUE, TRUE, &c),
339 : maechler 1736 1, 0, Rkind, "",
340 :     /* FIXME: drops dimnames */ R_NilValue);
341 : bates 1366 }

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