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_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_Tsparse(SEXP x) |
SEXP Csparse_to_Tsparse(SEXP x) |
42 |
{ |
{ |
|
#ifdef USE_CHOLMOD |
|
43 |
cholmod_sparse *chxs = as_cholmod_sparse(x); |
cholmod_sparse *chxs = as_cholmod_sparse(x); |
44 |
cholmod_triplet *chxt = cholmod_sparse_to_triplet(chxs, &c); |
cholmod_triplet *chxt = cholmod_sparse_to_triplet(chxs, &c); |
45 |
|
|
46 |
free(chxs); |
Free(chxs); |
47 |
return chm_triplet_to_SEXP(chxt, 1); |
return chm_triplet_to_SEXP(chxt, 1); |
|
#else |
|
|
error("General conversion requires CHOLMOD"); |
|
|
return R_NilValue; /* -Wall */ |
|
|
#endif /* USE_CHOLMOD */ |
|
48 |
} |
} |
49 |
|
|
50 |
SEXP Csparse_transpose(SEXP x) |
SEXP Csparse_transpose(SEXP x) |
51 |
{ |
{ |
|
#ifdef USE_CHOLMOD |
|
52 |
cholmod_sparse *chx = as_cholmod_sparse(x); |
cholmod_sparse *chx = as_cholmod_sparse(x); |
53 |
cholmod_sparse *chxt = cholmod_transpose(chx, (int) chx->xtype, &c); |
cholmod_sparse *chxt = cholmod_transpose(chx, (int) chx->xtype, &c); |
54 |
|
|
55 |
free(chx); |
Free(chx); |
56 |
return chm_sparse_to_SEXP(chxt, 1); |
return chm_sparse_to_SEXP(chxt, 1); |
|
#else |
|
|
error("General conversion requires CHOLMOD"); |
|
|
return R_NilValue; /* -Wall */ |
|
|
#endif /* USE_CHOLMOD */ |
|
57 |
} |
} |
58 |
|
|
|
|
|
59 |
SEXP Csparse_Csparse_prod(SEXP a, SEXP b) |
SEXP Csparse_Csparse_prod(SEXP a, SEXP b) |
60 |
{ |
{ |
61 |
#ifdef USE_CHOLMOD |
cholmod_sparse *cha = as_cholmod_sparse(a), |
62 |
cholmod_sparse *cha = as_cholmod_sparse(a), *chb = as_cholmod_sparse(b); |
*chb = as_cholmod_sparse(b); |
63 |
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); |
64 |
|
|
65 |
free(cha); free(chb); |
Free(cha); Free(chb); |
66 |
return chm_sparse_to_SEXP(chc, 1); |
return chm_sparse_to_SEXP(chc, 1); |
|
#else |
|
|
error("General multiplication requires CHOLMOD"); |
|
|
return R_NilValue; /* -Wall */ |
|
|
#endif /* USE_CHOLMOD */ |
|
67 |
} |
} |
68 |
|
|
69 |
SEXP Csparse_dense_prod(SEXP a, SEXP b) |
SEXP Csparse_dense_prod(SEXP a, SEXP b) |
70 |
{ |
{ |
|
#ifdef USE_CHOLMOD |
|
71 |
cholmod_sparse *cha = as_cholmod_sparse(a); |
cholmod_sparse *cha = as_cholmod_sparse(a); |
72 |
cholmod_dense *chb = as_cholmod_dense(b); |
cholmod_dense *chb = as_cholmod_dense(b); |
73 |
cholmod_dense *chc = cholmod_allocate_dense(cha->nrow, chb->ncol, |
cholmod_dense *chc = cholmod_allocate_dense(cha->nrow, chb->ncol, |
75 |
double alpha = 1, beta = 0; |
double alpha = 1, beta = 0; |
76 |
|
|
77 |
cholmod_sdmult(cha, 0, &alpha, &beta, chb, chc, &c); |
cholmod_sdmult(cha, 0, &alpha, &beta, chb, chc, &c); |
78 |
free(cha); free(chb); |
Free(cha); Free(chb); |
79 |
|
return chm_dense_to_SEXP(chc, 1); |
80 |
|
} |
81 |
|
|
82 |
|
SEXP Csparse_dense_crossprod(SEXP a, SEXP b) |
83 |
|
{ |
84 |
|
cholmod_sparse *cha = as_cholmod_sparse(a); |
85 |
|
cholmod_dense *chb = as_cholmod_dense(b); |
86 |
|
cholmod_dense *chc = cholmod_allocate_dense(cha->ncol, chb->ncol, |
87 |
|
cha->ncol, chb->xtype, &c); |
88 |
|
double alpha = 1, beta = 0; |
89 |
|
|
90 |
|
cholmod_sdmult(cha, 1, &alpha, &beta, chb, chc, &c); |
91 |
|
Free(cha); Free(chb); |
92 |
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 */ |
|
93 |
} |
} |
94 |
|
|
95 |
SEXP Csparse_crossprod(SEXP x, SEXP trans, SEXP triplet) |
SEXP Csparse_crossprod(SEXP x, SEXP trans, SEXP triplet) |
96 |
{ |
{ |
|
#ifdef USE_CHOLMOD |
|
97 |
int trip = asLogical(triplet), |
int trip = asLogical(triplet), |
98 |
tr = asLogical(trans); /* gets reversed because _aat is tcrossprod */ |
tr = asLogical(trans); /* gets reversed because _aat is tcrossprod */ |
99 |
cholmod_triplet |
cholmod_triplet |
103 |
: as_cholmod_sparse(x); |
: as_cholmod_sparse(x); |
104 |
|
|
105 |
if (!tr) |
if (!tr) |
106 |
chxt = cholmod_transpose(chx, (int) chx->xtype, &c); |
chxt = cholmod_transpose(chx, chx->xtype, &c); |
107 |
chcp = cholmod_aat((!tr) ? chxt : chx, (int *) NULL, 0, chx->xtype, &c); |
chcp = cholmod_aat((!tr) ? chxt : chx, (int *) NULL, 0, chx->xtype, &c); |
108 |
if(!chcp) |
if(!chcp) |
109 |
error("Csparse_crossprod(): error return from cholmod_aat()"); |
error("Csparse_crossprod(): error return from cholmod_aat()"); |
110 |
|
cholmod_band_inplace(0, chcp->ncol, chcp->xtype, chcp, &c); |
111 |
|
chcp->stype = 1; |
112 |
if (trip) { |
if (trip) { |
113 |
cholmod_free_sparse(&chx, &c); |
cholmod_free_sparse(&chx, &c); |
114 |
free(cht); |
Free(cht); |
115 |
} else { |
} else { |
116 |
free(chx); |
Free(chx); |
117 |
} |
} |
118 |
if (!tr) cholmod_free_sparse(&chxt, &c); |
if (!tr) cholmod_free_sparse(&chxt, &c); |
119 |
return chm_sparse_to_SEXP(chcp, 1); |
return chm_sparse_to_SEXP(chcp, 1); |
|
#else |
|
|
error("General crossproduct requires CHOLMOD"); |
|
|
return R_NilValue; /* -Wall */ |
|
|
#endif /* USE_CHOLMOD */ |
|
120 |
} |
} |
121 |
|
|
122 |
|
SEXP Csparse_horzcat(SEXP x, SEXP y) |
123 |
|
{ |
124 |
|
cholmod_sparse *chx = as_cholmod_sparse(x), |
125 |
|
*chy = as_cholmod_sparse(y), *ans; |
126 |
|
|
127 |
|
ans = cholmod_horzcat(chx, chy, 1, &c); |
128 |
|
Free(chx); Free(chy); |
129 |
|
return chm_sparse_to_SEXP(ans, 1); |
130 |
|
} |
131 |
|
|
132 |
|
SEXP Csparse_vertcat(SEXP x, SEXP y) |
133 |
|
{ |
134 |
|
cholmod_sparse *chx = as_cholmod_sparse(x), |
135 |
|
*chy = as_cholmod_sparse(y), *ans; |
136 |
|
|
137 |
|
ans = cholmod_vertcat(chx, chy, 1, &c); |
138 |
|
Free(chx); Free(chy); |
139 |
|
return chm_sparse_to_SEXP(ans, 1); |
140 |
|
} |
141 |
|
|
142 |
|
SEXP Csparse_band(SEXP x, SEXP k1, SEXP k2) |
143 |
|
{ |
144 |
|
cholmod_sparse *chx = as_cholmod_sparse(x), *ans; |
145 |
|
|
146 |
|
ans = cholmod_band(chx, asInteger(k1), asInteger(k2), chx->xtype, &c); |
147 |
|
Free(chx); |
148 |
|
return chm_sparse_to_SEXP(ans, 1); |
149 |
|
} |