7SEXP
dense_band(SEXP from,
const char *
class,
int a,
int b)
9 int packed =
class[2] ==
'p';
13 int *pdim =
DIM(from), m = pdim[0], n = pdim[1];
14 if ((m == 0 || n == 0 || (a <= 1 - m && b >= n - 1)) &&
15 (m != n || n > 1 ||
class[1] ==
't'))
19 tr =
class[1] ==
't' || (m == n && (a >= 0 || b <= 0));
20 sy = !tr &&
class[1] ==
's' && a == -b;
23 char ul0 =
'\0', ct0 =
'\0', nu0 =
'\0';
26 if (
class[1] ==
's' &&
class[0] ==
'z')
28 if (
class[1] ==
't') {
30 if ((ul0 ==
'U') ? (a <= 0 && b >= n - 1) : (a <= 1 - m && b >= 0))
35 char cl[] =
"...Matrix";
37 cl[1] = (ge) ?
'g' : ((sy) ?
's' :
't') ;
38 cl[2] = (ge) ?
'e' : ((packed) ?
'p' : ((sy) ?
'y' :
'r'));
44 char ul1 = (tr &&
class[1] !=
't') ? ((a >= 0) ?
'U' :
'L') : ul0;
45 if (ul1 !=
'\0' && ul1 !=
'U')
47 if (ct0 !=
'\0' && ct0 !=
'C' && sy)
49 if (nu0 !=
'\0' && nu0 !=
'N' && tr && a <= 0 && b >= 0)
53 x1 = PROTECT(Rf_allocVector(
TYPEOF(x0), (!packed || ge) ? (R_xlen_t) m * n : (R_xlen_t)
PACKED_LENGTH((
size_t) n)));
58 a_ = (size_t) ((int_fast64_t) m + a),
59 b_ = (size_t) ((int_fast64_t) m + b);
63 c##TYPE *px0 = c##PTR(x0), *px1 = c##PTR(x1); \
64 if (ge && class[1] != 'g') { \
66 c##NAME(force2)(px1, px0, n_, ul0, ct0, nu0); \
68 c##NAME( pack1)(px1, px0, n_, ul0, ct0, nu0); \
71 } else if (tr && class[1] == 's' && ul0 != ul1) { \
73 c##NAME(trans2)(px1, px0, m_, n_ , ct0); \
75 c##NAME(trans1)(px1, px0, n_, ul0, ct0); \
80 c##NAME( band2)(px1, px0, m_, n_ , a_, b_); \
82 c##NAME( band1)(px1, px0, n_, ul0, a_, b_); \
99 int *pdim =
DIM(from), m = pdim[0], n = pdim[1];
100 if ((m == 0 || n == 0 || (a <= 1 - m && b >= n - 1)) &&
101 (m != n || n > 1 ||
class[1] ==
't'))
105 tr =
class[1] ==
't' || (m == n && (a >= 0 || b <= 0));
106 sy = !tr &&
class[1] ==
's' && a == -b;
109 char ul0 =
'\0', ct0 =
'\0', nu0 =
'\0';
112 if (
class[1] ==
's' &&
class[0] ==
'z')
114 if (
class[1] ==
't') {
116 if ((ul0 ==
'U') ? (a <= 0 && b >= n - 1) : (a <= 1 - m && b >= 0))
121 char cl[] =
"...Matrix";
123 cl[1] = (ge) ?
'g' : ((sy) ?
's' :
't');
130 char ul1 = (tr &&
class[1] !=
't') ? ((a >= 0) ?
'U' :
'L') : ul0;
131 if (ul1 !=
'\0' && ul1 !=
'U')
133 if (ct0 !=
'\0' && ct0 !=
'C' && sy)
135 if (nu0 !=
'\0' && nu0 !=
'N' && tr && a <= 0 && b >= 0)
138 if (
class[2] !=
'T') {
140 if (
class[2] ==
'R') {
148 p1 = PROTECT(Rf_allocVector(INTSXP, XLENGTH(p0)));
149 int *pp0 = INTEGER(p0), *pi0 = INTEGER(i0),
150 *pp1 = INTEGER(p1), *iwork = NULL,
151 j, k, kend, nnz0 = pp0[n], nnz1 = 0, d;
155 if (
class[1] !=
's' || sy) {
156 for (j = 0, k = 0; j < n; ++j) {
159 if ((d = j - pi0[k]) >= a && d <= b)
167 if (
class[0] !=
'n') {
176 memset(pp1, 0,
sizeof(
int) * (
size_t) n);
177 for (j = 0, k = 0; j < n; ++j) {
180 if ((d = j - pi0[k]) >= a && d <= b)
182 if (d != 0 && -d >= a && -d <= b)
188 for (j = 0; j < n; ++j) {
195 SEXP i1 = PROTECT(Rf_allocVector(INTSXP, nnz1));
196 int *pi1 = INTEGER(i1);
202 SEXP x0 = PROTECT(GET_SLOT(from, Matrix_xSym)), \
203 x1 = PROTECT(Rf_allocVector(c##TYPESXP, nnz1)); \
204 c##TYPE *px0 = c##PTR(x0), *px1 = c##PTR(x1); \
205 SET_SLOT(to, Matrix_xSym, x1); \
208 if (class[1] != 's' || sy) \
209 for (j = 0, k = 0; j < n; ++j) { \
212 if ((d = j - pi0[k]) >= a && d <= b) { \
222 for (j = 0, k = 0; j < n; ++j) { \
225 if ((d = j - pi0[k]) >= a && d <= b) { \
227 if (ct0 == 'C' && d == 0) \
228 c##ASSIGN_PROJ_REAL(px1[iwork[j]], px0[k]); \
230 c##ASSIGN_IDEN (px1[iwork[j]], px0[k]); \
232 pi1[iwork[j]++] = pi0[k]; \
234 if (d != 0 && -d >= a && -d <= b) { \
237 c##ASSIGN_CONJ(px1[iwork[pi0[k]]], px0[k]); \
239 c##ASSIGN_IDEN(px1[iwork[pi0[k]]], px0[k]); \
241 pi1[iwork[pi0[k]]++] = j; \
246 Matrix_Free(iwork, n); \
260 int *pi0 = INTEGER(i0), *pj0 = INTEGER(j0), d;
261 R_xlen_t k, nnz0 = XLENGTH(i0), nnz1 = 0;
263 if (
class[1] !=
's' || sy) {
264 for (k = 0; k < nnz0; ++k)
265 if ((d = pj0[k] - pi0[k]) >= a && d <= b)
270 if (
class[0] !=
'n') {
279 for (k = 0; k < nnz0; ++k) {
280 if ((d = pj0[k] - pi0[k]) >= a && d <= b)
282 if (d != 0 && -d >= a && -d <= b)
286 SEXP i1 = PROTECT(Rf_allocVector(INTSXP, nnz1)),
287 j1 = PROTECT(Rf_allocVector(INTSXP, nnz1));
288 int *pi1 = INTEGER(i1), *pj1 = INTEGER(j1);
295 SEXP x0 = PROTECT(GET_SLOT(from, Matrix_xSym)), \
296 x1 = PROTECT(Rf_allocVector(c##TYPESXP, nnz1)); \
297 c##TYPE *px0 = c##PTR(x0), *px1 = c##PTR(x1); \
298 SET_SLOT(to, Matrix_xSym, x1); \
301 if (class[1] != 's' || sy) \
302 for (k = 0; k < nnz0; ++k) { \
303 if ((d = pj0[k] - pi0[k]) >= a && d <= b) { \
312 for (k = 0; k < nnz0; ++k) { \
313 if ((d = pj0[k] - pi0[k]) >= a && d <= b) { \
317 if (ct0 == 'C' && d == 0) \
318 c##ASSIGN_PROJ_REAL(*px1, px0[k]); \
320 c##ASSIGN_IDEN (*px1, px0[k]); \
324 if (d != 0 && -d >= a && -d <= b) { \
329 c##ASSIGN_CONJ(*px1, px0[k]); \
331 c##ASSIGN_IDEN(*px1, px0[k]); \
352 if (
TYPEOF(s_from) != OBJSXP) {
360 int *pdim =
DIM(s_from), m = pdim[0], n = pdim[1], a, b;
361 if (s_a == R_NilValue)
363 else if ((a = Rf_asInteger(s_a)) == NA_INTEGER || a < -m || a > n)
364 Rf_error(
_(
"'%s' (%d) must be an integer from %s (%d) to %s (%d)"),
365 "k1", a,
"-Dim[1]", -m,
"Dim[2]", n);
366 if (s_b == R_NilValue)
368 else if ((b = Rf_asInteger(s_b)) == NA_INTEGER || b < -m || b > n)
369 Rf_error(
_(
"'%s' (%d) must be an integer from %s (%d) to %s (%d)"),
370 "k2", b,
"-Dim[1]", -m,
"Dim[2]", n);
372 Rf_error(
_(
"'%s' (%d) must be less than or equal to '%s' (%d)"),
384 int *pdim =
DIM(s_from), m = pdim[0], n = pdim[1], a, b;
385 if (s_a == R_NilValue)
387 else if ((a = Rf_asInteger(s_a)) == NA_INTEGER || a < -m || a > n)
388 Rf_error(
_(
"'%s' (%d) must be an integer from %s (%d) to %s (%d)"),
389 "k1", a,
"-Dim[1]", -m,
"Dim[2]", n);
390 if (s_b == R_NilValue)
392 else if ((b = Rf_asInteger(s_b)) == NA_INTEGER || b < -m || b > n)
393 Rf_error(
_(
"'%s' (%d) must be an integer from %s (%d) to %s (%d)"),
394 "k2", b,
"-Dim[1]", -m,
"Dim[2]", n);
396 Rf_error(
_(
"'%s' (%d) must be less than or equal to '%s' (%d)"),
SEXP matrix_as_dense(SEXP from, const char *zzz, char ul, char ct, char nu, int mg, int new)