1 |
/* Sparse matrices in compressed column-oriented form */ |
/* Sparse matrices in compressed column-oriented form */ |
2 |
#include "Csparse.h" |
#include "Csparse.h" |
3 |
|
#include "Tsparse.h" |
4 |
#include "chm_common.h" |
#include "chm_common.h" |
5 |
|
|
6 |
SEXP Csparse_validate(SEXP x) |
SEXP Csparse_validate(SEXP x) |
203 |
|
|
204 |
SEXP Csparse_Csparse_prod(SEXP a, SEXP b) |
SEXP Csparse_Csparse_prod(SEXP a, SEXP b) |
205 |
{ |
{ |
206 |
CHM_SP cha = AS_CHM_SP(a), chb = AS_CHM_SP(b); |
CHM_SP |
207 |
CHM_SP chc = cholmod_ssmult(cha, chb, 0, cha->xtype, 1, &c); |
cha = AS_CHM_SP(Csparse_diagU2N(a)), |
208 |
|
chb = AS_CHM_SP(Csparse_diagU2N(b)), |
209 |
|
chc = cholmod_ssmult(cha, chb, 0, cha->xtype, 1, &c); |
210 |
SEXP dn = allocVector(VECSXP, 2); |
SEXP dn = allocVector(VECSXP, 2); |
211 |
R_CheckStack(); |
R_CheckStack(); |
212 |
|
|
220 |
SEXP Csparse_Csparse_crossprod(SEXP a, SEXP b, SEXP trans) |
SEXP Csparse_Csparse_crossprod(SEXP a, SEXP b, SEXP trans) |
221 |
{ |
{ |
222 |
int tr = asLogical(trans); |
int tr = asLogical(trans); |
223 |
CHM_SP cha = AS_CHM_SP(a), chb = AS_CHM_SP(b), chTr, chc; |
CHM_SP |
224 |
|
cha = AS_CHM_SP(Csparse_diagU2N(a)), |
225 |
|
chb = AS_CHM_SP(Csparse_diagU2N(b)), |
226 |
|
chTr, chc; |
227 |
SEXP dn = allocVector(VECSXP, 2); |
SEXP dn = allocVector(VECSXP, 2); |
228 |
R_CheckStack(); |
R_CheckStack(); |
229 |
|
|
241 |
|
|
242 |
SEXP Csparse_dense_prod(SEXP a, SEXP b) |
SEXP Csparse_dense_prod(SEXP a, SEXP b) |
243 |
{ |
{ |
244 |
CHM_SP cha = AS_CHM_SP(a); |
CHM_SP cha = AS_CHM_SP(Csparse_diagU2N(a)); |
245 |
SEXP b_M = PROTECT(mMatrix_as_dgeMatrix(b)); |
SEXP b_M = PROTECT(mMatrix_as_dgeMatrix(b)); |
246 |
CHM_DN chb = AS_CHM_DN(b_M); |
CHM_DN chb = AS_CHM_DN(b_M); |
247 |
CHM_DN chc = cholmod_allocate_dense(cha->nrow, chb->ncol, cha->nrow, |
CHM_DN chc = cholmod_allocate_dense(cha->nrow, chb->ncol, cha->nrow, |
261 |
|
|
262 |
SEXP Csparse_dense_crossprod(SEXP a, SEXP b) |
SEXP Csparse_dense_crossprod(SEXP a, SEXP b) |
263 |
{ |
{ |
264 |
CHM_SP cha = AS_CHM_SP(a); |
CHM_SP cha = AS_CHM_SP(Csparse_diagU2N(a)); |
265 |
SEXP b_M = PROTECT(mMatrix_as_dgeMatrix(b)); |
SEXP b_M = PROTECT(mMatrix_as_dgeMatrix(b)); |
266 |
CHM_DN chb = AS_CHM_DN(b_M); |
CHM_DN chb = AS_CHM_DN(b_M); |
267 |
CHM_DN chc = cholmod_allocate_dense(cha->ncol, chb->ncol, cha->ncol, |
CHM_DN chc = cholmod_allocate_dense(cha->ncol, chb->ncol, cha->ncol, |
284 |
{ |
{ |
285 |
int trip = asLogical(triplet), |
int trip = asLogical(triplet), |
286 |
tr = asLogical(trans); /* gets reversed because _aat is tcrossprod */ |
tr = asLogical(trans); /* gets reversed because _aat is tcrossprod */ |
287 |
CHM_TR cht = trip ? AS_CHM_TR(x) : (CHM_TR) NULL; |
CHM_TR cht = trip ? AS_CHM_TR(Tsparse_diagU2N(x)) : (CHM_TR) NULL; |
288 |
CHM_SP chcp, chxt, |
CHM_SP chcp, chxt, |
289 |
chx = trip ? cholmod_triplet_to_sparse(cht, cht->nnz, &c) : AS_CHM_SP(x); |
chx = (trip ? |
290 |
|
cholmod_triplet_to_sparse(cht, cht->nnz, &c) : |
291 |
|
AS_CHM_SP(Csparse_diagU2N(x))); |
292 |
SEXP dn = PROTECT(allocVector(VECSXP, 2)); |
SEXP dn = PROTECT(allocVector(VECSXP, 2)); |
293 |
R_CheckStack(); |
R_CheckStack(); |
294 |
|
|
295 |
if (!tr) chxt = cholmod_transpose(chx, chx->xtype, &c); |
if (!tr) chxt = cholmod_transpose(chx, chx->xtype, &c); |
296 |
chcp = cholmod_aat((!tr) ? chxt : chx, (int *) NULL, 0, chx->xtype, &c); |
chcp = cholmod_aat((!tr) ? chxt : chx, (int *) NULL, 0, chx->xtype, &c); |
297 |
if(!chcp) error(_("Csparse_crossprod(): error return from cholmod_aat()")); |
if(!chcp) { |
298 |
|
UNPROTECT(1); |
299 |
|
error(_("Csparse_crossprod(): error return from cholmod_aat()")); |
300 |
|
} |
301 |
cholmod_band_inplace(0, chcp->ncol, chcp->xtype, chcp, &c); |
cholmod_band_inplace(0, chcp->ncol, chcp->xtype, chcp, &c); |
302 |
chcp->stype = 1; |
chcp->stype = 1; |
303 |
if (trip) cholmod_free_sparse(&chx, &c); |
if (trip) cholmod_free_sparse(&chx, &c); |
359 |
|
|
360 |
SEXP Csparse_diagU2N(SEXP x) |
SEXP Csparse_diagU2N(SEXP x) |
361 |
{ |
{ |
362 |
if (*diag_P(x) != 'U') {/* "trivially fast" when there's no 'diag' slot at all */ |
const char *cl = class_P(x); |
363 |
|
/* dtCMatrix, etc; [1] = the second character =?= 't' for triangular */ |
364 |
|
if (cl[1] != 't' || *diag_P(x) != 'U') { |
365 |
|
/* "trivially fast" when not triangular (<==> no 'diag' slot), or not *unit* triangular */ |
366 |
return (x); |
return (x); |
367 |
} |
} |
368 |
else { |
else { |
407 |
if (!f) |
if (!f) |
408 |
error(_("failure to open file \"%s\" for writing"), |
error(_("failure to open file \"%s\" for writing"), |
409 |
CHAR(asChar(fname))); |
CHAR(asChar(fname))); |
410 |
if (!cholmod_write_sparse(f, AS_CHM_SP(x), (CHM_SP)NULL, |
if (!cholmod_write_sparse(f, AS_CHM_SP(Csparse_diagU2N(x)), |
411 |
(char*) NULL, &c)) |
(CHM_SP)NULL, (char*) NULL, &c)) |
412 |
error(_("cholmod_write_sparse returned error code")); |
error(_("cholmod_write_sparse returned error code")); |
413 |
fclose(f); |
fclose(f); |
414 |
return R_NilValue; |
return R_NilValue; |