3#define MK(_FORMAT_ ) Rf_mkString(_FORMAT_ )
4#define MS(_FORMAT_, ...) Matrix_sprintf(_FORMAT_, __VA_ARGS__)
6#define RMK(_FORMAT_ ) \
8#define RMS(_FORMAT_, ...) \
9 return MS(_FORMAT_, __VA_ARGS__)
10#define RMKMS(_FORMAT_, ...) \
11 return MK(MS(_FORMAT_, __VA_ARGS__))
13#define FRMK(_FORMAT_ ) \
15 Matrix_Free(work, lwork); \
18#define FRMS(_FORMAT_, ...) \
20 Matrix_Free(work, lwork); \
21 RMS (_FORMAT_, __VA_ARGS__); \
23#define FRMKMS(_FORMAT_, ...) \
25 Matrix_Free(work, lwork); \
26 RMKMS(_FORMAT_, __VA_ARGS__); \
38 RMS(
_(
"'%s' slot is not of type \"%s\""),
"Dim",
"integer");
39 if (XLENGTH(dim) != 2)
40 RMS(
_(
"'%s' slot does not have length %d"),
"Dim", 2);
41 int *pdim = INTEGER(dim), m = pdim[0], n = pdim[1];
42 if (m == NA_INTEGER || n == NA_INTEGER)
43 RMS(
_(
"'%s' slot contains NA"),
"Dim");
45 RMS(
_(
"'%s' slot has negative elements"),
"Dim");
53 return (msg) ? Rf_mkString(msg) : Rf_ScalarLogical(1);
59 if (
TYPEOF(dimnames) != VECSXP)
60 RMS(
_(
"'%s' slot is not a list"),
"Dimnames");
61 if (XLENGTH(dimnames) != 2)
62 RMS(
_(
"'%s' slot does not have length %d"),
"Dimnames", 2);
73 for (i = 0; i < 2; ++i) {
74 s = VECTOR_ELT(dimnames, i);
78 RMS(
_(
"%s[[%d]] is not NULL or a vector"),
"Dimnames", i + 1);
80 if (ns != pdim[i] && ns != 0)
81 RMS(
_(
"length of %s[[%d]] (%lld) is not equal to %s[%d] (%d)"),
82 "Dimnames", i + 1, (
long long) ns,
83 "Dim" , i + 1, pdim[i]);
92 return (msg) ? Rf_mkString(msg) : Rf_ScalarLogical(1);
99 for (i = 0; i < 2 && !fixup; ++i)
101 (s = VECTOR_ELT(dimnames, i)) != R_NilValue &&
102 (LENGTH(s) == 0 ||
TYPEOF(s) != STRSXP);
105 SEXP dimnames_ = PROTECT(Rf_allocVector(VECSXP, 2));
106 for (i = 0; i < 2; ++i) {
107 if ((s = VECTOR_ELT(dimnames, i)) == R_NilValue || LENGTH(s) == 0)
111 else if (
TYPEOF(s) == INTSXP && Rf_inherits(s,
"factor"))
112 PROTECT(s = Rf_asCharacterFactor(s));
114 PROTECT(s = Rf_coerceVector(s, STRSXP));
117 SET_VECTOR_ELT(dimnames_, i, s);
120 s = Rf_getAttrib(dimnames, R_NamesSymbol);
121 if (s != R_NilValue) {
123 Rf_setAttrib(dimnames_, R_NamesSymbol, s);
146 return (msg) ? Rf_mkString(msg) : Rf_ScalarLogical(1);
149#define KINDMATRIX_VALIDATE(_PREFIX_, _SEXPTYPE_) \
150SEXP _PREFIX_ ## Matrix_validate(SEXP obj) \
152 SEXP x = GET_SLOT(obj, Matrix_xSym); \
153 if (TYPEOF(x) != _SEXPTYPE_) \
154 RMKMS(_("'%s' slot is not of type \"%s\""), \
155 "x", Rf_type2char(_SEXPTYPE_)); \
156 return Rf_ScalarLogical(1); \
163#undef KINDMATRIX_VALIDATE
168 if (
TYPEOF(factors) != VECSXP)
169 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"factors",
"list");
170 if (XLENGTH(factors) > 0) {
172 SEXP nms = Rf_getAttrib(factors, R_NamesSymbol);
174 if (nms == R_NilValue)
175 RMKMS(
_(
"'%s' slot has no '%s' attribute"),
"factors",
"names");
178 return Rf_ScalarLogical(1);
183 int *pdim =
DIM(obj), n = pdim[1];
185 RMKMS(
_(
"%s[1] != %s[2] (matrix is not square)"),
"Dim",
"Dim");
187#ifdef ENFORCE_SYMMETRIC_DIMNAMES
196# define ANY_TO_STRING(x) \
197 ((TYPEOF(x) == INTSXP && Rf_inherits(x, "factor")) \
198 ? Rf_asCharacterFactor(x) \
199 : Rf_coerceVector(x, STRSXP))
202 ndn = Rf_getAttrib(dn, R_NamesSymbol);
205 const char *ndn0, *ndn1;
206 if (ndn != R_NilValue &&
207 *(ndn0 = CHAR(STRING_ELT(ndn, 0))) !=
'\0' &&
208 *(ndn1 = CHAR(STRING_ELT(ndn, 1))) !=
'\0' &&
209 strcmp(ndn0, ndn1) != 0)
210 RMKMS(
_(
"%s[1] differs from %s[2]"),
"Dimnames",
"Dimnames");
214 if ((rn = VECTOR_ELT(dn, 0)) != R_NilValue &&
215 (cn = VECTOR_ELT(dn, 1)) != R_NilValue &&
216 LENGTH(rn) == n && LENGTH(cn) == n && rn != cn) {
219 PROTECT(rn = ANY_TO_STRING(rn));
220 PROTECT(cn = ANY_TO_STRING(cn));
223 RMKMS(
_(
"%s[1] differs from %s[2]"),
"Dimnames",
"Dimnames");
232 if (
TYPEOF(uplo) != STRSXP)
233 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"uplo",
"character");
234 if (XLENGTH(uplo) != 1)
235 RMKMS(
_(
"'%s' slot does not have length %d"),
"uplo", 1);
236 const char *ul = CHAR(STRING_ELT(uplo, 0));
237 if (ul[0] ==
'\0' || ul[1] !=
'\0' || (ul[0] !=
'U' && ul[0] !=
'L'))
238 RMKMS(
_(
"'%s' slot is not \"%s\" or \"%s\""),
"uplo",
"U",
"L");
242 if (
TYPEOF(trans) != STRSXP)
243 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"trans",
"character");
244 if (XLENGTH(trans) != 1)
245 RMKMS(
_(
"'%s' slot does not have length %d"),
"trans", 1);
246 const char *ct = CHAR(STRING_ELT(trans, 0));
247 if (ct[0] ==
'\0' || ct[1] !=
'\0' || (ct[0] !=
'C' && ct[0] !=
'T'))
248 RMKMS(
_(
"'%s' slot is not \"%s\" or \"%s\""),
"trans",
"C",
"T");
256 int *pdim =
DIM(obj), n = pdim[1];
258 RMKMS(
_(
"%s[1] != %s[2] (matrix is not square)"),
"Dim",
"Dim");
261 if (
TYPEOF(uplo) != STRSXP)
262 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"uplo",
"character");
263 if (XLENGTH(uplo) != 1)
264 RMKMS(
_(
"'%s' slot does not have length %d"),
"uplo", 1);
265 const char *ul = CHAR(STRING_ELT(uplo, 0));
266 if (ul[0] ==
'\0' || ul[1] !=
'\0' || (ul[0] !=
'U' && ul[0] !=
'L'))
267 RMKMS(
_(
"'%s' slot is not \"%s\" or \"%s\""),
"uplo",
"U",
"L");
270 if (
TYPEOF(diag) != STRSXP)
271 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"diag",
"character");
272 if (XLENGTH(diag) != 1)
273 RMKMS(
_(
"'%s' slot does not have length %d"),
"diag", 1);
274 const char *nu = CHAR(STRING_ELT(diag, 0));
275 if (nu[0] ==
'\0' || nu[1] !=
'\0' || (nu[0] !=
'N' && nu[0] !=
'U'))
276 RMKMS(
_(
"'%s' slot is not \"%s\" or \"%s\""),
"diag",
"N",
"U");
278 return Rf_ScalarLogical(1);
283 int *pdim =
DIM(obj), m = pdim[0], n = pdim[1];
285 if (XLENGTH(x) != (int_fast64_t) m * n)
286 RMKMS(
_(
"'%s' slot does not have length %s"),
"x",
"prod(Dim)");
287 return Rf_ScalarLogical(1);
294 if (XLENGTH(x) != n + ((int_fast64_t) n * (n - 1)) / 2)
295 RMKMS(
_(
"'%s' slot does not have length %s"),
"x",
"Dim[1]*(Dim[1]+1)/2");
296 return Rf_ScalarLogical(1);
301 int *pdim =
DIM(obj), m = pdim[0], n = pdim[1];
308 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"p",
"integer");
309 if (XLENGTH(p) - 1 != n)
310 RMKMS(
_(
"'%s' slot does not have length %s"),
"p",
"Dim[2]+1");
311 int *pp = INTEGER(p);
313 RMKMS(
_(
"first element of '%s' slot is not 0"),
"p");
315 for (j = 1; j <= n; ++j) {
316 if (pp[j] == NA_INTEGER)
317 RMKMS(
_(
"'%s' slot contains NA"),
"p");
318 if (pp[j] < pp[j - 1])
319 RMKMS(
_(
"'%s' slot is not nondecreasing"),
"p");
320 if (pp[j] - pp[j - 1] > m)
321 RMKMS(
_(
"first differences of '%s' slot exceed %s"),
"p",
"Dim[1]");
325 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"i",
"integer");
326 if (XLENGTH(i) < pp[n])
327 RMKMS(
_(
"'%s' slot has length less than %s"),
"i",
"p[length(p)]");
328 int *pi = INTEGER(i), k, kend, ik, i0;
329 for (j = 1, k = 0; j <= n; ++j) {
334 if (ik == NA_INTEGER)
335 RMKMS(
_(
"'%s' slot contains NA"),
"i");
336 if (ik < 0 || ik >= m)
337 RMKMS(
_(
"'%s' slot has elements not in {%s}"),
338 "i",
"0,...,Dim[1]-1");
340 RMKMS(
_(
"'%s' slot is not increasing within columns"),
"i");
346 return Rf_ScalarLogical(1);
351 int *pdim =
DIM(obj), m = pdim[0], n = pdim[1];
358 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"p",
"integer");
359 if (XLENGTH(p) - 1 != m)
360 RMKMS(
_(
"'%s' slot does not have length %s"),
"p",
"Dim[1]+1");
361 int *pp = INTEGER(p);
363 RMKMS(
_(
"first element of '%s' slot is not 0"),
"p");
365 for (i = 1; i <= m; ++i) {
366 if (pp[i] == NA_INTEGER)
367 RMKMS(
_(
"'%s' slot contains NA"),
"p");
368 if (pp[i] < pp[i - 1])
369 RMKMS(
_(
"'%s' slot is not nondecreasing"),
"p");
370 if (pp[i] - pp[i - 1] > n)
371 RMKMS(
_(
"first differences of '%s' slot exceed %s"),
"p",
"Dim[2]");
375 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"j",
"integer");
376 if (XLENGTH(j) < pp[m])
377 RMKMS(
_(
"'%s' slot has length less than %s"),
"j",
"p[length(p)]");
378 int *pj = INTEGER(j), k, kend, jk, j0;
379 for (i = 1, k = 0; i <= m; ++i) {
384 if (jk == NA_INTEGER)
385 RMKMS(
_(
"'%s' slot contains NA"),
"j");
386 if (jk < 0 || jk >= n)
387 RMKMS(
_(
"'%s' slot has elements not in {%s}"),
388 "j",
"0,...,Dim[2]-1");
390 RMKMS(
_(
"'%s' slot is not increasing within rows"),
"j");
396 return Rf_ScalarLogical(1);
401 int *pdim =
DIM(obj), m = pdim[0], n = pdim[1];
408 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"i",
"integer");
410 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"j",
"integer");
411 R_xlen_t nnz = XLENGTH(i);
412 if (XLENGTH(j) != nnz)
413 RMKMS(
_(
"'%s' and '%s' slots do not have equal length"),
"i",
"j");
415 if (m == 0 || n == 0)
416 RMKMS(
_(
"'%s' slot has nonzero length but %s is 0"),
"i",
"prod(Dim)");
417 int *pi = INTEGER(i), *pj = INTEGER(j);
419 if (*pi == NA_LOGICAL)
420 RMKMS(
_(
"'%s' slot contains NA"),
"i");
421 if (*pj == NA_LOGICAL)
422 RMKMS(
_(
"'%s' slot contains NA"),
"j");
423 if (*pi < 0 || *pi >= m)
424 RMKMS(
_(
"'%s' slot has elements not in {%s}"),
425 "i",
"0,...,Dim[1]-1");
426 if (*pj < 0 || *pj >= n)
427 RMKMS(
_(
"'%s' slot has elements not in {%s}"),
428 "j",
"0,...,Dim[2]-1");
434 return Rf_ScalarLogical(1);
439 int *pdim =
DIM(obj), n = pdim[1];
441 RMKMS(
_(
"%s[1] != %s[2] (matrix is not square)"),
"Dim",
"Dim");
444 if (
TYPEOF(diag) != STRSXP)
445 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"diag",
"character");
446 if (XLENGTH(diag) != 1)
447 RMKMS(
_(
"'%s' slot does not have length %d"),
"diag", 1);
448 const char *nu = CHAR(STRING_ELT(diag, 0));
449 if (nu[0] ==
'\0' || nu[1] !=
'\0' || (nu[0] !=
'N' && nu[0] !=
'U'))
450 RMKMS(
_(
"'%s' slot is not \"%s\" or \"%s\""),
"diag",
"N",
"U");
451 int nonunit = nu[0] ==
'N';
454 if (XLENGTH(x) != ((nonunit) ? n : 0))
455 RMKMS(
_(
"'%s' slot is \"%s\" but '%s' slot does not have length %s"),
456 "diag", (nonunit) ?
"N" :
"U",
"x", (nonunit) ?
"Dim[1]" :
"0");
458 return Rf_ScalarLogical(1);
464 if (
TYPEOF(margin) != INTSXP)
465 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"margin",
"integer");
466 if (XLENGTH(margin) != 1)
467 RMKMS(
_(
"'%s' slot does not have length %d"),
"margin", 1);
468 int mg = INTEGER(margin)[0] - 1;
469 if (mg != 0 && mg != 1)
470 RMKMS(
_(
"'%s' slot is not %d or %d"),
"margin", 1, 2);
472 int *pdim =
DIM(obj), m = pdim[0], n = pdim[1];
474 RMKMS(
_(
"%s-by-%s %s invalid for positive '%s' when %s=%d"),
475 (mg == 0) ?
"m" :
"0", (mg == 0) ?
"0" :
"n",
"indMatrix",
476 (mg == 0) ?
"m" :
"n",
"margin", (mg == 0) ? 1 : 2);
479 if (
TYPEOF(perm) != INTSXP)
480 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"perm",
"integer");
481 if (XLENGTH(perm) != m)
482 RMKMS(
_(
"'%s' slot does not have length %s"),
"perm",
"Dim[margin]");
483 int *pperm = INTEGER(perm);
485 if (*pperm == NA_INTEGER)
486 RMKMS(
_(
"'%s' slot contains NA"),
"perm");
487 if (*pperm < 1 || *pperm > n)
488 RMKMS(
_(
"'%s' slot has elements not in {%s}"),
489 "perm",
"1,...,Dim[1+margin%%2]");
493 return Rf_ScalarLogical(1);
498 int *pdim =
DIM(obj), n = pdim[1];
500 RMKMS(
_(
"%s[1] != %s[2] (matrix is not square)"),
"Dim",
"Dim");
507 int j, *pperm = INTEGER(perm);
508 for (j = 0; j < n; ++j) {
509 if (work[*pperm - 1])
510 FRMKMS(
_(
"'%s' slot contains duplicates"),
"perm");
511 work[*(pperm++) - 1] = 1;
516 return Rf_ScalarLogical(1);
522 int *pp = INTEGER(p), n = (int) (XLENGTH(p) - 1);
529 int *pi = INTEGER(i), j, k, kend;
533 for (j = 0, k = 0; j < n; ++j) {
537 RMKMS(
_(
"%s=\"%s\" but there are entries below the diagonal"),
543 for (j = 0, k = 0; j < n; ++j) {
547 RMKMS(
_(
"%s=\"%s\" but there are entries above the diagonal"),
555 return Rf_ScalarLogical(1);
560 if (
DIAG(obj) ==
'N')
564 int *pp = INTEGER(p), n = (int) (XLENGTH(p) - 1);
571 int *pi = INTEGER(i), j, k, kend;
575 for (j = 0, k = 0; j < n; ++j) {
579 RMKMS(
_(
"%s=\"%s\" but there are entries below the diagonal"),
582 RMKMS(
_(
"%s=\"%s\" but there are entries on the diagonal"),
588 for (j = 0, k = 0; j < n; ++j) {
592 RMKMS(
_(
"%s=\"%s\" but there are entries above the diagonal"),
595 RMKMS(
_(
"%s=\"%s\" but there are entries on the diagonal"),
603 return Rf_ScalarLogical(1);
609 int *pp = INTEGER(p), m = (int) (XLENGTH(p) - 1);
616 int *pj = INTEGER(j), i, k, kend;
620 for (i = 0, k = 0; i < m; ++i) {
624 RMKMS(
_(
"%s=\"%s\" but there are entries below the diagonal"),
630 for (i = 0, k = 0; i < m; ++i) {
634 RMKMS(
_(
"%s=\"%s\" but there are entries above the diagonal"),
642 return Rf_ScalarLogical(1);
647 if (
DIAG(obj) ==
'N')
651 int *pp = INTEGER(p), m = (int) (XLENGTH(p) - 1);
658 int *pj = INTEGER(j), i, k, kend;
662 for (i = 0, k = 0; i < m; ++i) {
666 RMKMS(
_(
"%s=\"%s\" but there are entries below the diagonal"),
669 RMKMS(
_(
"%s=\"%s\" but there are entries on the diagonal"),
675 for (i = 0, k = 0; i < m; ++i) {
679 RMKMS(
_(
"%s=\"%s\" but there are entries above the diagonal"),
682 RMKMS(
_(
"%s=\"%s\" but there are entries on the diagonal"),
690 return Rf_ScalarLogical(1);
696 R_xlen_t nnz = XLENGTH(i);
703 int *pi = INTEGER(i), *pj = INTEGER(j);
708 if (*(pi++) > *(pj++))
709 RMKMS(
_(
"%s=\"%s\" but there are entries below the diagonal"),
713 if (*(pi++) < *(pj++))
714 RMKMS(
_(
"%s=\"%s\" but there are entries above the diagonal"),
719 return Rf_ScalarLogical(1);
724 if (
DIAG(obj) ==
'N')
728 R_xlen_t nnz = XLENGTH(i);
735 int *pi = INTEGER(i), *pj = INTEGER(j);
741 RMKMS(
_(
"%s=\"%s\" but there are entries below the diagonal"),
744 RMKMS(
_(
"%s=\"%s\" but there are entries on the diagonal"),
752 RMKMS(
_(
"%s=\"%s\" but there are entries above the diagonal"),
755 RMKMS(
_(
"%s=\"%s\" but there are entries on the diagonal"),
763 return Rf_ScalarLogical(1);
771 if (XLENGTH(x) != XLENGTH(i))
772 RMKMS(
_(
"'%s' and '%s' slots do not have equal length"),
"i",
"x");
773 return Rf_ScalarLogical(1);
779 if (
TYPEOF(val) != STRSXP)
787 if (
TYPEOF(val) != STRSXP)
797 if (XLENGTH(x) != XLENGTH(j))
798 RMKMS(
_(
"'%s' and '%s' slots do not have equal length"),
"j",
"x");
799 return Rf_ScalarLogical(1);
805 if (
TYPEOF(val) != STRSXP)
813 if (
TYPEOF(val) != STRSXP)
823 if (XLENGTH(x) != XLENGTH(i))
824 RMKMS(
_(
"'%s' and '%s' slots do not have equal length"),
"i",
"x");
825 return Rf_ScalarLogical(1);
831 if (
TYPEOF(val) != STRSXP)
839 if (
TYPEOF(val) != STRSXP)
851 int n =
DIM(obj)[1], j;
852 R_xlen_t n1a = (R_xlen_t) n + 1;
855 if (
TYPEOF(x) == REALSXP) {
856 double *px = REAL(x);
857 for (j = 0; j < n; ++j, px += n1a)
858 if (!ISNAN(*px) && *px < 0.0)
859 RMK(
_(
"matrix has negative diagonal elements"));
861 if (
TRANS(obj) !=
'C')
862 RMKMS(
_(
"'%s' slot is not \"%s\""),
"trans",
"C");
863 Rcomplex *px = COMPLEX(x);
864 for (j = 0; j < n; ++j, px += n1a)
865 if (!ISNAN((*px).r) && (*px).r < 0.0)
866 RMK(
_(
"matrix has diagonal elements with negative real part"));
869 return Rf_ScalarLogical(1);
874 int n =
DIM(obj)[1], j;
878 if (
TYPEOF(x) == REALSXP) {
879 double *px = REAL(x);
881 for (j = 0; j < n; px += (++j)+1)
882 if (!ISNAN(*px) && *px < 0.0)
883 RMK(
_(
"matrix has negative diagonal elements"));
885 for (j = 0; j < n; px += n-(j++))
886 if (!ISNAN(*px) && *px < 0.0)
887 RMK(
_(
"matrix has negative diagonal elements"));
890 if (
TRANS(obj) !=
'C')
891 RMKMS(
_(
"'%s' slot is not \"%s\""),
"trans",
"C");
892 Rcomplex *px = COMPLEX(x);
894 for (j = 0; j < n; px += (++j)+1)
895 if (!ISNAN((*px).r) && (*px).r < 0.0)
896 RMK(
_(
"matrix has diagonal elements with negative real part"));
898 for (j = 0; j < n; px += n-(j++))
899 if (!ISNAN((*px).r) && (*px).r < 0.0)
900 RMK(
_(
"matrix has diagonal elements with negative real part"));
904 return Rf_ScalarLogical(1);
909 int n =
DIM(obj)[1], j;
916 int *pp = INTEGER(p), *pi = INTEGER(i);
917 if (
TYPEOF(x) == REALSXP) {
918 double *px = REAL(x);
920 for (j = 0; j < n; ++j)
921 if (pp[j + 1] - pp[j] > 0 && pi[pp[j + 1] - 1] == j &&
922 !ISNAN(px[pp[j + 1] - 1]) && px[pp[j + 1] - 1] < 0.0)
923 RMK(
_(
"matrix has negative diagonal elements"));
925 for (j = 0; j < n; ++j)
926 if (pp[j + 1] - pp[j] > 0 && pi[pp[j]] == j &&
927 !ISNAN(px[pp[j]]) && px[pp[j]] < 0.0)
928 RMK(
_(
"matrix has negative diagonal elements"));
931 if (
TRANS(obj) !=
'C')
932 RMKMS(
_(
"'%s' slot is not \"%s\""),
"trans",
"C");
933 Rcomplex *px = COMPLEX(x);
935 for (j = 0; j < n; ++j)
936 if (pp[j + 1] - pp[j] > 0 && pi[pp[j + 1] - 1] == j &&
937 !ISNAN(px[pp[j + 1] - 1].r) && px[pp[j + 1] - 1].r < 0.0)
938 RMK(
_(
"matrix has diagonal elements with negative real part"));
940 for (j = 0; j < n; ++j)
941 if (pp[j + 1] - pp[j] > 0 && pi[pp[j]] == j &&
942 !ISNAN(px[pp[j]].r) && px[pp[j]].r < 0.0)
943 RMK(
_(
"matrix has diagonal elements with negative real part"));
947 return Rf_ScalarLogical(1);
952 int m =
DIM(obj)[0], i;
959 int *pp = INTEGER(p), *pj = INTEGER(j);
960 if (
TYPEOF(x) == REALSXP) {
961 double *px = REAL(x);
963 for (i = 0; i < m; ++i)
964 if (pp[i + 1] - pp[i] > 0 && pj[pp[i]] == i &&
965 !ISNAN(px[pp[i]]) && px[pp[i]] < 0.0)
966 RMK(
_(
"matrix has negative diagonal elements"));
968 for (i = 0; i < m; ++i)
969 if (pp[i + 1] - pp[i] > 0 && pj[pp[i + 1] - 1] == i &&
970 !ISNAN(px[pp[i + 1] - 1]) && px[pp[i + 1] - 1] < 0.0)
971 RMK(
_(
"matrix has negative diagonal elements"));
974 if (
TRANS(obj) !=
'C')
975 RMKMS(
_(
"'%s' slot is not \"%s\""),
"trans",
"C");
976 Rcomplex *px = COMPLEX(x);
978 for (i = 0; i < m; ++i)
979 if (pp[i + 1] - pp[i] > 0 && pj[pp[i]] == i &&
980 !ISNAN(px[pp[i]].r) && px[pp[i]].r < 0.0)
981 RMK(
_(
"matrix has diagonal elements with negative real part"));
983 for (i = 0; i < m; ++i)
984 if (pp[i + 1] - pp[i] > 0 && pj[pp[i + 1] - 1] == i &&
985 !ISNAN(px[pp[i + 1] - 1].r) && px[pp[i + 1] - 1].r < 0.0)
986 RMK(
_(
"matrix has diagonal elements with negative real part"));
990 return Rf_ScalarLogical(1);
1001 int *pi = INTEGER(i), *pj = INTEGER(j);
1002 R_xlen_t nnz = XLENGTH(x);
1007 if (
TYPEOF(x) == REALSXP) {
1008 double *px = REAL(x);
1015 if (!ISNAN(work[n]) && work[n] < 0.0)
1016 FRMK(
_(
"matrix has negative diagonal elements"));
1018 if (
TRANS(obj) !=
'C')
1019 RMKMS(
_(
"'%s' slot is not \"%s\""),
"trans",
"C");
1020 Rcomplex *px = COMPLEX(x);
1023 work[*pi] += (*px).r;
1027 if (!ISNAN(work[n]) && work[n] < 0.0)
1028 FRMK(
_(
"matrix has diagonal elements with negative real part"));
1032 return Rf_ScalarLogical(1);
1037 int n =
DIM(obj)[1], j;
1038 R_xlen_t n1a = (R_xlen_t) n + 1;
1041 double *px = REAL(x);
1042 for (j = 0; j < n; ++j, px += n1a)
1043 if (ISNAN(*px) || *px != 1.0)
1044 RMK(
_(
"matrix has nonunit diagonal elements"));
1047 if (
TYPEOF(sd) != REALSXP)
1048 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"sd",
"double");
1049 if (XLENGTH(sd) != n)
1050 RMKMS(
_(
"'%s' slot does not have length %s"),
"sd",
"Dim[1]");
1051 double *psd = REAL(sd);
1052 for (j = 0; j < n; ++j)
1053 if (!ISNAN(psd[j]) && psd[j] < 0.0)
1054 RMKMS(
_(
"'%s' slot has negative elements"),
"sd");
1056 return Rf_ScalarLogical(1);
1061 int n =
DIM(obj)[1], j;
1062 char ul =
UPLO(obj);
1065 double *px = REAL(x);
1067 for (j = 0; j < n; px += (++j)+1)
1068 if (ISNAN(*px) || *px != 1.0)
1069 RMK(
_(
"matrix has nonunit diagonal elements"));
1071 for (j = 0; j < n; px += n-(j++))
1072 if (ISNAN(*px) || *px != 1.0)
1073 RMK(
_(
"matrix has nonunit diagonal elements"));
1077 if (
TYPEOF(sd) != REALSXP)
1078 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"sd",
"double");
1079 if (XLENGTH(sd) != n)
1080 RMKMS(
_(
"'%s' slot does not have length %s"),
"sd",
"Dim[1]");
1081 double *psd = REAL(sd);
1082 for (j = 0; j < n; ++j)
1083 if (!ISNAN(psd[j]) && psd[j] < 0.0)
1084 RMKMS(
_(
"'%s' slot has negative elements"),
"sd");
1086 return Rf_ScalarLogical(1);
1092 if (
TYPEOF(length) != INTSXP &&
TYPEOF(length) != REALSXP)
1093 RMKMS(
_(
"'%s' slot is not of type \"%s\" or \"%s\""),
1094 "length",
"integer",
"double");
1095 if (XLENGTH(length) != 1)
1096 RMKMS(
_(
"'%s' slot does not have length %d"),
"length", 1);
1098 if (
TYPEOF(length) == INTSXP) {
1099 int n_ = INTEGER(length)[0];
1100 if (n_ == NA_INTEGER)
1101 RMKMS(
_(
"'%s' slot is NA"),
"length");
1103 RMKMS(
_(
"'%s' slot is negative"),
"length");
1104 n = (int_fast64_t) n_;
1106 double n_ = REAL(length)[0];
1108 RMKMS(
_(
"'%s' slot is NA"),
"length");
1110 RMKMS(
_(
"'%s' slot is negative"),
"length");
1112 RMKMS(
_(
"'%s' slot exceeds %s"),
"length",
"2^53");
1113 n = (int_fast64_t) n_;
1118 RMKMS(
_(
"'%s' slot is not of type \"%s\" or \"%s\""),
1119 "i",
"integer",
"double");
1120 R_xlen_t nnz = XLENGTH(i);
1122 RMKMS(
_(
"'%s' slot has length greater than '%s' slot"),
"i",
"length");
1123 if (
TYPEOF(i) == INTSXP) {
1124 int *pi = INTEGER(i), max = (n > INT_MAX) ? INT_MAX : (int) n, last = 0;
1126 if (*pi == NA_INTEGER)
1127 RMKMS(
_(
"'%s' slot contains NA"),
"i");
1128 if (*pi < 1 || *pi > max)
1129 RMKMS(
_(
"'%s' slot has elements not in {%s}"),
1130 "i",
"1,...,length");
1132 RMKMS(
_(
"'%s' slot is not increasing"),
"i");
1136 double *pi = REAL(i), max = (double) n, last = 0.0, tmp;
1139 RMKMS(
_(
"'%s' slot contains NA"),
"i");
1140 tmp = trunc(*(pi++));
1141 if (tmp < 1.0 || tmp > max)
1142 RMKMS(
_(
"'%s' slot has elements not in {%s} after truncation towards zero"),
1143 "i",
"1,...,length");
1145 RMKMS(
_(
"'%s' slot is not increasing after truncation towards zero"),
"i");
1150 return Rf_ScalarLogical(1);
1153#define KINDVECTOR_VALIDATE(_PREFIX_, _SEXPTYPE_) \
1154SEXP _PREFIX_ ## sparseVector_validate(SEXP obj) \
1156 SEXP x = PROTECT(GET_SLOT(obj, Matrix_xSym)), \
1157 i = PROTECT(GET_SLOT(obj, Matrix_iSym)); \
1159 if (TYPEOF(x) != _SEXPTYPE_) \
1160 RMKMS(_("'%s' slot is not of type \"%s\""), \
1161 "x", Rf_type2char(_SEXPTYPE_)); \
1162 if (XLENGTH(x) != XLENGTH(i)) \
1163 RMKMS(_("'%s' and '%s' slots do not have equal length"), \
1165 return Rf_ScalarLogical(1); \
1171#undef KINDVECTOR_VALIDATE
1180 int *pdim =
DIM(obj), n = pdim[1];
1182 RMKMS(
_(
"%s[1] != %s[2] (matrix is not square)"),
"Dim",
"Dim");
1183 int_fast64_t nn = (int_fast64_t) n * n;
1187 RMKMS(
_(
"'%s' slot is not of type \"%s\" or \"%s\""),
1188 "x",
"double",
"complex");
1189 if (XLENGTH(x) != nn && XLENGTH(x) != 0)
1190 RMKMS(
_(
"'%s' slot does not have length %s or length %s"),
1191 "x",
"prod(Dim)",
"0");
1192 int normal = n > 0 && XLENGTH(x) == 0;
1196 RMKMS(
_(
"'%s' and '%s' slots do not have the same type"),
1198 if (XLENGTH(vectors) != nn && XLENGTH(vectors) != 0)
1199 RMKMS(
_(
"'%s' slot does not have length %s or length %s"),
1200 "vectors",
"prod(Dim)",
"0");
1203 if (
TYPEOF(values) != REALSXP) {
1205 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
1206 "values",
"double");
1207 else if (
TYPEOF(x) != CPLXSXP)
1208 RMKMS(
_(
"'%s' slot is not of type \"%s\" or \"%s\""),
1209 "values",
"double",
"complex");
1211 if (XLENGTH(values) != n)
1212 RMKMS(
_(
"'%s' slot does not have length %s"),
"values",
"Dim[1]");
1214 return Rf_ScalarLogical(1);
1219 int *pdim =
DIM(obj), m = pdim[0], n = pdim[1], r = (m < n) ? m : n;
1223 RMKMS(
_(
"'%s' slot is not of type \"%s\" or \"%s\""),
1224 "x",
"double",
"complex");
1225 if (XLENGTH(x) != (int_fast64_t) m * n)
1226 RMKMS(
_(
"'%s' slot does not have length %s"),
"x",
"prod(Dim)");
1229 if (
TYPEOF(beta) != REALSXP &&
TYPEOF(beta) != CPLXSXP)
1230 RMKMS(
_(
"'%s' and '%s' slots do not have the same type"),
1232 if (XLENGTH(beta) != r)
1233 RMKMS(
_(
"'%s' slot does not have length %s"),
"beta",
"min(Dim)");
1236 if (
TYPEOF(perm) != INTSXP)
1237 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"perm",
"integer");
1238 if (XLENGTH(perm) != n)
1239 RMKMS(
_(
"'%s' slot does not have length %s"),
"perm",
"Dim[2]");
1243 int j, *pperm = INTEGER(perm);
1244 for (j = 0; j < n; ++j) {
1245 if (*pperm == NA_INTEGER)
1246 FRMKMS(
_(
"'%s' slot contains NA"),
"perm");
1247 if (*pperm < 1 || *pperm > n)
1248 FRMKMS(
_(
"'%s' slot has elements not in {%s}"),
1249 "perm",
"1,...,Dim[2]");
1250 if (work[*pperm - 1])
1251 FRMKMS(
_(
"'%s' slot contains duplicates"),
"perm");
1252 work[*(pperm++)] = 1;
1256 return Rf_ScalarLogical(1);
1261 int *pdim =
DIM(obj), m = pdim[0], n = pdim[1], r = (m < n) ? m : n;
1265 RMKMS(
_(
"'%s' slot is not of type \"%s\" or \"%s\""),
1266 "x",
"double",
"complex");
1267 if (XLENGTH(x) != (int_fast64_t) m * n)
1268 RMKMS(
_(
"'%s' slot does not have length %s"),
"x",
"prod(Dim)");
1271 if (
TYPEOF(perm) != INTSXP)
1272 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"perm",
"integer");
1273 if (XLENGTH(perm) != r)
1274 RMKMS(
_(
"'%s' slot does not have length %s"),
"perm",
"min(Dim)");
1275 int *pperm = INTEGER(perm);
1277 if (*pperm == NA_INTEGER)
1278 RMKMS(
_(
"'%s' slot contains NA"),
"perm");
1279 if (*pperm < 1 || *pperm > m)
1280 RMKMS(
_(
"'%s' slot has elements not in {%s}"),
1281 "perm",
"1,...,Dim[1]");
1285 return Rf_ScalarLogical(1);
1290 int *pdim =
DIM(obj), n = pdim[1];
1292 RMKMS(
_(
"%s[1] != %s[2] (matrix is not square)"),
"Dim",
"Dim");
1295 if (
TYPEOF(uplo) != STRSXP)
1296 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"uplo",
"character");
1297 if (XLENGTH(uplo) != 1)
1298 RMKMS(
_(
"'%s' slot does not have length %d"),
"uplo", 1);
1299 const char *ul = CHAR(STRING_ELT(uplo, 0));
1300 if (ul[0] ==
'\0' || ul[1] !=
'\0' || (ul[0] !=
'U' && ul[0] !=
'L'))
1301 RMKMS(
_(
"'%s' slot is not \"%s\" or \"%s\""),
"uplo",
"U",
"L");
1305 if (
TYPEOF(trans) != STRSXP)
1306 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"trans",
"character");
1307 if (XLENGTH(trans) != 1)
1308 RMKMS(
_(
"'%s' slot does not have length %d"),
"trans", 1);
1309 const char *ct = CHAR(STRING_ELT(trans, 0));
1310 if (ct[0] ==
'\0' || ct[1] !=
'\0' || (ct[0] !=
'C' && ct[0] !=
'T'))
1311 RMKMS(
_(
"'%s' slot is not \"%s\" or \"%s\""),
"trans",
"C",
"T");
1316 RMKMS(
_(
"'%s' slot is not of type \"%s\" or \"%s\""),
1317 "x",
"double",
"complex");
1318 int packed = XLENGTH(x) != (int_fast64_t) n * n;
1319 if (packed && XLENGTH(x) != n + ((int_fast64_t) n * (n - 1)) / 2)
1320 RMKMS(
_(
"'%s' slot does not have length %s or length %s"),
1321 "x",
"prod(Dim)",
"Dim[1]*(Dim[1]+1)/2");
1324 if (
TYPEOF(perm) != INTSXP)
1325 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"perm",
"integer");
1326 if (XLENGTH(perm) != n)
1327 RMKMS(
_(
"'%s' slot does not have length %s"),
"perm",
"Dim[1]");
1328 int n_ = n, *pperm = INTEGER(perm);
1330 if (*pperm == NA_INTEGER)
1331 RMKMS(
_(
"'%s' slot contains NA"),
"perm");
1332 if (*pperm < -n || *pperm == 0 || *pperm > n)
1333 RMKMS(
_(
"'%s' slot has elements not in {%s}\\{%s}"),
1334 "perm",
"-Dim[1],...,Dim[1]",
"0");
1338 }
else if (n_ > 1 && *(pperm + 1) == *pperm) {
1342 RMKMS(
_(
"'%s' slot has unpaired negative elements"),
"perm");
1345 return Rf_ScalarLogical(1);
1350 int *pdim =
DIM(obj), n = pdim[1];
1352 RMKMS(
_(
"%s[1] != %s[2] (matrix is not square)"),
"Dim",
"Dim");
1355 if (
TYPEOF(uplo) != STRSXP)
1356 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"uplo",
"character");
1357 if (XLENGTH(uplo) != 1)
1358 RMKMS(
_(
"'%s' slot does not have length %d"),
"uplo", 1);
1359 const char *ul = CHAR(STRING_ELT(uplo, 0));
1360 if (ul[0] ==
'\0' || ul[1] !=
'\0' || (ul[0] !=
'U' && ul[0] !=
'L'))
1361 RMKMS(
_(
"'%s' slot is not \"%s\" or \"%s\""),
"uplo",
"U",
"L");
1365 RMKMS(
_(
"'%s' slot is not of type \"%s\" or \"%s\""),
1366 "x",
"double",
"complex");
1367 int j, packed = XLENGTH(x) != (int_fast64_t) n * n;
1368 if (packed && XLENGTH(x) != n + ((int_fast64_t) n * (n - 1)) / 2)
1369 RMKMS(
_(
"'%s' slot does not have length %s or length %s"),
1370 "x",
"prod(Dim)",
"Dim[1]*(Dim[1]+1)/2");
1373 if (
TYPEOF(x) == REALSXP) {
1374 double *px = REAL(x);
1376 R_xlen_t n1a = (R_xlen_t) n + 1;
1377 for (j = 0; j < n; ++j, px += n1a)
1378 if (!ISNAN(*px) && *px < 0.0)
1379 RMK(
_(
"Cholesky factor has negative diagonal elements"));
1380 }
else if (*ul ==
'U') {
1381 for (j = 0; j < n; ++j, px += (++j)+1)
1382 if (!ISNAN(*px) && *px < 0.0)
1383 RMK(
_(
"Cholesky factor has negative diagonal elements"));
1385 for (j = 0; j < n; ++j, px += n-(j++))
1386 if (!ISNAN(*px) && *px < 0.0)
1387 RMK(
_(
"Cholesky factor has negative diagonal elements"));
1390 Rcomplex *px = COMPLEX(x);
1392 R_xlen_t n1a = (R_xlen_t) n + 1;
1393 for (j = 0; j < n; ++j, px += n1a)
1394 if (!ISNAN((*px).r) && (*px).r < 0.0)
1395 RMK(
_(
"Cholesky factor has diagonal elements with negative real part"));
1396 }
else if (*ul ==
'U') {
1397 for (j = 0; j < n; ++j, px += (++j)+1)
1398 if (!ISNAN((*px).r) && (*px).r < 0.0)
1399 RMK(
_(
"Cholesky factor has diagonal elements with negative real part"));
1401 for (j = 0; j < n; ++j, px += n-(j++))
1402 if (!ISNAN((*px).r) && (*px).r < 0.0)
1403 RMK(
_(
"Cholesky factor has diagonal elements with negative real part"));
1408 if (
TYPEOF(perm) != INTSXP)
1409 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"perm",
"integer");
1410 if (XLENGTH(perm) != n && XLENGTH(perm) != 0)
1411 RMKMS(
_(
"'%s' slot does not have length %s or length %s"),
1412 "perm",
"Dim[1]",
"0");
1413 if (LENGTH(perm) == n) {
1417 int *pperm = INTEGER(perm);
1418 for (j = 0; j < n; ++j) {
1419 if (*pperm == NA_INTEGER)
1420 FRMKMS(
_(
"'%s' slot contains NA"),
"perm");
1421 if (*pperm < 0 || *pperm >= n)
1422 FRMKMS(
_(
"'%s' slot has elements not in {%s}"),
1423 "perm",
"0,...,Dim[1]-1");
1425 FRMKMS(
_(
"'%s' slot contains duplicates"),
"perm");
1426 work[*(pperm++)] = 1;
1431 return Rf_ScalarLogical(1);
1438 int *pdim =
DIM(obj), m = pdim[0], n = pdim[1];
1440 RMK(
_(
"matrix has more columns than rows"));
1443 if (
TYPEOF(beta) != REALSXP)
1444 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"beta",
"double");
1445 if (XLENGTH(beta) != n)
1446 RMKMS(
_(
"'%s' slot does not have length %s"),
"beta",
"Dim[2]");
1449 int *pp, *pi, *pq, j, k, kend, m0;
1454 pdim =
DIM(V); m0 = pdim[0];
1457 RMKMS(
_(
"'%s' slot has fewer than %s rows"),
"V",
"Dim[1]");
1459 RMKMS(
_(
"'%s' slot has more than %s rows"),
"V",
"Dim[1]+Dim[2]");
1461 RMKMS(
_(
"'%s' slot does not have %s columns"),
"V",
"Dim[2]");
1464 for (j = 0, k = 0; j < n; ++j) {
1468 RMKMS(
_(
"'%s' slot must be lower trapezoidal but has entries above the diagonal"),
"V");
1479 RMKMS(
_(
"'%s' slot does not have %s row"),
"R",
"nrow(V)");
1481 RMKMS(
_(
"'%s' slot does not have %s columns"),
"R",
"Dim[2]");
1484 for (j = 0, k = 0; j < n; ++j) {
1486 if (k < kend && pi[kend - 1] > j)
1487 RMKMS(
_(
"'%s' slot must be upper trapezoidal but has entries below the diagonal"),
"R");
1495 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"p",
"integer");
1497 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"q",
"integer");
1498 if (XLENGTH(p) != m0)
1499 RMKMS(
_(
"'%s' slot does not have length %s"),
"p",
"nrow(V)");
1500 if (XLENGTH(q) != n && XLENGTH(q) != 0)
1501 RMKMS(
_(
"'%s' slot does not have length %s or length %s"),
1502 "q",
"Dim[2]",
"0");
1507 for (j = 0; j < m0; ++j) {
1508 if (*pp == NA_INTEGER)
1509 FRMKMS(
_(
"'%s' slot contains NA"),
"p");
1510 if (*pp < 0 || *pp >= m0)
1511 FRMKMS(
_(
"'%s' slot has elements not in {%s}"),
1512 "p",
"0,...,nrow(V)-1");
1514 FRMKMS(
_(
"'%s' slot contains duplicates"),
"p");
1517 if (LENGTH(q) == n) {
1519 for (j = 0; j < n; ++j) {
1520 if (*pq == NA_INTEGER)
1521 FRMKMS(
_(
"'%s' slot contains NA"),
"q");
1522 if (*pq < 0 || *pq >= n)
1523 FRMKMS(
_(
"'%s' slot has elements not in {%s}"),
1524 "q",
"0,...,Dim[2]-1");
1526 FRMKMS(
_(
"'%s' slot contains duplicates"),
"q");
1532 return Rf_ScalarLogical(1);
1539 int *pdim =
DIM(obj), n = pdim[1];
1541 RMKMS(
_(
"%s[1] != %s[2] (matrix is not square)"),
"Dim",
"Dim");
1549 if (pdim[0] != n || pdim[1] != n)
1550 RMKMS(
_(
"dimensions of '%s' slot are not identical to '%s'"),
"L",
"Dim");
1552 RMKMS(
_(
"'%s' slot is upper (not lower) triangular"),
"L");
1560 int *pp = INTEGER(p), *pi = INTEGER(i), j, k, kend;
1561 if (
TYPEOF(x) == REALSXP) {
1562 double *px = REAL(x);
1563 for (j = 0, k = 0; j < n; ++j) {
1565 if (kend == k || pi[k] != j || px[k] != 1.0)
1566 RMKMS(
_(
"'%s' slot has nonunit diagonal elements"),
"L");
1570 Rcomplex *px = COMPLEX(x);
1571 for (j = 0, k = 0; j < n; ++j) {
1573 if (kend == k || pi[k] != j || px[k].r != 1.0 || px[k].i != 0.0)
1574 RMKMS(
_(
"'%s' slot has nonunit diagonal elements"),
"L");
1584 if (pdim[0] != n || pdim[1] != n)
1585 RMKMS(
_(
"dimensions of '%s' slot are not identical to '%s'"),
"U",
"Dim");
1587 RMKMS(
_(
"'%s' slot is lower (not upper) triangular"),
"U");
1593 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"p",
"integer");
1595 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"q",
"integer");
1596 if (XLENGTH(p) != n)
1597 RMKMS(
_(
"'%s' slot does not have length %s"),
"p",
"Dim[1]");
1598 if (XLENGTH(q) != n && XLENGTH(q) != 0)
1599 RMKMS(
_(
"'%s' slot does not have length %s or length %s"),
1600 "q",
"Dim[2]",
"0");
1604 int j, *pp = INTEGER(p);
1605 for (j = 0; j < n; ++j) {
1606 if (*pp == NA_INTEGER)
1607 FRMKMS(
_(
"'%s' slot contains NA"),
"p");
1608 if (*pp < 0 || *pp >= n)
1609 FRMKMS(
_(
"'%s' slot has elements not in {%s}"),
1610 "p",
"0,...,Dim[1]-1");
1612 FRMKMS(
_(
"'%s' slot contains duplicates"),
"p");
1615 if (LENGTH(q) == n) {
1616 int *pq = INTEGER(q);
1617 for (j = 0; j < n; ++j) {
1618 if (*pq == NA_INTEGER)
1619 FRMKMS(
_(
"'%s' slot contains NA"),
"q");
1620 if (*pq < 0 || *pq >= n)
1621 FRMKMS(
_(
"'%s' slot has elements not in {%s}"),
1622 "q",
"0,...,Dim[2]-1");
1624 FRMKMS(
_(
"'%s' slot contains duplicates"),
"q");
1630 return Rf_ScalarLogical(1);
1635 int *pdim =
DIM(obj), n = pdim[1];
1637 RMKMS(
_(
"%s[1] != %s[2] (matrix is not square)"),
"Dim",
"Dim");
1640 if (
TYPEOF(ordering) != INTSXP)
1641 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"ordering",
"integer");
1642 if (XLENGTH(ordering) != 1)
1643 RMKMS(
_(
"'%s' slot does not have length %d"),
"ordering", 1);
1644 int or = INTEGER(ordering)[0];
1645 if (or < 0 || or > 6)
1646 RMKMS(
_(
"'%s' is not in %d:%d"),
"ordering", 0, 6);
1649 if (
TYPEOF(perm) != INTSXP)
1650 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"perm",
"integer");
1652 if (XLENGTH(perm) != 0)
1653 RMKMS(
_(
"'%s' slot does not have length %d"),
"perm", 0);
1655 if (XLENGTH(perm) != n)
1656 RMKMS(
_(
"'%s' slot does not have length %s"),
"perm",
"Dim[1]");
1660 int j, *pperm = INTEGER(perm);
1661 for (j = 0; j < n; ++j) {
1662 if (*pperm == NA_INTEGER)
1663 FRMKMS(
_(
"'%s' slot contains NA"),
"perm");
1664 if (*pperm < 0 || *pperm >= n)
1665 FRMKMS(
_(
"'%s' slot has elements not in {%s}"),
1666 "perm",
"0,...,Dim[1]-1");
1668 FRMKMS(
_(
"'%s' slot contains duplicates"),
"perm");
1669 work[*(pperm++)] = 1;
1675 if (
TYPEOF(colcount) != INTSXP)
1676 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"colcount",
"integer");
1677 if (XLENGTH(colcount) != n)
1678 RMKMS(
_(
"'%s' slot does not have length %s"),
"colcount",
"Dim[2]");
1679 int j, *pcolcount = INTEGER(colcount);
1680 for (j = 0; j < n; ++j) {
1681 if (pcolcount[j] == NA_INTEGER)
1682 RMKMS(
_(
"'%s' slot contains NA"),
"colcount");
1683 if (pcolcount[j] < 0 || pcolcount[j] > n - j)
1684 RMKMS(
_(
"%s is not in {%s}"),
"colcount[j]",
"0,...,Dim[2]-j+1");
1687 return Rf_ScalarLogical(1);
1694 return Rf_ScalarLogical(1);
1696 int n =
DIM(obj)[1];
1698 RMKMS(
_(
"%s is not representable as \"%s\""),
"Dim[2]+1",
"integer");
1701 int mr = INTEGER(minor)[0];
1702 if (mr < 0 || mr > n)
1703 RMKMS(
_(
"'%s' slot is not in {%s}"),
"minor",
"{0,...,Dim[2]}");
1706 if (
TYPEOF(is_ll) != LGLSXP)
1707 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"is_ll",
"logical");
1708 if (XLENGTH(is_ll) != 1)
1709 RMKMS(
_(
"'%s' slot does not have length %d"),
"is_ll", 1);
1710 int ll = LOGICAL(is_ll)[0];
1711 if (ll == NA_LOGICAL)
1712 RMKMS(
_(
"'%s' slot is NA"),
"is_ll");
1715 if (
TYPEOF(is_monotonic) != LGLSXP)
1716 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"is_monotonic",
"logical");
1717 if (XLENGTH(is_monotonic) != 1)
1718 RMKMS(
_(
"'%s' slot does not have length %d"),
"is_monotonic", 1);
1719 int mt = LOGICAL(is_monotonic)[0];
1720 if (mt == NA_LOGICAL)
1721 RMKMS(
_(
"'%s' slot is NA"),
"is_monotonic");
1731 if (
TYPEOF(next) != INTSXP)
1732 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"next",
"integer");
1733 if (
TYPEOF(prev) != INTSXP)
1734 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"prev",
"integer");
1735 if (XLENGTH(next) - 2 != n)
1736 RMKMS(
_(
"'%s' slot does not have length %s"),
"next",
"Dim[2]+2");
1737 if (XLENGTH(prev) - 2 != n)
1738 RMKMS(
_(
"'%s' slot does not have length %s"),
"prev",
"Dim[2]+2");
1739 int *pnext = INTEGER(next), *pprev = INTEGER(prev),
1740 j1 = pnext[n + 1], j2 = pprev[n], count = n + 1;
1742 if (j1 < 0 || j1 > n)
1743 RMKMS(
_(
"%s has elements not in {%s}"),
1744 "`next`[-(Dim[2]+1)]",
"0,...,Dim[2]");
1745 if (j2 < 0 || j2 > n + 1 || j2 == n)
1746 RMKMS(
_(
"%s has elements not in {%s}\\{%s}"),
1747 "`prev`[-(Dim[2]+2)]",
"0,...,Dim[2]+1",
"Dim[2]");
1748 if ((count > 1) && mt && (pnext[j1] != j1 + 1 || pprev[j2] != j2 - 1))
1749 RMKMS(
_(
"'%s' slot is %s but columns are not stored in increasing order"),
1750 "is_monotonic",
"TRUE");
1751 if ((count >= 1) ? j1 == n : j1 != n)
1752 RMKMS(
_(
"traversal of '%s' slot does not complete in exactly %s steps"),
1753 "next",
"length(`next`)");
1754 if ((count >= 1) ? j2 == n + 1 : j2 != n + 1)
1755 RMKMS(
_(
"traversal of '%s' slot does not complete in exactly %s steps"),
1756 "prev",
"length(`prev`)");
1761 RMKMS(
_(
"%s is not %d"),
"`next`[Dim[2]+1]", -1);
1763 RMKMS(
_(
"%s is not %d"),
"`prev`[Dim[2]+2]", -1);
1765 if (
TYPEOF(nz) != INTSXP)
1766 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"nz",
"integer");
1767 if (XLENGTH(nz) != n)
1768 RMKMS(
_(
"'%s' slot does not have length %s"),
"nz",
"Dim[2]");
1769 int j, *pnz = INTEGER(nz);
1770 for (j = 0; j < n; ++j) {
1771 if (pnz[j] == NA_INTEGER)
1772 RMKMS(
_(
"'%s' slot contains NA"),
"nz");
1773 if (pnz[j] < 1 || pnz[j] > n - j)
1774 RMKMS(
_(
"%s is not in {%s}"),
"nz[j]",
"1,...,Dim[1]-j+1");
1778 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"p",
"integer");
1779 if (XLENGTH(p) - 1 != n)
1780 RMKMS(
_(
"'%s' slot does not have length %s"),
"p",
"Dim[2]+1");
1782 int *pp = INTEGER(p);
1784 RMKMS(
_(
"column '%s' is stored first but %s is not 0"),
"j",
"p[j]");
1785 for (j = 0; j < n; ++j) {
1787 if (pp[j2] == NA_INTEGER)
1788 RMKMS(
_(
"'%s' slot contains NA"),
"p");
1789 if (pp[j2] < pp[j1])
1790 RMKMS(
_(
"'%s' slot is not increasing when traversed in stored column order"),
"p");
1791 if (pp[j2] - pp[j1] < pnz[j1])
1792 RMKMS(
_(
"'%s' slot allocates fewer than %s elements for column '%s'"),
1794 if (pp[j2] - pp[j1] > n - j1)
1795 RMKMS(
_(
"'%s' slot allocates more than %s elements for column '%s'"),
1796 "i",
"Dim[2]-j+1",
"j");
1801 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"i",
"integer");
1802 if (XLENGTH(i) != pp[n])
1803 RMKMS(
_(
"'%s' slot does not have length %s"),
"i",
"p[length(p)]");
1804 int *pi = INTEGER(i), *pi_, k;
1806 for (j = 0; j < n; ++j) {
1809 RMKMS(
_(
"first entry in column '%s' does not have row index '%s'"),
1811 for (k = 1; k < pnz[j1]; ++k) {
1812 if (pi_[k] == NA_INTEGER)
1813 RMKMS(
_(
"'%s' slot contains NA"),
"i");
1814 if (pi_[k] < 0 || pi_[k] >= n)
1815 RMKMS(
_(
"'%s' slot has elements not in {%s}"),
1816 "i",
"0,...,Dim[1]-1");
1817 if (pi_[k] <= pi_[k - 1])
1818 RMKMS(
_(
"'%s' slot is not increasing within columns"),
"i");
1824 RMKMS(
_(
"'%s' slot is not of type \"%s\" or \"%s\""),
1825 "x",
"double",
"complex");
1826 if (XLENGTH(x) != pp[n])
1827 RMKMS(
_(
"'%s' slot does not have length %s"),
"x",
"p[length(p)]");
1831 if (
TYPEOF(x) == REALSXP) {
1832 double *px = REAL(x);
1833 for (j = 0; j < n; ++j)
1834 if (!ISNAN(px[pp[j]]) && px[pp[j]] < 0.0)
1835 RMK(
_(
"Cholesky factor has negative diagonal elements"));
1837 Rcomplex *px = COMPLEX(x);
1838 for (j = 0; j < n; ++j)
1839 if (!ISNAN(px[pp[j]].r) && px[pp[j]].r < 0.0)
1840 RMK(
_(
"Cholesky factor has diagonal elements with negative real part"));
1844 return Rf_ScalarLogical(1);
1853 int mr = INTEGER(minor)[0];
1854 if (mr < 0 || mr > n)
1855 RMKMS(
_(
"'%s' slot is not in {%s}"),
"minor",
"{0,...,Dim[2]}");
1867 if (
TYPEOF(super) != INTSXP)
1868 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"super",
"integer");
1869 R_xlen_t nsuper1a = XLENGTH(super);
1870 if (nsuper1a - 1 < ((n > 0) ? 1 : 0))
1871 RMKMS(
_(
"'%s' slot has length less than %d"),
"super", 2);
1872 if (nsuper1a - 1 > n)
1873 RMKMS(
_(
"'%s' slot has length greater than %s"),
"super",
"Dim[2]+1");
1874 int k, nsuper = (int) (nsuper1a - 1), *psuper = INTEGER(super);
1876 RMKMS(
_(
"first element of '%s' slot is not 0"),
"super");
1877 if (psuper[nsuper] != n)
1878 RMKMS(
_(
"last element of '%s' slot is not %s"),
"super",
"Dim[2]");
1879 for (k = 1; k <= nsuper; ++k) {
1880 if (psuper[k] == NA_INTEGER)
1881 RMKMS(
_(
"'%s' slot contains NA"),
"super");
1882 if (psuper[k] <= psuper[k - 1])
1883 RMKMS(
_(
"'%s' slot is not increasing"),
"super");
1886 if (
TYPEOF(pi) != INTSXP)
1887 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"pi",
"integer");
1888 if (
TYPEOF(px) != INTSXP)
1889 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"px",
"integer");
1890 if (XLENGTH(pi) != nsuper1a)
1891 RMKMS(
_(
"'%s' and '%s' slots do not have equal length"),
"pi",
"super");
1892 if (XLENGTH(px) != nsuper1a)
1893 RMKMS(
_(
"'%s' and '%s' slots do not have equal length"),
"px",
"super");
1894 int *ppi = INTEGER(pi), *ppx = INTEGER(px), nr, nc, l, ml = 0;
1896 RMKMS(
_(
"first element of '%s' slot is not 0"),
"pi");
1898 RMKMS(
_(
"first element of '%s' slot is not 0"),
"px");
1899 for (k = 1; k <= nsuper; ++k) {
1900 if (ppi[k] == NA_INTEGER)
1901 RMKMS(
_(
"'%s' slot contains NA"),
"pi");
1902 if (ppx[k] == NA_INTEGER)
1903 RMKMS(
_(
"'%s' slot contains NA"),
"px");
1904 if (ppi[k] <= ppi[k - 1])
1905 RMKMS(
_(
"'%s' slot is not increasing"),
"pi");
1906 if (ppx[k] <= ppx[k - 1])
1907 RMKMS(
_(
"'%s' slot is not increasing"),
"px");
1908 nr = ppi[k] - ppi[k - 1];
1909 nc = psuper[k] - psuper[k - 1];
1911 RMKMS(
_(
"first differences of '%s' slot are less than those of '%s' slot"),
1913 if ((int_fast64_t) nr * nc > INT_MAX)
1914 RMKMS(
_(
"supernode lengths exceed %s"),
"2^31-1");
1916 if (ppx[k] - ppx[k - 1] != l)
1917 RMKMS(
_(
"first differences of '%s' slot are not equal to supernode lengths"),
1924 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"s",
"integer");
1925 if (XLENGTH(s) != ppi[nsuper])
1926 RMKMS(
_(
"'%s' slot does not have length %s"),
"s",
"pi[length(pi)]");
1927 int i, j, *ps = INTEGER(s);
1928 for (k = 1; k <= nsuper; ++k) {
1929 nr = ppi[k] - ppi[k-1];
1930 nc = psuper[k] - (j = psuper[k-1]);
1931 for (i = 0; i < nr; ++i) {
1932 if (ps[i] == NA_INTEGER)
1933 RMKMS(
_(
"'%s' slot contains NA"),
"s");
1934 if (ps[i] < 0 || ps[i] >= n)
1935 RMKMS(
_(
"'%s' slot has elements not in {%s}"),
1936 "s",
"0,...,Dim[1]-1");
1939 RMKMS(
_(
"'%s' slot is wrong within diagonal blocks (row and column indices do not coincide)"),
"s");
1941 if (ps[i] <= ps[i-1])
1942 RMKMS(
_(
"'%s' slot is not increasing within supernodes"),
"s");
1948 if (
TYPEOF(maxcsize) != INTSXP)
1949 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"maxesize",
"integer");
1950 if (XLENGTH(maxcsize) != 1)
1951 RMKMS(
_(
"'%s' slot does not have length %d"),
"maxcsize", 1);
1952 int mc = INTEGER(maxcsize)[0];
1953 if (mc < 0 || mc > ml)
1954 RMKMS(
_(
"'%s' slot is negative or exceeds maximum supernode length"),
"maxcsize");
1956 if (
TYPEOF(maxesize) != INTSXP)
1957 RMKMS(
_(
"'%s' slot is not of type \"%s\""),
"maxesize",
"integer");
1958 if (XLENGTH(maxesize) != 1)
1959 RMKMS(
_(
"'%s' slot does not have length %d"),
"maxesize", 1);
1960 int me = INTEGER(maxesize)[0];
1961 if (me < 0 || me > n)
1962 RMKMS(
_(
"'%s' slot is negative or exceeds %s"),
"maxesize",
"Dim[1]");
1965 return Rf_ScalarLogical(1);
1968 RMKMS(
_(
"'%s' slot is not of type \"%s\" or \"%s\""),
1969 "x",
"double",
"complex");
1970 if (XLENGTH(x) != ppx[nsuper])
1971 RMKMS(
_(
"'%s' slot does not have length %s"),
"x",
"px[length(px)]");
1974 if (
TYPEOF(x) == REALSXP) {
1975 double *pu = REAL(x), *pv;
1976 for (k = 0; k < nsuper; ++k) {
1977 nr = ppi[k+1] - ppi[k];
1978 nc = psuper[k+1] - psuper[k];
1980 for (j = 0; j < nc; ++j) {
1981 if (!ISNAN(*pv) && *pv < 0.0)
1982 RMK(
_(
"Cholesky factor has diagonal elements with negative real part"));
1983 pv += (R_xlen_t) nr + 1;
1987 Rcomplex *pu = COMPLEX(x), *pv;
1988 for (k = 0; k < nsuper; ++k) {
1989 nr = ppi[k+1] - ppi[k];
1990 nc = psuper[k+1] - psuper[k];
1992 for (j = 0; j < nc; ++j) {
1993 if (!ISNAN((*pv).r) && (*pv).r < 0.0)
1994 RMK(
_(
"Cholesky factor has diagonal elements with negative real part"));
1995 pv += (R_xlen_t) nr + 1;
2000 return Rf_ScalarLogical(1);
2006#ifndef MATRIX_DISABLE_VALIDITY
2010# define IS_VALID(_CLASS_) \
2012 status = _CLASS_ ## _validate(obj); \
2013 if (TYPEOF(status) == STRSXP) \
2014 Rf_error(_("invalid class \"%s\" object: %s"), \
2015 cl, CHAR(STRING_ELT(status, 0))); \
2018#define IS_VALID_SPARSE(_C_) \
2020 IS_VALID(_C_ ## sparseMatrix); \
2021 if (cl[0] == 'n') { \
2023 IS_VALID(s ## _C_ ## Matrix); \
2024 else if (cl[1] == 't') \
2025 IS_VALID(t ## _C_ ## Matrix); \
2028 IS_VALID(xg ## _C_ ## Matrix); \
2029 else if (cl[1] == 's' || cl[1] == 'p') \
2030 IS_VALID(xs ## _C_ ## Matrix); \
2031 else if (cl[1] == 't') \
2032 IS_VALID(xt ## _C_ ## Matrix); \
2034 IS_VALID(xp ## _C_ ## Matrix); \
2040 const char *cl_ =
cl;
2042 cl = (
cl[2] !=
'p') ?
"dpoMatrix" :
"dppMatrix";
2043 else if (
cl[0] ==
'p')
2046 if (
cl[0] ==
'i' &&
cl[1] ==
'n' &&
cl[2] ==
'd') {
2053 if (
cl[0] ==
'n' &&
cl[2] !=
'C' &&
cl[2] !=
'R' &&
cl[2] !=
'T')
2055 else if (
cl[0] ==
'l')
2057 else if (
cl[0] ==
'i')
2059 else if (
cl[0] ==
'd')
2061 else if (
cl[0] ==
'z')
2066 else if (
cl[1] ==
's' ||
cl[1] ==
'p')
2068 else if (
cl[1] ==
't')
2070 else if (
cl[1] ==
'd') {
2077 else if (
cl[2] ==
'R')
2079 else if (
cl[2] ==
'T')
2081 else if (
cl[2] !=
'p') {
2097# undef IS_VALID_SPARSE
#define Matrix_Calloc(p, n, t)
#define Matrix_Free(p, n)
#define GET_SLOT(x, name)
int equalString(SEXP, SEXP, R_xlen_t)
#define HAS_SLOT(x, name)
SEXP triangularMatrix_validate(SEXP obj)
SEXP MatrixFactorization_validate(SEXP obj)
SEXP symmetricMatrix_validate(SEXP obj)
static char * Dim_validate(SEXP dim)
static char * DimNames_validate(SEXP dimnames, int *pdim)
SEXP tCMatrix_validate(SEXP obj)
SEXP denseBunchKaufman_validate(SEXP obj)
SEXP TsparseMatrix_validate(SEXP obj)
SEXP corMatrix_validate(SEXP obj)
SEXP pMatrix_validate(SEXP obj)
SEXP RsparseMatrix_validate(SEXP obj)
SEXP xgCMatrix_validate(SEXP obj)
SEXP diagonalMatrix_validate(SEXP obj)
SEXP copMatrix_validate(SEXP obj)
#define FRMKMS(_FORMAT_,...)
SEXP tRMatrix_validate(SEXP obj)
void validObject(SEXP obj, const char *cl)
SEXP sCMatrix_validate(SEXP obj)
SEXP simplicialCholesky_validate(SEXP obj)
#define RMS(_FORMAT_,...)
SEXP xpCMatrix_validate(SEXP obj)
SEXP sparseQR_validate(SEXP obj)
SEXP xppMatrix_validate(SEXP obj)
SEXP sparseVector_validate(SEXP obj)
SEXP packedMatrix_validate(SEXP obj)
SEXP sRMatrix_validate(SEXP obj)
SEXP xgRMatrix_validate(SEXP obj)
SEXP sparseCholesky_validate(SEXP obj)
SEXP R_Dim_validate(SEXP dim)
SEXP sparseLU_validate(SEXP obj)
SEXP xsTMatrix_validate(SEXP obj)
SEXP Matrix_validate(SEXP obj)
SEXP R_DimNames_fixup(SEXP dimnames)
SEXP xtCMatrix_validate(SEXP obj)
SEXP supernodalCholesky_validate(SEXP obj)
#define RMKMS(_FORMAT_,...)
#define KINDMATRIX_VALIDATE(_PREFIX_, _SEXPTYPE_)
SEXP R_DimNames_validate(SEXP dimnames, SEXP dim)
#define KINDVECTOR_VALIDATE(_PREFIX_, _SEXPTYPE_)
#define IS_VALID_SPARSE(_C_)
SEXP xpoMatrix_validate(SEXP obj)
SEXP xgTMatrix_validate(SEXP obj)
SEXP denseQR_validate(SEXP obj)
SEXP unpackedMatrix_validate(SEXP obj)
SEXP generalMatrix_validate(SEXP obj)
SEXP denseSchur_validate(SEXP obj)
SEXP xtTMatrix_validate(SEXP obj)
SEXP xsRMatrix_validate(SEXP obj)
SEXP CsparseMatrix_validate(SEXP obj)
SEXP xpRMatrix_validate(SEXP obj)
SEXP tTMatrix_validate(SEXP obj)
SEXP xtRMatrix_validate(SEXP obj)
SEXP indMatrix_validate(SEXP obj)
SEXP denseLU_validate(SEXP obj)
SEXP xpTMatrix_validate(SEXP obj)
SEXP denseCholesky_validate(SEXP obj)
SEXP sTMatrix_validate(SEXP obj)
#define IS_VALID(_CLASS_)
SEXP xsCMatrix_validate(SEXP obj)