11 int *pdim =
DIM(obj), m = pdim[0], n = pdim[1],
12 r = (m < n) ? m : n, packed =
class[2] ==
'p';
14 char ul =
'\0', ct =
'\0', nu =
'\0';
17 if (
class[1] ==
's' &&
class[0] ==
'z')
24 if (nu !=
'\0' && nu !=
'N') {
30 PROTECT(ans = Rf_allocVector(
kindToType(
class[0]), r));
33 size_t m_ = (size_t) m, n_ = (
size_t) n, r_ = (size_t) r;
37 c##TYPE *pa = c##PTR(ans), *px = c##PTR(x); \
39 c##NAME(copy2)(r_, pa, 1, px, m_ + 1); \
41 c##NAME(copy1)(n_, pa, 1, 0, 0, px, 2 , 1, 0); \
43 c##NAME(copy1)(n_, pa, 1, 0, 0, px, n_, 1, 1); \
52 if (
class[0] ==
'z' &&
class[1] ==
's' && ct ==
'C')
53 zvreal(COMPLEX(ans), NULL, r_);
61 rn = VECTOR_ELT(dn, 0),
62 cn = VECTOR_ELT(dn, 1);
63 if (cn == R_NilValue) {
64 if (
class[1] ==
's' && rn != R_NilValue)
65 Rf_setAttrib(ans, R_NamesSymbol, rn);
68 Rf_setAttrib(ans, R_NamesSymbol, cn);
69 else if (rn != R_NilValue &&
71 Rf_setAttrib(ans, R_NamesSymbol, (r == m) ? rn : cn);
82 int *pdim =
DIM(obj), m = pdim[0], n = pdim[1],
85 char ul =
'\0', ct =
'\0', nu =
'\0';
88 if (
class[1] ==
's' &&
class[0] ==
'z')
95 if (nu !=
'\0' && nu !=
'N') {
99 }
else if (
class[2] !=
'T') {
101 PROTECT(ans = Rf_allocVector(
kindToType(
class[0]), r));
106 int *pp = INTEGER(p), *pi = INTEGER(i), j, k, kend,
107 up = (
class[2] ==
'C') == (ul ==
'U');
112 c##TYPE *pa = c##PTR(ans); \
114 SEXP x = GET_SLOT(obj, Matrix_xSym); \
115 c##TYPE *px = c##PTR(x); \
117 if (class[1] == 'g') \
118 for (j = 0, k = 0; j < r; ++j) { \
125 pa[j] = c##IFELSE_NPATTERN(px[k], c##UNIT); \
131 for (j = 0, k = 0; j < r; ++j) { \
133 pa[j] = (k < kend && pi[(up) ? kend - 1 : k] == j) \
134 ? c##IFELSE_NPATTERN(px[(up) ? kend - 1 : k], c##UNIT) \
146 if (
class[0] ==
'z' &&
class[1] ==
's' && ct ==
'C')
147 zvreal(COMPLEX(ans), NULL, (
size_t) r);
151 PROTECT(ans = Rf_allocVector(
kindToType(
class[0]), r));
155 int *pi = INTEGER(i), *pj = INTEGER(j);
156 R_xlen_t k, kend = XLENGTH(i);
160 c##TYPE *pa = c##PTR(ans); \
161 memset(pa, 0, sizeof(c##TYPE) * (size_t) r); \
163 SEXP x = GET_SLOT(obj, Matrix_xSym); \
164 c##TYPE *px = c##PTR(x); \
166 for (k = 0; k < kend; ++k) \
167 if (pi[k] == pj[k]) \
168 c##INCREMENT_IDEN(pa[pi[k]], px[k]); \
177 if (
class[0] ==
'z' &&
class[1] ==
's' && ct ==
'C')
178 zvreal(COMPLEX(ans), NULL, (
size_t) r);
185 SEXP dn = PROTECT(
DIMNAMES(obj, 0)),
186 rn = VECTOR_ELT(dn, 0),
187 cn = VECTOR_ELT(dn, 1);
188 if (cn == R_NilValue) {
189 if (
class[1] ==
's' && rn != R_NilValue)
190 Rf_setAttrib(ans, R_NamesSymbol, rn);
193 Rf_setAttrib(ans, R_NamesSymbol, cn);
194 else if (rn != R_NilValue &&
196 Rf_setAttrib(ans, R_NamesSymbol, (r == m) ? rn : cn);
229 int *pdim =
DIM(from), m = pdim[0], n = pdim[1],
230 r = (m < n) ? m : n, packed =
class[2] ==
'p';
234 char ul =
'\0', ct =
'\0';
235 if (
class[1] !=
'g' && (ul =
UPLO(from)) !=
'U')
237 if (
class[1] ==
's' &&
class[0] ==
'z' && (ct =
TRANS(from)) !=
'C')
247 size_t m_ = (size_t) m, n_ = (
size_t) n, r_ = (size_t) r,
248 d_ = (LENGTH(value) == r) ? 1 : 0;
252 c##TYPE *px = c##PTR(x), *pv = c##PTR(value); \
254 c##NAME(copy2)(r_, px, m_ + 1, pv, d_); \
255 else if (ul == 'U') \
256 c##NAME(copy1)(n_, px, 2 , 1, 0, pv, d_, 0, 0); \
258 c##NAME(copy1)(n_, px, n_, 1, 1, pv, d_, 0, 0); \
275 int *pdim =
DIM(from), m = pdim[0], n = pdim[1],
280 char ul =
'\0', ct =
'\0', nu =
'\0';
281 if (
class[1] !=
'g' && (ul =
UPLO(from)) !=
'U')
283 if (
class[1] ==
's' &&
class[0] ==
'z' && (ct =
TRANS(from)) !=
'C')
285 if (
class[1] ==
't' && (nu =
DIAG(from)) !=
'N')
290 if (
class[2] !=
'T') {
298 p1 = PROTECT(Rf_allocVector(INTSXP, XLENGTH(p0)));
299 int *pp0 = INTEGER(p0), *pp1 = INTEGER(p1), *pi0 = INTEGER(i0),
300 j, k, kend, nd0 = 0, nd1 = 0,
301 up = (
class[2] ==
'C') == (ul ==
'U');
305 if (
class[1] ==
'g') {
306 for (j = 0, k = 0; j < r; ++j) {
319 for (j = r; j < n; ++j)
320 pp1[j] = pp0[j] - nd0;
322 else if (
class[1] ==
's' || nu ==
'N')
323 for (j = 0, k = 0; j < n; ++j) {
325 if (k < kend && pi0[(up) ? kend - 1 : k] == j)
331 for (j = 0; j < n; ++j)
336 c##TYPE *pv = c##PTR(value); \
337 if (LENGTH(value) == r) { \
339 for (j = 0; j < r; ++j) { \
340 if (c##NOT_ZERO(pv[j])) \
344 for (j = r; j < n; ++j) \
346 } else if (c##NOT_ZERO(pv[0])) { \
348 for (j = 0; j < r; ++j) \
350 for (j = r; j < n; ++j) \
359 if (nd1 - nd0 > INT_MAX - pp0[n - 1])
360 Rf_error(
_(
"%s cannot exceed %s"),
"p[length(p)]",
"2^31-1");
362 SEXP i1 = PROTECT(Rf_allocVector(INTSXP, pp1[n - 1]));
363 int *pi1 = INTEGER(i1);
369 SEXP x0 = PROTECT(GET_SLOT(from, Matrix_xSym)), \
370 x1 = PROTECT(Rf_allocVector(c##TYPESXP, pp1[n - 1])); \
371 c##TYPE *px0 = c##PTR(x0), *px1 = c##PTR(x1); \
372 SET_SLOT(to, Matrix_xSym, x1); \
375 c##TYPE *pv = c##PTR(value); \
376 for (j = 0, k = 0; j < r; ++j) { \
378 while (k < kend && pi0[k] < j) { \
385 if (k < kend && pi0[k] == j) \
387 if (mode > 1 || (mode > 0 && c##NOT_ZERO(pv[j]))) { \
390 *(px1++) = pv[(mode > 1) ? 0 : j]; \
401 for (j = r; j < n; ++j) { \
423 int *pi0 = INTEGER(i0), *pj0 = INTEGER(j0), j;
424 R_xlen_t k, nd0 = 0, nd1 = 0, nnz0 = XLENGTH(i0), nnz1 = nnz0;
426 if (nu ==
'\0' || nu ==
'N')
427 for (k = 0; k < nnz0; ++k)
428 if (pi0[k] == pj0[k])
433 c##TYPE *pv = c##PTR(value); \
434 if (LENGTH(value) == r) { \
436 for (j = 0; j < r; ++j) \
437 if (c##NOT_ZERO(pv[j])) \
439 } else if (c##NOT_ZERO(pv[0])) { \
449 if (nd1 - nd0 > R_XLEN_T_MAX - nnz0)
450 Rf_error(
_(
"%s cannot exceed %s"),
"length(i)",
"R_XLEN_T_MAX");
453 SEXP i1 = PROTECT(Rf_allocVector(INTSXP, nnz1)),
454 j1 = PROTECT(Rf_allocVector(INTSXP, nnz1));
455 int *pi1 = INTEGER(i1), *pj1 = INTEGER(j1);
462 SEXP x0 = PROTECT(GET_SLOT(from, Matrix_xSym)), \
463 x1 = PROTECT(Rf_allocVector(c##TYPESXP, nnz1)); \
464 c##TYPE *px0 = c##PTR(x0), *px1 = c##PTR(x1); \
465 SET_SLOT(to, Matrix_xSym, x1); \
468 c##TYPE *pv = c##PTR(value); \
469 for (k = 0; k < nnz0; ++k) { \
470 if (pi0[k] != pj0[k]) { \
479 for (j = 0; j < r; ++j) { \
480 *(pi1++) = *(pj1++) = j; \
486 for (j = 0; j < r; ++j) { \
487 if (c##NOT_ZERO(pv[j])) { \
488 *(pi1++) = *(pj1++) = j; \
520 Rf_error(
_(
"replacement diagonal has incompatible type \"%s\""),
525 int *pdim =
DIM(s_from), m = pdim[0], n = pdim[1],
527 if (XLENGTH(s_value) != 1 && XLENGTH(s_value) != r)
528 Rf_error(
_(
"replacement diagonal has wrong length"));
534 if (
class[1] ==
's' &&
class[0] ==
'z' && tv == tx &&
535 TRANS(s_from) ==
'C') {
542 PROTECT(s_value = Rf_coerceVector(s_value, tx));
546#ifndef MATRIX_ENABLE_IMATRIX
549 PROTECT(s_value = Rf_coerceVector(s_value, REALSXP));
554#ifndef MATRIX_ENABLE_IMATRIX
578 Rf_error(
_(
"replacement diagonal has incompatible type \"%s\""),
583 int *pdim =
DIM(s_from), m = pdim[0], n = pdim[1],
585 if (XLENGTH(s_value) != 1 && XLENGTH(s_value) != r)
586 Rf_error(
_(
"replacement diagonal has wrong length"));
591 if (
class[1] ==
's' &&
class[0] ==
'z' && tv == tx &&
592 TRANS(s_from) ==
'C') {
598 PROTECT(s_value = Rf_coerceVector(s_value, tx));
602#ifndef MATRIX_ENABLE_IMATRIX
605 PROTECT(s_value = Rf_coerceVector(s_value, REALSXP));
610#ifndef MATRIX_ENABLE_IMATRIX
623 if (
class[1] !=
't' ||
DIAG(from) ==
'N')
625 SEXP value = PROTECT(Rf_ScalarLogical(1)),
636 if (
class[1] !=
't' ||
DIAG(from) !=
'N')
639 int n =
DIM(from)[1];
642 if (
UPLO(from) !=
'U')
645 else if (
UPLO(from) ==
'U')
669 int n =
DIM(s_trf)[1];
670 char ul =
UPLO(s_trf);
671 SEXP ans = Rf_allocVector(
TYPEOF(x), n);
673 int j, root = Rf_asLogical(s_root),
674 packed = XLENGTH(x) != (int_fast64_t) n * n;
675 R_xlen_t dx = (!packed) ? (R_xlen_t) n + 1 : (ul ==
'U') ? 1 : (R_xlen_t) n + 1,
676 ddx = (!packed) ? 0 : (ul ==
'U') ? 1 : -1;
677 if (
TYPEOF(x) == CPLXSXP) {
678 Rcomplex *pa = COMPLEX(ans), *px = COMPLEX(x);
679 for (j = 0; j < n; ++j) {
688 double *pa = REAL(ans), *px = REAL(x);
689 for (j = 0; j < n; ++j) {
704 cholmod_factor *L =
M2CHF(s_trf, 1);
706 SEXP ans = Rf_allocVector((L->xtype == CHOLMOD_COMPLEX) ? CPLXSXP : REALSXP, n);
708 int j, root = Rf_asLogical(s_root);
711 nsuper = (int) L->nsuper,
712 *psuper = (
int *) L->super,
713 *ppi = (
int *) L->pi,
714 *ppx = (
int *) L->px;
716 if (L->xtype == CHOLMOD_COMPLEX) {
717 Rcomplex *pa = COMPLEX(ans), *px = (Rcomplex *) L->x, *py;
718 for (k = 0; k < nsuper; ++k) {
719 nc = psuper[k + 1] - psuper[k];
720 nr1a = (R_xlen_t) (ppi[k + 1] - ppi[k]) + 1;
722 for (j = 0; j < nc; ++j) {
732 double *pa = REAL(ans), *px = (
double *) L->x, *py;
733 for (k = 0; k < nsuper; ++k) {
734 nc = psuper[k + 1] - psuper[k];
735 nr1a = (R_xlen_t) (ppi[k + 1] - ppi[k]) + 1;
737 for (j = 0; j < nc; ++j) {
747 int *pp = (
int *) L->p;
748 if (L->xtype == CHOLMOD_COMPLEX) {
749 Rcomplex *pa = COMPLEX(ans), *px = (Rcomplex *) L->x;
751 for (j = 0; j < n; ++j) {
752 pa[j].r = px[pp[j]].r;
758 for (j = 0; j < n; ++j) {
759 pa[j].r = px[pp[j]].r;
762 pa[j].r = (pa[j].r >= 0.0) ? sqrt(pa[j].r) : R_NaN;
766 double *pa = REAL(ans), *px = (
double *) L->x;
768 for (j = 0; j < n; ++j) {
774 for (j = 0; j < n; ++j) {
777 pa[j] = (pa[j] >= 0.0) ? sqrt(pa[j]) : R_NaN;
SEXP dense_as_kind(SEXP, const char *, char, int)
SEXP sparse_as_general(SEXP, const char *)
SEXP sparse_as_kind(SEXP, const char *, char)
#define SWITCH5(c, template)
#define SWITCH4(c, template)
#define SWAP(a, b, t, op)
SEXP allocUnit(SEXPTYPE, R_xlen_t)
const char * valid_dense[]
#define SET_DIMNAMES(x, mode, value)
SEXP duplicateVector(SEXP)
char typeToKind(SEXPTYPE)
const char * Matrix_class(SEXP, const char **, int, const char *)
SEXPTYPE kindToType(char)
#define DIMNAMES(x, mode)
#define GET_SLOT(x, name)
int equalString(SEXP, SEXP, R_xlen_t)
SEXP newObject(const char *)
const char * valid_sparse[]
#define SET_SLOT(x, name, value)
#define VALID_LOGIC2(s, d)
SEXP sparse_band(SEXP from, const char *class, int a, int b)
cholmod_factor * M2CHF(SEXP obj, int values)
SEXP dense_as_general(SEXP from, const char *class, int new)
SEXP R_sparse_diag_set(SEXP s_from, SEXP s_value)
SEXP R_dense_diag_get(SEXP s_obj, SEXP s_names)
SEXP R_dense_diag_set(SEXP s_from, SEXP s_value)
SEXP sparse_diag_N2U(SEXP from, const char *class)
SEXP dense_diag_set(SEXP from, const char *class, SEXP value, int new)
SEXP sparse_diag_U2N(SEXP from, const char *class)
SEXP dense_diag_get(SEXP obj, const char *class, int names)
SEXP R_sparse_diag_U2N(SEXP s_from)
SEXP sparseCholesky_diag_get(SEXP s_trf, SEXP s_root)
SEXP R_sparse_diag_N2U(SEXP s_from)
SEXP sparse_diag_set(SEXP from, const char *class, SEXP value)
SEXP denseCholesky_diag_get(SEXP s_trf, SEXP s_root)
SEXP R_sparse_diag_get(SEXP s_obj, SEXP s_names)
SEXP sparse_diag_get(SEXP obj, const char *class, int names)
void zvreal(Rcomplex *x, const Rcomplex *y, size_t n)