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