12 if (*(p++) == NA_INTEGER)
19 if (TYPEOF(s) == INTSXP) \
25#define iOOB(s, t, mn) (s == NA_INTEGER || (t = s - 1) >= mn)
26#define dOOB(s, t, mn) (ISNAN(s) || s >= 0x1.0p+63 || (t = (int_fast64_t) s - 1) >= mn)
34#define nCAST(x) (x != 0)
39 R_xlen_t slen = XLENGTH(s);
40 SEXP ans = Rf_allocVector(
kindToType(
class[0]), slen);
45 int *pdim =
DIM(obj), m = pdim[0], n = pdim[1];
46 int_fast64_t mn = (int_fast64_t) m * n;
48 int packed =
class[2] ==
'p';
49 char ul =
'\0', ct =
'\0', nu =
'\0';
52 if (
class[1] ==
's' &&
class[0] ==
'z')
59 int_fast64_t b, i, j, k;
61 int ge =
class[1] ==
'g', sy =
class[1] ==
's', he = sy && ct ==
'C',
62 un =
class[1] ==
't' && nu !=
'N', up = ul ==
'U';
66 d##TYPE *ps = d##PTR(s); \
67 c##TYPE *pa = c##PTR(ans); \
68 c##TYPE *px = c##PTR(x); \
69 for (l = 0; l < slen; ++l) { \
70 if (d##OOB(ps[l], k, mn)) { \
75 c##ASSIGN_IDEN(pa[l], c##CAST(px[k])); \
80 b = (up) ? j - i : i - j; \
98 k = DENSE_INDEX_U(i, j, m); \
100 k = DENSE_INDEX_U(j, i, m); \
103 k = DENSE_INDEX_L(i, j, m); \
105 k = DENSE_INDEX_L(j, i, m); \
108 c##ASSIGN_IDEN (pa[l], c##CAST(px[k])); \
110 c##ASSIGN_PROJ_REAL(pa[l], c##CAST(px[k])); \
112 c##ASSIGN_CONJ (pa[l], c##CAST(px[k])); \
130 R_xlen_t slen = XLENGTH(s);
131 SEXP ans = Rf_allocVector(
kindToType(
class[0]), slen);
136 if (
class[2] ==
'T') {
143 int *pdim =
DIM(obj), m = pdim[0], n = pdim[1];
144 int_fast64_t mn = (int_fast64_t) m * n;
146 char ul =
'\0', ct =
'\0', nu =
'\0';
149 if (
class[1] ==
's' &&
class[0] ==
'z')
157 int *pp = INTEGER(p_), *pi = INTEGER(i_), j_, k_, kend_;
160 int *po_i = (
TYPEOF(o) == INTSXP) ? INTEGER(o) : NULL;
161 double *po_d = (
TYPEOF(o) == REALSXP) ? REAL(o) : NULL;
167 int byrow =
class[2] ==
'R',
168 sy =
class[1] ==
's', he = sy && ct ==
'C',
169 un =
class[1] ==
't' && nu !=
'N', up = (!byrow) == (ul ==
'U');
172 (po_i) ? (R_xlen_t) po_i[l] - 1 : (po_d) ? (R_xlen_t) po_d[l] - 1 : l
180 if (d##OOB(ps[l_], k, mn)) \
191 if (sy && (b = (up) ? j - i : i - j) < 0) \
198#define SUB1__(d, c) \
200 d##TYPE *ps = d##PTR(s); \
201 c##TYPE *pa = c##PTR(ans); \
203 SEXP x = GET_SLOT(obj, Matrix_xSym); \
204 c##TYPE *px = c##PTR(x); \
211 while (k_ < kend_ && j == j_) { \
216 pa[l_] = (un && i == j) ? c##UNIT : c##ZERO; \
217 else if (!he || b > 0) \
218 c##ASSIGN_IDEN (pa[l_], c##CAST(px[k_])); \
220 c##ASSIGN_PROJ_REAL(pa[l_], c##CAST(px[k_])); \
222 c##ASSIGN_CONJ (pa[l_], c##CAST(px[k_])); \
227 pa[l_] = (un && i == j) ? c##UNIT : c##ZERO; \
249#define nCAST(x) (x != 0)
254 R_xlen_t slen = XLENGTH(s);
255 SEXP ans = Rf_allocVector(
kindToType(
class[0]), slen);
261 int_fast64_t nn = (int_fast64_t) n * n, n1a = (int_fast64_t) n + 1;
263 int un =
DIAG(obj) !=
'N';
270#define SUB1__(d, c) \
272 d##TYPE *ps = d##PTR(s); \
273 c##TYPE *pa = c##PTR(ans); \
274 c##TYPE *px = c##PTR(x); \
275 for (l = 0; l < slen; ++l) { \
276 if (d##OOB(ps[l], k, nn)) \
283 pa[l] = c##CAST(px[k / n1a]); \
298 R_xlen_t slen = XLENGTH(s);
299 SEXP ans = Rf_allocVector(LGLSXP, slen);
303 int *pa = LOGICAL(ans);
305 int *pdim =
DIM(obj), m = pdim[0], n = pdim[1];
306 int_fast64_t mn = (int_fast64_t) m * n;
309 int *pperm = INTEGER(perm), mg =
MARGIN(obj);
314#define SUB1__(d, c) \
316 d##TYPE *ps = d##PTR(s); \
317 for (l = 0; l < slen; ++l) { \
318 if (d##OOB(ps[l], k, mn)) \
319 pa[l] = NA_LOGICAL; \
321 pa[l] = k / m == pperm[k % m] - 1; \
323 pa[l] = k % m == pperm[k / m] - 1; \
374#define nCAST(x) (x != 0)
379 int slen = (int) (XLENGTH(s) / 2);
380 SEXP ans = Rf_allocVector(
kindToType(
class[0]), slen);
384 int m =
DIM(obj)[0], *ps0 = INTEGER(s), *ps1 = ps0 + slen;
386 int packed =
class[2] ==
'p';
387 char ul =
'\0', ct =
'\0', nu =
'\0';
390 if (
class[1] ==
's' &&
class[0] ==
'z')
397 int_fast64_t b, i, j, k;
399 int ge =
class[1] ==
'g', sy =
class[1] ==
's', he = sy && ct ==
'C',
400 un =
class[1] ==
't' && nu !=
'N', up = ul ==
'U';
404 c##TYPE *pa = c##PTR(ans); \
405 c##TYPE *px = c##PTR(x); \
406 for (l = 0; l < slen; ++l) { \
407 if (ps0[l] == NA_INTEGER || ps1[l] == NA_INTEGER) { \
411 i = (int_fast64_t) ps0[l] - 1; \
412 j = (int_fast64_t) ps1[l] - 1; \
414 c##ASSIGN_IDEN(pa[l], c##CAST(px[DENSE_INDEX_N(i, j, m)])); \
417 b = (up) ? j - i : i - j; \
423 if (un && b == 0) { \
435 k = DENSE_INDEX_U(i, j, m); \
437 k = DENSE_INDEX_U(j, i, m); \
440 k = DENSE_INDEX_L(i, j, m); \
442 k = DENSE_INDEX_L(j, i, m); \
445 c##ASSIGN_IDEN (pa[l], c##CAST(px[k])); \
447 c##ASSIGN_PROJ_REAL(pa[l], c##CAST(px[k])); \
449 c##ASSIGN_CONJ (pa[l], c##CAST(px[k])); \
467 int slen = (int) (XLENGTH(s) / 2);
468 SEXP ans = Rf_allocVector(
kindToType(
class[0]), slen);
472 int *ps0 = INTEGER(s), *ps1 = ps0 + slen;
474 if (
class[2] ==
'T') {
481 char ul =
'\0', ct =
'\0', nu =
'\0';
484 if (
class[1] ==
's' &&
class[0] ==
'z')
492 int *pp = INTEGER(p_), *pi = INTEGER(i_), j_, k_, kend_;
495 int *po_i = (
TYPEOF(o) == INTSXP) ? INTEGER(o) : NULL;
496 double *po_d = (
TYPEOF(o) == REALSXP) ? REAL(o) : NULL;
501 int byrow =
class[2] ==
'R',
502 sy =
class[1] ==
's', he = sy && ct ==
'C',
503 un =
class[1] ==
't' && nu !=
'N', up = (!byrow) == (ul ==
'U');
506 (po_i) ? (int) po_i[l] - 1 : (po_d) ? (int) po_d[l] - 1 : l
514 if (ps0[l_] == NA_INTEGER || ps1[l_] == NA_INTEGER) \
525 if (sy && (b = (up) ? j - i : i - j) < 0) \
534 c##TYPE *pa = c##PTR(ans); \
536 SEXP x = GET_SLOT(obj, Matrix_xSym); \
537 c##TYPE *px = c##PTR(x); \
544 while (k_ < kend_ && j == j_) { \
549 pa[l_] = (un && i == j) ? c##UNIT : c##ZERO; \
550 else if (!he || b > 0) \
551 c##ASSIGN_IDEN (pa[l_], c##CAST(px[k_])); \
553 c##ASSIGN_PROJ_REAL(pa[l_], c##CAST(px[k_])); \
555 c##ASSIGN_CONJ (pa[l_], c##CAST(px[k_])); \
560 pa[l_] = (un && i == j) ? c##UNIT : c##ZERO; \
582#define nCAST(x) (x != 0)
587 int slen = (int) (XLENGTH(s) / 2);
588 SEXP ans = Rf_allocVector(
kindToType(
class[0]), slen);
592 int *ps0 = INTEGER(s), *ps1 = ps0 + slen;
594 int un =
DIAG(obj) !=
'N';
601 c##TYPE *pa = c##PTR(ans); \
602 c##TYPE *px = c##PTR(x); \
603 for (l = 0; l < slen; ++l) { \
604 if (ps0[l] == NA_INTEGER || ps1[l] == NA_INTEGER) \
606 else if (ps0[l] != ps1[l]) \
611 pa[l] = c##CAST(px[ps0[l] - 1]); \
626 int slen = (int) (XLENGTH(s) / 2);
627 SEXP ans = Rf_allocVector(LGLSXP, slen);
631 int *ps0 = INTEGER(s), *ps1 = ps0 + slen, *pa = LOGICAL(ans);
634 int *pperm = INTEGER(perm), mg =
MARGIN(obj);
638 for (l = 0; l < slen; ++l) {
639 if (ps0[l] == NA_INTEGER || ps1[l] == NA_INTEGER)
642 pa[l] = ps1[l] == pperm[ps0[l]] - 1;
644 pa[l] = ps0[l] == pperm[ps1[l]] - 1;
688int stay_sy(
int *pi,
int ni,
int *pj,
int nj,
int n,
char uplo,
int checkNA)
690 if (!pi || !pj || ni != nj ||
691 memcmp(pi, pj,
sizeof(
int) * (
size_t) ni) != 0)
693 int ki, r = (uplo ==
'U') ? 1 : -1;
694 if (checkNA &&
anyNA(pi, ni))
700 else if (pi[0] < pi[1]) {
701 for (ki = 2; ki < ni; ++ki)
702 if (pi[ki - 1] >= pi[ki])
706 for (ki = 2; ki < ni; ++ki)
707 if (pi[ki - 1] <= pi[ki])
717int stay_tr(
int *pi,
int ni,
int *pj,
int nj,
int n,
char uplo,
int checkNA)
719 if (!pi || !pj || ni != nj)
721 int ki, kj, iden = memcmp(pi, pj,
sizeof(
int) * (
size_t) ni) == 0,
722 r = (uplo ==
'U') ? 1 : -1;
723 if (checkNA && (
anyNA(pi, ni) || (!iden &&
anyNA(pj, ni))))
730 else if (pi[0] < pi[1]) {
731 for (ki = 2; ki < ni; ++ki)
732 if (pi[ki - 1] >= pi[ki])
736 for (ki = 2; ki < ni; ++ki)
737 if (pi[ki - 1] <= pi[ki])
748 for (kj = 0; kj < nj; ++kj)
749 for (ki = kj + 1, j = pj[kj]; ki < ni; ++ki)
755 for (kj = 0; kj < nj; ++kj)
756 for (ki = 0, j = pj[kj]; ki < kj; ++ki)
762 for (kj = 0; kj < nj; ++kj)
763 for (ki = 0, j = pj[kj]; ki < kj; ++ki)
769 for (kj = 0; kj < nj; ++kj)
770 for (ki = kj + 1, j = pj[kj]; ki < ni; ++ki)
780int stay_di(
int *pi,
int ni,
int *pj,
int nj,
int n,
int checkNA)
782 if (!pi || !pj || ni != nj)
784 int ki, kj, iden = memcmp(pi, pj,
sizeof(
int) * (
size_t) ni) == 0;
785 if (checkNA && (
anyNA(pi, ni) || (!iden &&
anyNA(pj, ni))))
792 for (ki = 0; ki < ni; ++ki) {
803 for (kj = 0; kj < nj; ++kj) {
805 for (ki = 0; ki < kj; ++ki)
808 for (ki = kj + 1; ki < ni; ++ki)
819 if (si == R_NilValue && sj == R_NilValue)
822 int *pdim =
DIM(obj), m = pdim[0], n = pdim[1],
823 ki, mi = si == R_NilValue, ni = (mi) ? m : LENGTH(si),
824 kj, mj = sj == R_NilValue, nj = (mj) ? n : LENGTH(sj),
825 *pi = (mi) ? NULL : INTEGER(si),
826 *pj = (mj) ? NULL : INTEGER(sj);
828 int packed =
class[2] ==
'p';
829 char ul0 =
'\0', ct0 =
'\0', nu0 =
'\0';
832 if (
class[1] ==
's' &&
class[0] ==
'z')
837 int stay = (
class[1] ==
'g') ? 0 : (
class[1] ==
's')
838 ?
stay_sy(pi, ni, pj, nj, n, ul0, 1)
839 :
stay_tr(pi, ni, pj, nj, n, ul0, 1);
840 int_fast64_t ninj = (int_fast64_t) ni * nj,
841 xlen = (!packed || stay == 0) ? ninj : ni + (ninj - ni) / 2;
842 if (xlen > R_XLEN_T_MAX)
843 Rf_error(
_(
"attempt to allocate vector of length exceeding %s"),
846 char ul1 =
'\0', ct1 =
'\0', nu1 =
'\0';
849 ul1 = (stay > 0) ?
'U' :
'L';
850 if (
class[1] ==
's' &&
class[0] ==
'z')
853 nu1 = (
ABS(stay) == 1) ?
'N' : nu0;
856 int he =
class[1] ==
's' && ct0 ==
'C' && ct1 !=
'C',
857 un =
class[1] ==
't' && nu0 !=
'N';
859 char cl[] =
"...Matrix";
861 cl[1] = (stay == 0) ?
'g' :
class[1];
862 cl[2] = (stay == 0) ?
'e' :
class[2];
866 if (
cl[1] !=
'g' && ul1 !=
'U')
868 if (
cl[1] ==
's' && ct1 !=
'C' &&
cl[0] ==
'z')
870 if (
cl[1] ==
't' && nu1 !=
'N')
874 x1 = PROTECT(Rf_allocVector(
TYPEOF(x0), (R_xlen_t) xlen));
881 c##TYPE *px0 = c##PTR(x0), *px1 = c##PTR(x1); \
882 if (!packed && cl[1] != 'g') \
883 memset(px1, 0, sizeof(c##TYPE) * (size_t) XLENGTH(x1)); \
884 if (class[1] == 'g') { \
885 SUB2__(c, ASSIGN_GE, N, N, for (ki = 0; ki < ni; ++ki), , ); \
886 } else if (class[1] == 's' && !packed) { \
889 SUB2__(c, ASSIGN_SY, U, N, for (ki = 0; ki < ni; ++ki), , ); \
891 SUB2__(c, ASSIGN_SY, L, N, for (ki = 0; ki < ni; ++ki), , ); \
892 } else if (ul1 == 'U') { \
894 SUB2__(c, ASSIGN_SY, U, N, for (ki = 0; ki <= kj; ++ki), \
895 , px1 += ni - kj - 1); \
897 SUB2__(c, ASSIGN_SY, L, N, for (ki = 0; ki <= kj; ++ki), \
898 , px1 += ni - kj - 1); \
901 SUB2__(c, ASSIGN_SY, U, N, for (ki = kj; ki < ni; ++ki), \
904 SUB2__(c, ASSIGN_SY, L, N, for (ki = kj; ki < ni; ++ki), \
907 } else if (class[1] == 's') { \
910 SUB2__(c, ASSIGN_SY, U, U, for (ki = 0; ki < ni; ++ki), , ); \
912 SUB2__(c, ASSIGN_SY, L, L, for (ki = 0; ki < ni; ++ki), , ); \
913 } else if (ul1 == 'U') { \
915 SUB2__(c, ASSIGN_SY, U, U, for (ki = 0; ki <= kj; ++ki), , ); \
917 SUB2__(c, ASSIGN_SY, L, L, for (ki = 0; ki <= kj; ++ki), , ); \
920 SUB2__(c, ASSIGN_SY, U, U, for (ki = kj; ki < ni; ++ki), , ); \
922 SUB2__(c, ASSIGN_SY, L, L, for (ki = kj; ki < ni; ++ki), , ); \
924 } else if (!packed) { \
927 SUB2__(c, ASSIGN_TR, U, N, for (ki = 0; ki < ni; ++ki), , ); \
929 SUB2__(c, ASSIGN_TR, L, N, for (ki = 0; ki < ni; ++ki), , ); \
930 } else if (ul1 == 'U') { \
932 SUB2__(c, ASSIGN_TR, U, N, for (ki = 0; ki <= kj; ++ki), \
933 , px1 += ni - kj - 1); \
935 SUB2__(c, ASSIGN_TR, L, N, for (ki = 0; ki <= kj; ++ki), \
936 , px1 += ni - kj - 1); \
939 SUB2__(c, ASSIGN_TR, U, N, for (ki = kj; ki < ni; ++ki), \
942 SUB2__(c, ASSIGN_TR, L, N, for (ki = kj; ki < ni; ++ki), \
948 SUB2__(c, ASSIGN_TR, U, U, for (ki = 0; ki < ni; ++ki), , ); \
950 SUB2__(c, ASSIGN_TR, L, L, for (ki = 0; ki < ni; ++ki), , ); \
951 } else if (ul1 == 'U') { \
953 SUB2__(c, ASSIGN_TR, U, U, for (ki = 0; ki <= kj; ++ki), , ); \
955 SUB2__(c, ASSIGN_TR, L, L, for (ki = 0; ki <= kj; ++ki), , ); \
958 SUB2__(c, ASSIGN_TR, U, U, for (ki = kj; ki < ni; ++ki), , ); \
960 SUB2__(c, ASSIGN_TR, L, L, for (ki = kj; ki < ni; ++ki), , ); \
965#define SUB2__(c, assign, s, t, __for__, jump0, jump1) \
967 for (kj = 0; kj < nj; ++kj) { \
970 else if (pj[kj] != NA_INTEGER) \
984 else if (pi[ki] != NA_INTEGER) \
990 assign(c, px0, px1, i, j, m, s, t); \
997#define ASSIGN_GE(c, x, y, i, j, m, s, t) \
999 c##ASSIGN_IDEN(*y, x[DENSE_INDEX_N(i, j, m)]); \
1002#define ASSIGN_SY(c, x, y, i, j, m, s, t) \
1005 c##ASSIGN_PROJ_REAL(*y, x[DENSE_INDEX_##t(i, j, m)]); \
1006 else if (CMP_##s(i, j)) \
1007 c##ASSIGN_IDEN (*y, x[DENSE_INDEX_##t(i, j, m)]); \
1009 c##ASSIGN_CONJ (*y, x[DENSE_INDEX_##t(j, i, m)]); \
1011 c##ASSIGN_IDEN (*y, x[DENSE_INDEX_##t(j, i, m)]); \
1014#define ASSIGN_TR(c, x, y, i, j, m, s, t) \
1017 c##ASSIGN_IDEN(*y, c##UNIT); \
1018 else if (CMP_##s(i, j)) \
1019 c##ASSIGN_IDEN(*y, x[DENSE_INDEX_##t(i, j, m)]); \
1021 c##ASSIGN_IDEN(*y, c##ZERO); \
1024#define CMP_U(i, j) i <= j
1025#define CMP_L(i, j) i >= j
1039 if (si == R_NilValue && sj == R_NilValue)
1042 int mg = (
class[2] ==
'R') ? 0 : 1;
1044 SWAP(si, sj, SEXP, );
1046 int *pdim =
DIM(obj), m = pdim[mg == 0], n = pdim[mg != 0],
1047 ki, mi = si == R_NilValue, ni = (mi) ? m : LENGTH(si),
1048 kj, mj = sj == R_NilValue, nj = (mj) ? n : LENGTH(sj),
1049 *pi = (mi) ? NULL : INTEGER(si),
1050 *pj = (mj) ? NULL : INTEGER(sj);
1052 Rf_error(
_(
"NA subscripts in %s not supported for '%s' inheriting from %s"),
1053 "x[i, j]",
"x",
"sparseMatrix");
1055 char ul0 =
'\0', ct0 =
'\0', nu0 =
'\0';
1056 if (
class[1] !=
'g') {
1059 ul0 = (ul0 ==
'U') ?
'L' :
'U';
1061 if (
class[1] ==
's' &&
class[0] ==
'z')
1063 if (
class[1] ==
't')
1066 int stay = (
class[1] ==
'g') ? 0 : (
class[1] ==
's')
1067 ?
stay_sy(pi, ni, pj, nj, n, ul0, 0)
1068 :
stay_tr(pi, ni, pj, nj, n, ul0, 0);
1070 char ul1 =
'\0', ct1 =
'\0', nu1 =
'\0';
1072 if (
class[1] !=
'g')
1073 ul1 = (stay > 0) ?
'U' :
'L';
1074 if (
class[1] ==
's' &&
class[0] ==
'z')
1076 if (
class[1] ==
't')
1077 nu1 = (
ABS(stay) == 1) ?
'N' : nu0;
1080 char class__[] =
"...Matrix";
1081 class__[0] =
class[0];
1082 class__[1] =
class[1];
1083 class__[2] =
class[2];
1084 PROTECT_INDEX pid_obj;
1085 PROTECT_WITH_INDEX(obj, &pid_obj);
1086 if (
class[2] ==
'T') {
1092 if (
class[1] !=
'g' &&
ABS(stay) <= 1) {
1099 char cl[] =
"...Matrix";
1103 if (
ABS(stay) <= ((
class[1] !=
's') ? 0 : 1))
1105 PROTECT_INDEX pid_ans;
1107 PROTECT_WITH_INDEX(ans =
newObject(
cl), &pid_ans);
1109 SET_DIM(ans, (mg == 0) ? nj : ni, (mg == 0) ? ni : nj);
1110 if (
cl[1] !=
'g' && (mg == (ul1 !=
'U')))
1112 if (
cl[1] ==
's' && ct1 !=
'C' &&
cl[0] ==
'z')
1114 if (
cl[1] ==
't' && nu1 !=
'N')
1120 p1 = PROTECT(Rf_allocVector(INTSXP, (R_xlen_t) nj + 1));
1121 int *pp0 = INTEGER(p0), *pi0 = INTEGER(i0), *pp1 = INTEGER(p1),
1123 pp0++; *(pp1++) = 0;
1128 int_fast64_t nnz = 0;
1129 for (kj = 0; kj < nj; ++kj) {
1131 nnz += pp0[j] - pp0[j - 1];
1132 pp1[kj] = (int) nnz;
1135 Rf_error(
_(
"%s too dense for %s; would have more than %s nonzero entries"),
1136 "x[i, j]",
"[CR]sparseMatrix",
"2^31-1");
1138 SEXP i1 = PROTECT(Rf_allocVector(INTSXP, (
int) nnz));
1139 int *pi1 = INTEGER(i1);
1145 SEXP x0 = PROTECT(GET_SLOT(obj, Matrix_xSym)), \
1146 x1 = PROTECT(Rf_allocVector(c##TYPESXP, (int) nnz)); \
1147 c##TYPE *px0 = c##PTR(x0), *px1 = c##PTR(x1); \
1148 SET_SLOT(ans, Matrix_xSym, x1); \
1151 for (kj = 0; kj < nj; ++kj) { \
1155 while (k < kend) { \
1156 *(pi1++) = pi0[k]; \
1158 *(px1++) = px0[k]; \
1174 size_t liwork = (size_t) ((int_fast64_t) m + m + ni);
1177 int *iworkA = iwork, *iworkB = iworkA + m, *iworkC = iworkB + m,
1184 for (ki = ni - 1; ki >= 0; --ki) {
1187 iworkC[ki] = iworkB[i];
1194 int_fast64_t nnz = 0;
1195 for (kj = 0; kj < nj; ++kj) {
1196 j = (mj) ? kj : pj[kj] - 1;
1200 nnz += iworkA[pi0[k]];
1203 pp1[kj] = (int) nnz;
1206 Rf_error(
_(
"%s too dense for %s; would have more than %s nonzero entries"),
1207 "x[i, j]",
"[CR]sparseMatrix",
"2^31-1");
1209 SEXP i1 = PROTECT(Rf_allocVector(INTSXP, (
int) nnz));
1210 int *pi1 = INTEGER(i1), d;
1216 SEXP x0 = PROTECT(GET_SLOT(obj, Matrix_xSym)), \
1217 x1 = PROTECT(Rf_allocVector(c##TYPESXP, (int) nnz)); \
1218 c##TYPE *px0 = c##PTR(x0), *px1 = c##PTR(x1); \
1219 SET_SLOT(ans, Matrix_xSym, x1); \
1222 for (kj = 0; kj < nj; ++kj) { \
1223 j = (mj) ? kj : pj[kj] - 1; \
1226 while (k < kend) { \
1233 *(px1++) = px0[k]; \
1249 liwork = (size_t) ((int_fast64_t) ni + 1 + ((ni < nj) ? nj : ni) + nnz);
1252#define TEMPLATE(c) \
1254 c##TYPE *px1 = NULL, *work = NULL; \
1256 SEXP x1 = PROTECT(GET_SLOT(ans, Matrix_xSym)); \
1258 Matrix_Calloc(work, nnz, c##TYPE); \
1260 c##cspsort(INTEGER(p1), INTEGER(i1), px1, ni, nj, iwork, work); \
1262 Matrix_Free(work, nnz); \
1278 if (
class[1] ==
's' &&
ABS(stay) == 1) {
1284 if (
class[2] ==
'T') {
1298 if (si == R_NilValue && sj == R_NilValue)
1301 int n =
DIM(obj)[1],
1302 ki, mi = si == R_NilValue, ni = (mi) ? n : LENGTH(si),
1303 kj, mj = sj == R_NilValue, nj = (mj) ? n : LENGTH(sj),
1304 *pi = (mi) ? NULL : INTEGER(si),
1305 *pj = (mj) ? NULL : INTEGER(sj);
1307 Rf_error(
_(
"NA subscripts in %s not supported for '%s' inheriting from %s"),
1308 "x[i, j]",
"x",
"sparseMatrix");
1309 int stay =
stay_di(pi, ni, pj, nj, n, 0);
1311 char nu0 =
DIAG(obj),
1312 nu1 = (stay <= 0) ?
'\0' : (stay <= 1) ?
'N' : nu0;
1314 int un = nu0 !=
'N';
1316 char cl[] =
"...Matrix";
1318 cl[1] = (stay <= 0) ?
'g' :
'd';
1319 cl[2] = (stay <= 0) ?
'C' :
'i';
1324 if (nu1 !=
'\0' && nu1 !=
'N') {
1328 }
else if (nu1 !=
'\0') {
1331 x1 = PROTECT(Rf_allocVector(
TYPEOF(x0), ni));
1338 c##TYPE *px0 = c##PTR(x0) - 1, *px1 = c##PTR(x1); \
1339 for (ki = 0; ki < ni; ++ki) \
1340 if (*(pi++) != (j = *(pj++))) \
1341 *(px1++) = c##ZERO; \
1343 *(px1++) = (un) ? c##UNIT : px0[j]; \
1356 SEXP p1 = PROTECT(Rf_allocVector(INTSXP, (R_xlen_t) nj + 1));
1357 int *pp1 = INTEGER(p1), j;
1361#define TEMPLATE(c) \
1363 c##TYPE *px0 = c##PTR(x0); \
1364 for (kj = 0; kj < nj; ++kj) { \
1365 j = (mj) ? kj : pj[kj] - 1; \
1367 if (un || c##NOT_ZERO(px0[j])) { \
1369 for (ki = 0; ki < ni; ++ki) \
1373 for (ki = 0; ki < ni; ++ki) \
1374 if (pi[ki] - 1 == j) \
1377 if (pp1[kj] > INT_MAX - pp1[kj - 1]) \
1380 pp1[kj] += pp1[kj - 1]; \
1390 Rf_error(
_(
"%s too dense for %s; would have more than %s nonzero entries"),
1391 "x[i, j]",
"[CR]sparseMatrix",
"2^31-1");
1393 SEXP i1 = PROTECT(Rf_allocVector(INTSXP, pp1[nj - 1]));
1394 int *pi1 = INTEGER(i1);
1399 c##TYPE *px0 = c##PTR(x0); \
1401 SEXP x1 = PROTECT(Rf_allocVector(c##TYPESXP, pp1[nj - 1])); \
1402 c##TYPE *px1 = c##PTR(x1); \
1403 SET_SLOT(ans, Matrix_xSym, x1); \
1406 for (kj = 0; kj < nj; ++kj) { \
1407 j = (mj) ? kj : pj[kj] - 1; \
1408 if (un || c##NOT_ZERO(px0[j])) { \
1410 for (ki = 0; ki < ni; ++ki) \
1414 *(px1++) = (un) ? c##UNIT : px0[j]; \
1418 for (ki = 0; ki < ni; ++ki) \
1419 if (pi[ki] - 1 == j) { \
1422 *(px1++) = (un) ? c##UNIT : px0[j]; \
1445 if (si == R_NilValue && sj == R_NilValue)
1449 int isPerm(
const int *,
int,
int);
1450 void invertPerm(
const int *,
int *,
int,
int,
int);
1454 SWAP(si, sj, SEXP, );
1456 int *pdim =
DIM(obj), m = pdim[mg != 0], n = pdim[mg == 0],
1457 ki, mi = si == R_NilValue, ni = (mi) ? m : LENGTH(si),
1458 kj, mj = sj == R_NilValue, nj = (mj) ? n : LENGTH(sj),
1459 *pi = (mi) ? NULL : INTEGER(si),
1460 *pj = (mj) ? NULL : INTEGER(sj);
1462 Rf_error(
_(
"NA subscripts in %s not supported for '%s' inheriting from %s"),
1463 "x[i, j]",
"x",
"sparseMatrix");
1464 int stay =
class[0] ==
'p';
1466 PROTECT_INDEX pid_perm;
1468 int *pperm = INTEGER(perm);
1469 PROTECT_WITH_INDEX(perm, &pid_perm);
1471 PROTECT_INDEX pid_ans;
1473 PROTECT_WITH_INDEX(ans, &pid_ans);
1476 stay = stay && ni == m &&
isPerm(pi, m, 1);
1477 REPROTECT(ans =
newObject((stay) ?
"pMatrix" :
"indMatrix"), pid_ans);
1479 SET_DIM(ans, (mg == 0) ? ni : n, (mg == 0) ? n : ni);
1483 REPROTECT(perm = Rf_allocVector(INTSXP, ni), pid_perm);
1484 pperm = INTEGER(perm);
1487 for (ki = 0; ki < ni; ++ki)
1488 pperm[ki] = tmp[pi[ki]];
1496 stay = stay && nj == n &&
isPerm(pj, n, 1);
1497 REPROTECT(ans =
newObject((stay) ?
"pMatrix" : (mg == 0) ?
"ngCMatrix" :
"ngRMatrix"), pid_ans);
1499 SET_DIM(ans, (mg == 0) ? m : nj, (mg == 0) ? nj : m);
1502 size_t liwork = (size_t) ((stay) ? nj : (int_fast64_t) n + 1 + n + m);
1510 REPROTECT(perm = Rf_allocVector(INTSXP, nj), pid_perm);
1511 pperm = INTEGER(perm);
1514 for (kj = 0; kj < nj; ++kj)
1515 pperm[kj] = iwork[tmp[kj]];
1522 int *pp0 = iwork, *pp_ = pp0 + n + 1, *pi0 = pp_ + n;
1524 SEXP p1 = PROTECT(Rf_allocVector(INTSXP, (R_xlen_t) nj + 1));
1525 int *pp1 = INTEGER(p1), i, j, k, kend, k_;
1530 for (i = 0; i < m; ++i)
1534 int_fast64_t nnz = 0;
1535 for (kj = 0; kj < nj; ++kj) {
1537 pp1[kj] = (int) nnz;
1540 Rf_error(
_(
"%s too dense for %s; would have more than %s nonzero entries"),
1541 "x[i, j]",
"[CR]sparseMatrix",
"2^31-1");
1543 SEXP i1 = PROTECT(Rf_allocVector(INTSXP, pp1[nj - 1]));
1544 int *pi1 = INTEGER(i1);
1548 for (j = 0; j < n; ++j)
1549 pp0[j + 1] += (pp_[j] = pp0[j]);
1553 for (i = 0; i < m; ++i)
1554 pi0[pp0[pperm[i]]++] = i;
1558 for (kj = 0, k = 0; kj < nj; ++kj) {
1562 pi1[k++] = pi0[k_++];
SEXP sparse_as_general(SEXP, const char *)
SEXP sparse_force_symmetric(SEXP, const char *, char, char)
SEXP sparse_as_Csparse(SEXP, const char *)
#define SWITCH5(c, template)
#define SWITCH4(c, template)
#define SWAP(a, b, t, op)
#define Matrix_Calloc(p, n, t)
const char * Matrix_class(SEXP, const char **, int, const char *)
SEXPTYPE kindToType(char)
#define Matrix_Free(p, n)
#define GET_SLOT(x, name)
void validObject(SEXP, const char *)
SEXP newObject(const char *)
const char * valid_matrix[]
#define SET_SLOT(x, name, value)
SEXP sparse_as_Tsparse(SEXP from, const char *class)
int isPerm(const int *p, int n, int off)
void invertPerm(const int *p, int *ip, int n, int off, int ioff)
SEXP R_subscript_1ary(SEXP s_obj, SEXP s_s, SEXP s_o)
static int stay_di(int *pi, int ni, int *pj, int nj, int n, int checkNA)
static int stay_tr(int *pi, int ni, int *pj, int nj, int n, char uplo, int checkNA)
static SEXP dense_subscript_1ary_2col(SEXP obj, const char *class, SEXP s)
static SEXP index_subscript_2ary(SEXP obj, const char *class, SEXP si, SEXP sj)
static SEXP dense_subscript_2ary(SEXP obj, const char *class, SEXP si, SEXP sj)
static int stay_sy(int *pi, int ni, int *pj, int nj, int n, char uplo, int checkNA)
static SEXP diagonal_subscript_1ary(SEXP obj, const char *class, SEXP s)
static SEXP sparse_subscript_1ary_2col(SEXP obj, const char *class, SEXP s, SEXP o)
SEXP R_subscript_2ary(SEXP s_obj, SEXP s_si, SEXP s_sj)
static SEXP dense_subscript_1ary(SEXP obj, const char *class, SEXP s)
static SEXP index_subscript_1ary_2col(SEXP obj, const char *class, SEXP s)
static SEXP sparse_subscript_2ary(SEXP obj, const char *class, SEXP si, SEXP sj)
static SEXP diagonal_subscript_2ary(SEXP obj, const char *class, SEXP si, SEXP sj)
static SEXP sparse_subscript_1ary(SEXP obj, const char *class, SEXP s, SEXP o)
SEXP R_subscript_1ary_2col(SEXP s_obj, SEXP s_s, SEXP s_o)
static SEXP index_subscript_1ary(SEXP obj, const char *class, SEXP s)
static int anyNA(int *p, int n)
static SEXP diagonal_subscript_1ary_2col(SEXP obj, const char *class, SEXP s)