7 PROTECT(ans = allocVector(INTSXP, 3));
11 PROTECT(nms = allocVector(STRSXP, 3));
12 SET_STRING_ELT(nms, 0, mkChar(
"package"));
13 SET_STRING_ELT(nms, 1, mkChar(
"abi"));
14 SET_STRING_ELT(nms, 2, mkChar(
"suitesparse"));
15 setAttrib(ans, R_NamesSymbol, nms);
23 int i, j, n_ = asInteger(n), packed_ = asLogical(packed),
24 upper_ = asLogical(upper), diag_ = asLogical(diag);
27 nx = (packed_) ? n_ + (nn - n_) / 2 : nn,
28 nr = (diag_) ? n_ + (nn - n_) / 2 : (nn - n_) / 2;
30 error(
_(
"indices would exceed %s"),
"2^53");
31 if (nr > R_XLEN_T_MAX)
32 error(
_(
"attempt to allocate vector of length exceeding %s"),
41 } else if (upper_) { \
42 for (j = 0; j < n_; ++j) { \
43 for (i = 0; i < j; ++i) \
48 for (j = 0; j < n_; ++j) { \
50 for (i = j+1; i < n_; ++i) \
56 for (j = 0; j < n_; ++j) { \
57 for (i = 0; i <= j; ++i) \
62 for (j = 0; j < n_; ++j) { \
64 for (i = j; i < n_; ++i) \
70 for (j = 0; j < n_; ++j) { \
71 for (i = 0; i < j; ++i) \
76 for (j = 0; j < n_; ++j) { \
78 for (i = j+1; i < n_; ++i) \
87 PROTECT(r = allocVector(REALSXP, (R_xlen_t) nr));
88 double k = 1.0, nr_ = (double) nr, *pr = REAL(r);
94 PROTECT(r = allocVector(INTSXP, (R_xlen_t) nr));
95 int k = 1, nr_ = (int) nr, *pr = INTEGER(r);
110 int j, n_ = asInteger(n), packed_ = asLogical(packed),
111 upper_ = asLogical(upper);
114 nx = (packed_) ? n_ + (nn - n_) / 2 : nn;
116 error(
_(
"indices would exceed %s"),
"2^53");
121 for (j = 0; j < n_; ++j) { \
125 } else if (upper_) { \
126 for (j = 0; j < n_; ++j) { \
131 for (j = 0; j < n_; ++j) { \
140 PROTECT(r = allocVector(REALSXP, n_));
141 double k = 1.0, *pr = REAL(r);
147 PROTECT(r = allocVector(INTSXP, n_));
148 int k = 1, *pr = INTEGER(r);
159SEXP
R_nnz(SEXP x, SEXP countNA, SEXP nnzmax)
161 int do_countNA = asLogical(countNA);
162 R_xlen_t n = XLENGTH(x), nnz = 0;
163 double n_ = asReal(nnzmax);
164 if (!ISNAN(n_) && n_ >= 0.0 && n_ < (
double) n)
167#define DO_NNZ(_CTYPE_, _PTR_, _ISNA_, _ISNZ_, _STRICTLY_ISNZ_) \
169 _CTYPE_ *px = _PTR_(x); \
170 if (do_countNA == NA_LOGICAL) { \
173 return ScalarInteger(NA_INTEGER); \
178 } else if (do_countNA != 0) { \
186 if (_STRICTLY_ISNZ_(*px)) \
216 return (nnz <= INT_MAX)
217 ? ScalarInteger((
int) nnz) : ScalarReal((
double) nnz);
225#define TRUE_ ScalarLogical(1)
226#define FALSE_ ScalarLogical(0)
232 if (!isVectorAtomic(x)) {
233 if (length(x) == 0)
return TRUE_;
235 error(
_(
"Argument must be numeric-like atomic vector"));
237 R_xlen_t i, n = XLENGTH(x);
238 if (n == 0)
return TRUE_;
243 int *xx = LOGICAL(x);
244 for (i = 0; i < n; i++)
245 if (xx[i] == NA_LOGICAL || xx[i] != 0)
return FALSE_;
250 int *xx = INTEGER(x);
251 for (i = 0; i < n; i++)
252 if (xx[i] == NA_INTEGER || xx[i] != 0)
return FALSE_;
257 double *xx = REAL(x);
258 for (i = 0; i < n; i++)
259 if (ISNAN(xx[i]) || xx[i] != 0.)
return FALSE_;
264 unsigned char *xx = RAW(x);
265 for (i = 0; i < n; i++)
266 if (xx[i] != 0)
return FALSE_;
270 error(
_(
"Argument must be numeric-like atomic vector"));
278 if (!isVectorAtomic(x)) {
279 if (length(x) == 0)
return FALSE_;
281 error(
_(
"Argument must be numeric-like atomic vector"));
283 R_xlen_t i, n = XLENGTH(x);
284 if (n == 0)
return FALSE_;
289 int *xx = LOGICAL(x);
290 for (i = 0; i < n; i++)
if (xx[i] == 0)
return TRUE_;
295 int *xx = INTEGER(x);
296 for (i = 0; i < n; i++)
if (xx[i] == 0)
return TRUE_;
301 double *xx = REAL(x);
302 for (i = 0; i < n; i++)
if (xx[i] == 0.)
return TRUE_;
307 unsigned char *xx = RAW(x);
308 for (i = 0; i < n; i++)
if (xx[i] == 0)
return TRUE_;
312 error(
_(
"Argument must be numeric-like atomic vector"));
327 SEXP vals, ans, snr, snc, dimnames;
328 int nr = 1, nc = 1, byrow, miss_nr, miss_nc;
332 vals = CAR(args); args = CDR(args);
334 switch (TYPEOF(vals)) {
345 error(
_(
"'data' must be of a vector type"));
347 lendat = XLENGTH(vals);
348 snr = CAR(args); args = CDR(args);
349 snc = CAR(args); args = CDR(args);
350 byrow = asLogical(CAR(args)); args = CDR(args);
351 if (byrow == NA_INTEGER)
352 error(
_(
"invalid '%s' argument"),
"byrow");
353 dimnames = CAR(args);
355 miss_nr = asLogical(CAR(args)); args = CDR(args);
356 miss_nc = asLogical(CAR(args));
359 if (!isNumeric(snr)) error(
_(
"non-numeric matrix extent"));
361 if (nr == NA_INTEGER)
362 error(
_(
"invalid 'nrow' value (too large or NA)"));
364 error(
_(
"invalid 'nrow' value (< 0)"));
367 if (!isNumeric(snc)) error(
_(
"non-numeric matrix extent"));
369 if (nc == NA_INTEGER)
370 error(
_(
"invalid 'ncol' value (too large or NA)"));
372 error(
_(
"invalid 'ncol' value (< 0)"));
374 if (miss_nr && miss_nc) {
375 if (lendat > INT_MAX) error(
"data is too long");
377 }
else if (miss_nr) {
378 if (lendat > (
double) nc * INT_MAX) error(
"data is too long");
379 nr = (int) ceil((
double) lendat / (double) nc);
380 }
else if (miss_nc) {
381 if (lendat > (
double) nr * INT_MAX) error(
"data is too long");
382 nc = (int) ceil((
double) lendat / (double) nr);
386 R_xlen_t nrc = (R_xlen_t) nr * nc;
387 if (lendat > 1 && nrc % lendat != 0) {
388 if ((lendat > nr && (lendat / nr) * nr != lendat) ||
389 (lendat < nr && (nr / lendat) * lendat != nr))
390 warning(
_(
"data length [%lld] is not a sub-multiple "
391 "or multiple of the number of rows [%d]"),
392 (
long long)lendat, nr);
393 else if ((lendat > nc && (lendat / nc) * nc != lendat) ||
394 (lendat < nc && (nc / lendat) * lendat != nc))
395 warning(
_(
"data length [%lld] is not a sub-multiple "
396 "or multiple of the number of columns [%d]"),
397 (
long long)lendat, nc);
398 }
else if (lendat > 1 && nrc == 0)
399 warning(
_(
"data length exceeds size of matrix"));
402#ifndef LONG_VECTOR_SUPPORT
403 if ((
double) nr * (
double) nc > INT_MAX)
404 error(
_(
"too many elements specified"));
407 PROTECT(ans = allocMatrix(TYPEOF(vals), nr, nc));
410 copyMatrix(ans, vals, byrow);
412 copyListMatrix(ans, vals, byrow);
413 }
else if (isVector(vals)) {
414 R_xlen_t N = (R_xlen_t) nr * nc, i;
415 switch (TYPEOF(vals)) {
417 for (i = 0; i < N; i++)
418 SET_STRING_ELT(ans, i, NA_STRING);
421 for (i = 0; i < N; i++)
422 LOGICAL(ans)[i] = NA_LOGICAL;
425 for (i = 0; i < N; i++)
426 INTEGER(ans)[i] = NA_INTEGER;
429 for (i = 0; i < N; i++)
430 REAL(ans)[i] = NA_REAL;
437 Rcomplex zna = { .r = NA_REAL, .i = 0.0 };
438 for (i = 0; i < N; i++)
439 COMPLEX(ans)[i] = zna;
444 memset(RAW(ans), 0, N);
451 if (!isNull(dimnames)&& length(dimnames) > 0)
452 ans = dimnamesgets(ans, dimnames);
471 for (j = 0; j < ncol; j++) {
472 int j2 = mp[j+1], jj;
473 for (jj = mp[j]; jj < j2; jj++)
485 int col = asLogical(colP);
487 SEXP indP = PROTECT(
GET_SLOT(x, indSym)),
491 n_el = INTEGER(pP)[nouter];
494 ij = INTEGER(ans = PROTECT(allocMatrix(INTSXP, n_el, 2)));
499 for(i = 0; i < n_el; i++)
500 ij[i] = INTEGER(indP)[i];
502 for(i = 0; i < n_el; i++)
503 ij[i + n_el] = INTEGER(indP)[i];
511 int n = length(pP) - 1;
512 int *p = INTEGER(pP);
513 SEXP ans = PROTECT(allocVector(INTSXP, p[n]));
532 int *ij_di = NULL, n, nprot=1;
533 Rboolean check_bounds = asLogical(chk_bnds), one_ind = asLogical(orig_1);
535 if (TYPEOF(di) != INTSXP) {
536 di = PROTECT(coerceVector(di, INTSXP));
539 if (TYPEOF(ij) != INTSXP) {
540 ij = PROTECT(coerceVector(ij, INTSXP));
544 (ij_di = INTEGER(getAttrib(ij, R_DimSymbol)))[1] != 2)
545 error(
_(
"Argument ij must be 2-column integer matrix"));
547 int *Di = INTEGER(di), *IJ = INTEGER(ij),
550 if ((Di[0] * (
double) Di[1]) >= 1 + (
double)INT_MAX) {
551 ans = PROTECT(allocVector(REALSXP, n));
552 double *ii = REAL(ans), nr = (double) Di[0];
554#define do_ii_FILL(_i_, _j_) \
556 if (check_bounds) { \
557 for (i = 0; i < n; i++) { \
558 if (_i_[i] == NA_INTEGER || _j_[i] == NA_INTEGER) \
559 ii[i] = NA_INTEGER; \
561 register int i_i, j_i; \
569 if (i_i < 0 || i_i >= Di[0]) \
570 error(_("subscript 'i' out of bounds in M[ij]")); \
571 if (j_i < 0 || j_i >= Di[1]) \
572 error(_("subscript 'j' out of bounds in M[ij]")); \
573 ii[i] = i_i + j_i * nr; \
577 for (i = 0; i < n; i++) \
578 ii[i] = (_i_[i] == NA_INTEGER || _j_[i] == NA_INTEGER) \
581 ? ((_i_[i]-1) + (_j_[i]-1) * nr) \
582 : _i_[i] + _j_[i] * nr); \
587 ans = PROTECT(allocVector(INTSXP, n));
588 int *ii = INTEGER(ans), nr = Di[0];
610 int n = LENGTH(i), nprot = 1;
611 Rboolean check_bounds = asLogical(chk_bnds), one_ind = asLogical(orig_1);
613 if (TYPEOF(di)!= INTSXP) {
614 di = PROTECT(coerceVector(di,INTSXP));
617 if (TYPEOF(i) != INTSXP) {
618 i = PROTECT(coerceVector(i, INTSXP));
621 if (TYPEOF(j) != INTSXP) {
622 j = PROTECT(coerceVector(j, INTSXP));
626 error(
_(
"i and j must be integer vectors of the same length"));
628 int *Di = INTEGER(di), *i_ = INTEGER(i), *j_ = INTEGER(j);
630 if ((Di[0] * (
double) Di[1]) >= 1 + (
double) INT_MAX) {
631 ans = PROTECT(allocVector(REALSXP, n));
632 double *ii = REAL(ans), nr = (double) Di[0];
636 ans = PROTECT(allocVector(INTSXP, n));
637 int *ii = INTEGER(ans), nr = Di[0];
#define STRICTLY_ISNZ_REAL(_X_)
long long Matrix_int_fast64_t
#define ISNA_LOGICAL(_X_)
#define STRICTLY_ISNZ_LOGICAL(_X_)
#define STRICTLY_ISNZ_COMPLEX(_X_)
#define ISNA_INTEGER(_X_)
#define STRICTLY_ISNZ_INTEGER(_X_)
#define ERROR_INVALID_TYPE(_X_, _FUNC_)
#define ISNZ_LOGICAL(_X_)
#define ISNZ_INTEGER(_X_)
#define GET_SLOT(x, what)
#define ISNZ_COMPLEX(_X_)
#define ISNA_COMPLEX(_X_)
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 R_index_diagonal(SEXP n, SEXP packed, SEXP upper)
SEXP R_nnz(SEXP x, SEXP countNA, SEXP nnzmax)
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.
#define DO_NNZ(_CTYPE_, _PTR_, _ISNA_, _ISNZ_, _STRICTLY_ISNZ_)
#define do_ii_FILL(_i_, _j_)
SEXP R_Matrix_version(void)
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}.
SEXP R_index_triangle(SEXP n, SEXP packed, SEXP upper, SEXP diag)
SEXP Matrix_expand_pointers(SEXP pP)
#define MATRIX_SUITESPARSE_VERSION
#define MATRIX_PACKAGE_VERSION
#define MATRIX_ABI_VERSION