5SEXP
dense_band(SEXP from,
const char *
class,
int a,
int b)
11 int *pdim = INTEGER(dim), m = pdim[0], n = pdim[1];
15 if (a <= 1 - m && b >= n - 1 &&
16 (
class[1] ==
't' || m != n || m > 1 || n > 1))
19 int ge = 0, sy = 0, tr = 0;
20 ge = m != n || !((tr = a >= 0 || b <= 0 ||
class[1] ==
't') ||
21 (sy = a == -b &&
class[1] ==
's'));
23#define BAND_CASES(_DO_) \
28 _DO_(i, int, LOGICAL); \
31 _DO_(i, int, INTEGER); \
34 _DO_(d, double, REAL); \
37 _DO_(z, Rcomplex, COMPLEX); \
44#define BAND2(_PREFIX_, _CTYPE_, _PTR_) \
45 _PREFIX_ ## band2(_PTR_(x1), m, n, a, b, di)
47#define BAND1(_PREFIX_, _CTYPE_, _PTR_) \
48 _PREFIX_ ## band1(_PTR_(x1), n, a, b, ul1, di)
50#define DCPY2(_PREFIX_, _CTYPE_, _PTR_) \
52 _CTYPE_ *px0 = _PTR_(x0), *px1 = _PTR_(x1); \
53 Matrix_memset(px1, 0, XLENGTH(x1), sizeof(_CTYPE_)); \
54 if (a <= 0 && b >= 0) \
55 _PREFIX_ ## dcpy2(px1, px0, n, XLENGTH(x1), 'U', di); \
58#define DCPY1(_PREFIX_, _CTYPE_, _PTR_) \
60 _CTYPE_ *px0 = _PTR_(x0), *px1 = _PTR_(x1); \
61 Matrix_memset(px1, 0, XLENGTH(x1), sizeof(_CTYPE_)); \
62 if (a <= 0 && b >= 0) \
63 _PREFIX_ ## dcpy1(px1, px0, n, XLENGTH(x1), ul1, ul0, di); \
66 char ul0 =
'U', ul1 =
'U', di =
'N';
67 if (
class[1] !=
'g') {
77 ul0 = *CHAR(STRING_ELT(uplo, 0));
80 if (
class[1] ==
't') {
83 ? (a <= 0 && b >= n - 1) : (b >= 0 && a <= 1 - m))
85 else if (a <= 0 && b >= 0) {
87 di = *CHAR(STRING_ELT(diag, 0));
93 char cl[] =
"...Matrix";
95 cl[1] = (ge) ?
'g' : ((sy) ?
's' :
't') ;
96 cl[2] = (ge) ?
'e' : ((
class[2] !=
'p') ? ((sy) ?
'y' :
'r') :
'p');
105 if (
class[1] !=
's' || sy)
114 PROTECT(x1 = duplicate(x0));
115 if (ATTRIB(x1) != R_NilValue) {
116 SET_ATTRIB(x1, R_NilValue);
128 ul1 = (tr &&
class[1] !=
't') ? ((a >= 0) ?
'U' :
'L') : ul0;
130 SEXP uplo = PROTECT(mkString(
"L"));
135 SEXP diag = PROTECT(mkString(
"U"));
140 if (tr &&
class[1] ==
't') {
141 if ((ul0 ==
'U') ? (b <= 0) : (a >= 0)) {
143 PROTECT(x1 = allocVector(TYPEOF(x0), XLENGTH(x0)));
149 PROTECT(x1 = duplicate(x0));
156 if (sy || (tr && (
class[1] ==
'g' || ul0 == ul1 || n <= 1))) {
157 PROTECT(x1 = duplicate(x0));
158 if (ATTRIB(x1) != R_NilValue) {
159 SET_ATTRIB(x1, R_NilValue);
191 if (!IS_S4_OBJECT(from)) {
198 int ivalid = R_check_class_etc(from,
valid);
203 int *pdim = INTEGER(dim), m = pdim[0], n = pdim[1];
207 if (k1 == R_NilValue)
209 else if ((a = asInteger(k1)) == NA_INTEGER || a < -m || a > n)
210 error(
_(
"'%s' (%d) must be an integer from %s (%d) to %s (%d)"),
211 "k1", a,
"-Dim[1]", -m,
"Dim[2]", n);
212 if (k2 == R_NilValue)
214 else if ((b = asInteger(k2)) == NA_INTEGER || b < -m || b > n)
215 error(
_(
"'%s' (%d) must be an integer from %s (%d) to %s (%d)"),
216 "k2", b,
"-Dim[1]", -m,
"Dim[2]", n);
218 error(
_(
"'%s' (%d) must be less than or equal to '%s' (%d)"),
229 int *pdim = INTEGER(dim), m = pdim[0], n = pdim[1], r = (m < n) ? m : n, j;
232 char ul =
'U', di =
'N';
233 if (
class[1] !=
'g') {
234 if (
class[2] ==
'p') {
236 ul = *CHAR(STRING_ELT(uplo, 0));
239 if (
class[1] ==
't') {
241 di = *CHAR(STRING_ELT(diag, 0));
247 res = PROTECT(allocVector(TYPEOF(x), r));
249#define DG_LOOP(_CTYPE_, _PTR_, _ONE_) \
251 _CTYPE_ *pres = _PTR_(res), *px = _PTR_(x); \
253 for (j = 0; j < r; ++j) \
255 else if (class[2] != 'p') { \
256 R_xlen_t m1a = (R_xlen_t) m + 1; \
257 for (j = 0; j < r; ++j, px += m1a) \
260 else if (ul == 'U') \
261 for (j = 0; j < n; px += (++j) + 1) \
264 for (j = 0; j < n; px += n - (j++)) \
291 rn = VECTOR_ELT(dn, 0),
292 cn = VECTOR_ELT(dn, 1);
293 if (cn == R_NilValue) {
294 if (
class[1] ==
's' && rn != R_NilValue)
295 setAttrib(res, R_NamesSymbol, rn);
298 setAttrib(res, R_NamesSymbol, cn);
299 else if (rn != R_NilValue &&
301 setAttrib(res, R_NamesSymbol, (r == m) ? rn : cn);
315 int ivalid = R_check_class_etc(obj,
valid);
320 if (TYPEOF(names) != LGLSXP || LENGTH(names) < 1 ||
321 (names_ = LOGICAL(names)[0]) == NA_LOGICAL)
322 error(
_(
"'%s' must be %s or %s"),
"names",
"TRUE",
"FALSE");
330 int v = LENGTH(value) != 1;
333 int *pdim = INTEGER(dim), m = pdim[0], n = pdim[1], r = (m < n) ? m : n, j;
343 if (
class[1] !=
'g') {
345 ul = *CHAR(STRING_ELT(uplo, 0));
359#define DS_LOOP(_CTYPE_, _PTR_) \
361 _CTYPE_ *px = _PTR_(x), *pvalue = _PTR_(value); \
362 if (class[2] != 'p') { \
363 R_xlen_t m1a = (R_xlen_t) m + 1; \
365 for (j = 0; j < r; ++j, px += m1a) \
368 for (j = 0; j < r; ++j, px += m1a) \
370 } else if (ul == 'U') { \
372 for (j = 0; j < n; px += (++j) + 1) \
375 for (j = 0; j < n; px += (++j) + 1) \
379 for (j = 0; j < n; px += n - (j++)) \
382 for (j = 0; j < n; px += n - (j++)) \
414 int ivalid = R_check_class_etc(from,
valid);
417 const char *
class =
valid[ivalid];
419 SEXPTYPE tx =
kindToType(
class[0]), tv = TYPEOF(value);
428 error(
_(
"replacement diagonal has incompatible type \"%s\""),
434 int *pdim = INTEGER(dim), m = pdim[0], n = pdim[1], r = (m < n) ? m : n;
435 R_xlen_t len = XLENGTH(value);
436 if (len != 1 && len != r)
437 error(
_(
"replacement diagonal has wrong length"));
442 PROTECT(value = coerceVector(value, tx));
446#ifndef MATRIX_ENABLE_IMATRIX
449 PROTECT(value = coerceVector(value, REALSXP));
454#ifndef MATRIX_ENABLE_IMATRIX
457 class =
valid[R_check_class_etc(from,
valid)];
471 int *pdim = INTEGER(dim), m = pdim[0], n = pdim[1], i, j;
483 if (
class[1] ==
's' ||
class[1] ==
'p' ||
class[1] ==
'o')
490 if (
class[1] !=
'g') {
492 ul = *CHAR(STRING_ELT(uplo, 0));
495 PROTECT(uplo = mkString(
"L"));
499 if (
class[1] ==
't') {
501 char di = *CHAR(STRING_ELT(diag, 0));
507 if (LENGTH(factors) > 0)
511 if (
class[1] ==
'o' && n > 0) {
520 x1 = PROTECT(allocVector(TYPEOF(x0), XLENGTH(x0)));
523#define TRANS_LOOP(_CTYPE_, _PTR_) \
525 _CTYPE_ *px0 = _PTR_(x0), *px1 = _PTR_(x1); \
526 if (class[2] != 'p') { \
527 R_xlen_t mn1s = XLENGTH(x0) - 1; \
528 for (j = 0; j < m; ++j, px0 -= mn1s) \
529 for (i = 0; i < n; ++i, px0 += m) \
531 } else if (ul == 'U') { \
532 for (j = 0; j < n; ++j) \
533 for (i = j; i < n; ++i) \
534 *(px1++) = *(px0 + PACKED_AR21_UP(j, i)); \
536 R_xlen_t n2 = (R_xlen_t) n * 2; \
537 for (j = 0; j < n; ++j) \
538 for (i = 0; i <= j; ++i) \
539 *(px1++) = *(px0 + PACKED_AR21_LO(j, i, n2)); \
570 static const char *
valid[] = {
571 "dpoMatrix",
"dppMatrix",
"corMatrix",
"copMatrix",
573 int ivalid = R_check_class_etc(from,
valid);
582 char ul0 =
'U', ul1 =
'U', di =
'N';
583 if (
class[1] !=
'g') {
585 ul0 = ul1 = *CHAR(STRING_ELT(uplo, 0));
587 if (
class[1] ==
't') {
589 di = *CHAR(STRING_ELT(diag, 0));
597 if (
class[1] ==
's') {
602 if (
class[0] ==
'z') {
614 char cl[] =
".s.Matrix";
616 cl[2] = (
class[2] !=
'p') ?
'y' :
'p';
620 int *pdim = INTEGER(dim), n = pdim[0];
622 error(
_(
"attempt to symmetrize a non-square matrix"));
632 SEXP uplo = PROTECT(mkString(
"L"));
639 if (
class[1] ==
'g' || ul0 == ul1)
642 SEXP x1 = PROTECT(allocVector(TYPEOF(x0), XLENGTH(x0)));
645 R_xlen_t len = XLENGTH(x1);
647#define DCPY(_PREFIX_, _CTYPE_, _PTR_) \
649 _CTYPE_ *px0 = _PTR_(x0), *px1 = _PTR_(x1); \
650 Matrix_memset(px1, 0, len, sizeof(_CTYPE_)); \
651 if (class[2] != 'p') \
652 _PREFIX_ ## dcpy2(px1, px0, n, len, '\0', di); \
654 _PREFIX_ ## dcpy1(px1, px0, n, len, ul1, ul0, di); \
660 DCPY(i,
int, LOGICAL);
663 DCPY(i,
int, INTEGER);
666 DCPY(d,
double, REAL);
669 DCPY(z, Rcomplex, COMPLEX);
687 int ivalid = R_check_class_etc(from,
valid);
692 if (uplo != R_NilValue) {
693 if (TYPEOF(uplo) != STRSXP || LENGTH(uplo) < 1 ||
694 (uplo = STRING_ELT(uplo, 0)) == NA_STRING ||
695 ((ul = *CHAR(uplo)) !=
'U' && ul !=
'L'))
696 error(
_(
"invalid '%s' to '%s'"),
"uplo", __func__);
704 if (
class[0] !=
'z' &&
class[0] !=
'd') {
709 if (
class[0] !=
'z' &&
class[1] ==
's')
713 char cl[] =
".s.Matrix";
714 cl[0] = (
class[0] !=
'z') ?
'd' :
'z';
715 cl[2] = (
class[2] !=
'p') ?
'y' :
'p';
719 int *pdim = INTEGER(dim), n = pdim[0];
721 error(
_(
"attempt to get symmetric part of non-square matrix"));
733 char ul =
'U', di =
'N';
734 if (
class[1] !=
'g') {
736 ul = *CHAR(STRING_ELT(uplo, 0));
740 if (
class[1] ==
't') {
742 di = *CHAR(STRING_ELT(diag, 0));
748 if (
class[0] ==
'z' ||
class[0] ==
'd') {
755 if (
class[1] ==
's') {
764#define SP_LOOP(_CTYPE_, _PTR_, _INCREMENT_, _SCALE1_, _ONE_) \
766 _CTYPE_ *px = _PTR_(x); \
767 if (class[1] == 'g') { \
769 for (j = 0; j < n; ++j) { \
770 for (i = j + 1; i < n; ++i) { \
773 _INCREMENT_((*px), (*py)); \
774 _SCALE1_((*px), 0.5); \
776 px = (py += j + 2); \
778 } else if (class[2] != 'p') { \
780 for (j = 0; j < n; ++j) { \
781 for (i = 0; i < j; ++i) { \
782 _SCALE1_((*px), 0.5); \
788 for (j = 0; j < n; ++j) { \
790 for (i = j + 1; i < n; ++i) { \
791 _SCALE1_((*px), 0.5); \
797 R_xlen_t n1a = (R_xlen_t) n + 1; \
799 for (j = 0; j < n; ++j, px += n1a) \
804 for (j = 0; j < n; ++j) { \
805 for (i = 0; i < j; ++i) { \
806 _SCALE1_((*px), 0.5); \
813 for (j = 0; j < n; px += (++j) + 1) \
817 for (j = 0; j < n; ++j) { \
819 for (i = j + 1; i < n; ++i) { \
820 _SCALE1_((*px), 0.5); \
826 for (j = 0; j < n; px += n - (j++)) \
847 int ivalid = R_check_class_etc(from,
valid);
856 if (
class[0] !=
'z' &&
class[0] !=
'd') {
863 char cl[] =
"...Matrix";
864 cl[0] = (
class[0] !=
'z') ?
'd' :
'z';
865 cl[1] = (
class[1] !=
's') ?
'g' :
's';
866 cl[2] = (
class[1] !=
's') ?
'e' :
867 ((
class[0] !=
'z') ?
'C' : ((
class[2] !=
'p') ?
'y' :
'p'));
871 int *pdim = INTEGER(dim), n = pdim[0];
873 error(
_(
"attempt to get skew-symmetric part of non-square matrix"));
886 if (
class[1] !=
'g') {
888 ul = *CHAR(STRING_ELT(uplo, 0));
889 if (
class[1] ==
's' && ul !=
'U')
894 if (
class[1] ==
's' &&
class[0] !=
'z') {
896 SEXP p = PROTECT(allocVector(INTSXP, (R_xlen_t) n + 1));
897 int *pp = INTEGER(p);
906 if (
class[1] ==
's') {
917 if (
class[2] ==
'p' ||
class[0] ==
'z' ||
class[0] ==
'd') {
919 error(
_(
"attempt to allocate vector of length exceeding %s"),
921 x1 = allocVector(TYPEOF(x0), (R_xlen_t) n * n);
927 R_xlen_t upos = 0, lpos = 0;
929#define SP_LOOP(_CTYPE_, _PTR_, _INCREMENT_, _ASSIGN_, _ZERO_) \
931 _CTYPE_ *px0 = _PTR_(x0), *px1 = _PTR_(x1); \
932 if (class[1] == 'g') { \
933 for (j = 0; j < n; ++j) { \
935 for (i = 0; i < j; ++i) { \
936 _ASSIGN_(px1[upos], 0.5 * px0[upos]); \
937 _INCREMENT_(px1[upos], -0.5 * px0[lpos]); \
938 _ASSIGN_(px1[lpos], -px1[upos]); \
942 px1[upos] = _ZERO_; \
945 } else if (class[2] != 'p') { \
947 for (j = 0; j < n; ++j) { \
949 for (i = 0; i < j; ++i) { \
950 _ASSIGN_(px1[upos], 0.5 * px0[upos]); \
951 _ASSIGN_(px1[lpos], -px1[upos]); \
955 px1[upos] = _ZERO_; \
959 for (j = 0; j < n; ++j) { \
961 px1[lpos] = _ZERO_; \
962 for (i = j + 1; i < n; ++i) { \
965 _ASSIGN_(px1[lpos], 0.5 * px0[lpos]); \
966 _ASSIGN_(px1[upos], -px1[lpos]); \
973 for (j = 0; j < n; ++j, ++px0) { \
975 for (i = 0; i < j; ++i, ++px0) { \
976 _ASSIGN_(px1[upos], 0.5 * (*px0)); \
977 _ASSIGN_(px1[lpos], -px1[upos]); \
981 px1[upos] = _ZERO_; \
985 for (j = 0; j < n; ++j, ++px0) { \
987 px1[lpos] = _ZERO_; \
988 for (i = j + 1; i < n; ++i, ++px0) { \
991 _ASSIGN_(px1[lpos], 0.5 * (*px0)); \
992 _ASSIGN_(px1[upos], -px1[lpos]); \
1014 int ivalid = R_check_class_etc(from,
valid);
1023 if (
class[1] ==
's')
1032 if (
class[1] ==
't')
1036 int *pdim = INTEGER(dim), n = pdim[0];
1045#define IS_LOOP(_CTYPE_, _PTR_, _NOTREAL_, _NOTCONJ_) \
1047 _CTYPE_ *px = _PTR_(x), *py = px; \
1048 for (j = 0; j < n; px = (py += (++j) + 1)) { \
1049 if (_NOTREAL_((*px))) \
1051 for (i = j + 1; i < n; ++i) { \
1054 if (_NOTCONJ_((*px), (*py))) \
1088 if (!IS_S4_OBJECT(obj)) {
1095 int ivalid = R_check_class_etc(obj,
valid);
1100 if (TYPEOF(checkDN) != LGLSXP || LENGTH(checkDN) < 1 ||
1101 (checkDN_ = LOGICAL(checkDN)[0]) == NA_LOGICAL)
1102 error(
_(
"'%s' must be %s or %s"),
"checkDN",
"TRUE",
"FALSE");
1111 if (
class[1] ==
't') {
1113 char ul = *CHAR(STRING_ELT(uplo, 0));
1114 if (upper == NA_LOGICAL || (upper != 0) == (ul ==
'U'))
1115 return (ul ==
'U') ? 1 : -1;
1117 return (ul ==
'U') ? -1 : 1;
1122 if (
class[1] ==
's') {
1126 char ul = *CHAR(STRING_ELT(uplo, 0));
1127 if (upper == NA_LOGICAL)
1128 return (ul ==
'U') ? 1 : -1;
1130 return (upper != 0) ? 1 : -1;
1134 int *pdim = INTEGER(dim), n = pdim[0];
1138 return (upper != 0) ? 1 : -1;
1143#define IT_LOOP(_CTYPE_, _PTR_, _ISNZ_) \
1146 if (upper == NA_LOGICAL) { \
1148 for (j = 0; j < n; px += (++j)) { \
1150 for (i = j + 1; i < n; ++i, px += 1) { \
1151 if (_ISNZ_(*px)) { \
1160 for (j = 0; j < n; px += n - (++j)) { \
1161 for (i = 0; i < j; ++i, px += 1) { \
1162 if (_ISNZ_(*px)) { \
1172 } else if (upper != 0) { \
1174 for (j = 0; j < n; px += (++j)) { \
1176 for (i = j + 1; i < n; ++i, px += 1) \
1183 for (j = 0; j < n; px += n - (++j)) { \
1184 for (i = 0; i < j; ++i, px += 1) \
1220 if (!IS_S4_OBJECT(obj)) {
1227 int ivalid = R_check_class_etc(obj,
valid);
1231 if (TYPEOF(upper) != LGLSXP || LENGTH(upper) < 1)
1232 error(
_(
"'%s' must be %s or %s or %s"),
"upper",
"TRUE",
"FALSE",
"NA");
1233 int upper_ = LOGICAL(upper)[0];
1236 SEXP ans = allocVector(LGLSXP, 1);
1237 LOGICAL(ans)[0] = ans_ != 0;
1238 if (upper_ == NA_LOGICAL && ans_ != 0) {
1241 SEXP kindSym = NULL;
1242 SEXP kindVal = PROTECT(mkString((ans_ > 0) ?
"U" :
"L"));
1243 if (!kindSym) kindSym = install(
"kind");
1244 setAttrib(ans, kindSym, kindVal);
1254 int *pdim = INTEGER(dim), n = pdim[0];
1261 if (
class[1] !=
'g') {
1263 ul = *CHAR(STRING_ELT(uplo, 0));
1269#define ID_LOOP(_CTYPE_, _PTR_, _ISNZ_) \
1271 _CTYPE_ *px = _PTR_(x); \
1272 if (class[1] == 'g') { \
1273 for (j = 0; j < n; ++j) { \
1274 for (i = 0; i < j; ++i) { \
1280 for (i = j + 1; i < n; ++i) { \
1286 } else if (class[2] != 'p') { \
1288 for (j = 0; j < n; ++j) { \
1289 for (i = 0; i < j; ++i) { \
1297 for (j = 0; j < n; ++j) { \
1299 for (i = j + 1; i < n; ++i) { \
1308 for (j = 0; j < n; ++j) { \
1309 for (i = 0; i < j; ++i) { \
1317 for (j = 0; j < n; ++j) { \
1319 for (i = j + 1; i < n; ++i) { \
1357 if (!IS_S4_OBJECT(obj)) {
1364 int ivalid = R_check_class_etc(obj,
valid);
1373#define CAST_PATTERN(_X_) (_X_ != 0)
1374#define CAST_LOGICAL(_X_) (_X_ != 0)
1375#define CAST_INTEGER(_X_) _X_
1376#define CAST_REAL(_X_) _X_
1377#define CAST_COMPLEX(_X_) _X_
1381 switch (class[0]) { \
1384 SUM_LOOP(int, LOGICAL, double, REAL, \
1385 0.0, 1.0, NA_REAL, ISNA_PATTERN, \
1386 CAST_PATTERN, INCREMENT_REAL, SCALE2_REAL); \
1388 SUM_LOOP(int, LOGICAL, int, INTEGER, \
1389 0, 1, NA_INTEGER, ISNA_PATTERN, \
1390 CAST_PATTERN, INCREMENT_INTEGER, SCALE2_REAL); \
1394 SUM_LOOP(int, LOGICAL, double, REAL, \
1395 0.0, 1.0, NA_REAL, ISNA_LOGICAL, \
1396 CAST_LOGICAL, INCREMENT_REAL, SCALE2_REAL); \
1398 SUM_LOOP(int, LOGICAL, int, INTEGER, \
1399 0, 1, NA_INTEGER, ISNA_LOGICAL, \
1400 CAST_LOGICAL, INCREMENT_INTEGER, SCALE2_REAL); \
1403 SUM_LOOP(int, INTEGER, double, REAL, \
1404 0.0, 1.0, NA_REAL, ISNA_INTEGER, \
1405 CAST_INTEGER, INCREMENT_REAL, SCALE2_REAL); \
1408 SUM_LOOP(double, REAL, double, REAL, \
1409 0.0, 1.0, NA_REAL, ISNA_REAL, \
1410 CAST_REAL, INCREMENT_REAL, SCALE2_REAL); \
1413 SUM_LOOP(Rcomplex, COMPLEX, Rcomplex, COMPLEX, \
1414 Matrix_zzero, Matrix_zone, Matrix_zna, ISNA_COMPLEX, \
1415 CAST_COMPLEX, INCREMENT_COMPLEX, SCALE2_COMPLEX); \
1422#define SUM_TYPEOF(c) (c == 'z') ? CPLXSXP : ((mean || c == 'd' || c == 'i') ? REALSXP : INTSXP)
1426 int m,
int n,
char ul,
char di,
int narm,
int mean,
1429 int i, j, count = -1, narm_ = narm && mean &&
class[0] !=
'n',
1430 unpacked =
class[2] !=
'p';
1432#define SUM_LOOP(_CTYPE0_, _PTR0_, _CTYPE1_, _PTR1_, \
1433 _ZERO_, _ONE_, _NA_, _ISNA_, \
1434 _CAST_, _INCREMENT_, _SCALE2_) \
1436 _CTYPE0_ *px0 = _PTR0_( x); \
1437 _CTYPE1_ *px1 = _PTR1_(res), tmp; \
1438 if (class[1] == 'g') { \
1439 for (j = 0; j < n; ++j) { \
1441 SUM_KERNEL(for (i = 0; i < m; ++i), _NA_, _ISNA_, \
1442 _CAST_, _INCREMENT_, _SCALE2_); \
1445 } else if (di == 'N') { \
1447 for (j = 0; j < n; ++j) { \
1449 SUM_KERNEL(for (i = 0; i <= j; ++i), _NA_, _ISNA_, \
1450 _CAST_, _INCREMENT_, _SCALE2_); \
1456 for (j = 0; j < n; ++j) { \
1460 SUM_KERNEL(for (i = j; i < n; ++i), _NA_, _ISNA_, \
1461 _CAST_, _INCREMENT_, _SCALE2_); \
1467 for (j = 0; j < n; ++j) { \
1469 SUM_KERNEL(for (i = 0; i < j; ++i), _NA_, _ISNA_, \
1470 _CAST_, _INCREMENT_, _SCALE2_); \
1477 for (j = 0; j < n; ++j) { \
1482 SUM_KERNEL(for (i = j + 1; i < n; ++i), _NA_, _ISNA_, \
1483 _CAST_, _INCREMENT_, _SCALE2_); \
1490#define SUM_KERNEL(_FOR_, _NA_, _ISNA_, _CAST_, _INCREMENT_, _SCALE2_) \
1495 if (_ISNA_(*px0)) { \
1501 tmp = _CAST_(*px0); \
1502 _INCREMENT_((*px1), tmp); \
1507 _SCALE2_((*px1), count); \
1520 int m,
int n,
char ul,
char di,
int narm,
int mean,
1523 int i, j, *count = NULL, narm_ = narm && mean &&
class[0] !=
'n',
1524 unpacked =
class[2] !=
'p', symmetric =
class[1] ==
's';
1527 for (i = 0; i < m; ++i)
1531#define SUM_LOOP(_CTYPE0_, _PTR0_, _CTYPE1_, _PTR1_, \
1532 _ZERO_, _ONE_, _NA_, _ISNA_, \
1533 _CAST_, _INCREMENT_, _SCALE2_) \
1535 _CTYPE0_ *px0 = _PTR0_( x); \
1536 _CTYPE1_ *px1 = _PTR1_(res), tmp = (di == 'N') ? _ZERO_ : _ONE_; \
1537 for (i = 0; i < m; ++i) \
1539 if (class[1] == 'g') { \
1540 for (j = 0; j < n; ++j) \
1541 SUM_KERNEL(for (i = 0; i < m; ++i), _NA_, _ISNA_, \
1542 _CAST_, _INCREMENT_); \
1543 } else if (class[1] == 's' || di == 'N') { \
1545 for (j = 0; j < n; ++j) { \
1546 SUM_KERNEL(for (i = 0; i <= j; ++i), _NA_, _ISNA_, \
1547 _CAST_, _INCREMENT_); \
1552 for (j = 0; j < n; ++j) { \
1555 SUM_KERNEL(for (i = j; i < n; ++i), _NA_, _ISNA_, \
1556 _CAST_, _INCREMENT_); \
1561 for (j = 0; j < n; ++j) { \
1562 SUM_KERNEL(for (i = 0; i < j; ++i), _NA_, _ISNA_, \
1563 _CAST_, _INCREMENT_); \
1569 for (j = 0; j < n; ++j) { \
1573 SUM_KERNEL(for (i = j + 1; i < n; ++i), _NA_, _ISNA_, \
1574 _CAST_, _INCREMENT_); \
1580 for (i = 0; i < m; ++i) \
1581 _SCALE2_(px1[i], count[i]); \
1583 for (i = 0; i < m; ++i) \
1584 _SCALE2_(px1[i], n); \
1588#define SUM_KERNEL(_FOR_, _NA_, _ISNA_, _CAST_, _INCREMENT_) \
1591 int again = symmetric && i != j; \
1592 if (_ISNA_(*px0)) { \
1597 } else if (narm_) { \
1603 tmp = _CAST_(*px0); \
1604 _INCREMENT_(px1[i], tmp); \
1606 _INCREMENT_(px1[j], tmp); \
1626 int *pdim = INTEGER(dim), m = pdim[0], n = pdim[1],
1627 r = (margin == 0) ? m : n;
1629 SEXP res = PROTECT(allocVector(
SUM_TYPEOF(
class[0]), r)),
1632 SEXP dimnames = (
class[1] !=
's')
1635 marnames = VECTOR_ELT(dimnames, margin);
1636 if (marnames != R_NilValue) {
1638 setAttrib(res, R_NamesSymbol, marnames);
1642 char ul =
'U', di =
'N';
1643 if (
class[1] !=
'g') {
1645 ul = *CHAR(STRING_ELT(uplo, 0));
1646 if (
class[1] ==
't') {
1648 di = *CHAR(STRING_ELT(diag, 0));
1652 if (margin == 0 ||
class[1] ==
's')
1663 SEXP narm, SEXP mean)
1666 int ivalid = R_check_class_etc(obj,
valid);
1671 if (TYPEOF(margin) != INTSXP || LENGTH(margin) < 1 ||
1672 ((margin_ = INTEGER(margin)[0]) != 0 && margin_ != 1))
1673 error(
_(
"'%s' must be %d or %d"),
"margin", 0, 1);
1676 if (TYPEOF(narm) != LGLSXP || LENGTH(narm) < 1 ||
1677 (narm_ = LOGICAL(narm)[0]) == NA_LOGICAL)
1678 error(
_(
"'%s' must be %s or %s"),
"narm",
"TRUE",
"FALSE");
1681 if (TYPEOF(mean) != LGLSXP || LENGTH(mean) < 1 ||
1682 (mean_ = LOGICAL(mean)[0]) == NA_LOGICAL)
1683 error(
_(
"'%s' must be %s or %s"),
"mean",
"TRUE",
"FALSE");
1691#define TRY_INCREMENT(_LABEL_) \
1694 ? ( t <= MATRIX_INT_FAST64_MAX - s) \
1695 : (-t <= s - MATRIX_INT_FAST64_MIN)) { \
1705#define LONGDOUBLE_AS_DOUBLE(v) \
1706 (v > DBL_MAX) ? R_PosInf : ((v < -DBL_MAX) ? R_NegInf : (double) v);
1713 int *pdim = INTEGER(dim), m = pdim[0], n = pdim[1];
1715 char ul =
'U', di =
'N';
1716 if (
class[1] !=
'g') {
1718 ul = *CHAR(STRING_ELT(uplo, 0));
1719 if (
class[1] ==
't') {
1721 di = *CHAR(STRING_ELT(diag, 0));
1726 int i, j, unpacked =
class[2] !=
'p', symmetric =
class[1] ==
's';
1730 if (class[1] == 'g') { \
1731 for (j = 0; j < n; ++j) \
1732 SUM_KERNEL(for (i = 0; i < m; ++i)); \
1733 } else if (class[1] == 's' || di == 'N') { \
1735 for (j = 0; j < n; ++j) { \
1736 SUM_KERNEL(for (i = 0; i <= j; ++i)); \
1741 for (j = 0; j < n; ++j) { \
1744 SUM_KERNEL(for (i = j; i < m; ++i)); \
1749 for (j = 0; j < n; ++j) { \
1750 SUM_KERNEL(for (i = 0; i < j; ++i)); \
1756 for (j = 0; j < n; ++j) { \
1760 SUM_KERNEL(for (i = j + 1; i < m; ++i)); \
1766 if (
class[0] ==
'n') {
1767 int *px = LOGICAL(x);
1770#define SUM_KERNEL(_FOR_) \
1774 s += (symmetric && i != j) ? 2 : 1; \
1784 res = allocVector(INTSXP, 1);
1785 INTEGER(res)[0] = (int) s;
1787 res = allocVector(REALSXP, 1);
1788 REAL(res)[0] = (double) s;
1793 if (!narm && (
class[0] ==
'l' ||
class[0] ==
'i')) {
1794 int *px = (
class[0] ==
'l') ? LOGICAL(x) : INTEGER(x);
1796#define SUM_KERNEL(_FOR_) \
1799 if (*px == NA_INTEGER) { \
1800 res = allocVector(INTSXP, 1); \
1801 INTEGER(res)[0] = NA_INTEGER; \
1814 if (
class[0] ==
'z') {
1815 Rcomplex *px = COMPLEX(x);
1816 long double zr = (di ==
'N') ? 0.0L : n, zi = 0.0L;
1818#define SUM_KERNEL(_FOR_) \
1821 if (!(narm && (ISNAN((*px).r) || ISNAN((*px).i)))) { \
1822 zr += (symmetric && i != j) \
1823 ? 2.0L * (*px).r : (*px).r; \
1824 zi += (symmetric && i != j) \
1825 ? 2.0L * (*px).i : (*px).i; \
1835 res = allocVector(CPLXSXP, 1);
1838 }
else if (
class[0] ==
'd') {
1839 double *px = REAL(x);
1840 long double zr = (di ==
'N') ? 0.0L : n;
1842#define SUM_KERNEL(_FOR_) \
1845 if (!(narm && ISNAN(*px))) \
1846 zr += (symmetric && i != j) \
1847 ? 2.0L * *px : *px; \
1856 res = allocVector(REALSXP, 1);
1859 int *px = (
class[0] ==
'i') ? INTEGER(x) : LOGICAL(x);
1861 unsigned int count = 0;
1864#define SUM_KERNEL(_FOR_) \
1867 if (!(narm && *px == NA_INTEGER)) { \
1868 int d = (symmetric && i != j) ? 2 : 1; \
1869 if (count > UINT_MAX - d) \
1870 TRY_INCREMENT(ifover); \
1871 t += (d == 2) ? 2LL * *px : *px; \
1885 long double zr = (di ==
'N') ? 0.0L : n;
1886 px = (
class[0] ==
'i') ? INTEGER(x) : LOGICAL(x);
1888#define SUM_KERNEL(_FOR_) \
1891 if (!(narm && *px == NA_INTEGER)) \
1892 zr += (symmetric && i != j) \
1893 ? 2.0L * *px : *px; \
1902 res = allocVector(REALSXP, 1);
1904 }
else if (s > INT_MIN && s <= INT_MAX) {
1905 res = allocVector(INTSXP, 1);
1906 INTEGER(res)[0] = (int) s;
1908 res = allocVector(REALSXP, 1);
1909 REAL(res)[0] = (double) s;
1922 int ivalid = R_check_class_etc(obj,
valid);
1927 if (TYPEOF(narm) != LGLSXP || LENGTH(narm) < 1 ||
1928 (narm_ = LOGICAL(narm)[0]) == NA_LOGICAL)
1929 error(
_(
"'%s' must be %s or %s"),
"narm",
"TRUE",
"FALSE");
1936 SEXP res = PROTECT(allocVector((
class[0] ==
'z') ? CPLXSXP : REALSXP, 1));
1939 int *pdim = INTEGER(dim), m = pdim[0], n = pdim[1];
1941 char ul =
'U', di =
'N';
1942 if (
class[1] !=
'g') {
1944 ul = *CHAR(STRING_ELT(uplo, 0));
1945 if (
class[1] ==
't') {
1947 di = *CHAR(STRING_ELT(diag, 0));
1952 int i, j, unpacked =
class[2] !=
'p', symmetric =
class[1] ==
's';
1953 long double zr = 1.0L, zi = 0.0L;
1957 if (class[1] == 'g') { \
1958 for (j = 0; j < n; ++j) \
1959 PROD_KERNEL(for (i = 0; i < m; ++i)); \
1960 } else if (class[1] == 's') { \
1962 for (j = 0; j < n; ++j) { \
1963 PROD_KERNEL(for (i = 0; i <= j; ++i)); \
1968 for (j = 0; j < n; ++j) { \
1971 PROD_KERNEL(for (i = j; i < m; ++i)); \
1974 } else if (di == 'N') { \
1976 for (j = 0; j < n; ++j) { \
1977 if (j == 1) { zr *= 0.0L; zi *= 0.0L; } \
1978 PROD_KERNEL(for (i = 0; i <= j; ++i)); \
1983 for (j = 0; j < n; ++j) { \
1984 if (j == 1) { zr *= 0.0L; zi *= 0.0L; } \
1987 PROD_KERNEL(for (i = j; i < m; ++i)); \
1992 for (j = 0; j < n; ++j) { \
1993 if (j == 1) { zr *= 0.0L; zi *= 0.0L; } \
1994 PROD_KERNEL(for (i = 0; i < j; ++i)); \
2000 for (j = 0; j < n; ++j) { \
2001 if (j == 1) { zr *= 0.0L; zi *= 0.0L; } \
2005 PROD_KERNEL(for (i = j + 1; i < m; ++i)); \
2011 if (
class[0] ==
'n') {
2012 int *px = LOGICAL(x);
2013 if (
class[1] ==
't')
2014 REAL(res)[0] = (n > 1 || (n == 1 && *px == 0)) ? 0.0 : 1.0;
2017#define PROD_KERNEL(_FOR_) \
2021 REAL(res)[0] = 0.0; \
2039 if (
class[0] ==
'z') {
2040 Rcomplex *px = COMPLEX(x);
2041 long double zr0, zi0;
2043#define PROD_KERNEL(_FOR_) \
2046 if (!(narm && (ISNAN((*px).r) || ISNAN((*px).i)))) { \
2047 zr0 = zr; zi0 = zi; \
2048 zr = zr0 * (*px).r - zi0 * (*px).i; \
2049 zi = zr0 * (*px).i + zi0 * (*px).r; \
2050 if (symmetric && i != j) { \
2051 zr0 = zr; zi0 = zi; \
2052 zr = zr0 * (*px).r - zi0 * (*px).i; \
2053 zi = zr0 * (*px).i + zi0 * (*px).r; \
2064 }
else if (
class[0] ==
'd') {
2065 double *px = REAL(x);
2067#define PROD_KERNEL(_FOR_) \
2070 if (!(narm && ISNAN(*px))) \
2071 zr *= (symmetric && i != j) \
2072 ? (long double) *px * *px : *px; \
2082 int *px = (
class[0] ==
'l') ? LOGICAL(x) : INTEGER(x);
2084#define PROD_KERNEL(_FOR_) \
2087 if (*px != NA_INTEGER) \
2088 zr *= (symmetric && i != j) \
2089 ? (long double) *px * *px : *px; \
2104 if (
class[0] ==
'z') {
2117 int ivalid = R_check_class_etc(obj,
valid);
2122 if (TYPEOF(narm) != LGLSXP || LENGTH(narm) < 1 ||
2123 (narm_ = LOGICAL(narm)[0]) == NA_LOGICAL)
2124 error(
_(
"'%s' must be %s or %s"),
"narm",
"TRUE",
"FALSE");
2130#undef LONGDOUBLE_AS_DOUBLE
2152int left_cyclic(
double *x,
int ldx,
int j,
int k,
2153 double *cosines,
double *sines)
2156 error(
_(
"incorrect left cyclic shift, j (%d) < 0"),
2159 error(
_(
"incorrect left cyclic shift, j (%d) >= k (%d)"),
2162 error(
_(
"incorrect left cyclic shift, k (%d) > ldx (%d)"),
2165 double *lastcol = (
double *) R_alloc((
size_t) k + 1,
sizeof(double));
2168 for (i = 0; i <= j; i++)
2169 lastcol[i] = x[i + j*ldx];
2171 for (i = j+1; i <= k; i++)
2173 for (
int jj = j+1, ind = 0; jj <= k; jj++, ind++) {
2175 int diagind = jj*(ldx+1);
2176 double tmp = x[diagind], cc, ss;
2179 F77_CALL(drotg)(x+diagind-1, &tmp, cosines+ind, sines+ind);
2183 for (i = 0; i < jj; i++)
2184 x[i + (jj-1)*ldx] = x[i+jj*ldx];
2186 for (i = jj; i < k; i++) {
2187 tmp = cc*x[(jj-1)+i*ldx] + ss*x[jj+i*ldx];
2188 x[jj+i*ldx] = cc*x[jj+i*ldx] - ss*x[(jj-1)+i*ldx];
2189 x[(jj-1)+i*ldx] = tmp;
2192 lastcol[jj] = -ss*lastcol[jj-1]; lastcol[jj-1] *= cc;
2195 for(i = 0; i <= k; i++)
2196 x[i+k*ldx] = lastcol[i];
2201SEXP getGivens(
double *x,
int ldx,
int jmin,
int rank)
2203 int shiftlen = (rank - jmin) - 1;
2204 SEXP ans = PROTECT(allocVector(VECSXP, 4)), nms, cosines, sines;
2205 SET_VECTOR_ELT(ans, 0, ScalarInteger(jmin));
2206 SET_VECTOR_ELT(ans, 1, ScalarInteger(rank));
2207 SET_VECTOR_ELT(ans, 2, cosines = allocVector(REALSXP, shiftlen));
2208 SET_VECTOR_ELT(ans, 3, sines = allocVector(REALSXP, shiftlen));
2209 setAttrib(ans, R_NamesSymbol, nms = allocVector(STRSXP, 4));
2210 SET_STRING_ELT(nms, 0, mkChar(
"jmin"));
2211 SET_STRING_ELT(nms, 1, mkChar(
"rank"));
2212 SET_STRING_ELT(nms, 2, mkChar(
"cosines"));
2213 SET_STRING_ELT(nms, 3, mkChar(
"sines"));
2214 if (left_cyclic(x, ldx, jmin, rank - 1, REAL(cosines), REAL(sines)))
2215 error(
_(
"unknown error in getGivens"));
2221SEXP checkGivens(SEXP X, SEXP jmin, SEXP rank)
2223 if (!(isReal(X) && isMatrix(X)))
2224 error(
_(
"X must be a numeric (double precision) matrix"));
2225 SEXP ans = PROTECT(allocVector(VECSXP, 2)),
2226 Xcp = PROTECT(duplicate(X));
2227 int Xdims = INTEGER(getAttrib(X, R_DimSymbol));
2228 SET_VECTOR_ELT(ans, 0, Xcp);
2229 SET_VECTOR_ELT(ans, 1, getGivens(REAL(Xcp), Xdims[0],
2230 asInteger(jmin), asInteger(rank)));
2235SEXP lsq_dense_chol(SEXP X, SEXP y)
2237 if (!(isReal(X) && isMatrix(X)))
2238 error(
_(
"X must be a numeric (double precision) matrix"));
2239 if (!(isReal(y) && isMatrix(y)))
2240 error(
_(
"y must be a numeric (double precision) matrix"));
2241 int *Xdims = INTEGER(getAttrib(X, R_DimSymbol)),
2242 *ydims = INTEGER(getAttrib(y, R_DimSymbol));
2243 if (Xdims[0] != ydim[0])
2244 error(
_(
"number of rows in y (%d) does not match "
2245 "number of rows in X (%d)"),
2246 ydims[0], Xdims[0]);
2247 int n = Xdims[0], p = Xdims[1], k = ydims[1];
2249 return allocMatrix(REALSXP, p, k);
2250 SEXP ans = PROTECT(allocMatrix(REALSXP, p, k));
2251 double d_one = 1.0, d_zero = 0.0,
2252 *xpx = (
double *) R_alloc((
size_t) p * p,
sizeof(double));
2254 F77_CALL(dgemm)(
"T",
"N", &p, &k, &n, &d_one, REAL(X), &n, REAL(y),
2256 F77_CALL(dsyrk)(
"U",
"T", &p, &n, &d_one, REAL(X), &n, &d_zero,
2258 F77_CALL(dposv)(
"U", &p, &k, xpx, &p, REAL(ans), &p, &info
FCONE);
2260 error(
_(
"LAPACK dposv returned error code %d"), info);
2265SEXP lsq_dense_qr(SEXP X, SEXP y)
2267 if (!(isReal(X) && isMatrix(X)))
2268 error(
_(
"X must be a numeric (double precision) matrix"));
2269 if (!(isReal(y) && isMatrix(y)))
2270 error(
_(
"y must be a numeric (double precision) matrix"));
2271 int *Xdims = INTEGER(getAttrib(X, R_DimSymbol)),
2272 *ydims = INTEGER(getAttrib(y, R_DimSymbol));
2273 if (Xdims[0] != ydim[0])
2274 error(
_(
"number of rows in y (%d) does not match "
2275 "number of rows in X (%d)"),
2276 ydims[0], Xdims[0]);
2277 int n = Xdims[0], p = Xdims[1], k = ydims[1];
2279 return allocMatrix(REALSXP, p, k);
2280 SEXP ans = PROTECT(duplicate(y));
2281 double *xvals = (
double *) R_alloc((
size_t) n * p,
sizeof(double)),
2283 int lwork = -1, info;
2284 Memcpy(xvals, REAL(X), (
size_t) n * p);
2285 F77_CALL(dgels)(
"N", &n, &p, &k, xvals, &n, REAL(ans), &n,
2286 &tmp, &lwork, &info
FCONE);
2288 error(
_(
"LAPACK dgels returned error code %d"), info);
2290 work = (
double *) R_alloc((
size_t) lwork,
sizeof(double));
2291 F77_CALL(dgels)(
"N", &n, &p, &k, xvals, &n, REAL(ans), &n,
2292 work, &lwork, &info
FCONE);
2294 error(
_(
"LAPACK dgels returned error code %d"), info);
2310SEXP lapack_qr(SEXP Xin, SEXP tl)
2312 if (!(isReal(Xin) && isMatrix(Xin)))
2313 error(
_(
"X must be a real (numeric) matrix"));
2314 double tol = asReal(tl);
2316 error(
_(
"tol, given as %g, must be >= 0"), tol);
2318 error(
_(
"tol, given as %g, must be <= 1"), tol);
2319 SEXP ans = PROTECT(allocVector(VECSXP, 5)), X, qraux, pivot;
2320 int *Xdims = INTEGER(getAttrib(Xin, R_DimSymbol)),
2324 trsz = (n < p) ? n : p,
2326 SET_VECTOR_ELT(ans, 0, X = duplicate(Xin));
2327 SET_VECTOR_ELT(ans, 2, qraux = allocVector(REALSXP, trsz));
2328 SET_VECTOR_ELT(ans, 3, pivot = allocVector(INTSXP, p));
2329 for (i = 0; i < p; i++)
2330 INTEGER(pivot)[i] = i + 1;
2331 SEXP nms, Givens = PROTECT(allocVector(VECSXP, trsz - 1));
2332 setAttrib(ans, R_NamesSymbol, nms = allocVector(STRSXP, 5));
2333 SET_STRING_ELT(nms, 0, mkChar(
"qr"));
2334 SET_STRING_ELT(nms, 1, mkChar(
"rank"));
2335 SET_STRING_ELT(nms, 2, mkChar(
"qraux"));
2336 SET_STRING_ELT(nms, 3, mkChar(
"pivot"));
2337 SET_STRING_ELT(nms, 4, mkChar(
"Givens"));
2338 int rank = trsz, nGivens = 0;
2340 if (n > 0 && p > 0) {
2341 double *xpt = REAL(X), *work, tmp;
2342 int *iwork, lwork, info;
2344 F77_CALL(dgeqrf)(&n, &p, xpt, &n, REAL(qraux), &tmp, &lwork,
2347 error(
_(
"LAPACK dgeqrf returned error code %d"), info);
2349 work = (
double *) R_alloc(((
size_t) lwork < (size_t) 3 * trsz)
2350 ? (size_t) 3 * trsz : (size_t) lwork,
2352 F77_CALL(dgeqrf)(&n, &p, xpt, &n, REAL(qraux), work, &lwork,
2355 error(
_(
"LAPACK dgeqrf returned error code %d"), info);
2356 iwork = (
int *) R_alloc((
size_t) trsz,
sizeof(int));
2357 F77_CALL(dtrcon)(
"1",
"U",
"N", &rank, xpt, &n, &rcond,
2360 error(
_(
"LAPACK dtrcon returned error code %d"), info);
2361 while (rcond < tol) {
2362 double el, minabs = (xpt[0] < 0.0) ? -xpt[0]: xpt[0];
2364 for (i = 1; i < rank; i++) {
2373 if (jmin < (rank - 1)) {
2374 SET_VECTOR_ELT(Givens, nGivens,
2375 getGivens(xpt, n, jmin, rank));
2380 F77_CALL(dtrcon)(
"1",
"U",
"N", &rank, xpt, &n, &rcond,
2383 error(
_(
"LAPACK dtrcon returned error code %d"), info);
2387 SET_VECTOR_ELT(ans, 1, ScalarInteger(rank));
2388 SET_VECTOR_ELT(ans, 4, Gcpy = allocVector(VECSXP, nGivens));
2389 for (i = 0; i < nGivens; i++)
2390 SET_VECTOR_ELT(Gcpy, i, VECTOR_ELT(Givens, i));
2391 setAttrib(ans, install(
"useLAPACK"), ScalarLogical(1));
2392 setAttrib(ans, install(
"rcond"), ScalarReal(rcond));
#define SCALE1_REAL(_X_, _A_)
long long Matrix_int_fast64_t
#define NOTCONJ_LOGICAL(_X_, _Y_)
#define NOTREAL_LOGICAL(_X_)
#define ERROR_INVALID_CLASS(_X_, _FUNC_)
#define NOTREAL_INTEGER(_X_)
#define NOTCONJ_PATTERN(_X_, _Y_)
#define NOTREAL_PATTERN(_X_)
#define NOTREAL_COMPLEX(_X_)
#define NOTREAL_REAL(_X_)
#define INCREMENT_REAL(_X_, _Y_)
char typeToKind(SEXPTYPE)
#define ASSIGN_COMPLEX(_X_, _Y_)
#define ISNZ_PATTERN(_X_)
#define INCREMENT_COMPLEX(_X_, _Y_)
#define ASSIGN_REAL(_X_, _Y_)
SEXPTYPE kindToType(char)
#define NOTCONJ_REAL(_X_, _Y_)
#define NOTCONJ_COMPLEX(_X_, _Y_)
#define ISNZ_LOGICAL(_X_)
#define Matrix_Free(_VAR_, _N_)
#define ISNZ_INTEGER(_X_)
#define SET_SLOT(x, what, value)
#define GET_SLOT(x, what)
#define Matrix_Calloc(_VAR_, _N_, _CTYPE_)
#define ISNZ_COMPLEX(_X_)
#define SCALE1_COMPLEX(_X_, _A_)
SEXP newObject(const char *)
#define NOTCONJ_INTEGER(_X_, _Y_)
SEXP get_symmetrized_DimNames(SEXP obj, int J)
int DimNames_is_symmetric(SEXP dn)
void set_reversed_DimNames(SEXP obj, SEXP dn)
void set_symmetrized_DimNames(SEXP obj, SEXP dn, int J)
static const char * valid[]
SEXP dense_as_kind(SEXP from, const char *class, char kind, int new)
SEXP matrix_as_dense(SEXP from, const char *zzz, char ul, char di, int trans, int new)
SEXP dense_as_general(SEXP from, const char *class, int new)
#define ID_LOOP(_CTYPE_, _PTR_, _ISNZ_)
SEXP R_dense_is_diagonal(SEXP obj)
SEXP dense_force_symmetric(SEXP from, const char *class, char ul)
#define TRANS_LOOP(_CTYPE_, _PTR_)
SEXP R_dense_skewpart(SEXP from)
int dense_is_symmetric(SEXP obj, const char *class, int checkDN)
#define SP_LOOP(_CTYPE_, _PTR_, _INCREMENT_, _SCALE1_, _ONE_)
SEXP R_dense_transpose(SEXP from)
SEXP R_dense_diag_get(SEXP obj, SEXP names)
SEXP dense_band(SEXP from, const char *class, int a, int b)
SEXP dense_diag_set(SEXP from, const char *class, SEXP value, int new)
#define IS_LOOP(_CTYPE_, _PTR_, _NOTREAL_, _NOTCONJ_)
SEXP R_dense_is_triangular(SEXP obj, SEXP upper)
#define DS_LOOP(_CTYPE_, _PTR_)
SEXP R_dense_is_symmetric(SEXP obj, SEXP checkDN)
#define DCPY1(_PREFIX_, _CTYPE_, _PTR_)
SEXP R_dense_symmpart(SEXP from)
SEXP dense_symmpart(SEXP from, const char *class)
SEXP R_dense_band(SEXP from, SEXP k1, SEXP k2)
SEXP dense_sum(SEXP obj, const char *class, int narm)
SEXP dense_diag_get(SEXP obj, const char *class, int names)
#define BAND2(_PREFIX_, _CTYPE_, _PTR_)
static void dense_colsum(SEXP x, const char *class, int m, int n, char ul, char di, int narm, int mean, SEXP res)
#define IT_LOOP(_CTYPE_, _PTR_, _ISNZ_)
#define BAND1(_PREFIX_, _CTYPE_, _PTR_)
int dense_is_triangular(SEXP obj, const char *class, int upper)
SEXP dense_skewpart(SEXP from, const char *class)
static void dense_rowsum(SEXP x, const char *class, int m, int n, char ul, char di, int narm, int mean, SEXP res)
SEXP R_dense_marginsum(SEXP obj, SEXP margin, SEXP narm, SEXP mean)
#define LONGDOUBLE_AS_DOUBLE(v)
#define DCPY2(_PREFIX_, _CTYPE_, _PTR_)
#define SUM_LOOP(_CTYPE0_, _PTR0_, _CTYPE1_, _PTR1_, _ZERO_, _ONE_, _NA_, _ISNA_, _CAST_, _INCREMENT_, _SCALE2_)
SEXP dense_transpose(SEXP from, const char *class)
SEXP R_dense_force_symmetric(SEXP from, SEXP uplo)
SEXP R_dense_diag_set(SEXP from, SEXP value)
SEXP dense_marginsum(SEXP obj, const char *class, int margin, int narm, int mean)
SEXP R_dense_sum(SEXP obj, SEXP narm)
SEXP R_dense_prod(SEXP obj, SEXP narm)
#define TRY_INCREMENT(_LABEL_)
int dense_is_diagonal(SEXP obj, const char *class)
SEXP dense_prod(SEXP obj, const char *class, int narm)
#define DG_LOOP(_CTYPE_, _PTR_, _ONE_)
#define DCPY(_PREFIX_, _CTYPE_, _PTR_)
void * Matrix_memset(void *dest, int ch, R_xlen_t length, size_t size)
int equal_character_vectors(SEXP s1, SEXP s2, int n)