9 char ul,
char ct,
char nu,
10 int m,
int n,
int byrow, SEXP dimnames)
12 SEXPTYPE tf =
TYPEOF(from);
13 char cl[] =
"...Matrix";
14 cl[0] = (zzz[0] ==
'.') ?
typeToKind(tf) : ((zzz[0] ==
',') ? ((tf == CPLXSXP) ?
'z' :
'd') : zzz[0]);
17#ifndef MATRIX_ENABLE_IMATRIX
22 int packed =
cl[2] ==
'p';
23 PROTECT(from = Rf_coerceVector(from, tt));
25 if (
cl[1] !=
'g' && m != n)
26 Rf_error(
_(
"attempt to construct non-square %s"),
27 (
cl[1] ==
's' ||
cl[1] ==
'p') ?
"symmetricMatrix" :
"triangularMatrix");
29 int_fast64_t mn = (int_fast64_t) m * n,
30 xlen = (!packed) ? mn : n + (mn - n) / 2;
31 if (xlen > R_XLEN_T_MAX)
32 Rf_error(
_(
"attempt to allocate vector of length exceeding %s"),
37 if (dimnames != R_NilValue)
39 if (
cl[1] !=
'g' && ul !=
'U')
41 if (
cl[1] ==
's' && ct !=
'C' &&
cl[0] ==
'z')
43 if (
cl[1] ==
't' && nu !=
'N')
47 SEXP x = PROTECT(Rf_allocVector(tt, (R_xlen_t) xlen));
48 R_xlen_t k, r = XLENGTH(from);
49 int i, j, recycle = r < mn;
53 c##TYPE *dest = c##PTR(x), *src = c##PTR(from); \
57 } else if (r == 1) { \
60 } else if (!packed) { \
62 c##NAME(trans2)(dest, src, (size_t) n, (size_t) m, \
63 (!byrow) ? 'N' : 'T'); \
69 *(dest++) = src[k++]; \
72 for (j = 0; j < n; ++j) { \
74 for (i = 0; i < m; ++i) { \
82 } else if (ul == 'U') { \
85 for (j = 0; j < n; ++j) { \
86 for (i = 0; i <= j; ++i) { \
87 if (recycle) k %= r; \
88 *(dest++) = src[k++]; \
93 for (j = 0; j < n; ++j) { \
95 for (i = 0; i <= j; ++i) { \
96 if (recycle) k %= r; \
105 for (j = 0; j < n; ++j) { \
106 for (i = j; i < n; ++i) { \
107 if (recycle) k %= r; \
108 *(dest++) = src[k++]; \
114 for (j = 0; j < n; ++j) { \
116 for (i = 0; i <= j; ++i) { \
117 if (recycle) k %= r; \
118 *(dest++) = src[k]; \
138 SEXP s_uplo, SEXP s_trans, SEXP s_diag,
139 SEXP s_m, SEXP s_n, SEXP s_byrow,
154 if (
TYPEOF(s_zzz) != STRSXP || LENGTH(s_zzz) < 1 ||
155 (s_zzz = STRING_ELT(s_zzz, 0)) == NA_STRING ||
156 (zzz = CHAR(s_zzz))[0] ==
'\0' ||
158 !((zzz[1] ==
'g' && (zzz[2] ==
'e' )) ||
159 (zzz[1] ==
's' && (zzz[2] ==
'y' || zzz[2] ==
'p')) ||
160 (zzz[1] ==
'p' && (zzz[2] ==
'o' || zzz[2] ==
'p')) ||
161 (zzz[1] ==
't' && (zzz[2] ==
'r' || zzz[2] ==
'p'))))
162 Rf_error(
_(
"second argument of '%s' does not specify a subclass of %s"),
163 __func__,
"denseMatrix");
165 char ul =
'\0', ct =
'\0', nu =
'\0';
171 if (s_m != R_NilValue) {
172 if (
TYPEOF(s_m) == INTSXP) {
174 if (LENGTH(s_m) >= 1 && (tmp = INTEGER(s_m)[0]) != NA_INTEGER &&
177 }
else if (
TYPEOF(s_m) == REALSXP) {
179 if (LENGTH(s_m) >= 1 && !ISNAN(tmp = REAL(s_m)[0]) &&
181 if (trunc(tmp) > INT_MAX)
182 Rf_error(
_(
"dimensions cannot exceed %s"),
"2^31-1");
187 Rf_error(
_(
"invalid '%s' to '%s'"),
"m", __func__);
191 if (s_n != R_NilValue) {
192 if (
TYPEOF(s_n) == INTSXP) {
194 if (LENGTH(s_n) >= 1 && (tmp = INTEGER(s_n)[0]) != NA_INTEGER &&
197 }
else if (
TYPEOF(s_n) == REALSXP) {
199 if (LENGTH(s_n) >= 1 && !ISNAN(tmp = REAL(s_n)[0]) &&
201 if (trunc(tmp) > INT_MAX)
202 Rf_error(
_(
"dimensions cannot exceed %s"),
"2^31-1");
207 Rf_error(
_(
"invalid '%s' to '%s'"),
"n", __func__);
213 if (s_dimnames != R_NilValue)
214 if (
TYPEOF(s_dimnames) != VECSXP || LENGTH(s_dimnames) != 2)
215 Rf_error(
_(
"invalid '%s' to '%s'"),
"dimnames", __func__);
217 R_xlen_t vlen = XLENGTH(s_from);
218 if (zzz[1] !=
'g' && (m < 0) != (n < 0)) {
223 }
else if (m < 0 && n < 0) {
225 Rf_error(
_(
"dimensions cannot exceed %s"),
"2^31-1");
229 if (vlen > (int_fast64_t) INT_MAX * n) {
231 Rf_error(
_(
"nonempty vector supplied for empty matrix"));
233 Rf_error(
_(
"dimensions cannot exceed %s"),
"2^31-1");
235 m = (int) ((n == 0) ? 0 : vlen / n + (vlen % n != 0));
237 if (vlen > (int_fast64_t) m * INT_MAX) {
239 Rf_error(
_(
"nonempty vector supplied for empty matrix"));
241 Rf_error(
_(
"dimensions cannot exceed %s"),
"2^31-1");
243 n = (int) ((m == 0) ? 0 : vlen / m + (vlen % m != 0));
246 int_fast64_t mlen = (int_fast64_t) m * n;
250 Rf_warning(
_(
"nonempty vector supplied for empty matrix"));
251 else if (vlen > mlen)
252 Rf_warning(
_(
"vector length (%lld) exceeds matrix length (%d * %d)"),
253 (
long long) vlen, m, n);
254 else if (mlen % vlen != 0)
255 Rf_warning(
_(
"matrix length (%d * %d) is not a multiple of vector length (%lld)"),
256 m, n, (
long long) vlen);
263 char ul,
char ct,
char nu,
int mg,
int new)
265 SEXPTYPE tf =
TYPEOF(from);
266 char cl[] =
"...Matrix";
267 cl[0] = (zzz[0] ==
'.') ?
typeToKind(tf) : ((zzz[0] ==
',') ? ((tf == CPLXSXP) ?
'z' :
'd') : zzz[0]);
270#ifndef MATRIX_ENABLE_IMATRIX
275 int packed =
cl[2] ==
'p';
276 PROTECT(from = Rf_coerceVector(from, tt));
281 SEXP dim = Rf_getAttrib(from, R_DimSymbol), dimnames;
282 int *pdim, isM, m, n, doDN;
283 R_xlen_t mn = XLENGTH(from);
285 isM =
TYPEOF(dim) == INTSXP && LENGTH(dim) == 2;
295 PROTECT(dimnames = Rf_getAttrib(from, R_DimNamesSymbol));
297 doDN = dimnames != R_NilValue;
302 Rf_error(
_(
"dimensions cannot exceed %s"),
"2^31-1");
306 pdim[1] = n = (int) mn;
308 pdim[0] = m = (int) mn;
312 SEXP nms = PROTECT(Rf_getAttrib(from, R_NamesSymbol));
314 doDN = nms != R_NilValue;
316 PROTECT(dimnames = Rf_allocVector(VECSXP, 2));
318 SET_VECTOR_ELT(dimnames, mg == 0, nms);
323 if (
cl[1] !=
'g' && m != n)
324 Rf_error(
_(
"attempt to construct non-square %s"),
325 (
cl[1] ==
's' ||
cl[1] ==
'p') ?
"symmetricMatrix" :
"triangularMatrix");
329 if (
cl[1] !=
'g' && ul !=
'U')
331 if (
cl[1] ==
's' && ct !=
'C' &&
cl[0] ==
'z')
333 if (
cl[1] ==
't' && nu !=
'N')
340 if (
new <= 0 || (
new <= 1 && !ANY_ATTRIB(from)) ||
341 !MAYBE_REFERENCED(from)) {
343 if (
new > 0 && ANY_ATTRIB(from))
357 PROTECT(x = Rf_allocVector(tt, n + (mn - n) / 2));
361 c##NAME(pack2)(c##PTR(x), c##PTR(from), (size_t) n, \
378 SEXP s_uplo, SEXP s_trans, SEXP s_diag, SEXP s_margin)
392 if (
TYPEOF(s_zzz) != STRSXP || LENGTH(s_zzz) < 1 ||
393 (s_zzz = STRING_ELT(s_zzz, 0)) == NA_STRING ||
394 (zzz = CHAR(s_zzz))[0] ==
'\0' ||
396 !((zzz[1] ==
'g' && (zzz[2] ==
'e' )) ||
397 (zzz[1] ==
's' && (zzz[2] ==
'y' || zzz[2] ==
'p')) ||
398 (zzz[1] ==
'p' && (zzz[2] ==
'o' || zzz[2] ==
'p')) ||
399 (zzz[1] ==
't' && (zzz[2] ==
'r' || zzz[2] ==
'p'))))
400 Rf_error(
_(
"second argument of '%s' does not specify a subclass of %s"),
401 __func__,
"denseMatrix");
403 char ul =
'\0', ct =
'\0', nu =
'\0';
416 packed = packed &&
class[1] !=
'g';
418 char cl[] =
"...Matrix";
421 cl[2] = (packed) ?
'p' : ((
class[1] ==
'g') ?
'e' : ((
class[1] ==
's') ?
'y' : ((
class[1] ==
'p') ?
'o' :
'r')));
424 int *pdim =
DIM(from), m = pdim[0], n = pdim[1];
425 int_fast64_t mn = (int_fast64_t) m * n,
426 xlen = (!packed) ? mn : n + (mn - n) / 2;
427 if (xlen > R_XLEN_T_MAX)
428 Rf_error(
_(
"attempt to allocate vector of length exceeding %s"),
430 if (
class[2] !=
'C' && packed && mn > R_XLEN_T_MAX)
431 Rf_error(
_(
"coercing n-by-n %s to %s is not supported for n*n exceeding %s"),
432 "[RT]sparseMatrix",
"packedMatrix",
"R_XLEN_T_MAX");
434 if (bytes > 0x1.0p+30 )
435 Rf_warning(
_(
"sparse->dense coercion: allocating vector of size %0.1f GiB"),
440 char ul =
'\0', ct =
'\0', nu =
'\0';
441 if (
class[1] !=
'g' && (ul =
UPLO(from)) !=
'U')
443 if (
class[1] ==
's' &&
class[0] ==
'z' && (ct =
TRANS(from)) !=
'C')
445 if (
class[1] ==
't' && (nu =
DIAG(from)) !=
'N')
451 int *pp0, *pi0, *pj0, nprotect = 1;
453 pp0 = pi0 = pj0 = NULL;
454 if (
class[2] !=
'T') {
457 pp0 = INTEGER(p0) + 1;
459 if (
class[2] !=
'R') {
464 if (
class[2] !=
'C') {
473 SEXP x0 = PROTECT(GET_SLOT(from, Matrix_xSym)); \
474 c##TYPE *px0 = c##PTR(x0); \
476 SEXP x1 = PROTECT(Rf_allocVector(c##TYPESXP, (R_xlen_t) xlen)); \
477 c##TYPE *px1 = c##PTR(x1); \
478 memset(px1, 0, sizeof(c##TYPE) * (size_t) xlen); \
479 switch (class[2]) { \
484 for (j = 0, k = 0; j < n; ++j) { \
487 px1[*pi0] = c##IFELSE_NPATTERN(*px0, 1); \
488 ++k; ++pi0; c##IF_NPATTERN(++px0); \
492 else if (ul == 'U') \
493 for (j = 0, k = 0; j < n; ++j) { \
496 px1[*pi0] = c##IFELSE_NPATTERN(*px0, 1); \
497 ++k; ++pi0; c##IF_NPATTERN(++px0); \
502 for (j = 0, k = 0; j < n; ++j) { \
505 px1[*pi0 - j] = c##IFELSE_NPATTERN(*px0, 1); \
506 ++k; ++pi0; c##IF_NPATTERN(++px0); \
515 int_fast64_t index, m1 = (int_fast64_t) m; \
517 for (i = 0, k = 0; i < m; ++i) { \
521 px1[index] = c##IFELSE_NPATTERN(*px0, 1); \
522 ++k; ++pj0; c##IF_NPATTERN(++px0); \
526 else if (ul == 'U') \
527 for (i = 0, k = 0; i < m; ++i) { \
530 index = DENSE_INDEX_U((int_fast64_t) i, (int_fast64_t) *pj0, m1); \
531 px1[index] = c##IFELSE_NPATTERN(*px0, 1); \
532 ++k; ++pj0; c##IF_NPATTERN(++px0); \
536 for (i = 0, k = 0; i < m; ++i) { \
539 index = DENSE_INDEX_L((int_fast64_t) i, (int_fast64_t) *pj0, m1); \
540 px1[index] = c##IFELSE_NPATTERN(*px0, 1); \
541 ++k; ++pj0; c##IF_NPATTERN(++px0); \
548 R_xlen_t k, kend = XLENGTH(i0); \
549 int_fast64_t index, m1 = (int_fast64_t) m; \
551 for (k = 0; k < kend; ++k) { \
552 index = m1 * *pj0 + *pi0; \
553 c##INCREMENT_IDEN(px1[index], *px0); \
554 ++pi0; ++pj0; c##IF_NPATTERN(++px0); \
556 else if (ul == 'U') \
557 for (k = 0; k < kend; ++k) { \
558 index = DENSE_INDEX_U((int_fast64_t) *pi0, (int_fast64_t) *pj0, m1); \
559 c##INCREMENT_IDEN(px1[index], *px0); \
560 ++pi0; ++pj0; c##IF_NPATTERN(++px0); \
563 for (k = 0; k < kend; ++k) { \
564 index = DENSE_INDEX_L((int_fast64_t) *pi0, (int_fast64_t) *pj0, m1); \
565 c##INCREMENT_IDEN(px1[index], *px0); \
566 ++pi0; ++pj0; c##IF_NPATTERN(++px0); \
573 SET_SLOT(to, Matrix_xSym, x1); \
600 char kind,
char shape,
int packed,
603 packed = packed && shape !=
'g';
605 char cl[] =
"...Matrix";
606 cl[0] = (kind ==
'.') ?
class[0] : ((kind ==
',') ? ((
class[0] ==
'z') ?
'z' :
'd') : kind);
608 cl[2] = (packed) ?
'p' : ((shape ==
'g') ?
'e' : ((shape ==
's') ?
'y' : ((shape ==
'p') ?
'o' :
'r')));
611 int n =
DIM(from)[0];
612 int_fast64_t nn = (int_fast64_t) n * n,
613 xlen = (!packed) ? nn : n + (nn - n) / 2;
614 if (xlen > R_XLEN_T_MAX)
615 Rf_error(
_(
"attempt to allocate vector of length exceeding %s"),
618 if (bytes > 0x1.0p+30 )
619 Rf_warning(
_(
"sparse->dense coercion: allocating vector of size %0.1f GiB"),
623 if (
cl[1] !=
'g' && ul !=
'U')
625 if (
cl[1] ==
's' && ct !=
'C' &&
cl[0] ==
'z')
628 char nu =
DIAG(from);
629 if (
cl[1] ==
't' && nu !=
'N')
633 if (
cl[0] !=
class[0]) {
634 if (
class[0] ==
'n' &&
cl[0] ==
'l')
644 SEXP x1 = PROTECT(Rf_allocVector(
TYPEOF(x0), (R_xlen_t) xlen));
648 c##TYPE *px0 = c##PTR(x0), *px1 = c##PTR(x1); \
650 c##NAME(force2)(px1, px0, (size_t) n, ul, 1, -nu); \
652 c##NAME(force1)(px1, px0, (size_t) n, ul, 1, -nu); \
667 SEXP s_kind, SEXP s_shape, SEXP s_packed,
668 SEXP s_uplo, SEXP s_trans)
679 char ul =
'\0', ct =
'\0';
690 char cl[] =
".geMatrix";
691 cl[0] = (kind ==
'.') ?
'n' : ((kind ==
',') ?
'd' : kind);
694 int *pdim =
DIM(from), m = pdim[0], n = pdim[1];
695 int_fast64_t xlen = (int_fast64_t) m * n;
696 if (xlen > R_XLEN_T_MAX)
697 Rf_error(
_(
"attempt to allocate vector of length exceeding %s"),
700 if (bytes > 0x1.0p+30 )
701 Rf_warning(
_(
"sparse->dense coercion: allocating vector of size %0.1f GiB"),
707 int *pperm = INTEGER(perm);
709 SEXP x = PROTECT(Rf_allocVector(
kindToType(
cl[0]), (R_xlen_t) xlen));
713 c##TYPE *px = c##PTR(x); \
714 memset(px, 0, sizeof(c##TYPE) * (size_t) xlen); \
716 int_fast64_t m1 = (int_fast64_t) m; \
717 for (int i = 0; i < m; ++i) { \
718 px[m1 * (*(pperm++) - 1)] = c##UNIT; \
722 for (int j = 0; j < n; ++j) { \
723 px[ *(pperm++) - 1 ] = c##UNIT; \
751 char ul,
char ct,
char nu,
752 int m,
int n,
int byrow, SEXP dimnames)
755 int_fast64_t r = (int_fast64_t)
756 ((
TYPEOF(length0) == INTSXP) ? INTEGER(length0)[0] : REAL(length0)[0]);
762 char cl[] =
"...Matrix";
763 cl[0] = (zzz[0] ==
'.')
765 : ((zzz[0] ==
',') ? ((tf == CPLXSXP) ?
'z' :
'd') : zzz[0]);
767 cl[2] = (byrow) ?
'R' :
'C';
768#ifndef MATRIX_ENABLE_IMATRIX
773 if (x0 != R_NilValue &&
cl[0] !=
'n') {
774 x0 = Rf_coerceVector(x0, tt);
779 if (
cl[1] !=
'g' && m != n)
780 Rf_error(
_(
"attempt to construct non-square %s"),
781 (
cl[1] ==
's' ||
cl[1] ==
'p') ?
"symmetricMatrix" :
"triangularMatrix");
785 if (dimnames != R_NilValue)
787 if (
cl[1] !=
'g' && ul !=
'U')
789 if (
cl[1] ==
's' && ct !=
'C' &&
cl[0] ==
'z')
791 if (
cl[1] ==
't' && nu !=
'N')
794 int_fast64_t pos, mn = (int_fast64_t) m * n, nnz1 = 0;
795 R_xlen_t k = 0, nnz0 = XLENGTH(i0);
797#define TEMPLATE__(d) \
799 d##TYPE *pi0 = d##PTR(i0); \
802 else if (cl[1] == 'g') { \
808 while (k < nnz0 && (int_fast64_t) pi0[k++] <= mn) \
811 int_fast64_t mn_mod_r = mn % r; \
812 nnz1 = nnz0 * (mn / r); \
813 while (k < nnz0 && (int_fast64_t) pi0[k++] <= mn_mod_r) \
817 else if (cl[1] == 's' || cl[1] == 'p' || nu == 'N') { \
819 nnz1 = n + (mn - n) / 2; \
820 else if (r >= mn) { \
821 if ((ul == 'U') == !byrow) { \
822 while (k < nnz0 && (pos = (int_fast64_t) pi0[k++] - 1) < mn) \
823 if (pos % n <= pos / n) \
826 while (k < nnz0 && (pos = (int_fast64_t) pi0[k++] - 1) < mn) \
827 if (pos % n >= pos / n) \
832 int_fast64_t a = 0; \
833 if ((ul == 'U') == !byrow) \
836 while (k < nnz0 && (pos = a + (int_fast64_t) pi0[k++] - 1) < mn) \
837 if (pos % n <= pos / n) \
844 while (k < nnz0 && (pos = a + (int_fast64_t) pi0[k++] - 1) < mn) \
845 if (pos % n >= pos / n) \
853 nnz1 = (mn - n) / 2; \
854 else if (r >= mn) { \
855 if ((ul == 'U') == !byrow) { \
856 while (k < nnz0 && (pos = (int_fast64_t) pi0[k++] - 1) < mn) \
857 if (pos % n < pos / n) \
860 while (k < nnz0 && (pos = (int_fast64_t) pi0[k++] - 1) < mn) \
861 if (pos % n > pos / n) \
866 int_fast64_t a = 0; \
867 if ((ul == 'U') == !byrow) \
870 while (k < nnz0 && (pos = a + (int_fast64_t) pi0[k++] - 1) < mn) \
871 if (pos % n < pos / n) \
878 while (k < nnz0 && (pos = a + (int_fast64_t) pi0[k++] - 1) < mn) \
879 if (pos % n > pos / n) \
895 Rf_error(
_(
"attempt to construct %s with more than %s nonzero entries"),
896 "sparseMatrix",
"2^31-1");
902 p1 = PROTECT(Rf_allocVector(INTSXP, (R_xlen_t) n + 1)),
903 i1 = PROTECT(Rf_allocVector(INTSXP, nnz1));
904 int *pp1 = INTEGER(p1) + 1, *pi1 = INTEGER(i1), i, j;
905 memset(pp1 - 1, 0,
sizeof(
int) * ((
size_t) n + 1));
911#define TEMPLATE__(d, c0, c1) \
913 d ##TYPE *pi0 = d ##PTR(i0); \
915 c0##TYPE *px0 = c0##PTR(x0); \
918 SEXP x1 = PROTECT(Rf_allocVector(c1##TYPESXP, nnz1)); \
919 c1##TYPE *px1 = c1##PTR(x1); \
923 else if (cl[1] == 'g') { \
925 for (j = 0; j < n; ++j) { \
927 for (i = 0; i < m; ++i) { \
935 while (k < nnz0 && (pos = (int_fast64_t) pi0[k] - 1) < mn) { \
937 *(pi1++) = pos % m; \
939 *(px1++) = c0##IFELSE_NPATTERN(px0[k], c1##UNIT); \
944 int_fast64_t a = 0; \
947 while (k < nnz0 && (pos = a + (int_fast64_t) pi0[k] - 1) < mn) { \
949 *(pi1++) = pos % m; \
951 *(px1++) = c0##IFELSE_NPATTERN(px0[k], c1##UNIT); \
959 else if (cl[1] == 's' || cl[1] == 'p' || nu == 'N') { \
961 if ((ul == 'U') == !byrow) \
962 for (j = 0; j < n; ++j) { \
964 for (i = 0; i <= j; ++i) { \
972 for (j = 0; j < n; ++j) { \
974 for (i = j; i < n; ++i) { \
982 else if (r >= mn) { \
983 if ((ul == 'U') == !byrow) \
984 while (k < nnz0 && (pos = (int_fast64_t) pi0[k] - 1) < mn) { \
985 if ((i = (int) pos % n) <= (j = (int) pos / n)) { \
989 *(px1++) = c0##IFELSE_NPATTERN(px0[k], c1##UNIT); \
995 while (k < nnz0 && (pos = (int_fast64_t) pi0[k] - 1) < mn) { \
996 if ((i = (int) pos % n) >= (j = (int) pos / n)) { \
1000 *(px1++) = c0##IFELSE_NPATTERN(px0[k], c1##UNIT); \
1007 int_fast64_t a = 0; \
1008 if ((ul == 'U') == !byrow) \
1011 while (k < nnz0 && (pos = a + (int_fast64_t) pi0[k] - 1) < mn) { \
1012 if ((i = (int) pos % n) <= (j = (int) pos / n)) { \
1016 *(px1++) = c0##IFELSE_NPATTERN(px0[k], c1##UNIT); \
1026 while (k < nnz0 && (pos = a + (int_fast64_t) pi0[k] - 1) < mn) { \
1027 if ((i = (int) pos % n) >= (j = (int) pos / n)) { \
1031 *(px1++) = c0##IFELSE_NPATTERN(px0[k], c1##UNIT); \
1042 if ((ul == 'U') == !byrow) \
1043 for (j = 0; j < n; ++j) { \
1045 for (i = 0; i < j; ++i) { \
1048 *(px1++) = c1##NA; \
1053 for (j = 0; j < n; ++j) { \
1054 pp1[j] = n - j - 1; \
1055 for (i = j + 1; i < n; ++i) { \
1058 *(px1++) = c1##NA; \
1063 else if (r >= mn) { \
1064 if ((ul == 'U') == !byrow) \
1065 while (k < nnz0 && (pos = (int_fast64_t) pi0[k] - 1) < mn) { \
1066 if ((i = (int) pos % n) < (j = (int) pos / n)) { \
1070 *(px1++) = c0##IFELSE_NPATTERN(px0[k], c1##UNIT); \
1076 while (k < nnz0 && (pos = (int_fast64_t) pi0[k] - 1) < mn) { \
1077 if ((i = (int) pos % n) > (j = (int) pos / n)) { \
1081 *(px1++) = c0##IFELSE_NPATTERN(px0[k], c1##UNIT); \
1088 int_fast64_t a = 0; \
1089 if ((ul == 'U') == !byrow) \
1092 while (k < nnz0 && (pos = a + (int_fast64_t) pi0[k] - 1) < mn) { \
1093 if ((i = (int) pos % n) < (j = (int) pos / n)) { \
1097 *(px1++) = c0##IFELSE_NPATTERN(px0[k], c1##UNIT); \
1107 while (k < nnz0 && (pos = a + (int_fast64_t) pi0[k] - 1) < mn) { \
1108 if ((i = (int) pos % n) > (j = (int) pos / n)) { \
1112 *(px1++) = c0##IFELSE_NPATTERN(px0[k], c1##UNIT); \
1122 SET_SLOT(to, Matrix_xSym, x1); \
1127 if (x0 == R_NilValue) {
1129#define TEMPLATE(c) \
1131 if (TYPEOF(i0) == INTSXP) \
1132 TEMPLATE__(i, n, c); \
1134 TEMPLATE__(d, n, c); \
1143#define TEMPLATE(c) \
1145 if (TYPEOF(i0) == INTSXP) \
1146 TEMPLATE__(i, c, c); \
1148 TEMPLATE__(d, c, c); \
1159 for (j = 0; j < n; ++j)
1160 pp1[j] += pp1[j - 1];
1181 SEXP s_uplo, SEXP s_trans, SEXP s_diag,
1182 SEXP s_m, SEXP s_n, SEXP s_byrow,
1188 if (
TYPEOF(s_zzz) != STRSXP || LENGTH(s_zzz) < 1 ||
1189 (s_zzz = STRING_ELT(s_zzz, 0)) == NA_STRING ||
1190 (zzz = CHAR(s_zzz))[0] ==
'\0' ||
1191 (zzz[1] !=
'g' && zzz[1] !=
's' && zzz[1] !=
'p' && zzz[1] !=
't') ||
1192 (zzz[2] !=
'C' && zzz[2] !=
'R' && zzz[2] !=
'T'))
1193 Rf_error(
_(
"second argument of '%s' does not specify a subclass of %s"),
1194 __func__,
"[CRT]sparseMatrix");
1196 char ul =
'\0', ct =
'\0', nu =
'\0';
1202 if (s_m != R_NilValue) {
1203 if (
TYPEOF(s_m) == INTSXP) {
1205 if (LENGTH(s_m) >= 1 && (tmp = INTEGER(s_m)[0]) != NA_INTEGER &&
1208 }
else if (
TYPEOF(s_m) == REALSXP) {
1210 if (LENGTH(s_m) >= 1 && !ISNAN(tmp = REAL(s_m)[0]) &&
1212 if (trunc(tmp) > INT_MAX)
1213 Rf_error(
_(
"dimensions cannot exceed %s"),
"2^31-1");
1218 Rf_error(
_(
"invalid '%s' to '%s'"),
"m", __func__);
1222 if (s_n != R_NilValue) {
1223 if (
TYPEOF(s_n) == INTSXP) {
1225 if (LENGTH(s_n) >= 1 && (tmp = INTEGER(s_n)[0]) != NA_INTEGER &&
1228 }
else if (
TYPEOF(s_n) == REALSXP) {
1230 if (LENGTH(s_n) >= 1 && !ISNAN(tmp = REAL(s_n)[0]) &&
1232 if (trunc(tmp) > INT_MAX)
1233 Rf_error(
_(
"dimensions cannot exceed %s"),
"2^31-1");
1238 Rf_error(
_(
"invalid '%s' to '%s'"),
"n", __func__);
1244 if (s_dimnames != R_NilValue)
1245 if (
TYPEOF(s_dimnames) != VECSXP || LENGTH(s_dimnames) != 2)
1246 Rf_error(
_(
"invalid '%s' to '%s'"),
"dimnames", __func__);
1249 int_fast64_t vlen = (int_fast64_t)
1250 ((
TYPEOF(tmp) == INTSXP) ? INTEGER(tmp)[0] : REAL(tmp)[0]);
1251 if (zzz[1] !=
'g' && (m < 0) != (n < 0)) {
1256 }
else if (m < 0 && n < 0) {
1258 Rf_error(
_(
"dimensions cannot exceed %s"),
"2^31-1");
1262 if (vlen > (int_fast64_t) INT_MAX * n) {
1264 Rf_error(
_(
"nonempty vector supplied for empty matrix"));
1266 Rf_error(
_(
"dimensions cannot exceed %s"),
"2^31-1");
1268 m = (int) ((n == 0) ? 0 : vlen / n + (vlen % n != 0));
1270 if (vlen > (int_fast64_t) m * INT_MAX) {
1272 Rf_error(
_(
"nonempty vector supplied for empty matrix"));
1274 Rf_error(
_(
"dimensions cannot exceed %s"),
"2^31-1");
1276 n = (int) ((m == 0) ? 0 : vlen / m + (vlen % m != 0));
1279 int_fast64_t mlen = (int_fast64_t) m * n;
1283 Rf_warning(
_(
"nonempty vector supplied for empty matrix"));
1284 else if (vlen > mlen)
1285 Rf_warning(
_(
"vector length (%lld) exceeds matrix length (%d * %d)"),
1286 (
long long) vlen, m, n);
1287 else if (mlen % vlen != 0)
1288 Rf_warning(
_(
"matrix length (%d * %d) is not a multiple of vector length (%lld)"),
1289 m, n, (
long long) vlen);
1296 char ul,
char ct,
char nu,
int mg)
1298 char cl[] =
"...Matrix";
1301 cl[2] = (zzz[1] ==
'g') ?
'e' : ((zzz[1] ==
's') ?
'y' : ((zzz[1] ==
'p') ?
'o' :
'r'));
1302#ifndef MATRIX_ENABLE_IMATRIX
1307 PROTECT_WITH_INDEX(from, &pid);
1318 SEXP s_uplo, SEXP s_trans, SEXP s_diag, SEXP s_margin)
1320 switch (
TYPEOF(s_from)) {
1332 if (
TYPEOF(s_zzz) != STRSXP || LENGTH(s_zzz) < 1 ||
1333 (s_zzz = STRING_ELT(s_zzz, 0)) == NA_STRING ||
1334 (zzz = CHAR(s_zzz))[0] ==
'\0' ||
1335 (zzz[1] !=
'g' && zzz[1] !=
's' && zzz[1] !=
'p' && zzz[1] !=
't') ||
1336 (zzz[2] !=
'C' && zzz[2] !=
'R' && zzz[2] !=
'T'))
1337 Rf_error(
_(
"second argument of '%s' does not specify a subclass of %s"),
1338 __func__,
"[CRT]sparseMatrix");
1340 char ul =
'\0', ct =
'\0', nu =
'\0';
1353 int packed =
class[2] ==
'p';
1355 char cl[] =
"...Matrix";
1361 int *pdim =
DIM(from), m = pdim[0], n = pdim[1];
1365 char ul =
'\0', ct =
'\0', nu =
'\0';
1366 if (
class[1] !=
'g' && (ul =
UPLO(from)) !=
'U')
1368 if (
class[1] ==
's' &&
class[0] ==
'z' && (ct =
TRANS(from)) !=
'C')
1370 if (
class[1] ==
't' && (nu =
DIAG(from)) !=
'N')
1374 int i, j, *pp1, *pi1, *pj1;
1375 p1 = i1 = j1 = NULL;
1376 pp1 = pi1 = pj1 = NULL;
1383 int r = (
cl[2] ==
'C') ? n : m;
1384 PROTECT(p1 = Rf_allocVector(INTSXP, (R_xlen_t) r + 1));
1393 if (nnz > INT_MAX) \
1394 Rf_error(_("attempt to construct %s with more than %s nonzero entries"), \
1395 "sparseMatrix", "2^31-1"); \
1396 *(pp1++) = (int) nnz; \
1399#define DAS_BYCOL(c, kernel, hook) \
1401 if (class[1] == 'g') { \
1402 for (j = 0; j < n; ++j) { \
1403 for (i = 0; i < m; ++i) { \
1404 if (c##NOT_ZERO(*px0)) \
1410 } else if (!packed && (nu == '\0' || nu == 'N')) { \
1412 for (j = 0; j < n; ++j) { \
1413 for (i = 0; i < j; ++i) { \
1414 if (c##NOT_ZERO(*px0)) \
1418 for (; i <= j; ++i) { \
1419 if ((ct == 'C') ? c##NOT_ZERO_REAL(*px0) : c##NOT_ZERO(*px0)) \
1427 for (j = 0; j < n; ++j) { \
1429 for (i = j; i <= j; ++i) { \
1430 if ((ct == 'C') ? c##NOT_ZERO_REAL(*px0) : c##NOT_ZERO(*px0)) \
1434 for (; i < m; ++i) { \
1435 if (c##NOT_ZERO(*px0)) \
1441 } else if (!packed) { \
1443 for (j = 0; j < n; ++j) { \
1444 for (i = 0; i < j; ++i) { \
1445 if (c##NOT_ZERO(*px0)) \
1453 for (j = 0; j < n; ++j) { \
1455 for (i = j + 1; i < m; ++i) { \
1456 if (c##NOT_ZERO(*px0)) \
1462 } else if (nu == '\0' || nu == 'N') { \
1464 for (j = 0; j < n; ++j) { \
1465 for (i = 0; i < j; ++i) { \
1466 if (c##NOT_ZERO(*px0)) \
1470 for (; i <= j; ++i) { \
1471 if ((ct == 'C') ? c##NOT_ZERO_REAL(*px0) : c##NOT_ZERO(*px0)) \
1478 for (j = 0; j < n; ++j) { \
1479 for (i = j; i <= j; ++i) { \
1480 if ((ct == 'C') ? c##NOT_ZERO_REAL(*px0) : c##NOT_ZERO(*px0)) \
1484 for (; i < m; ++i) { \
1485 if (c##NOT_ZERO(*px0)) \
1493 for (j = 0; j < n; ++j) { \
1494 for (i = 0; i < j; ++i) { \
1495 if (c##NOT_ZERO(*px0)) \
1503 for (j = 0; j < n; ++j) { \
1505 for (i = j + 1; i < m; ++i) { \
1506 if (c##NOT_ZERO(*px0)) \
1515#define DAS_BYROW(c, kernel, hook) \
1518 if (class[1] == 'g') { \
1519 d = (int_fast64_t) m * n - 1; \
1520 for (i = 0; i < m; ++i) { \
1521 for (j = 0; j < n; ++j) { \
1522 if (c##NOT_ZERO(*px0)) \
1529 } else if (!packed && (nu == '\0' || nu == 'N')) { \
1531 d = (int_fast64_t) m * n - 1; \
1532 for (i = 0; i < m; ++i) { \
1533 for (j = i; j <= i; ++j) { \
1534 if ((ct == 'C') ? c##NOT_ZERO_REAL(*px0) : c##NOT_ZERO(*px0)) \
1538 for (; j < n; ++j) { \
1539 if (c##NOT_ZERO(*px0)) \
1548 for (i = 0; i < m; ++i) { \
1549 for (j = 0; j < i; ++j) { \
1550 if (c##NOT_ZERO(*px0)) \
1554 for (; j <= i; ++j) { \
1555 if ((ct == 'C') ? c##NOT_ZERO_REAL(*px0) : c##NOT_ZERO(*px0)) \
1563 } else if (!packed) { \
1565 d = (int_fast64_t) m * n - 1; \
1566 for (i = 0; i < m; ++i) { \
1568 for (j = i + 1; j < n; ++j) { \
1569 if (c##NOT_ZERO(*px0)) \
1578 for (i = 0; i < m; ++i) { \
1579 for (j = 0; j < i; ++j) { \
1580 if (c##NOT_ZERO(*px0)) \
1589 } else if (nu == '\0' || nu == 'N') { \
1591 d = PACKED_LENGTH((int_fast64_t) n) - 1; \
1592 for (i = 0; i < m; ++i) { \
1593 for (j = i; j <= i; ++j) { \
1594 if ((ct == 'C') ? c##NOT_ZERO_REAL(*px0) : c##NOT_ZERO(*px0)) \
1598 for (; j < n; ++j) { \
1599 if (c##NOT_ZERO(*px0)) \
1603 px0 -= (d -= i + 1); \
1608 for (i = 0; i < m; ++i) { \
1609 for (j = 0; j < i; ++j) { \
1610 if (c##NOT_ZERO(*px0)) \
1614 for (; j <= i; ++j) { \
1615 if ((ct == 'C') ? c##NOT_ZERO_REAL(*px0) : c##NOT_ZERO(*px0)) \
1619 px0 -= (d += m - i - 1); \
1625 d = PACKED_LENGTH((int_fast64_t) n) - 1; \
1626 for (i = 0; i < m; ++i) { \
1628 for (j = i + 1; j < n; ++j) { \
1629 if (c##NOT_ZERO(*px0)) \
1633 px0 -= (d -= i + 1); \
1638 for (i = 0; i < m; ++i) { \
1639 for (j = 0; j < i; ++j) { \
1640 if (c##NOT_ZERO(*px0)) \
1645 px0 -= (d += m - i - 1); \
1652#define TEMPLATE(c) \
1654 c##TYPE *px0 = c##PTR(x0); \
1657 DAS_BYCOL(c, ++nnz, DAS_CHECK); \
1660 DAS_BYROW(c, ++nnz, DAS_CHECK); \
1663 DAS_BYCOL(c, ++nnz, ); \
1675 PROTECT(i1 = Rf_allocVector(INTSXP, nnz));
1681 PROTECT(j1 = Rf_allocVector(INTSXP, nnz));
1687#define TEMPLATE(c) \
1689 c##TYPE *px0 = c##PTR(x0); \
1691 SEXP x1 = PROTECT(Rf_allocVector(c##TYPESXP, nnz)); \
1692 c##TYPE *px1 = c##PTR(x1); \
1699 c##IF_NPATTERN(*(px1++) = *px0); \
1706 c##IF_NPATTERN(*(px1++) = *px0); \
1714 c##IF_NPATTERN(*(px1++) = *px0); \
1721 SET_SLOT(to, Matrix_xSym, x1); \
1730 UNPROTECT(nprotect);
1746 char kind,
char shape,
char repr,
1749 char cl[] =
"...Matrix";
1750 cl[0] = (kind ==
'.') ?
class[0] : ((kind ==
',') ? ((
class[0] ==
'z') ?
'z' :
'd') : kind);
1755 int n =
DIM(from)[1];
1759 char nu =
DIAG(from);
1760 if (
cl[1] !=
'g' && ul !=
'U')
1762 if (
cl[1] ==
's' && ct !=
'C' &&
cl[0] ==
'z')
1764 if (
cl[1] ==
't' && nu !=
'N')
1770 SEXP p1 = PROTECT(
allocZero(INTSXP, (R_xlen_t) n + 1));
1776 SEXP p1 = PROTECT(
allocSeqInt(0, (R_xlen_t) n + 1));
1797 if (
cl[0] !=
class[0]) {
1798 if (
class[0] ==
'n' &&
cl[0] ==
'l')
1802 if (
class[0] ==
'n')
1811#define TEMPLATE(c) \
1813 c##TYPE *px0 = c##PTR(x0); \
1814 for (j = 0; j < n; ++j) { \
1815 if ((ct == 'C') ? c##NOT_ZERO_REAL(*px0) : c##NOT_ZERO(*px0)) \
1827 SEXP p1 = PROTECT(Rf_allocVector(INTSXP, (R_xlen_t) n + 1));
1828 int *pp1 = INTEGER(p1);
1832#define TEMPLATE(c) \
1834 c##TYPE *px0 = c##PTR(x0); \
1835 for (j = 0; j < n; ++j) { \
1836 if ((ct == 'C') ? c##NOT_ZERO_REAL(*px0) : c##NOT_ZERO(*px0)) \
1851 SEXP i1 = PROTECT(Rf_allocVector(INTSXP, nnz));
1852 int *pi1 = INTEGER(i1);
1859 for (j = 0; j < n; ++j)
1865#define TEMPLATE(c) \
1867 c##TYPE *px0 = c##PTR(x0); \
1869 SEXP x1 = PROTECT(Rf_allocVector(c##TYPESXP, nnz)); \
1870 c##TYPE *px1 = c##PTR(x1); \
1872 for (j = 0; j < n; ++j) { \
1873 if ((ct == 'C') ? c##NOT_ZERO_REAL(*px0) : c##NOT_ZERO(*px0)) { \
1882 SET_SLOT(to, Matrix_xSym, x1); \
1899 SEXP s_kind, SEXP s_shape, SEXP s_repr,
1900 SEXP s_uplo, SEXP s_trans)
1904 char kind, shape, repr;
1909 char ul =
'\0', ct =
'\0';
1920 char cl[] =
".g.Matrix";
1921 cl[0] = (kind ==
'.') ?
'n' : ((kind ==
',') ?
'd' : kind);
1922 cl[2] = (repr ==
'.') ? ((mg == 0) ?
'R' :
'C') : repr;
1925 int *pdim =
DIM(from), m = pdim[0], n = pdim[1],
1926 r = (mg == 0) ? m : n, s = (mg == 0) ? n : m;
1931 int *pperm = INTEGER(perm), k;
1934 SEXP i = PROTECT(Rf_allocVector(INTSXP, r)),
1935 j = PROTECT(Rf_allocVector(INTSXP, r));
1936 int *pi = INTEGER(i), *pj = INTEGER(j);
1937 for (k = 0; k < r; ++k) {
1939 *(pj++) = *(pperm++) - 1;
1944 }
else if ((
cl[2] ==
'C') == (mg != 0)) {
1945 SEXP p = PROTECT(Rf_allocVector(INTSXP, (R_xlen_t) r + 1)),
1946 i = PROTECT(Rf_allocVector(INTSXP, r));
1947 int *pp = INTEGER(p), *pi = INTEGER(i);
1948 for (k = 0; k < r; ++k) {
1950 *(pi++) = *(pperm++) - 1;
1957 SEXP p = PROTECT(Rf_allocVector(INTSXP, (R_xlen_t) s + 1));
1958 int *pp = INTEGER(p);
1959 memset(pp, 0,
sizeof(
int) * ((
size_t) s + 1));
1960 for (k = 0; k < r; ++k)
1962 for (k = 0; k < s; ++k)
1964 SEXP j = PROTECT(Rf_allocVector(INTSXP, r));
1965 int *pj = INTEGER(j), *work;
1967 memcpy(work, pp,
sizeof(
int) * (
size_t) s);
1969 for (k = 0; k < r; ++k)
1970 pj[work[pperm[k]]++] = k;
2004 else if (kind ==
',')
2005 kind = (
class[0] ==
'z') ?
'z' :
'd';
2006 if (kind ==
class[0])
2010 int packed =
class[2] ==
'p';
2012 char cl[] =
"...Matrix";
2016 if (
cl[1] ==
'p' &&
cl[0] !=
'z' &&
cl[0] !=
'd') {
2023 int *pdim =
DIM(from), m = pdim[0], n = pdim[1];
2026 if (
class[1] !=
'g' &&
UPLO(from) !=
'U')
2028 if (
class[1] ==
't' &&
DIAG(from) !=
'N')
2036 REPROTECT(x = Rf_coerceVector(x, tt), pid);
2037 if (
class[0] ==
'n')
2042 if (
class[0] ==
'n')
2046 if (
class[0] ==
'n') {
2048 int *px = LOGICAL(x);
2049 for (R_xlen_t k = 0, kend = XLENGTH(x); k < kend; ++k) {
2050 if (*(px++) == NA_LOGICAL) {
2080 else if (kind ==
',')
2081 kind = (
class[0] ==
'z') ?
'z' :
'd';
2082 if (kind ==
class[0])
2086 if (
class[2] ==
'T' && (
class[0] ==
'n' ||
class[0] ==
'l') &&
2087 kind !=
'n' && kind !=
'l') {
2094 char cl[] =
"...Matrix";
2098 if (
cl[1] ==
'p' &&
cl[0] !=
'z' &&
cl[0] !=
'd')
2102 int *pdim =
DIM(from), m = pdim[0], n = pdim[1];
2105 if (
class[1] !=
'g' &&
UPLO(from) !=
'U')
2107 if (
class[1] ==
't' &&
DIAG(from) !=
'N')
2111 if (
class[2] !=
'T') {
2114 nnz = INTEGER(p)[XLENGTH(p) - 1];
2118 if (
class[2] !=
'R') {
2125 if (
class[2] !=
'C') {
2132 if (
class[0] ==
'n') {
2136 }
else if (kind !=
'n') {
2140 REPROTECT(x = Rf_coerceVector(x, tt), pid);
2164 else if (kind ==
',')
2165 kind = (
class[0] ==
'z') ?
'z' :
'd';
2166 if (kind ==
class[0])
2170 char cl[] =
".diMatrix";
2174 int n =
DIM(from)[1];
2178 if (
DIAG(from) !=
'N')
2185 if (
class[0] ==
'n') {
2187 int *px = LOGICAL(x);
2188 for (
int j = 0; j < n; ++j) {
2189 if (*(px++) == NA_LOGICAL) {
2197 REPROTECT(x = Rf_coerceVector(x, tt), pid);
2198 if (
class[0] ==
'n')
2239 if (
class[1] ==
'g')
2242 int packed =
class[2] ==
'p';
2244 char cl[] =
".geMatrix";
2248 int n =
DIM(from)[1];
2249 if ((int_fast64_t) n * n > R_XLEN_T_MAX)
2250 Rf_error(
_(
"attempt to allocate vector of length exceeding %s"),
2255 char ul =
UPLO(from), ct =
'\0', nu =
'\0';
2256 if (
class[1] ==
's' &&
class[0] ==
'z')
2258 if (
class[1] ==
't')
2260 if (
class[1] !=
't')
2265 if (packed ||
new) {
2266 PROTECT(x1 = Rf_allocVector(
TYPEOF(x0), (R_xlen_t) n * n));
2270#define TEMPLATE(c) \
2272 c##TYPE *px0 = c##PTR(x0), *px1 = c##PTR(x1); \
2274 c##NAME( pack1)(px1, px0, (size_t) n, ul, ct, nu); \
2276 c##NAME(force2)(px1, px0, (size_t) n, ul, ct, nu); \
2278 c##NAME(force2)(px1, NULL, (size_t) n, ul, ct, nu); \
2287 UNPROTECT(nprotect);
2300 if (
class[1] ==
'g')
2303 char cl[] =
".g.Matrix";
2308 int n =
DIM(from)[1];
2312 if (
class[1] ==
't' &&
DIAG(from) ==
'N') {
2313 if (
class[2] !=
'T')
2315 if (
class[2] !=
'R')
2317 if (
class[2] !=
'C')
2319 if (
class[0] !=
'n')
2325 char ul =
UPLO(from), ct =
'\0';
2326 if (
class[1] ==
's' &&
class[0] ==
'z')
2328 if (
class[1] !=
't')
2331 if (
class[2] !=
'T') {
2335 i0 = PROTECT(
GET_SLOT(from, iSym)),
2336 p1 = PROTECT(Rf_allocVector(INTSXP, XLENGTH(p0)));
2337 int *pp0 = INTEGER(p0), *pi0 = INTEGER(i0), *pp1 = INTEGER(p1),
2339 pp0++; *(pp1++) = 0;
2342 if (
class[1] ==
's' ||
class[1] ==
'p') {
2343 memset(pp1, 0,
sizeof(
int) * (
size_t) n);
2344 for (j = 0, k = 0; j < n; ++j) {
2352 for (j = 1; j < n; ++j)
2353 pp1[j] += pp1[j - 1];
2354 if (pp1[n - 1] > INT_MAX - pp0[n - 1])
2355 Rf_error(
_(
"attempt to construct %s with more than %s nonzero entries"),
2356 "sparseMatrix",
"2^31-1");
2357 for (j = 0; j < n; ++j)
2360 if (n > INT_MAX - pp0[n - 1])
2361 Rf_error(
_(
"attempt to construct %s with more than %s nonzero entries"),
2362 "sparseMatrix",
"2^31-1");
2363 for (j = 0; j < n; ++j)
2364 pp1[j] = pp0[j] + j + 1;
2367 SEXP i1 = PROTECT(Rf_allocVector(INTSXP, pp1[n - 1]));
2368 int *pi1 = INTEGER(i1);
2371#define TEMPLATE(c) \
2374 SEXP x0 = PROTECT(GET_SLOT(from, Matrix_xSym)), \
2375 x1 = PROTECT(Rf_allocVector(c##TYPESXP, pp1[n - 1])); \
2376 c##TYPE *px0 = c##PTR(x0), *px1 = c##PTR(x1); \
2378 if (class[1] == 's' || class[1] == 'p') { \
2380 Matrix_Calloc(pp1_, n, int); \
2381 memcpy(pp1_, pp1 - 1, sizeof(int) * (size_t) n); \
2383 for (j = 0, k = 0; j < n; ++j) { \
2385 while (k < kend) { \
2386 if (pi0[k] == j) { \
2387 pi1[pp1_[j]] = pi0[k]; \
2389 c##ASSIGN_PROJ_REAL(px1[pp1_[j]], px0[k]); \
2393 pi1[pp1_[j]] = pi0[k]; \
2395 c##ASSIGN_IDEN(px1[pp1_[j]], px0[k]); \
2398 pi1[pp1_[pi0[k]]] = j; \
2400 c##ASSIGN_CONJ(px1[pp1_[pi0[k]]], px0[k]); \
2408 for (j = 0, k = 0; j < n; ++j) { \
2410 while (k < kend) { \
2411 if (pi0[k] == j) { \
2412 pi1[pp1_[j]] = pi0[k]; \
2414 c##ASSIGN_IDEN(px1[pp1_[j]], px0[k]); \
2418 pi1[pp1_[j]] = pi0[k]; \
2420 c##ASSIGN_IDEN(px1[pp1_[j]], px0[k]); \
2423 pi1[pp1_[pi0[k]]] = j; \
2425 c##ASSIGN_IDEN(px1[pp1_[pi0[k]]], px0[k]); \
2433 Matrix_Free(pp1_, n); \
2434 } else if ((class[2] == 'C') == (ul == 'U')) { \
2435 for (j = 0, k = 0; j < n; ++j) { \
2437 while (k < kend) { \
2438 *(pi1++) = *(pi0++); \
2440 *(px1++) = *(px0++); \
2446 *(px1++) = c##UNIT; \
2450 for (j = 0, k = 0; j < n; ++j) { \
2454 *(px1++) = c##UNIT; \
2456 while (k < kend) { \
2457 *(pi1++) = *(pi0++); \
2459 *(px1++) = *(px0++); \
2466 SET_SLOT(to, Matrix_xSym, x1); \
2481 int *pi0 = INTEGER(i0), *pj0 = INTEGER(j0), j;
2482 R_xlen_t k, nnz0 = XLENGTH(i0), nnz1;
2484 if (
class[1] ==
's' ||
class[1] ==
'p') {
2486 for (k = 0; k < nnz0; ++k)
2487 if (pi0[k] == pj0[k])
2491 if (nnz1 > R_XLEN_T_MAX - nnz0)
2492 Rf_error(
_(
"attempt to allocate vector of length exceeding %s"),
2496 SEXP i1 = PROTECT(Rf_allocVector(INTSXP, nnz1)),
2497 j1 = PROTECT(Rf_allocVector(INTSXP, nnz1));
2498 int *pi1 = INTEGER(i1), *pj1 = INTEGER(j1);
2502#define TEMPLATE(c) \
2505 SEXP x0 = PROTECT(GET_SLOT(from, Matrix_xSym)), \
2506 x1 = PROTECT(Rf_allocVector(c##TYPESXP, nnz1)); \
2507 c##TYPE *px0 = c##PTR(x0), *px1 = c##PTR(x1); \
2509 if (class[1] == 's' || class[1] == 'p') { \
2511 for (k = 0; k < nnz0; ++k) { \
2512 if (*pi0 == *pj0) { \
2516 c##ASSIGN_PROJ_REAL(*px1, *px0); \
2525 c##ASSIGN_IDEN(*px1, *px0); \
2527 c##ASSIGN_CONJ(*px1, *px0); \
2531 ++pi0; ++pj0; c##IF_NPATTERN(++px0); \
2534 for (k = 0; k < nnz0; ++k) { \
2535 if (*pi0 == *pj0) { \
2539 c##ASSIGN_IDEN(*px1, *px0); \
2548 c##ASSIGN_IDEN(*px1, *px0); \
2550 c##ASSIGN_IDEN(*px1, *px0); \
2554 ++pi0; ++pj0; c##IF_NPATTERN(++px0); \
2558 memcpy(pi1, pi0, sizeof( int) * (size_t) nnz0); \
2559 memcpy(pj1, pj0, sizeof( int) * (size_t) nnz0); \
2563 memcpy(px1, px0, sizeof(c##TYPE) * (size_t) nnz0); \
2566 for (j = 0; j < n; ++j) { \
2567 *(pi1++) = *(pj1++) = j; \
2569 *(px1++) = c##UNIT; \
2574 SET_SLOT(to, Matrix_xSym, x1); \
2600 if (
class[2] !=
'p')
2603 char cl[] =
"...Matrix";
2606 cl[2] = (
class[1] ==
's') ?
'y' : ((
class[1] ==
'p') ?
'o' :
'r');
2609 int n =
DIM(from)[0];
2610 if ((int_fast64_t) n * n > R_XLEN_T_MAX)
2611 Rf_error(
_(
"attempt to allocate vector of length exceeding %s"),
2616 char ul =
UPLO(from);
2619 if (
cl[1] ==
's' &&
cl[0] ==
'z' &&
TRANS(from) !=
'C')
2621 if (
cl[1] ==
't' &&
DIAG(from) !=
'N')
2629 x1 = PROTECT(Rf_allocVector(
TYPEOF(x0), (R_xlen_t) n * n));
2631#define TEMPLATE(c) \
2633 c##TYPE *px0 = c##PTR(x0), *px1 = c##PTR(x1); \
2634 c##NAME(pack1)(px1, px0, (size_t) n, ul, '\0', 'N'); \
2656 if (
class[2] ==
'p')
2659 char cl[] =
"...Matrix";
2663 if (
class[1] ==
'g')
2664 cl[1] = (nu ==
'\0') ?
's' :
't';
2667 int *pdim =
DIM(from), n = pdim[1];
2669 Rf_error(
_(
"attempt to pack non-square matrix"));
2673 if (
class[1] ==
'g') {
2680 if (
cl[1] ==
's' && ct !=
'C' &&
cl[0] ==
'z')
2682 if (
cl[1] ==
't' && nu !=
'N')
2685 if ((ul =
UPLO(from)) !=
'U')
2687 if (
cl[1] ==
's' &&
cl[0] ==
'z' && (ct =
TRANS(from)) !=
'C')
2689 if (
cl[1] ==
't' && (nu =
DIAG(from)) !=
'N')
2700#define TEMPLATE(c) \
2702 c##TYPE *px0 = c##PTR(x0), *px1 = c##PTR(x1); \
2703 c##NAME(pack2)(px1, px0, (size_t) n, ul, '\0', 'N'); \
2721 char ul =
'\0', ct =
'\0', nu =
'\0';
2722 if (
class[1] ==
'g') {
2723 if (s_uplo != R_NilValue)
VALID_UPLO (s_uplo , ul);
2724 if (s_trans != R_NilValue)
VALID_TRANS(s_trans, ct);
2725 if (s_diag != R_NilValue)
VALID_DIAG (s_diag , nu);
2733 if (
class[2] ==
'C')
2736 char cl[] =
"..CMatrix";
2741 int *pdim =
DIM(from), m = pdim[0], n = pdim[1];
2744 if (
class[1] !=
'g' &&
UPLO(from) !=
'U')
2746 if (
class[1] ==
's' &&
class[0] ==
'z' &&
TRANS(from) !=
'C')
2748 if (
class[1] ==
't' &&
DIAG(from) !=
'N')
2750 if (
class[1] !=
't')
2753 if (
class[2] ==
'R') {
2756 p1 = PROTECT(Rf_allocVector(INTSXP, (R_xlen_t) n + 1)),
2757 i1 = PROTECT(Rf_allocVector(INTSXP, INTEGER(p0)[m]));
2758 int *pp0 = INTEGER(p0), *pj0 = INTEGER(j0),
2759 *pp1 = INTEGER(p1), *pi1 = INTEGER(i1), *iwork = NULL;
2764#define TEMPLATE(c) \
2766 c##TYPE *px0 = NULL, *px1 = NULL; \
2768 SEXP x0 = PROTECT(GET_SLOT(from, Matrix_xSym)), \
2769 x1 = PROTECT(Rf_allocVector(c##TYPESXP, INTEGER(p0)[m])); \
2772 SET_SLOT(to, Matrix_xSym, x1); \
2775 c##csptrans(pp1, pi1, px1, pp0, pj0, px0, n, m, 'T', iwork); \
2786 if (XLENGTH(i0) > INT_MAX)
2787 Rf_error(
_(
"number of triplets to be aggregated exceeds %s"),
2789 int *pi0 = INTEGER(i0), nnz = (int) XLENGTH(i0);
2792 int *pj0 = INTEGER(j0), *pp1 = NULL, *pi1 = NULL, *iwork = NULL;
2793 size_t liwork = (size_t) ((int_fast64_t) m + 1 + m + n + nnz),
2794 lwork = (size_t) nnz;
2797#define TEMPLATE(c) \
2799 c##TYPE *px0 = NULL, *px1 = NULL, *work = NULL; \
2801 SEXP x0 = PROTECT(GET_SLOT(from, Matrix_xSym)); \
2803 Matrix_Calloc(work, lwork, c##TYPE); \
2805 c##tspsort(pp1, pi1, px1, pi0, pj0, px0, m, n, &nnz, iwork, work); \
2806 PROTECT(p1 = Rf_allocVector(INTSXP, (R_xlen_t) n + 1)), \
2807 PROTECT(i1 = Rf_allocVector(INTSXP, nnz)); \
2808 pp1 = INTEGER(p1); \
2809 pi1 = INTEGER(i1); \
2810 SET_SLOT(to, Matrix_pSym, p1); \
2811 SET_SLOT(to, Matrix_iSym, i1); \
2813 SEXP x1 = PROTECT(Rf_allocVector(c##TYPESXP, nnz)); \
2815 SET_SLOT(to, Matrix_xSym, x1); \
2817 c##tspsort(pp1, pi1, px1, pi0, pj0, px0, m, n, &nnz, iwork, work); \
2823 Matrix_Free(work, lwork); \
2849 if (
class[2] ==
'R')
2852 char cl[] =
"..RMatrix";
2857 int *pdim =
DIM(from), m = pdim[0], n = pdim[1];
2860 if (
class[1] !=
'g' &&
UPLO(from) !=
'U')
2862 if (
class[1] ==
's' &&
class[0] ==
'z' &&
TRANS(from) !=
'C')
2864 if (
class[1] ==
't' &&
DIAG(from) !=
'N')
2866 if (
class[1] !=
't')
2869 if (
class[2] ==
'C') {
2872 p1 = PROTECT(Rf_allocVector(INTSXP, (R_xlen_t) m + 1)),
2873 j1 = PROTECT(Rf_allocVector(INTSXP, INTEGER(p0)[n]));
2874 int *pp0 = INTEGER(p0), *pi0 = INTEGER(i0),
2875 *pp1 = INTEGER(p1), *pj1 = INTEGER(j1), *iwork = NULL;
2880#define TEMPLATE(c) \
2882 c##TYPE *px0 = NULL, *px1 = NULL; \
2884 SEXP x0 = PROTECT(GET_SLOT(from, Matrix_xSym)), \
2885 x1 = PROTECT(Rf_allocVector(c##TYPESXP, INTEGER(p0)[n])); \
2888 SET_SLOT(to, Matrix_xSym, x1); \
2891 c##csptrans(pp1, pj1, px1, pp0, pi0, px0, m, n, 'T', iwork); \
2902 if (XLENGTH(i0) > INT_MAX)
2903 Rf_error(
_(
"number of triplets to be aggregated exceeds %s"),
2905 int *pi0 = INTEGER(i0), nnz = (int) XLENGTH(i0);
2908 int *pj0 = INTEGER(j0), *pp1 = NULL, *pj1 = NULL, *iwork = NULL;
2909 size_t liwork = (size_t) ((int_fast64_t) n + 1 + n + m + nnz),
2910 lwork = (size_t) nnz;
2913#define TEMPLATE(c) \
2915 c##TYPE *px0 = NULL, *px1 = NULL, *work = NULL; \
2917 SEXP x0 = PROTECT(GET_SLOT(from, Matrix_xSym)); \
2919 Matrix_Calloc(work, nnz, c##TYPE); \
2921 c##tspsort(pp1, pj1, px1, pj0, pi0, px0, n, m, &nnz, iwork, work); \
2922 PROTECT(p1 = Rf_allocVector(INTSXP, (R_xlen_t) m + 1)), \
2923 PROTECT(j1 = Rf_allocVector(INTSXP, nnz)); \
2924 pp1 = INTEGER(p1); \
2925 pj1 = INTEGER(j1); \
2926 SET_SLOT(to, Matrix_pSym, p1); \
2927 SET_SLOT(to, Matrix_jSym, j1); \
2929 SEXP x1 = PROTECT(Rf_allocVector(c##TYPESXP, nnz)); \
2931 SET_SLOT(to, Matrix_xSym, x1); \
2933 c##tspsort(pp1, pj1, px1, pj0, pi0, px0, n, m, &nnz, iwork, work); \
2939 Matrix_Free(work, lwork); \
2965 if (
class[2] ==
'T')
2968 char cl[] =
"..TMatrix";
2973 int *pdim =
DIM(from), m = pdim[0], n = pdim[1];
2976 if (
class[1] !=
'g' &&
UPLO(from) !=
'U')
2978 if (
class[1] ==
's' &&
class[0] ==
'z' &&
TRANS(from) !=
'C')
2980 if (
class[1] ==
't' &&
DIAG(from) !=
'N')
2982 if (
class[1] !=
't')
2985 if (
class[2] ==
'R')
2989 int *pp0 = INTEGER(p0), nnz = pp0[n];
2993 i0 = PROTECT(
GET_SLOT(from, iSym));
2994 if (XLENGTH(i0) == nnz)
2997 SEXP i1 = PROTECT(Rf_allocVector(INTSXP, nnz));
2998 memcpy(INTEGER(i1), INTEGER(i0),
sizeof(
int) * (
size_t) nnz);
3004 j1 = PROTECT(Rf_allocVector(INTSXP, nnz));
3005 int *pj1 = INTEGER(j1), j, k, kend;
3007 for (j = 0, k = 0; j < n; ++j) {
3013 if (
class[0] !=
'n') {
3015 if (XLENGTH(x0) == nnz)
3018 SEXP x1 = PROTECT(Rf_allocVector(
TYPEOF(x0), nnz));
3020#define TEMPLATE(c) \
3021 memcpy(c##PTR(x1), c##PTR(x0), sizeof(c##TYPE) * (size_t) nnz)
3046 R_xlen_t vlen = XLENGTH(from);
3047 if (vlen > (1LL << 53))
3048 Rf_error(
_(
"%s length cannot exceed %s"),
"sparseVector",
"2^53");
3050 SEXPTYPE tf =
TYPEOF(from);
3051 char cl[] =
".sparseVector";
3052 cl[0] = (kind ==
'.') ?
typeToKind(tf) : ((kind ==
',') ? ((tf == CPLXSXP) ?
'z' :
'd') : kind);
3059 SEXP length = PROTECT(Rf_allocVector((vlen <= INT_MAX) ? INTSXP : REALSXP, 1));
3060 if (
TYPEOF(length) == INTSXP)
3061 INTEGER(length)[0] = (int) vlen;
3063 REAL(length)[0] = (double) vlen;
3068 R_xlen_t k, nnz = 0;
3070#define TEMPLATE(c) \
3072 c##TYPE *px0 = c##PTR(x0); \
3073 for (k = 0; k < vlen; ++k) \
3074 if (c##NOT_ZERO(px0[k])) \
3082 SEXP i1 = PROTECT(Rf_allocVector(
TYPEOF(length), nnz));
3085#define TEMPLATE(c) \
3087 if (TYPEOF(i1) == INTSXP) \
3093#define TEMPLATE__(d, c) \
3095 d##TYPE *pi1 = d##PTR(i1); \
3096 c##TYPE *px0 = c##PTR(x0); \
3098 SEXP x1 = PROTECT(Rf_allocVector(c##TYPESXP, nnz)); \
3099 c##TYPE *px1 = c##PTR(x1); \
3101 for (k = 0; k < vlen; ++k) \
3102 if (c##NOT_ZERO(px0[k])) { \
3103 *(pi1++) = (d##TYPE) (k + 1); \
3105 *(px1++) = px0[k]; \
3109 PROTECT(x1 = Rf_coerceVector(x1, tt)); \
3110 SET_SLOT(to, Matrix_xSym, x1); \
3126 switch (
TYPEOF(s_from)) {
3145 int *pdim =
DIM(from), m = pdim[0], n = pdim[1];
3146 int_fast64_t mn = (int_fast64_t) m * n;
3147 if (mn > (1LL << 53))
3148 Rf_error(
_(
"%s length cannot exceed %s"),
"sparseVector",
"2^53");
3150 char cl[] =
".sparseVector";
3157 SEXP length = PROTECT(Rf_allocVector((mn <= INT_MAX) ? INTSXP : REALSXP, 1));
3158 if (
TYPEOF(length) == INTSXP)
3159 INTEGER(length)[0] = (int) mn;
3161 REAL(length)[0] = (double) mn;
3166 PROTECT_WITH_INDEX(from, &pid);
3167 if (
class[2] ==
'T') {
3174 int *pp0 = INTEGER(p0), nnz = pp0[(
class[2] ==
'C') ? n : m];
3181 SEXP i1 = PROTECT(Rf_allocVector(
TYPEOF(length), nnz));
3184#define TEMPLATE(c) \
3186 if (TYPEOF(i1) == INTSXP) \
3192 if (
class[2] ==
'C') {
3194 int *pi0 = INTEGER(i0), j, k, kend;
3196#define TEMPLATE__(d, c) \
3198 d##TYPE *pi1 = d##PTR(i1), l = (d##TYPE) 1, dl = (d##TYPE) m; \
3200 SEXP x0 = PROTECT(GET_SLOT(from, Matrix_xSym)), \
3201 x1 = PROTECT(Rf_allocVector(c##TYPESXP, nnz)); \
3202 c##TYPE *px0 = c##PTR(x0), *px1 = c##PTR(x1); \
3203 SET_SLOT(to, Matrix_xSym, x1); \
3206 for (j = 0, k = 0; j < n; ++j) { \
3208 while (k < kend) { \
3210 *(px1++) = px0[k]; \
3212 *(pi1++) = pi0[k] + l; \
3226 int *pj0 = INTEGER(j0), i, j, k, kend, *iwork = NULL;
3227 size_t liwork = (size_t) n + 1;
3231 for (k = 0; k < nnz; ++k)
3233 for (j = 0; j < n; ++j)
3234 iwork[j] += iwork[j - 1];
3237#define TEMPLATE__(d, c) \
3239 d##TYPE *pi1 = d##PTR(i1); \
3241 SEXP x0 = PROTECT(GET_SLOT(from, Matrix_xSym)), \
3242 x1 = PROTECT(Rf_allocVector(c##TYPESXP, nnz)); \
3243 c##TYPE *px0 = c##PTR(x0), *px1 = c##PTR(x1); \
3244 SET_SLOT(to, Matrix_xSym, x1); \
3247 for (i = 0, k = 0; i < m; ++i) { \
3249 while (k < kend) { \
3252 px1[iwork[j] ] = px0[k]; \
3254 pi1[iwork[j]++] = i + 1 + j * (d##TYPE) m; \
3283 int n =
DIM(from)[0];
3284 int_fast64_t nn = (int_fast64_t) n * n;
3285 if (nn > (1LL << 53))
3286 Rf_error(
_(
"%s length cannot exceed %s"),
"sparseVector",
"2^53");
3288 char cl[] =
".sparseVector";
3295 SEXP length = PROTECT(Rf_allocVector((nn <= INT_MAX) ? INTSXP : REALSXP, 1));
3296 if (
TYPEOF(length) == INTSXP)
3297 INTEGER(length)[0] = (int) nn;
3299 REAL(length)[0] = (double) nn;
3305 char nu =
DIAG(from);
3312#define TEMPLATE(c) \
3314 c##TYPE *px0 = c##PTR(x0); \
3315 for (j = 0; j < n; ++j) \
3316 if (c##NOT_ZERO(px0[j])) \
3326 SEXP i1 = PROTECT(Rf_allocVector(
TYPEOF(length), nnz1));
3329#define TEMPLATE(c) \
3331 if (TYPEOF(i1) == INTSXP) \
3337#define TEMPLATE__(d, c) \
3339 d##TYPE *pi1 = d##PTR(i1), l = (d##TYPE) 1, dl = (d##TYPE) (n + 1); \
3340 c##TYPE *px0 = c##PTR(x0); \
3342 SEXP x1 = PROTECT(Rf_allocVector(c##TYPESXP, nnz1)); \
3343 c##TYPE *px1 = c##PTR(x1); \
3344 SET_SLOT(to, Matrix_xSym, x1); \
3347 for (j = 0; j < n; ++j) { \
3348 if (nu != 'N' || c##NOT_ZERO(px0[j])) { \
3351 *(px1++) = (nu != 'N') ? c##UNIT : px0[j]; \
3376 int *pdim =
DIM(from), m = pdim[0], n = pdim[1];
3377 int_fast64_t mn = (int_fast64_t) m * n;
3378 if (mn > (1LL << 53))
3379 Rf_error(
_(
"%s length cannot exceed %s"),
"sparseVector",
"2^53");
3386 SEXP length = PROTECT(Rf_allocVector((mn <= INT_MAX) ? INTSXP : REALSXP, 1));
3387 if (
TYPEOF(length) == INTSXP)
3388 INTEGER(length)[0] = (int) mn;
3390 REAL(length)[0] = (double) mn;
3395 int *pperm = INTEGER(perm), mg =
MARGIN(from);
3397 SEXP i1 = PROTECT(Rf_allocVector(
TYPEOF(length), (mg == 0) ? m : n));
3401 int i, j, *iwork = NULL;
3402 size_t liwork = (size_t) n + 1;
3404 for (i = 0; i < m; ++i)
3406 for (j = 1; j <= n; ++j)
3407 iwork[j] += iwork[j - 1];
3408 if (
TYPEOF(i1) == INTSXP) {
3409 int *pi1 = INTEGER(i1);
3410 for (i = 0; i < m; ++i) {
3412 pi1[iwork[j]++] = i + 1 + j * m;
3415 double *pi1 = REAL(i1);
3416 for (i = 0; i < m; ++i) {
3418 pi1[iwork[j]++] = i + 1 + j * (double) m;
3424 if (
TYPEOF(i1) == INTSXP) {
3425 int *pi1 = INTEGER(i1);
3426 for (j = 0; j < n; ++j)
3427 pi1[j] = pperm[j] + j * m;
3429 double *pi1 = REAL(i1);
3430 for (j = 0; j < n; ++j)
3431 pi1[j] = pperm[j] + j * (
double) m;
3451 SEXP to = R_NilValue;
3453 PROTECT_WITH_INDEX(s_from, &pid);
3458 if (
class[0] ==
'n') {
3459 int *px = LOGICAL(to);
3460 for (R_xlen_t k = 0, kend = XLENGTH(to); k < kend; ++k) {
3461 if (*(px++) == NA_LOGICAL) {
3484 REPROTECT(s_from =
diagonal_as_dense(s_from,
class,
'.',
'g', 0,
'\0',
'\0'), pid);
3501 if (
class[0] ==
'n') {
3520 SEXP to = R_NilValue;
3522 PROTECT_WITH_INDEX(s_from, &pid);
3544 REPROTECT(s_from =
diagonal_as_dense(s_from,
class,
'.',
'g', 0,
'\0',
'\0'), pid);
3557 Rf_setAttrib(to, R_DimSymbol, dim);
3560 SEXP dimnames = PROTECT(
DIMNAMES(s_from, 0));
3562 Rf_setAttrib(to, R_DimNamesSymbol, dimnames);
3571 if (
class[0] ==
'n')
3615 Rf_error(
_(
"attempt to pack a %s"),
"generalMatrix");
3630 Rf_error(
_(
"attempt to pack an %s"),
"indMatrix");
3757 if (sparse == NA_LOGICAL || sparse == 0)
3779 if (sparse == NA_LOGICAL)
3781 else if (sparse != 0)
3787 if (sparse == NA_LOGICAL || sparse != 0)
#define SWITCH5(c, template)
#define SWITCH4(c, template)
const char * valid_vector[]
#define SWAP(a, b, t, op)
const char * valid_diagonal[]
#define VALID_LOGIC3(s, d)
SEXP allocUnit(SEXPTYPE, R_xlen_t)
const char * valid_dense[]
#define Matrix_Calloc(p, n, t)
#define SET_DIMNAMES(x, mode, value)
SEXP duplicateVector(SEXP)
char typeToKind(SEXPTYPE)
#define VALID_SHAPE(s, c)
SEXP allocZero(SEXPTYPE, R_xlen_t)
const char * Matrix_class(SEXP, const char **, int, const char *)
SEXPTYPE kindToType(char)
#define Matrix_Free(p, n)
#define DIMNAMES(x, mode)
SEXP allocSeqInt(int, R_xlen_t)
#define ERROR_INVALID_TYPE(_X_, _FUNC_)
#define VALID_REPR(s, c, dot)
const char * valid_index[]
#define GET_SLOT(x, name)
#define VALID_TRANS(s, c)
SEXP newObject(const char *)
const char * valid_matrix[]
#define COPY_SLOT(dest, src, name)
const char * valid_sparse[]
#define SET_SLOT(x, name, value)
#define VALID_LOGIC2(s, d)
#define VALID_MARGIN(s, d)
SEXP sparse_aggregate(SEXP from, const char *class)
int DimNames_is_trivial(SEXP dn)
SEXP R_matrix_as_dense(SEXP s_from, SEXP s_zzz, SEXP s_uplo, SEXP s_trans, SEXP s_diag, SEXP s_margin)
SEXP R_dense_as_packed(SEXP s_from, SEXP s_uplo, SEXP s_trans, SEXP s_diag)
SEXP sparse_as_Csparse(SEXP from, const char *class)
SEXP R_index_as_kind(SEXP s_from, SEXP s_kind)
SEXP sparse_as_Rsparse(SEXP from, const char *class)
SEXP R_Matrix_as_packed(SEXP s_from)
SEXP Vector_as_sparse(SEXP from, const char *zzz, char ul, char ct, char nu, int m, int n, int byrow, SEXP dimnames)
SEXP R_diagonal_as_dense(SEXP s_from, SEXP s_kind, SEXP s_shape, SEXP s_packed, SEXP s_uplo, SEXP s_trans)
SEXP R_vector_as_Vector(SEXP s_from, SEXP s_kind)
SEXP R_Vector_as_sparse(SEXP s_from, SEXP s_zzz, SEXP s_uplo, SEXP s_trans, SEXP s_diag, SEXP s_m, SEXP s_n, SEXP s_byrow, SEXP s_dimnames)
SEXP R_Matrix_as_vector(SEXP s_from)
SEXP R_vector_as_dense(SEXP s_from, SEXP s_zzz, SEXP s_uplo, SEXP s_trans, SEXP s_diag, SEXP s_m, SEXP s_n, SEXP s_byrow, SEXP s_dimnames)
SEXP R_matrix_as_sparse(SEXP s_from, SEXP s_zzz, SEXP s_uplo, SEXP s_trans, SEXP s_diag, SEXP s_margin)
SEXP R_dense_as_kind(SEXP s_from, SEXP s_kind)
SEXP diagonal_as_dense(SEXP from, const char *class, char kind, char shape, int packed, char ul, char ct)
SEXP R_diagonal_as_Vector(SEXP s_from)
SEXP R_Matrix_as_matrix(SEXP s_from)
SEXP R_dense_as_general(SEXP s_from)
SEXP R_Matrix_as_Rsparse(SEXP s_from)
SEXP sparse_as_dense(SEXP from, const char *class, int packed)
SEXP R_sparse_as_Tsparse(SEXP s_from)
SEXP R_diagonal_as_sparse(SEXP s_from, SEXP s_kind, SEXP s_shape, SEXP s_repr, SEXP s_uplo, SEXP s_trans)
SEXP dense_as_sparse(SEXP from, const char *class, char repr)
SEXP R_sparse_as_kind(SEXP s_from, SEXP s_kind)
SEXP R_Matrix_as_kind(SEXP s_from, SEXP s_kind, SEXP s_sparse)
SEXP sparse_as_kind(SEXP from, const char *class, char kind)
SEXP dense_as_kind(SEXP from, const char *class, char kind, int new)
SEXP R_Matrix_as_general(SEXP s_from, SEXP s_kind)
SEXP diagonal_as_kind(SEXP from, const char *class, char kind)
SEXP sparse_as_Vector(SEXP from, const char *class)
SEXP R_dense_as_unpacked(SEXP s_from)
SEXP index_as_dense(SEXP from, const char *class, char kind)
SEXP diagonal_as_Vector(SEXP from, const char *class)
SEXP vector_as_Vector(SEXP from, char kind)
SEXP R_index_as_sparse(SEXP s_from, SEXP s_kind, SEXP s_repr)
SEXP R_diagonal_as_kind(SEXP s_from, SEXP s_kind)
SEXP diagonal_as_sparse(SEXP from, const char *class, char kind, char shape, char repr, char ul, char ct)
SEXP dense_as_packed(SEXP from, const char *class, char ul, char ct, char nu)
SEXP R_sparse_as_dense(SEXP s_from, SEXP s_packed)
SEXP matrix_as_sparse(SEXP from, const char *zzz, char ul, char ct, char nu, int mg)
SEXP sparse_as_Tsparse(SEXP from, const char *class)
SEXP R_dense_as_sparse(SEXP s_from, SEXP s_repr)
SEXP R_sparse_as_Csparse(SEXP s_from)
SEXP R_Matrix_as_Tsparse(SEXP s_from)
SEXP R_Matrix_as_Csparse(SEXP s_from)
SEXP R_Matrix_as_Vector(SEXP s_from)
SEXP R_Matrix_as_unpacked(SEXP s_from)
SEXP R_sparse_as_Vector(SEXP s_from)
SEXP dense_as_unpacked(SEXP from, const char *class)
SEXP dense_as_general(SEXP from, const char *class, int new)
SEXP R_sparse_as_Rsparse(SEXP s_from)
SEXP sparse_as_general(SEXP from, const char *class)
SEXP R_index_as_Vector(SEXP s_from)
SEXP index_as_Vector(SEXP from, const char *class)
SEXP R_sparse_as_general(SEXP s_from)
SEXP vector_as_dense(SEXP from, const char *zzz, char ul, char ct, char nu, int m, int n, int byrow, SEXP dimnames)
SEXP index_as_kind(SEXP from, const char *class, char kind)
SEXP R_index_as_dense(SEXP s_from, SEXP s_kind)
SEXP matrix_as_dense(SEXP from, const char *zzz, char ul, char ct, char nu, int mg, int new)
SEXP index_as_sparse(SEXP from, const char *class, char kind, char repr)