38 |
return chm_dense_to_SEXP(chxd, 1); |
return chm_dense_to_SEXP(chxd, 1); |
39 |
} |
} |
40 |
|
|
41 |
SEXP Csparse_to_Tsparse(SEXP x) |
SEXP Csparse_to_matrix(SEXP x) |
42 |
|
{ |
43 |
|
cholmod_sparse *chxs = as_cholmod_sparse(x); |
44 |
|
cholmod_dense *chxd = cholmod_sparse_to_dense(chxs, &c); |
45 |
|
|
46 |
|
Free(chxs); |
47 |
|
return chm_dense_to_matrix(chxd, 1, |
48 |
|
GET_SLOT(x, Matrix_DimNamesSym)); |
49 |
|
} |
50 |
|
|
51 |
|
SEXP Csparse_to_Tsparse(SEXP x, SEXP tri) |
52 |
{ |
{ |
53 |
cholmod_sparse *chxs = as_cholmod_sparse(x); |
cholmod_sparse *chxs = as_cholmod_sparse(x); |
54 |
cholmod_triplet *chxt = cholmod_sparse_to_triplet(chxs, &c); |
cholmod_triplet *chxt = cholmod_sparse_to_triplet(chxs, &c); |
55 |
|
int uploT = 0; char *diag = ""; |
56 |
|
|
57 |
Free(chxs); |
Free(chxs); |
58 |
return chm_triplet_to_SEXP(chxt, 1); |
if (asLogical(tri)) { /* triangular sparse matrices */ |
59 |
|
uploT = (strcmp(CHAR(asChar(GET_SLOT(x, Matrix_uploSym))), "U")) ? |
60 |
|
-1 : 1; |
61 |
|
diag = CHAR(asChar(GET_SLOT(x, Matrix_diagSym))); |
62 |
|
} |
63 |
|
return chm_triplet_to_SEXP(chxt, 1, uploT, diag, |
64 |
|
duplicate(GET_SLOT(x, Matrix_DimNamesSym))); |
65 |
} |
} |
66 |
|
|
67 |
SEXP Csparse_transpose(SEXP x) |
SEXP Csparse_transpose(SEXP x) |
68 |
{ |
{ |
69 |
cholmod_sparse *chx = as_cholmod_sparse(x); |
cholmod_sparse *chx = as_cholmod_sparse(x); |
70 |
cholmod_sparse *chxt = cholmod_transpose(chx, (int) chx->xtype, &c); |
cholmod_sparse *chxt = cholmod_transpose(chx, (int) chx->xtype, &c); |
71 |
|
SEXP dn = PROTECT(duplicate(GET_SLOT(x, Matrix_DimNamesSym))), tmp; |
72 |
|
|
73 |
Free(chx); |
Free(chx); |
74 |
return chm_sparse_to_SEXP(chxt, 1); |
tmp = VECTOR_ELT(dn, 0); /* swap the dimnames */ |
75 |
|
SET_VECTOR_ELT(dn, 0, VECTOR_ELT(dn, 1)); |
76 |
|
SET_VECTOR_ELT(dn, 1, tmp); |
77 |
|
UNPROTECT(1); |
78 |
|
return chm_sparse_to_SEXP(chxt, 1, 0, "", dn); |
79 |
} |
} |
80 |
|
|
81 |
SEXP Csparse_Csparse_prod(SEXP a, SEXP b) |
SEXP Csparse_Csparse_prod(SEXP a, SEXP b) |
83 |
cholmod_sparse *cha = as_cholmod_sparse(a), |
cholmod_sparse *cha = as_cholmod_sparse(a), |
84 |
*chb = as_cholmod_sparse(b); |
*chb = as_cholmod_sparse(b); |
85 |
cholmod_sparse *chc = cholmod_ssmult(cha, chb, 0, cha->xtype, 1, &c); |
cholmod_sparse *chc = cholmod_ssmult(cha, chb, 0, cha->xtype, 1, &c); |
86 |
|
SEXP dn = allocVector(VECSXP, 2); |
87 |
|
|
88 |
Free(cha); Free(chb); |
Free(cha); Free(chb); |
89 |
return chm_sparse_to_SEXP(chc, 1); |
SET_VECTOR_ELT(dn, 0, /* establish dimnames */ |
90 |
|
duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), 0))); |
91 |
|
SET_VECTOR_ELT(dn, 1, |
92 |
|
duplicate(VECTOR_ELT(GET_SLOT(b, Matrix_DimNamesSym), 1))); |
93 |
|
return chm_sparse_to_SEXP(chc, 1, 0, "", dn); |
94 |
} |
} |
95 |
|
|
96 |
SEXP Csparse_dense_prod(SEXP a, SEXP b) |
SEXP Csparse_dense_prod(SEXP a, SEXP b) |
128 |
cholmod_sparse *chcp, *chxt, |
cholmod_sparse *chcp, *chxt, |
129 |
*chx = trip ? cholmod_triplet_to_sparse(cht, cht->nnz, &c) |
*chx = trip ? cholmod_triplet_to_sparse(cht, cht->nnz, &c) |
130 |
: as_cholmod_sparse(x); |
: as_cholmod_sparse(x); |
131 |
|
SEXP dn = PROTECT(allocVector(VECSXP, 2)); |
132 |
|
|
133 |
if (!tr) |
if (!tr) |
134 |
chxt = cholmod_transpose(chx, chx->xtype, &c); |
chxt = cholmod_transpose(chx, chx->xtype, &c); |
144 |
Free(chx); |
Free(chx); |
145 |
} |
} |
146 |
if (!tr) cholmod_free_sparse(&chxt, &c); |
if (!tr) cholmod_free_sparse(&chxt, &c); |
147 |
return chm_sparse_to_SEXP(chcp, 1); |
/* create dimnames */ |
148 |
|
SET_VECTOR_ELT(dn, 0, |
149 |
|
duplicate(VECTOR_ELT(GET_SLOT(x, Matrix_DimNamesSym), |
150 |
|
(tr) ? 1 : 0))); |
151 |
|
SET_VECTOR_ELT(dn, 1, duplicate(VECTOR_ELT(dn, 0))); |
152 |
|
UNPROTECT(1); |
153 |
|
return chm_sparse_to_SEXP(chcp, 1, 0, "", dn); |
154 |
} |
} |
155 |
|
|
156 |
SEXP Csparse_horzcat(SEXP x, SEXP y) |
SEXP Csparse_horzcat(SEXP x, SEXP y) |
160 |
|
|
161 |
ans = cholmod_horzcat(chx, chy, 1, &c); |
ans = cholmod_horzcat(chx, chy, 1, &c); |
162 |
Free(chx); Free(chy); |
Free(chx); Free(chy); |
163 |
return chm_sparse_to_SEXP(ans, 1); |
/* FIXME: currently drops dimnames */ |
164 |
|
return chm_sparse_to_SEXP(ans, 1, 0, "", R_NilValue); |
165 |
} |
} |
166 |
|
|
167 |
SEXP Csparse_vertcat(SEXP x, SEXP y) |
SEXP Csparse_vertcat(SEXP x, SEXP y) |
171 |
|
|
172 |
ans = cholmod_vertcat(chx, chy, 1, &c); |
ans = cholmod_vertcat(chx, chy, 1, &c); |
173 |
Free(chx); Free(chy); |
Free(chx); Free(chy); |
174 |
return chm_sparse_to_SEXP(ans, 1); |
/* FIXME: currently drops dimnames */ |
175 |
|
return chm_sparse_to_SEXP(ans, 1, 0, "", R_NilValue); |
176 |
} |
} |
177 |
|
|
178 |
SEXP Csparse_band(SEXP x, SEXP k1, SEXP k2) |
SEXP Csparse_band(SEXP x, SEXP k1, SEXP k2) |
181 |
|
|
182 |
ans = cholmod_band(chx, asInteger(k1), asInteger(k2), chx->xtype, &c); |
ans = cholmod_band(chx, asInteger(k1), asInteger(k2), chx->xtype, &c); |
183 |
Free(chx); |
Free(chx); |
184 |
return chm_sparse_to_SEXP(ans, 1); |
return chm_sparse_to_SEXP(ans, 1, 0, "", R_NilValue); |
185 |
|
} |
186 |
|
|
187 |
|
SEXP Csparse_diagU2N(SEXP x) |
188 |
|
{ |
189 |
|
cholmod_sparse *chx = as_cholmod_sparse(x); |
190 |
|
cholmod_sparse *eye = cholmod_speye(chx->nrow, chx->ncol, chx->xtype, &c); |
191 |
|
double one[] = {1, 0}; |
192 |
|
cholmod_sparse *ans = cholmod_add(chx, eye, one, one, TRUE, TRUE, &c); |
193 |
|
int uploT = (strcmp(CHAR(asChar(GET_SLOT(x, Matrix_uploSym))), "U")) ? |
194 |
|
-1 : 1; |
195 |
|
|
196 |
|
Free(chx); cholmod_free_sparse(&eye, &c); |
197 |
|
return chm_sparse_to_SEXP(ans, 1, uploT, "N", |
198 |
|
duplicate(GET_SLOT(x, Matrix_DimNamesSym))); |
199 |
|
} |
200 |
|
|
201 |
|
SEXP Csparse_submatrix(SEXP x, SEXP i, SEXP j) |
202 |
|
{ |
203 |
|
cholmod_sparse *chx = as_cholmod_sparse(x); |
204 |
|
int rsize = (isNull(i)) ? -1 : LENGTH(i), |
205 |
|
csize = (isNull(j)) ? -1 : LENGTH(j); |
206 |
|
|
207 |
|
if (rsize >= 0 && !isInteger(i)) |
208 |
|
error(_("Index i must be NULL or integer")); |
209 |
|
if (csize >= 0 && !isInteger(j)) |
210 |
|
error(_("Index j must be NULL or integer")); |
211 |
|
return chm_sparse_to_SEXP(cholmod_submatrix(chx, INTEGER(i), rsize, |
212 |
|
INTEGER(j), csize, |
213 |
|
TRUE, TRUE, &c), |
214 |
|
1, 0, "", R_NilValue); |
215 |
} |
} |