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 |
{ |
{ |
29 |
return ScalarLogical(1); |
return ScalarLogical(1); |
30 |
} |
} |
31 |
|
|
32 |
SEXP Csparse_to_Tsparse(SEXP x) |
SEXP Csparse_to_dense(SEXP x) |
33 |
|
{ |
34 |
|
cholmod_sparse *chxs = as_cholmod_sparse(x); |
35 |
|
cholmod_dense *chxd = cholmod_sparse_to_dense(chxs, &c); |
36 |
|
|
37 |
|
Free(chxs); |
38 |
|
return chm_dense_to_SEXP(chxd, 1); |
39 |
|
} |
40 |
|
|
41 |
|
SEXP Csparse_to_logical(SEXP x, SEXP tri) |
42 |
|
{ |
43 |
|
cholmod_sparse *chxs = as_cholmod_sparse(x); |
44 |
|
cholmod_sparse |
45 |
|
*chxcp = cholmod_copy(chxs, chxs->stype, CHOLMOD_PATTERN, &c); |
46 |
|
int uploT = 0; char *diag = ""; |
47 |
|
|
48 |
|
Free(chxs); |
49 |
|
if (asLogical(tri)) { /* triangular sparse matrices */ |
50 |
|
uploT = (strcmp(CHAR(asChar(GET_SLOT(x, Matrix_uploSym))), "U")) ? |
51 |
|
-1 : 1; |
52 |
|
diag = CHAR(asChar(GET_SLOT(x, Matrix_diagSym))); |
53 |
|
} |
54 |
|
return chm_sparse_to_SEXP(chxcp, 1, uploT, diag, |
55 |
|
GET_SLOT(x, Matrix_DimNamesSym)); |
56 |
|
} |
57 |
|
|
58 |
|
SEXP Csparse_to_matrix(SEXP x) |
59 |
|
{ |
60 |
|
cholmod_sparse *chxs = as_cholmod_sparse(x); |
61 |
|
cholmod_dense *chxd = cholmod_sparse_to_dense(chxs, &c); |
62 |
|
|
63 |
|
Free(chxs); |
64 |
|
return chm_dense_to_matrix(chxd, 1, |
65 |
|
GET_SLOT(x, Matrix_DimNamesSym)); |
66 |
|
} |
67 |
|
|
68 |
|
SEXP Csparse_to_Tsparse(SEXP x, SEXP tri) |
69 |
{ |
{ |
|
#ifdef USE_CHOLMOD |
|
70 |
cholmod_sparse *chxs = as_cholmod_sparse(x); |
cholmod_sparse *chxs = as_cholmod_sparse(x); |
71 |
cholmod_triplet *chxt = cholmod_sparse_to_triplet(chxs, &c); |
cholmod_triplet *chxt = cholmod_sparse_to_triplet(chxs, &c); |
72 |
|
int uploT = 0; char *diag = ""; |
73 |
|
|
74 |
free(chxs); |
Free(chxs); |
75 |
return chm_triplet_to_SEXP(chxt, 1); |
if (asLogical(tri)) { /* triangular sparse matrices */ |
76 |
#else |
uploT = (strcmp(CHAR(asChar(GET_SLOT(x, Matrix_uploSym))), "U")) ? |
77 |
error("General conversion requires CHOLMOD"); |
-1 : 1; |
78 |
return R_NilValue; /* -Wall */ |
diag = CHAR(asChar(GET_SLOT(x, Matrix_diagSym))); |
79 |
#endif /* USE_CHOLMOD */ |
} |
80 |
|
return chm_triplet_to_SEXP(chxt, 1, uploT, diag, |
81 |
|
GET_SLOT(x, Matrix_DimNamesSym)); |
82 |
} |
} |
83 |
|
|
84 |
SEXP Csparse_transpose(SEXP x) |
SEXP Csparse_symmetric_to_general(SEXP x) |
85 |
|
{ |
86 |
|
cholmod_sparse *chx = as_cholmod_sparse(x), *chgx; |
87 |
|
|
88 |
|
if (!(chx->stype)) |
89 |
|
error(_("Nonsymmetric matrix in Csparse_symmeteric_to_general")); |
90 |
|
chgx = cholmod_copy(chx, 0, chx->xtype, &c); |
91 |
|
Free(chx); |
92 |
|
return chm_sparse_to_SEXP(chgx, 1, 0, "", |
93 |
|
GET_SLOT(x, Matrix_DimNamesSym)); |
94 |
|
} |
95 |
|
|
96 |
|
SEXP Csparse_transpose(SEXP x, SEXP tri) |
97 |
{ |
{ |
|
#ifdef USE_CHOLMOD |
|
98 |
cholmod_sparse *chx = as_cholmod_sparse(x); |
cholmod_sparse *chx = as_cholmod_sparse(x); |
99 |
cholmod_sparse *chxt = cholmod_transpose(chx, (int) chx->xtype, &c); |
cholmod_sparse *chxt = cholmod_transpose(chx, (int) chx->xtype, &c); |
100 |
|
SEXP dn = PROTECT(duplicate(GET_SLOT(x, Matrix_DimNamesSym))), tmp; |
101 |
|
int uploT = 0; char *diag = ""; |
102 |
|
|
103 |
free(chx); |
Free(chx); |
104 |
return chm_sparse_to_SEXP(chxt, 1); |
tmp = VECTOR_ELT(dn, 0); /* swap the dimnames */ |
105 |
#else |
SET_VECTOR_ELT(dn, 0, VECTOR_ELT(dn, 1)); |
106 |
error("General conversion requires CHOLMOD"); |
SET_VECTOR_ELT(dn, 1, tmp); |
107 |
return R_NilValue; /* -Wall */ |
UNPROTECT(1); |
108 |
#endif /* USE_CHOLMOD */ |
if (asLogical(tri)) { /* triangular sparse matrices */ |
109 |
|
uploT = (strcmp(CHAR(asChar(GET_SLOT(x, Matrix_uploSym))), "U")) ? |
110 |
|
1 : -1; /* switch upper and lower for transpose */ |
111 |
|
diag = CHAR(asChar(GET_SLOT(x, Matrix_diagSym))); |
112 |
|
} |
113 |
|
return chm_sparse_to_SEXP(chxt, 1, uploT, diag, dn); |
114 |
} |
} |
|
|
|
115 |
|
|
116 |
SEXP Csparse_Csparse_prod(SEXP a, SEXP b) |
SEXP Csparse_Csparse_prod(SEXP a, SEXP b) |
117 |
{ |
{ |
118 |
#ifdef USE_CHOLMOD |
cholmod_sparse *cha = as_cholmod_sparse(a), |
119 |
cholmod_sparse *cha = as_cholmod_sparse(a), *chb = as_cholmod_sparse(b); |
*chb = as_cholmod_sparse(b); |
120 |
cholmod_sparse *chc = cholmod_ssmult(cha, chb, 0, (int) cha->xtype, 1, &c); |
cholmod_sparse *chc = cholmod_ssmult(cha, chb, 0, cha->xtype, 1, &c); |
121 |
|
SEXP dn = allocVector(VECSXP, 2); |
122 |
free(cha); free(chb); |
|
123 |
return chm_sparse_to_SEXP(chc, 1); |
Free(cha); Free(chb); |
124 |
#else |
SET_VECTOR_ELT(dn, 0, /* establish dimnames */ |
125 |
error("General multiplication requires CHOLMOD"); |
duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), 0))); |
126 |
return R_NilValue; /* -Wall */ |
SET_VECTOR_ELT(dn, 1, |
127 |
#endif /* USE_CHOLMOD */ |
duplicate(VECTOR_ELT(GET_SLOT(b, Matrix_DimNamesSym), 1))); |
128 |
|
return chm_sparse_to_SEXP(chc, 1, 0, "", dn); |
129 |
} |
} |
130 |
|
|
131 |
SEXP Csparse_dense_prod(SEXP a, SEXP b) |
SEXP Csparse_dense_prod(SEXP a, SEXP b) |
132 |
{ |
{ |
|
#ifdef USE_CHOLMOD |
|
133 |
cholmod_sparse *cha = as_cholmod_sparse(a); |
cholmod_sparse *cha = as_cholmod_sparse(a); |
134 |
cholmod_dense *chb = as_cholmod_dense(b); |
cholmod_dense *chb = as_cholmod_dense(b); |
135 |
cholmod_dense *chc = cholmod_allocate_dense(cha->nrow, chb->ncol, |
cholmod_dense *chc = cholmod_allocate_dense(cha->nrow, chb->ncol, |
137 |
double alpha = 1, beta = 0; |
double alpha = 1, beta = 0; |
138 |
|
|
139 |
cholmod_sdmult(cha, 0, &alpha, &beta, chb, chc, &c); |
cholmod_sdmult(cha, 0, &alpha, &beta, chb, chc, &c); |
140 |
free(cha); free(chb); |
Free(cha); Free(chb); |
141 |
|
return chm_dense_to_SEXP(chc, 1); |
142 |
|
} |
143 |
|
|
144 |
|
SEXP Csparse_dense_crossprod(SEXP a, SEXP b) |
145 |
|
{ |
146 |
|
cholmod_sparse *cha = as_cholmod_sparse(a); |
147 |
|
cholmod_dense *chb = as_cholmod_dense(b); |
148 |
|
cholmod_dense *chc = cholmod_allocate_dense(cha->ncol, chb->ncol, |
149 |
|
cha->ncol, chb->xtype, &c); |
150 |
|
double alpha = 1, beta = 0; |
151 |
|
|
152 |
|
cholmod_sdmult(cha, 1, &alpha, &beta, chb, chc, &c); |
153 |
|
Free(cha); Free(chb); |
154 |
return chm_dense_to_SEXP(chc, 1); |
return chm_dense_to_SEXP(chc, 1); |
|
#else |
|
|
error("General multiplication requires CHOLMOD"); |
|
|
return R_NilValue; /* -Wall */ |
|
|
#endif /* USE_CHOLMOD */ |
|
155 |
} |
} |
156 |
|
|
157 |
SEXP Csparse_crossprod(SEXP x, SEXP trans, SEXP triplet) |
SEXP Csparse_crossprod(SEXP x, SEXP trans, SEXP triplet) |
158 |
{ |
{ |
|
#ifdef USE_CHOLMOD |
|
159 |
int trip = asLogical(triplet), |
int trip = asLogical(triplet), |
160 |
tr = asLogical(trans); /* gets reversed because _aat is tcrossprod */ |
tr = asLogical(trans); /* gets reversed because _aat is tcrossprod */ |
161 |
cholmod_triplet |
cholmod_triplet |
163 |
cholmod_sparse *chcp, *chxt, |
cholmod_sparse *chcp, *chxt, |
164 |
*chx = trip ? cholmod_triplet_to_sparse(cht, cht->nnz, &c) |
*chx = trip ? cholmod_triplet_to_sparse(cht, cht->nnz, &c) |
165 |
: as_cholmod_sparse(x); |
: as_cholmod_sparse(x); |
166 |
|
SEXP dn = PROTECT(allocVector(VECSXP, 2)); |
167 |
|
|
168 |
if (!tr) |
if (!tr) |
169 |
chxt = cholmod_transpose(chx, (int) chx->xtype, &c); |
chxt = cholmod_transpose(chx, chx->xtype, &c); |
170 |
chcp = cholmod_aat((!tr) ? chxt : chx, (int *) NULL, 0, chx->xtype, &c); |
chcp = cholmod_aat((!tr) ? chxt : chx, (int *) NULL, 0, chx->xtype, &c); |
171 |
if(!chcp) |
if(!chcp) |
172 |
error("Csparse_crossprod(): error return from cholmod_aat()"); |
error("Csparse_crossprod(): error return from cholmod_aat()"); |
173 |
|
cholmod_band_inplace(0, chcp->ncol, chcp->xtype, chcp, &c); |
174 |
|
chcp->stype = 1; |
175 |
if (trip) { |
if (trip) { |
176 |
cholmod_free_sparse(&chx, &c); |
cholmod_free_sparse(&chx, &c); |
177 |
free(cht); |
Free(cht); |
178 |
} else { |
} else { |
179 |
free(chx); |
Free(chx); |
180 |
} |
} |
181 |
if (!tr) cholmod_free_sparse(&chxt, &c); |
if (!tr) cholmod_free_sparse(&chxt, &c); |
182 |
return chm_sparse_to_SEXP(chcp, 1); |
/* create dimnames */ |
183 |
#else |
SET_VECTOR_ELT(dn, 0, |
184 |
error("General crossproduct requires CHOLMOD"); |
duplicate(VECTOR_ELT(GET_SLOT(x, Matrix_DimNamesSym), |
185 |
return R_NilValue; /* -Wall */ |
(tr) ? 1 : 0))); |
186 |
#endif /* USE_CHOLMOD */ |
SET_VECTOR_ELT(dn, 1, duplicate(VECTOR_ELT(dn, 0))); |
187 |
|
UNPROTECT(1); |
188 |
|
return chm_sparse_to_SEXP(chcp, 1, 0, "", dn); |
189 |
|
} |
190 |
|
|
191 |
|
SEXP Csparse_horzcat(SEXP x, SEXP y) |
192 |
|
{ |
193 |
|
cholmod_sparse *chx = as_cholmod_sparse(x), |
194 |
|
*chy = as_cholmod_sparse(y), *ans; |
195 |
|
|
196 |
|
ans = cholmod_horzcat(chx, chy, 1, &c); |
197 |
|
Free(chx); Free(chy); |
198 |
|
/* FIXME: currently drops dimnames */ |
199 |
|
return chm_sparse_to_SEXP(ans, 1, 0, "", R_NilValue); |
200 |
|
} |
201 |
|
|
202 |
|
SEXP Csparse_vertcat(SEXP x, SEXP y) |
203 |
|
{ |
204 |
|
cholmod_sparse *chx = as_cholmod_sparse(x), |
205 |
|
*chy = as_cholmod_sparse(y), *ans; |
206 |
|
|
207 |
|
ans = cholmod_vertcat(chx, chy, 1, &c); |
208 |
|
Free(chx); Free(chy); |
209 |
|
/* FIXME: currently drops dimnames */ |
210 |
|
return chm_sparse_to_SEXP(ans, 1, 0, "", R_NilValue); |
211 |
} |
} |
212 |
|
|
213 |
|
SEXP Csparse_band(SEXP x, SEXP k1, SEXP k2) |
214 |
|
{ |
215 |
|
cholmod_sparse *chx = as_cholmod_sparse(x), *ans; |
216 |
|
|
217 |
|
ans = cholmod_band(chx, asInteger(k1), asInteger(k2), chx->xtype, &c); |
218 |
|
Free(chx); |
219 |
|
return chm_sparse_to_SEXP(ans, 1, 0, "", R_NilValue); |
220 |
|
} |
221 |
|
|
222 |
|
SEXP Csparse_diagU2N(SEXP x) |
223 |
|
{ |
224 |
|
cholmod_sparse *chx = as_cholmod_sparse(x); |
225 |
|
cholmod_sparse *eye = cholmod_speye(chx->nrow, chx->ncol, chx->xtype, &c); |
226 |
|
double one[] = {1, 0}; |
227 |
|
cholmod_sparse *ans = cholmod_add(chx, eye, one, one, TRUE, TRUE, &c); |
228 |
|
int uploT = (strcmp(CHAR(asChar(GET_SLOT(x, Matrix_uploSym))), "U")) ? |
229 |
|
-1 : 1; |
230 |
|
|
231 |
|
Free(chx); cholmod_free_sparse(&eye, &c); |
232 |
|
return chm_sparse_to_SEXP(ans, 1, uploT, "N", |
233 |
|
duplicate(GET_SLOT(x, Matrix_DimNamesSym))); |
234 |
|
} |
235 |
|
|
236 |
|
SEXP Csparse_submatrix(SEXP x, SEXP i, SEXP j) |
237 |
|
{ |
238 |
|
cholmod_sparse *chx = as_cholmod_sparse(x); |
239 |
|
int rsize = (isNull(i)) ? -1 : LENGTH(i), |
240 |
|
csize = (isNull(j)) ? -1 : LENGTH(j); |
241 |
|
|
242 |
|
if (rsize >= 0 && !isInteger(i)) |
243 |
|
error(_("Index i must be NULL or integer")); |
244 |
|
if (csize >= 0 && !isInteger(j)) |
245 |
|
error(_("Index j must be NULL or integer")); |
246 |
|
return chm_sparse_to_SEXP(cholmod_submatrix(chx, INTEGER(i), rsize, |
247 |
|
INTEGER(j), csize, |
248 |
|
TRUE, TRUE, &c), |
249 |
|
1, 0, "", R_NilValue); |
250 |
|
} |