39cholmod_factor *
M2CHF(SEXP obj,
int values)
41 static const char *valid[] = {
42 "nsimplicialCholesky",
"nsupernodalCholesky",
43 "dsimplicialCholesky",
"dsupernodalCholesky",
44 "zsimplicialCholesky",
"zsupernodalCholesky",
"" };
45 const char *
class =
Matrix_class(obj, valid, -1, __func__);
46 cholmod_factor *L = (cholmod_factor *) R_alloc(1,
sizeof(cholmod_factor));
47 memset(L, 0,
sizeof(cholmod_factor));
48 values = values && (
class[0] ==
'd' ||
class[0] ==
'z');
53 L->ordering = INTEGER(ordering)[0];
54 L->is_super =
class[2] ==
'u';
55 L->n = (size_t) INTEGER(dim)[0];
57 if (L->ordering != CHOLMOD_NATURAL)
58 L->Perm = INTEGER(perm);
63 int n = (int) L->n, *Perm = (
int *) R_alloc(L->n,
sizeof(
int));
64 for (
int j = 0; j < n; ++j)
68 L->ColCount = INTEGER(colcount);
76 L->nsuper = (size_t) (LENGTH(super) - 1);
77 L->ssize = (size_t) INTEGER(pi)[L->nsuper];
78 L->xsize = (size_t) INTEGER(px)[L->nsuper];
79 L->maxcsize = (size_t) INTEGER(maxcsize)[0];
80 L->maxesize = (size_t) INTEGER(maxesize)[0];
81 L->super = INTEGER(super);
97 L->nzmax = (size_t) INTEGER(p)[L->n];
101 L->next = INTEGER(next);
102 L->prev = INTEGER(prev);
103 L->is_ll = LOGICAL(is_ll)[0] != 0;
104 L->is_monotonic = LOGICAL(is_monotonic)[0] != 0;
108 L->itype = CHOLMOD_INT;
109 L->xtype = CHOLMOD_PATTERN;
110 L->dtype = CHOLMOD_DOUBLE;
113 L->minor = (size_t) INTEGER(minor)[0];
118 L->xtype = CHOLMOD_REAL;
122 L->xtype = CHOLMOD_COMPLEX;
132cholmod_sparse *
M2CHS(SEXP obj,
int values)
135 cholmod_sparse *A = (cholmod_sparse *) R_alloc(1,
sizeof(cholmod_sparse));
136 memset(A, 0,
sizeof(cholmod_sparse));
137 values = values && (
class[0] ==
'd' ||
class[0] ==
'z');
138 int mg = (
class[2] ==
'C') ? 1 : 0;
142 A->nrow = (size_t) INTEGER(dim)[(mg == 1) ? 0 : 1];
143 A->ncol = (size_t) INTEGER(dim)[(mg == 1) ? 1 : 0];
144 A->nzmax = (size_t) INTEGER(p)[A->ncol];
148 A->itype = CHOLMOD_INT;
149 A->xtype = CHOLMOD_PATTERN;
150 A->dtype = CHOLMOD_DOUBLE;
153 if (
class[1] ==
's') {
155 A->stype = ((*CHAR(STRING_ELT(uplo, 0)) ==
'U') == (mg == 1)) ? 1 : -1;
162 A->xtype = CHOLMOD_REAL;
166 A->xtype = CHOLMOD_COMPLEX;
176cholmod_dense *
M2CHD(SEXP obj,
char trans)
178 static const char *valid[] = {
"dgeMatrix",
"zgeMatrix",
"" };
179 const char *
class =
Matrix_class(obj, valid, 0, __func__);
180 cholmod_dense *A = (cholmod_dense *) R_alloc(1,
sizeof(cholmod_dense));
181 memset(A, 0,
sizeof(cholmod_dense));
184 size_t m = (size_t) INTEGER(dim)[0], n = (size_t) INTEGER(dim)[1];
185 A->nrow = ((trans ==
'N') ? m : n);
186 A->ncol = ((trans ==
'N') ? n : m);
187 A->nzmax = A->nrow * A->ncol;
189 A->dtype = CHOLMOD_DOUBLE;
193 double *px = REAL(x);
195 double *py = (
double *) R_alloc(A->nzmax,
sizeof(
double));
200 A->xtype = CHOLMOD_REAL;
205 Rcomplex *px = COMPLEX(x);
207 Rcomplex *py = (Rcomplex *) R_alloc(A->nzmax,
sizeof(Rcomplex));
212 A->xtype = CHOLMOD_COMPLEX;
222SEXP
CHF2M(cholmod_factor *L,
int values)
225 (L->xtype == CHOLMOD_REAL || L->xtype == CHOLMOD_COMPLEX);
226 if (L->itype != CHOLMOD_INT)
228 if (values && L->dtype != CHOLMOD_DOUBLE)
231 return errorChar(
_(
"dimensions cannot exceed %s"),
"2^31-1");
233 if (L->maxcsize > INT_MAX)
234 return errorChar(
_(
"'%s' would overflow type \"%s\""),
235 "maxcsize",
"integer");
238 return errorChar(
_(
"n+1 would overflow type \"%s\""),
241 if (L->minor < L->n) {
243 return errorChar(
_(
"leading principal minor of order %d is not positive"),
246 return errorChar(
_(
"leading principal minor of order %d is zero"),
249 char class[] =
"...........Cholesky";
250 class[0] = (!values) ?
'n' : ((L->xtype == CHOLMOD_REAL) ?
'd' :
'z');
251 memcpy(
class + 1, (L->is_super) ?
"supernodal" :
"simplicial", 10);
255 INTEGER(ordering)[0] = L->ordering;
256 INTEGER(dim)[0] = INTEGER(dim)[1] = (int) L->n;
257 if (L->ordering != CHOLMOD_NATURAL) {
258 SEXP perm = PROTECT(Rf_allocVector(INTSXP, (R_xlen_t) L->n));
259 memcpy(INTEGER(perm), L->Perm,
sizeof(
int) * L->n);
263 SEXP colcount = PROTECT(Rf_allocVector(INTSXP, (R_xlen_t) L->n));
264 memcpy(INTEGER(colcount), L->ColCount,
sizeof(
int) * L->n);
270 super = PROTECT(Rf_allocVector(INTSXP, (R_xlen_t) (L->nsuper + 1))),
271 pi = PROTECT(Rf_allocVector(INTSXP, (R_xlen_t) (L->nsuper + 1))),
272 px = PROTECT(Rf_allocVector(INTSXP, (R_xlen_t) (L->nsuper + 1))),
273 s = PROTECT(Rf_allocVector(INTSXP, (R_xlen_t) L->ssize));
274 INTEGER(maxcsize)[0] = (int) L->maxcsize;
275 INTEGER(maxesize)[0] = (int) L->maxesize;
276 memcpy(INTEGER(super), L->super,
sizeof(
int) * (L->nsuper + 1));
277 memcpy(INTEGER(pi), L->pi,
sizeof(
int) * (L->nsuper + 1));
278 memcpy(INTEGER(px), L->px,
sizeof(
int) * (L->nsuper + 1));
279 memcpy(INTEGER(s), L->s,
sizeof(
int) * L->ssize);
287 SEXP p = PROTECT(Rf_allocVector(INTSXP, (R_xlen_t) (L->n + 1))),
288 i = PROTECT(Rf_allocVector(INTSXP, (R_xlen_t) L->nzmax)),
289 nz = PROTECT(Rf_allocVector(INTSXP, (R_xlen_t) L->n)),
290 next = PROTECT(Rf_allocVector(INTSXP, (R_xlen_t) (L->n + 2))),
291 prev = PROTECT(Rf_allocVector(INTSXP, (R_xlen_t) (L->n + 2))),
294 memcpy(INTEGER(p), L->p,
sizeof(
int) * (L->n + 1));
295 memcpy(INTEGER(i), L->i,
sizeof(
int) * L->nzmax);
296 memcpy(INTEGER(nz), L->nz,
sizeof(
int) * L->n);
297 memcpy(INTEGER(next), L->next,
sizeof(
int) * (L->n + 2));
298 memcpy(INTEGER(prev), L->prev,
sizeof(
int) * (L->n + 2));
299 LOGICAL(is_ll)[0] = L->is_ll != 0;
300 LOGICAL(is_monotonic)[0] = L->is_monotonic != 0;
311 INTEGER(minor)[0] = (int) L->minor;
313 size_t nnz = (L->is_super) ? L->xsize : L->nzmax;
314 if (L->xtype == CHOLMOD_REAL) {
315 PROTECT(x = Rf_allocVector(REALSXP, (R_xlen_t) nnz));
316 memcpy(REAL(x), L->x,
sizeof(
double) * nnz);
318 PROTECT(x = Rf_allocVector(CPLXSXP, (R_xlen_t) nnz));
319 memcpy(COMPLEX(x), L->x,
sizeof(Rcomplex) * nnz);
328SEXP
CHS2M(cholmod_sparse *A,
int values,
char shape)
330 cholmod_sparse *A_ = A;
332 (A->xtype == CHOLMOD_REAL || A->xtype == CHOLMOD_COMPLEX);
333 if (A->itype != CHOLMOD_INT)
335 if (values && A->dtype != CHOLMOD_DOUBLE)
337 if (A->nrow > INT_MAX || A->ncol > INT_MAX)
338 return errorChar(
_(
"dimensions cannot exceed %s"),
"2^31-1");
341 if (!A->packed || A->stype != 0)
342 A = cholmod_copy(A, A->stype, 2, &
c);
343 char class[] =
"..CMatrix";
344 class[0] = (!values) ?
'n' : ((A->xtype == CHOLMOD_REAL) ?
'd' :
'z');
346 int nnz = ((
int *) A->p)[A->ncol];
349 p = PROTECT(Rf_allocVector(INTSXP, (R_xlen_t) (A->ncol + 1))),
350 i = PROTECT(Rf_allocVector(INTSXP, (R_xlen_t) nnz));
351 INTEGER(dim)[0] = (int) A->nrow;
352 INTEGER(dim)[1] = (int) A->ncol;
353 memcpy(INTEGER(p), A->p,
sizeof(
int) * (A->ncol + 1));
354 memcpy(INTEGER(i), A->i,
sizeof(
int) * (
size_t) nnz);
359 if (A->xtype == CHOLMOD_REAL) {
360 PROTECT(x = Rf_allocVector(REALSXP, nnz));
361 memcpy(REAL(x), A->x,
sizeof(
double) * (
size_t) nnz);
363 PROTECT(x = Rf_allocVector(CPLXSXP, nnz));
364 memcpy(COMPLEX(x), A->x,
sizeof(Rcomplex) * (
size_t) nnz);
370 cholmod_free_sparse(&A, &
c);
375SEXP
CHD2M(cholmod_dense *A,
char trans,
char shape)
377 if (A->xtype != CHOLMOD_REAL && A->xtype != CHOLMOD_COMPLEX)
379 if (A->dtype != CHOLMOD_DOUBLE)
382 return errorChar(
_(
"leading dimension not equal to number of rows"));
383 if (A->nrow > INT_MAX || A->ncol > INT_MAX)
384 return errorChar(
_(
"dimensions cannot exceed %s"),
"2^31-1");
385 size_t m = A->nrow, n = A->ncol;
386 if (m * n > R_XLEN_T_MAX)
387 return errorChar(
_(
"attempt to allocate vector of length exceeding %s"),
389 char class[] =
"...Matrix";
390 class[0] = (A->xtype == CHOLMOD_REAL) ?
'd' :
'z';
392 class[2] = (shape ==
'g')
393 ?
'e' : ((shape ==
's') ?
'y' : ((shape ==
'p') ?
'o' :
'r'));
396 INTEGER(dim)[0] = (int) ((trans !=
'N') ? n : m);
397 INTEGER(dim)[1] = (int) ((trans !=
'N') ? m : n);
399 if (A->xtype == CHOLMOD_REAL) {
400 PROTECT(x = Rf_allocVector(REALSXP, (R_xlen_t) (m * n)));
401 double *px = REAL(x), *py = (
double *) A->x;
404 PROTECT(x = Rf_allocVector(CPLXSXP, (R_xlen_t) (m * n)));
405 Rcomplex *px = COMPLEX(x), *py = (Rcomplex *) A->x;