7 int i, j, n = Rf_asInteger(s_n), packed = Rf_asLogical(s_packed),
8 upper = Rf_asLogical(s_upper), diag = Rf_asLogical(s_diag);
10 nn = (int_fast64_t) n * n,
11 nx = (packed) ? n + (nn - n) / 2 : nn,
12 nr = (diag) ? n + (nn - n) / 2 : (nn - n) / 2;
14 Rf_error(
_(
"maximum index would exceed %s"),
"2^53");
15 if (nr > R_XLEN_T_MAX)
16 Rf_error(
_(
"attempt to allocate vector of length exceeding %s"),
26 for (j = 0; j < n; ++j) { \
27 for (i = 0; i < j; ++i) \
32 for (j = 0; j < n; ++j) { \
34 for (i = j+1; i < n; ++i) \
40 for (j = 0; j < n; ++j) { \
41 for (i = 0; i <= j; ++i) \
46 for (j = 0; j < n; ++j) { \
48 for (i = j; i < n; ++i) \
54 for (j = 0; j < n; ++j) { \
55 for (i = 0; i < j; ++i) \
60 for (j = 0; j < n; ++j) { \
62 for (i = j+1; i < n; ++i) \
71 PROTECT(r = Rf_allocVector(REALSXP, (R_xlen_t) nr));
72 double k = 1.0, nr_ = (double) nr, *pr = REAL(r);
78 PROTECT(r = Rf_allocVector(INTSXP, (R_xlen_t) nr));
79 int k = 1, nr_ = (int) nr, *pr = INTEGER(r);
94 int j, n = Rf_asInteger(s_n), packed = Rf_asLogical(s_packed),
95 upper = Rf_asLogical(s_upper);
97 nn = (int_fast64_t) n * n,
98 nx = (packed) ? n + (nn - n) / 2 : nn;
100 Rf_error(
_(
"maximum index would exceed %s"),
"2^53");
105 for (j = 0; j < n; ++j) { \
109 } else if (upper) { \
110 for (j = 0; j < n; ++j) { \
115 for (j = 0; j < n; ++j) { \
124 PROTECT(r = Rf_allocVector(REALSXP, n));
125 double k = 1.0, *pr = REAL(r);
131 PROTECT(r = Rf_allocVector(INTSXP, n));
132 int k = 1, *pr = INTEGER(r);
143SEXP
R_nnz(SEXP s_x, SEXP s_countNA, SEXP s_nnzmax)
145 int countNA = Rf_asLogical(s_countNA);
146 R_xlen_t n = XLENGTH(s_x), nnz = 0;
147 double nnzmax = Rf_asReal(s_nnzmax);
148 if (!ISNAN(nnzmax) && nnzmax >= 0.0 && nnzmax < (
double) n)
149 n = (R_xlen_t) nnzmax;
153 c##TYPE *px = c##PTR(s_x); \
154 if (countNA == NA_LOGICAL) { \
156 if (!c##NOT_NA(*px)) \
157 return Rf_ScalarInteger(NA_INTEGER); \
158 if (c##NOT_ZERO(*px)) \
162 } else if (countNA != 0) { \
164 if (c##NOT_ZERO(*px)) \
170 if (c##NOT_NA(*px) && c##NOT_ZERO(*px)) \
181 return (nnz <= INT_MAX)
182 ? Rf_ScalarInteger((
int) nnz) : Rf_ScalarReal((
double) nnz);
197 if (!Rf_isVectorAtomic(x)) {
198 if (Rf_length(x) == 0)
return TRUE_;
200 Rf_error(
_(
"Argument must be numeric-like atomic vector"));
202 R_xlen_t i, n = XLENGTH(x);
203 if (n == 0)
return TRUE_;
208 int *xx = LOGICAL(x);
209 for (i = 0; i < n; i++)
210 if (xx[i] == NA_LOGICAL || xx[i] != 0)
return FALSE_;
215 int *xx = INTEGER(x);
216 for (i = 0; i < n; i++)
217 if (xx[i] == NA_INTEGER || xx[i] != 0)
return FALSE_;
222 double *xx = REAL(x);
223 for (i = 0; i < n; i++)
224 if (ISNAN(xx[i]) || xx[i] != 0.)
return FALSE_;
229 unsigned char *xx = RAW(x);
230 for (i = 0; i < n; i++)
231 if (xx[i] != 0)
return FALSE_;
235 Rf_error(
_(
"Argument must be numeric-like atomic vector"));
292 SEXP vals, ans, snr, snc, dimnames;
293 int nr = 1, nc = 1, byrow, miss_nr, miss_nc;
297 vals = CAR(args); args = CDR(args);
310 Rf_error(
_(
"'data' must be of a vector type"));
312 lendat = XLENGTH(vals);
313 snr = CAR(args); args = CDR(args);
314 snc = CAR(args); args = CDR(args);
315 byrow = Rf_asLogical(CAR(args)); args = CDR(args);
316 if (byrow == NA_INTEGER)
317 Rf_error(
_(
"invalid '%s' argument"),
"byrow");
318 dimnames = CAR(args);
320 miss_nr = Rf_asLogical(CAR(args)); args = CDR(args);
321 miss_nc = Rf_asLogical(CAR(args));
324 if (!Rf_isNumeric(snr)) Rf_error(
_(
"non-numeric matrix extent"));
325 nr = Rf_asInteger(snr);
326 if (nr == NA_INTEGER)
327 Rf_error(
_(
"invalid 'nrow' value (too large or NA)"));
329 Rf_error(
_(
"invalid 'nrow' value (< 0)"));
332 if (!Rf_isNumeric(snc)) Rf_error(
_(
"non-numeric matrix extent"));
333 nc = Rf_asInteger(snc);
334 if (nc == NA_INTEGER)
335 Rf_error(
_(
"invalid 'ncol' value (too large or NA)"));
337 Rf_error(
_(
"invalid 'ncol' value (< 0)"));
339 if (miss_nr && miss_nc) {
340 if (lendat > INT_MAX) Rf_error(
"data is too long");
342 }
else if (miss_nr) {
343 if (lendat > (
double) nc * INT_MAX) Rf_error(
"data is too long");
344 nr = (int) ceil((
double) lendat / (
double) nc);
345 }
else if (miss_nc) {
346 if (lendat > (
double) nr * INT_MAX) Rf_error(
"data is too long");
347 nc = (int) ceil((
double) lendat / (
double) nr);
351 R_xlen_t nrc = (R_xlen_t) nr * nc;
352 if (lendat > 1 && nrc % lendat != 0) {
353 if ((lendat > nr && (lendat / nr) * nr != lendat) ||
354 (lendat < nr && (nr / lendat) * lendat != nr))
355 Rf_warning(
_(
"data length [%lld] is not a sub-multiple "
356 "or multiple of the number of rows [%d]"),
357 (
long long) lendat, nr);
358 else if ((lendat > nc && (lendat / nc) * nc != lendat) ||
359 (lendat < nc && (nc / lendat) * lendat != nc))
360 Rf_warning(
_(
"data length [%lld] is not a sub-multiple "
361 "or multiple of the number of columns [%d]"),
362 (
long long) lendat, nc);
363 }
else if (lendat > 1 && nrc == 0)
364 Rf_warning(
_(
"data length exceeds size of matrix"));
367#ifndef LONG_VECTOR_SUPPORT
368 if ((
double) nr * (
double) nc > INT_MAX)
369 Rf_error(
_(
"too many elements specified"));
372 PROTECT(ans = Rf_allocMatrix(
TYPEOF(vals), nr, nc));
373 if (Rf_isVector(vals)) {
375 Rf_copyMatrix(ans, vals, (Rboolean) byrow);
377 R_xlen_t N = (R_xlen_t) nr * nc, i;
380 for (i = 0; i < N; i++)
381 SET_STRING_ELT(ans, i, NA_STRING);
384 for (i = 0; i < N; i++)
385 LOGICAL(ans)[i] = NA_LOGICAL;
388 for (i = 0; i < N; i++)
389 INTEGER(ans)[i] = NA_INTEGER;
392 for (i = 0; i < N; i++)
393 REAL(ans)[i] = NA_REAL;
400 Rcomplex zna = { .r = NA_REAL, .i = 0.0 };
401 for (i = 0; i < N; i++)
402 COMPLEX(ans)[i] = zna;
407 memset(RAW(ans), 0, N);
415 if (dimnames != R_NilValue && Rf_length(dimnames) > 0)
416 ans = Rf_dimnamesgets(ans, dimnames);
449 int col = Rf_asLogical(colP);
451 SEXP indP = PROTECT(
GET_SLOT(x, indSym)),
455 n_el = INTEGER(pP)[nouter];
458 ij = INTEGER(ans = PROTECT(Rf_allocMatrix(INTSXP, n_el, 2)));
463 for(i = 0; i < n_el; i++)
464 ij[i] = INTEGER(indP)[i];
466 for(i = 0; i < n_el; i++)
467 ij[i + n_el] = INTEGER(indP)[i];
496 int *ij_di = NULL, n, nprot=1;
497 int check_bounds = Rf_asLogical(chk_bnds), one_ind = Rf_asLogical(orig_1);
499 if (
TYPEOF(di) != INTSXP) {
500 di = PROTECT(Rf_coerceVector(di, INTSXP));
503 if (
TYPEOF(ij) != INTSXP) {
504 ij = PROTECT(Rf_coerceVector(ij, INTSXP));
507 if (!Rf_isMatrix(ij) ||
508 (ij_di = INTEGER(Rf_getAttrib(ij, R_DimSymbol)))[1] != 2)
509 Rf_error(
_(
"Argument ij must be 2-column integer matrix"));
511 int *Di = INTEGER(di), *IJ = INTEGER(ij),
514 if ((Di[0] * (
double) Di[1]) >= 1 + (
double)INT_MAX) {
515 ans = PROTECT(Rf_allocVector(REALSXP, n));
516 double *ii = REAL(ans), nr = (double) Di[0];
518#define do_ii_FILL(_i_, _j_) \
520 if (check_bounds) { \
521 for (i = 0; i < n; i++) { \
522 if (_i_[i] == NA_INTEGER || _j_[i] == NA_INTEGER) \
523 ii[i] = NA_INTEGER; \
525 register int i_i, j_i; \
533 if (i_i < 0 || i_i >= Di[0]) \
534 Rf_error(_("subscript 'i' out of bounds in M[ij]")); \
535 if (j_i < 0 || j_i >= Di[1]) \
536 Rf_error(_("subscript 'j' out of bounds in M[ij]")); \
537 ii[i] = i_i + j_i * nr; \
541 for (i = 0; i < n; i++) \
542 ii[i] = (_i_[i] == NA_INTEGER || _j_[i] == NA_INTEGER) \
545 ? ((_i_[i]-1) + (_j_[i]-1) * nr) \
546 : _i_[i] + _j_[i] * nr); \
551 ans = PROTECT(Rf_allocVector(INTSXP, n));
552 int *ii = INTEGER(ans), nr = Di[0];
574 int n = LENGTH(i), nprot = 1;
575 int check_bounds = Rf_asLogical(chk_bnds), one_ind = Rf_asLogical(orig_1);
577 if (
TYPEOF(di)!= INTSXP) {
578 di = PROTECT(Rf_coerceVector(di,INTSXP));
581 if (
TYPEOF(i) != INTSXP) {
582 i = PROTECT(Rf_coerceVector(i, INTSXP));
585 if (
TYPEOF(j) != INTSXP) {
586 j = PROTECT(Rf_coerceVector(j, INTSXP));
590 Rf_error(
_(
"i and j must be integer vectors of the same length"));
592 int *Di = INTEGER(di), *i_ = INTEGER(i), *j_ = INTEGER(j);
594 if ((Di[0] * (
double) Di[1]) >= 1 + (
double) INT_MAX) {
595 ans = PROTECT(Rf_allocVector(REALSXP, n));
596 double *ii = REAL(ans), nr = (double) Di[0];
600 ans = PROTECT(Rf_allocVector(INTSXP, n));
601 int *ii = INTEGER(ans), nr = Di[0];
SEXP m_encodeInd2(SEXP i, SEXP j, SEXP di, SEXP orig_1, SEXP chk_bnds)
Encode Matrix index (i,j) |--> i + j * nrow {i,j : 0-origin}.
SEXP compressed_non_0_ij(SEXP x, SEXP colP)
Return a 2 column matrix '' cbind(i, j) '' of 0-origin index vectors (i,j) which entirely correspond ...
static int * expand_cmprPt(int ncol, const int mp[], int mj[])
Expand compressed pointers in the array mp into a full set of indices in the array mj.
SEXP m_encodeInd(SEXP ij, SEXP di, SEXP orig_1, SEXP chk_bnds)
Encode Matrix index (i,j) |--> i + j * nrow {i,j : 0-origin}.