-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy patharray.c
2405 lines (2208 loc) · 65.2 KB
/
array.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* R : A Computer Language for Statistical Data Analysis
* Copyright (C) 1998-2023 The R Core Team
* Copyright (C) 2002-2015 The R Foundation
* Copyright (C) 1995, 1996 Robert Gentleman and Ross Ihaka
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, a copy is available at
* https://www.R-project.org/Licenses/
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <Defn.h>
#include <Internal.h>
#include <Rmath.h>
#include <R_ext/RS.h> /* for R_Calloc/R_Free, F77_CALL */
// calls BLAS routines dgemm dgemv zgemm
#ifdef USE_NEW_ACCELERATE
# define ACCELERATE_NEW_LAPACK
// avoid conflicts over COMPLEX
# define USE_NON_APPLE_STANDARD_DATATYPES 0
# include <Accelerate/Accelerate.h>
# define FCONE
# pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#else
#include <R_ext/BLAS.h>
#endif
#include <R_ext/Itermacros.h>
#ifdef Win32
#include <trioremap.h> /* for %lld */
#endif
#include "duplicate.h"
#include <complex.h>
#include "Rcomplex.h" /* toC99 */
/* "GetRowNames" and "GetColNames" are utility routines which
* locate and return the row names and column names from the
* dimnames attribute of a matrix. They are useful because
* old versions of R used pair-based lists for dimnames
* whereas recent versions use vector based lists.
* These are now very old, plus
* ``When the "dimnames" attribute is
* grabbed off an array it is always adjusted to be a vector.''
They are used in bind.c and subset.c, and advertised in Rinternals.h
*/
SEXP Rf_GetRowNames(SEXP dimnames)
{
if (TYPEOF(dimnames) == VECSXP)
return VECTOR_ELT(dimnames, 0);
else
return R_NilValue;
}
SEXP Rf_GetColNames(SEXP dimnames)
{
if (TYPEOF(dimnames) == VECSXP)
return VECTOR_ELT(dimnames, 1);
else
return R_NilValue;
}
// .Internal(matrix(data, nrow, ncol, byrow, dimnames, missing(nrow), missing(ncol)))
attribute_hidden SEXP do_matrix(SEXP call, SEXP op, SEXP args, SEXP rho)
{
SEXP vals, ans, snr, snc, dimnames;
int nr = 1, nc = 1, byrow, miss_nr, miss_nc;
R_xlen_t lendat;
checkArity(op, args);
vals = CAR(args); args = CDR(args);
switch(TYPEOF(vals)) {
case LGLSXP:
case INTSXP:
case REALSXP:
case CPLXSXP:
case STRSXP:
case RAWSXP:
case EXPRSXP:
case VECSXP:
break;
default:
error(_("'data' must be of a vector type, was '%s'"),
R_typeToChar(vals));
}
lendat = XLENGTH(vals);
snr = CAR(args); args = CDR(args);
snc = CAR(args); args = CDR(args);
byrow = asLogical(CAR(args)); args = CDR(args);
if (byrow == NA_INTEGER)
error(_("invalid '%s' argument"), "byrow");
dimnames = CAR(args);
args = CDR(args);
miss_nr = asLogical(CAR(args)); args = CDR(args);
miss_nc = asLogical(CAR(args));
static int nowarn = -1;
if (nowarn == -1) {
char *p = getenv("_R_CHECK_MATRIX_DATA_");
nowarn = (p && StringTrue(p)) ? 1 : 0; // if(nowarn) <error>
}
if (!miss_nr) {
if (!isNumeric(snr)) error(_("non-numeric matrix extent"));
nr = asInteger(snr);
if (nr == NA_INTEGER)
error(_("invalid 'nrow' value (too large or NA)"));
if (nr < 0)
error(_("invalid 'nrow' value (< 0)"));
}
if (!miss_nc) {
if (!isNumeric(snc)) error(_("non-numeric matrix extent"));
nc = asInteger(snc);
if (nc == NA_INTEGER)
error(_("invalid 'ncol' value (too large or NA)"));
if (nc < 0)
error(_("invalid 'ncol' value (< 0)"));
}
if (miss_nr && miss_nc) {
if (lendat > INT_MAX) error("data is too long");
nr = (int) lendat;
} else if (miss_nr) {
if (lendat > (double) nc * INT_MAX) error("data is too long"); // incl lendat > nc == 0
if (nc == 0) // as lendat <= nc, have lendat == 0
nr = 0;
else
nr = (int) ceil((double) lendat / (double) nc);
} else if (miss_nc) {
if (lendat > (double) nr * INT_MAX) error("data is too long"); // incl lendat > nr == 0
if (nr == 0) // then lendat == 0
nc = 0;
else
nc = (int) ceil((double) lendat / (double) nr);
}
if (lendat > 0) {
R_xlen_t nrc = (R_xlen_t) nr * nc;
if (lendat > 1 && (nrc % lendat) != 0) { // ==> nrc > 0
if (((lendat > nr) && (lendat / nr) * nr != lendat) ||
((lendat < nr) && (nr / lendat) * lendat != nr))
warning(_("data length [%lld] is not a sub-multiple or multiple of the number of rows [%d]"),
(long long)lendat, nr);
else if (((lendat > nc) && (lendat / nc) * nc != lendat) ||
((lendat < nc) && (nc / lendat) * lendat != nc))
warning(_("data length [%lld] is not a sub-multiple or multiple of the number of columns [%d]"),
(long long)lendat, nc);
else if (nrc != lendat) {
if(nowarn)
error(_("data length differs from size of matrix: [%lld != %d x %d]"),
(long long)lendat, nr, nc);
else
warning(_("data length differs from size of matrix: [%lld != %d x %d]"),
(long long)lendat, nr, nc);
}
}
else if (lendat > 1 && nrc == 0) // for now *not* warning for e.g., matrix(NA, 0, 4)
warning(_("non-empty data for zero-extent matrix"));
}
#ifndef LONG_VECTOR_SUPPORT
if ((double)nr * (double)nc > INT_MAX)
error(_("too many elements specified"));
#endif
PROTECT(ans = allocMatrix(TYPEOF(vals), nr, nc));
if(lendat) {
if (isVector(vals))
copyMatrix(ans, vals, byrow);
else
copyListMatrix(ans, vals, byrow);
} else if (isVector(vals)) { /* fill with NAs */
R_xlen_t N = (R_xlen_t) nr * nc, i;
switch(TYPEOF(vals)) {
case STRSXP:
for (i = 0; i < N; i++)
SET_STRING_ELT(ans, i, NA_STRING);
break;
case LGLSXP:
for (i = 0; i < N; i++)
LOGICAL(ans)[i] = NA_LOGICAL;
break;
case INTSXP:
for (i = 0; i < N; i++)
INTEGER(ans)[i] = NA_INTEGER;
break;
case REALSXP:
for (i = 0; i < N; i++)
REAL(ans)[i] = NA_REAL;
break;
case CPLXSXP:
{
Rcomplex na_cmplx;
na_cmplx.r = NA_REAL;
na_cmplx.i = 0;
for (i = 0; i < N; i++)
COMPLEX(ans)[i] = na_cmplx;
}
break;
case RAWSXP:
if (N) memset(RAW(ans), 0, N);
break;
default:
/* don't fill with anything */
;
}
}
if(!isNull(dimnames) && length(dimnames) > 0)
ans = dimnamesgets(ans, dimnames);
UNPROTECT(1);
return ans;
}
SEXP Rf_allocMatrix(SEXPTYPE mode, int nrow, int ncol)
{
SEXP s, t;
R_xlen_t n;
if (nrow < 0 || ncol < 0)
error(_("negative extents to matrix"));
#ifndef LONG_VECTOR_SUPPORT
if ((double)nrow * (double)ncol > INT_MAX)
error(_("allocMatrix: too many elements specified"));
#endif
n = ((R_xlen_t) nrow) * ncol;
PROTECT(s = allocVector(mode, n));
PROTECT(t = allocVector(INTSXP, 2));
INTEGER(t)[0] = nrow;
INTEGER(t)[1] = ncol;
setAttrib(s, R_DimSymbol, t);
UNPROTECT(2);
return s;
}
/**
* Allocate a 3-dimensional array
*
* @param mode The R mode (e.g. INTSXP)
* @param nrow number of rows
* @param ncol number of columns
* @param nface number of faces
*
* @return A 3-dimensional array of the indicated dimensions and mode
*/
SEXP Rf_alloc3DArray(SEXPTYPE mode, int nrow, int ncol, int nface)
{
SEXP s, t;
R_xlen_t n;
if (nrow < 0 || ncol < 0 || nface < 0)
error(_("negative extents to 3D array"));
#ifndef LONG_VECTOR_SUPPORT
if ((double)nrow * (double)ncol * (double)nface > INT_MAX)
error(_("'alloc3DArray': too many elements specified"));
#endif
n = ((R_xlen_t) nrow) * ncol * nface;
PROTECT(s = allocVector(mode, n));
PROTECT(t = allocVector(INTSXP, 3));
INTEGER(t)[0] = nrow;
INTEGER(t)[1] = ncol;
INTEGER(t)[2] = nface;
setAttrib(s, R_DimSymbol, t);
UNPROTECT(2);
return s;
}
SEXP Rf_allocArray(SEXPTYPE mode, SEXP dims)
{
SEXP array;
int i;
R_xlen_t n = 1;
#ifndef LONG_VECTOR_SUPPORT
double dn = 1;
#endif
for (i = 0; i < LENGTH(dims); i++) {
#ifndef LONG_VECTOR_SUPPORT
dn *= INTEGER(dims)[i];
if(dn > INT_MAX)
error(_("'allocArray': too many elements specified by 'dims'"));
#endif
n *= INTEGER(dims)[i];
}
PROTECT(dims = duplicate(dims));
PROTECT(array = allocVector(mode, n));
setAttrib(array, R_DimSymbol, dims);
UNPROTECT(2);
return array;
}
/* DropDims strips away redundant dimensioning information. */
/* If there is an appropriate dimnames attribute the correct */
/* element is extracted and attached to the vector as a names */
/* attribute. Note that this function mutates x. */
/* Duplication should occur before this is called. */
attribute_hidden SEXP Rf_DropDims(SEXP x)
{
PROTECT(x);
SEXP dims = getAttrib(x, R_DimSymbol);
/* Check that dropping will actually do something. */
/* (1) Check that there is a "dim" attribute. */
if (dims == R_NilValue) {
UNPROTECT(1); /* x */
return x;
}
int ndims = LENGTH(dims);
int *dim = INTEGER(dims); // used several times
/* (2) Check whether there are redundant extents */
int i, n = 0;
for (i = 0; i < ndims; i++)
if (dim[i] != 1) n++;
if (n == ndims) {
UNPROTECT(1); /* x */
return x;
}
SEXP dimnames = PROTECT(getAttrib(x, R_DimNamesSymbol)),
newnames = R_NilValue;
if (n <= 1) {
/* We have reduced to a vector result.
If that has length one, it is ambiguous which dimnames to use,
so use it if there is only one (as from R 2.7.0).
*/
if (dimnames != R_NilValue) {
if(XLENGTH(x) != 1) {
for (i = 0; i < LENGTH(dims); i++) {
if (dim[i] != 1) {
newnames = VECTOR_ELT(dimnames, i);
break;
}
}
} else { /* drop all dims: keep names if unambiguous */
int cnt;
for(i = 0, cnt = 0; i < LENGTH(dims); i++)
if(VECTOR_ELT(dimnames, i) != R_NilValue) cnt++;
if(cnt == 1)
for (i = 0; i < LENGTH(dims); i++) {
newnames = VECTOR_ELT(dimnames, i);
if(newnames != R_NilValue) break;
}
}
}
PROTECT(newnames);
setAttrib(x, R_DimNamesSymbol, R_NilValue);
setAttrib(x, R_DimSymbol, R_NilValue);
setAttrib(x, R_NamesSymbol, newnames);
/* FIXME: the following is desirable, but pointless as long as
subset.c & others have a contrary version that leaves the
S4 class in, incorrectly, in the case of vectors. JMC
3/3/09 */
/* if(IS_S4_OBJECT(x)) {/\* no longer valid subclass of array or
matrix *\/ */
/* setAttrib(x, R_ClassSymbol, R_NilValue); */
/* UNSET_S4_OBJECT(x); */
/* } */
UNPROTECT(1); /* newnames */
} else {
// We have a lower dimensional array, and n == length(newdims)
SEXP newdims, dnn, newnamesnames = R_NilValue;
PROTECT(dnn = getAttrib(dimnames, R_NamesSymbol));
PROTECT(newdims = allocVector(INTSXP, n));
for (i = 0, n = 0; i < ndims; i++)
if (dim[i] != 1)
INTEGER(newdims)[n++] = dim[i];
if(!isNull(getAttrib(dims, R_NamesSymbol))) {
SEXP new_nms = PROTECT(allocVector(STRSXP, n));
SEXP nms_d = getAttrib(dims, R_NamesSymbol);
for (i = 0, n = 0; i < ndims; i++)
if (dim[i] != 1)
SET_STRING_ELT(new_nms, n++, STRING_ELT(nms_d, i));
setAttrib(newdims, R_NamesSymbol, new_nms);
UNPROTECT(1);
}
Rboolean havenames = FALSE;
if (!isNull(dimnames)) {
for (i = 0; i < ndims; i++)
if (dim[i] != 1 &&
VECTOR_ELT(dimnames, i) != R_NilValue)
havenames = TRUE;
if (havenames) {
PROTECT(newnames = allocVector(VECSXP, n));
PROTECT(newnamesnames = allocVector(STRSXP, n));
for (i = 0, n = 0; i < ndims; i++) {
if (dim[i] != 1) {
if(!isNull(dnn))
SET_STRING_ELT(newnamesnames, n,
STRING_ELT(dnn, i));
SET_VECTOR_ELT(newnames, n++, VECTOR_ELT(dimnames, i));
}
}
}
else dimnames = R_NilValue;
}
setAttrib(x, R_DimNamesSymbol, R_NilValue);
setAttrib(x, R_DimSymbol, newdims);
if (havenames)
{
if(!isNull(dnn))
setAttrib(newnames, R_NamesSymbol, newnamesnames);
setAttrib(x, R_DimNamesSymbol, newnames);
UNPROTECT(2); /* newnamesnames, newnames */
}
UNPROTECT(2); /* newdims, dnn */
}
UNPROTECT(2); /* dimnames, x */
return x;
}
attribute_hidden SEXP do_drop(SEXP call, SEXP op, SEXP args, SEXP rho)
{
SEXP x, xdims;
int i, n, shorten;
checkArity(op, args);
x = CAR(args);
if ((xdims = getAttrib(x, R_DimSymbol)) != R_NilValue) {
n = LENGTH(xdims);
shorten = 0;
for (i = 0; i < n; i++)
if (INTEGER(xdims)[i] == 1) shorten = 1;
if (shorten) {
if (MAYBE_REFERENCED(x)) x = R_duplicate_attr(x);
x = DropDims(x);
}
}
return x;
}
/* Length of Primitive Objects */
attribute_hidden SEXP do_length(SEXP call, SEXP op, SEXP args, SEXP rho)
{
checkArity(op, args);
check1arg(args, call, "x");
SEXP x = CAR(args), ans;
/* DispatchOrEval internal generic: length */
if (isObject(x) &&
DispatchOrEval(call, op, "length", args, rho, &ans, 0, 1)) {
if (length(ans) == 1 && TYPEOF(ans) == REALSXP) {
double d = REAL(ans)[0];
if (R_FINITE(d) && d >= 0. && d <= INT_MAX && floor(d) == d) {
PROTECT(ans);
ans = coerceVector(ans, INTSXP);
UNPROTECT(1);
return(ans);
}
}
return(ans);
}
#ifdef LONG_VECTOR_SUPPORT
// or use IS_LONG_VEC
R_xlen_t len = xlength(x);
if (len > INT_MAX) return ScalarReal((double) len);
#endif
return ScalarInteger(length(x));
}
attribute_hidden R_len_t dispatch_length(SEXP x, SEXP call, SEXP rho) {
R_xlen_t len = dispatch_xlength(x, call, rho);
#ifdef LONG_VECTOR_SUPPORT
if (len > INT_MAX) return R_BadLongVector(x, __FILE__, __LINE__);
#endif
return (R_len_t) len;
}
attribute_hidden R_xlen_t dispatch_xlength(SEXP x, SEXP call, SEXP rho) {
static SEXP length_op = NULL;
if (isObject(x)) {
SEXP len, args;
if (length_op == NULL)
length_op = R_Primitive("length");
PROTECT(args = list1(x));
/* DispatchOrEval internal generic: length */
if (DispatchOrEval(call, length_op, "length", args, rho, &len, 0, 1)) {
UNPROTECT(1);
return (R_xlen_t)
(TYPEOF(len) == REALSXP ? REAL(len)[0] : asInteger(len));
}
UNPROTECT(1);
}
return(xlength(x));
}
// auxiliary for do_lengths_*(), i.e., R's lengths()
static R_xlen_t getElementLength(SEXP x, R_xlen_t i, SEXP call, SEXP rho) {
SEXP x_elt;
R_xlen_t ans;
PROTECT(x_elt = dispatch_subset2(x, i, call, rho));
ans = dispatch_xlength(x_elt, call, rho);
UNPROTECT(1); /* x_elt */
return ans;
}
#ifdef LONG_VECTOR_SUPPORT
static SEXP do_lengths_long(SEXP x, SEXP call, SEXP rho)
{
SEXP ans;
R_xlen_t x_len, i;
double *ans_elt;
x_len = dispatch_xlength(x, call, rho);
PROTECT(ans = allocVector(REALSXP, x_len));
for (i = 0, ans_elt = REAL(ans); i < x_len; i++, ans_elt++)
*ans_elt = (double) getElementLength(x, i, call, rho);
UNPROTECT(1);
return ans;
}
#endif
attribute_hidden SEXP do_lengths(SEXP call, SEXP op, SEXP args, SEXP rho)
{
checkArity(op, args);
SEXP x = CAR(args), ans;
R_xlen_t x_len, i;
int *ans_elt;
int useNames = asLogical(CADR(args));
if (useNames == NA_LOGICAL)
error(_("invalid '%s' value"), "use.names");
/* DispatchOrEval internal generic: lengths */
if (DispatchOrEval(call, op, "lengths", args, rho, &ans, 0, 1))
return(ans);
Rboolean isList = isVectorList(x) || isS4(x);
if(!isList) switch(TYPEOF(x)) {
case NILSXP:
case CHARSXP:
case LGLSXP:
case INTSXP:
case REALSXP:
case CPLXSXP:
case STRSXP:
case RAWSXP:
break;
default:
error(_("'%s' must be a list or atomic vector"), "x");
}
x_len = dispatch_xlength(x, call, rho);
PROTECT(ans = allocVector(INTSXP, x_len));
if(isList) {
for (i = 0, ans_elt = INTEGER(ans); i < x_len; i++, ans_elt++) {
R_xlen_t x_elt_len = getElementLength(x, i, call, rho);
#ifdef LONG_VECTOR_SUPPORT
if (x_elt_len > INT_MAX) {
ans = do_lengths_long(x, call, rho);
UNPROTECT(1);
PROTECT(ans);
break;
}
#endif
*ans_elt = (int)x_elt_len;
}
} else { // atomic: every element has length 1
for (i = 0, ans_elt = INTEGER(ans); i < x_len; i++, ans_elt++)
*ans_elt = 1;
}
SEXP dim = getAttrib(x, R_DimSymbol);
if(!isNull(dim)) {
setAttrib(ans, R_DimSymbol, dim);
}
if(useNames) {
SEXP names = getAttrib(x, R_NamesSymbol);
if(!isNull(names)) setAttrib(ans, R_NamesSymbol, names);
SEXP dimnames = getAttrib(x, R_DimNamesSymbol);
if(!isNull(dimnames)) setAttrib(ans, R_DimNamesSymbol, dimnames);
}
UNPROTECT(1);
return ans;
}
attribute_hidden SEXP do_rowscols(SEXP call, SEXP op, SEXP args, SEXP rho)
{
checkArity(op, args);
SEXP dim = CAR(args);
int nprot = 0;
if (!isInteger(dim)) {
PROTECT(dim = coerceVector(dim, INTSXP)); nprot++;
}
if (LENGTH(dim) != 2)
error(_("a matrix-like object is required as argument to '%s'"),
(PRIMVAL(op) == 2) ? "col" : "row");
int nr = INTEGER(dim)[0],
nc = INTEGER(dim)[1];
if(nprot) UNPROTECT(nprot);
SEXP ans = allocMatrix(INTSXP, nr, nc);
R_xlen_t NR = nr;
switch (PRIMVAL(op)) {
case 1: // row() & .row()
for (int i = 0; i < nr; i++)
for (int j = 0; j < nc; j++)
INTEGER(ans)[i + j * NR] = i + 1;
break;
case 2: // col() & .col()
for (int i = 0; i < nr; i++)
for (int j = 0; j < nc; j++)
INTEGER(ans)[i + j * NR] = j + 1;
break;
}
return ans;
}
/*
Whenever vector x contains NaN or Inf (or -Inf), the function returns TRUE.
It can be imprecise: it can return TRUE in other cases as well.
A precise version of the function could be implemented as
for (R_xlen_t i = 0; i < n; i++)
if (!R_FINITE(x[i])) return TRUE;
return FALSE;
The present version is imprecise, but faster.
*/
static Rboolean mayHaveNaNOrInf(double *x, R_xlen_t n)
{
if ((n&1) != 0 && !R_FINITE(x[0]))
return TRUE;
for (R_xlen_t i = n&1; i < n; i += 2)
/* A precise version could use this condition:
*
* !R_FINITE(x[i]+x[i+1]) && (!R_FINITE(x[i]) || !R_FINITE(x[i+1]))
*
* The present imprecise version has been found to be faster
* with GCC and ICC in the common case when the sum of the two
* values is always finite.
*
* The present version is imprecise because the sum of two very
* large finite values (e.g. 1e308) may be infinite.
*/
if (!R_FINITE(x[i]+x[i+1]))
return TRUE;
return FALSE;
}
/*
This is an experimental version that has been observed to run fast on some
SIMD hardware with GCC and ICC.
Note that the OpenMP reduction assumes associativity of addition, which is
safe here, because the result is only used for an imprecise test for
the presence of NaN and Inf values.
*/
static Rboolean mayHaveNaNOrInf_simd(double *x, R_xlen_t n)
{
double s = 0;
/* SIMD reduction is supported since OpenMP 4.0. The value of _OPENMP is
unreliable in some compilers, so we depend on HAVE_OPENMP_SIMDRED,
which is normally set by configure based on a test. */
/* _OPENMP >= 201307 */
#if defined(_OPENMP) && HAVE_OPENMP_SIMDRED
#pragma omp simd reduction(+:s)
#endif
for (R_xlen_t i = 0; i < n; i++)
s += x[i];
return !R_FINITE(s);
}
static Rboolean cmayHaveNaNOrInf(Rcomplex *x, R_xlen_t n)
{
/* With HAVE_FORTRAN_DOUBLE_COMPLEX set, it should be clear that
Rcomplex has no padding, so we could probably use mayHaveNaNOrInf,
but better safe than sorry... */
if ((n&1) != 0 && (!R_FINITE(x[0].r) || !R_FINITE(x[0].i)))
return TRUE;
for (R_xlen_t i = n&1; i < n; i += 2)
if (!R_FINITE(x[i].r+x[i].i+x[i+1].r+x[i+1].i))
return TRUE;
return FALSE;
}
/* experimental version for SIMD hardware (see also mayHaveNaNOrInf_simd) */
static Rboolean cmayHaveNaNOrInf_simd(Rcomplex *x, R_xlen_t n)
{
double s = 0;
/* _OPENMP >= 201307 - see mayHaveNaNOrInf_simd */
#if defined(_OPENMP) && HAVE_OPENMP_SIMDRED
#pragma omp simd reduction(+:s)
#endif
for (R_xlen_t i = 0; i < n; i++) {
s += x[i].r;
s += x[i].i;
}
return !R_FINITE(s);
}
static void internal_matprod(double *x, int nrx, int ncx,
double *y, int nry, int ncy, double *z)
{
LDOUBLE sum;
#define MATPROD_BODY \
R_xlen_t NRX = nrx, NRY = nry; \
for (int i = 0; i < nrx; i++) \
for (int k = 0; k < ncy; k++) { \
sum = 0.0; \
for (int j = 0; j < ncx; j++) \
sum += x[i + j * NRX] * y[j + k * NRY]; \
z[i + k * NRX] = (double) sum; \
}
MATPROD_BODY;
}
static void simple_matprod(double *x, int nrx, int ncx,
double *y, int nry, int ncy, double *z)
{
double sum;
MATPROD_BODY;
}
static void internal_crossprod(double *x, int nrx, int ncx,
double *y, int nry, int ncy, double *z)
{
LDOUBLE sum;
#define CROSSPROD_BODY \
R_xlen_t NRX = nrx, NRY = nry, NCX = ncx; \
for (int i = 0; i < ncx; i++) \
for (int k = 0; k < ncy; k++) { \
sum = 0.0; \
for (int j = 0; j < nrx; j++) \
sum += x[j + i * NRX] * y[j + k * NRY]; \
z[i + k * NCX] = (double) sum; \
}
CROSSPROD_BODY;
}
static void simple_crossprod(double *x, int nrx, int ncx,
double *y, int nry, int ncy, double *z)
{
double sum;
CROSSPROD_BODY;
}
static void internal_tcrossprod(double *x, int nrx, int ncx,
double *y, int nry, int ncy, double *z)
{
LDOUBLE sum;
#define TCROSSPROD_BODY \
R_xlen_t NRX = nrx, NRY = nry; \
for (int i = 0; i < nrx; i++) \
for (int k = 0; k < nry; k++) { \
sum = 0.0; \
for (int j = 0; j < ncx; j++) \
sum += x[i + j * NRX] * y[k + j * NRY]; \
z[i + k * NRX] = (double) sum; \
}
TCROSSPROD_BODY;
}
static void simple_tcrossprod(double *x, int nrx, int ncx,
double *y, int nry, int ncy, double *z)
{
double sum;
TCROSSPROD_BODY;
}
static void matprod(double *x, int nrx, int ncx,
double *y, int nry, int ncy, double *z)
{
R_xlen_t NRX = nrx, NRY = nry;
if (nrx == 0 || ncx == 0 || nry == 0 || ncy == 0) {
/* zero-extent operations should return zeroes */
for(R_xlen_t i = 0; i < NRX*ncy; i++) z[i] = 0;
return;
}
switch(R_Matprod) {
case MATPROD_DEFAULT:
/* Don't trust the BLAS to handle NA/NaNs correctly: PR#4582
* The test is only O(n) here.
*
* MKL disclaimer: "LAPACK routines assume that input matrices
* do not contain IEEE 754 special values such as INF or NaN values.
* Using these special values may cause LAPACK to return unexpected
* results or become unstable."
*/
if (mayHaveNaNOrInf(x, NRX*ncx) || mayHaveNaNOrInf(y, NRY*ncy)) {
simple_matprod(x, nrx, ncx, y, nry, ncy, z);
return;
}
break; /* use blas */
case MATPROD_INTERNAL:
internal_matprod(x, nrx, ncx, y, nry, ncy, z);
return;
case MATPROD_BLAS:
break;
case MATPROD_DEFAULT_SIMD:
if (mayHaveNaNOrInf_simd(x, NRX*ncx) ||
mayHaveNaNOrInf_simd(y, NRY*ncy)) {
simple_matprod(x, nrx, ncx, y, nry, ncy, z);
return;
}
break; /* use blas */
}
char *transN = "N", *transT = "T";
double one = 1.0, zero = 0.0;
int ione = 1;
if (ncy == 1) /* matrix-vector or dot product */
F77_CALL(dgemv)(transN, &nrx, &ncx, &one, x,
&nrx, y, &ione, &zero, z, &ione FCONE);
else if (nrx == 1) /* vector-matrix */
/* Instead of xY, compute (xY)^T == (Y^T)(x^T)
The result is a vector, so transposing its content is no-op */
F77_CALL(dgemv)(transT, &nry, &ncy, &one, y,
&nry, x, &ione, &zero, z, &ione FCONE);
else /* matrix-matrix or outer product */
F77_CALL(dgemm)(transN, transN, &nrx, &ncy, &ncx, &one, x,
&nrx, y, &nry, &zero, z, &nrx FCONE FCONE);
}
static void internal_cmatprod(Rcomplex *x, int nrx, int ncx,
Rcomplex *y, int nry, int ncy, Rcomplex *z)
{
LDOUBLE sum_i, sum_r;
#define CMATPROD_BODY \
int i, j, k; \
double complex xij, yjk; \
R_xlen_t NRX = nrx, NRY = nry; \
for (i = 0; i < nrx; i++) \
for (k = 0; k < ncy; k++) { \
sum_r = 0.0; \
sum_i = 0.0; \
for (j = 0; j < ncx; j++) { \
xij = toC99(x + (i + j * NRX)); \
yjk = toC99(y + (j + k * NRY)); \
sum_r += creal(xij * yjk); \
sum_i += cimag(xij * yjk); \
} \
z[i + k * NRX].r = (double) sum_r; \
z[i + k * NRX].i = (double) sum_i; \
}
CMATPROD_BODY;
}
static void simple_cmatprod(Rcomplex *x, int nrx, int ncx,
Rcomplex *y, int nry, int ncy, Rcomplex *z)
{
double sum_i, sum_r;
CMATPROD_BODY;
}
static void internal_ccrossprod(Rcomplex *x, int nrx, int ncx,
Rcomplex *y, int nry, int ncy, Rcomplex *z)
{
LDOUBLE sum_i, sum_r;
#define CCROSSPROD_BODY \
int i, j, k; \
double complex xji, yjk; \
R_xlen_t NRX = nrx, NRY = nry, NCX = ncx; \
for (i = 0; i < ncx; i++) \
for (k = 0; k < ncy; k++) { \
sum_r = 0.0; \
sum_i = 0.0; \
for (j = 0; j < nrx; j++) { \
xji = toC99(x + (j + i * NRX)); \
yjk = toC99(y + (j + k * NRY)); \
sum_r += creal(xji * yjk); \
sum_i += cimag(xji * yjk); \
} \
z[i + k * NCX].r = (double) sum_r; \
z[i + k * NCX].i = (double) sum_i; \
}
CCROSSPROD_BODY;
}
static void simple_ccrossprod(Rcomplex *x, int nrx, int ncx,
Rcomplex *y, int nry, int ncy, Rcomplex *z)
{
double sum_i, sum_r;
CCROSSPROD_BODY;
}
static void internal_tccrossprod(Rcomplex *x, int nrx, int ncx,
Rcomplex *y, int nry, int ncy, Rcomplex *z)
{
LDOUBLE sum_i, sum_r;
#define TCCROSSPROD_BODY \
int i, j, k; \
double complex xij, ykj; \
R_xlen_t NRX = nrx, NRY = nry; \
for (i = 0; i < nrx; i++) \
for (k = 0; k < nry; k++) { \
sum_r = 0.0; \
sum_i = 0.0; \
for (j = 0; j < ncx; j++) { \
xij = toC99(x + (i + j * NRX)); \
ykj = toC99(y + (k + j * NRY)); \
sum_r += creal(xij * ykj); \
sum_i += cimag(xij * ykj); \
} \
z[i + k * NRX].r = (double) sum_r; \
z[i + k * NRX].i = (double) sum_i; \
}
TCCROSSPROD_BODY;
}
static void simple_tccrossprod(Rcomplex *x, int nrx, int ncx,
Rcomplex *y, int nry, int ncy, Rcomplex *z)
{
double sum_i, sum_r;
TCCROSSPROD_BODY;
}
static void cmatprod(Rcomplex *x, int nrx, int ncx,
Rcomplex *y, int nry, int ncy, Rcomplex *z)
{
R_xlen_t NRX = nrx, NRY = nry;
if (nrx == 0 || ncx == 0 || nry == 0 || ncy == 0) {
/* zero-extent operations should return zeroes */
for(R_xlen_t i = 0; i < NRX*ncy; i++) z[i].r = z[i].i = 0;
return;
}
#ifndef HAVE_FORTRAN_DOUBLE_COMPLEX
if (R_Matprod == MATPROD_INTERNAL)
internal_cmatprod(x, nrx, ncx, y, nry, ncy, z);
else
simple_cmatprod(x, nrx, ncx, y, nry, ncy, z);
#else
switch(R_Matprod) {
case MATPROD_DEFAULT:
if (cmayHaveNaNOrInf(x, NRX*ncx) || cmayHaveNaNOrInf(y, NRY*ncy)) {
simple_cmatprod(x, nrx, ncx, y, nry, ncy, z);
return;
}
break; /* use blas */
case MATPROD_INTERNAL:
internal_cmatprod(x, nrx, ncx, y, nry, ncy, z);
return;
case MATPROD_BLAS:
break;
case MATPROD_DEFAULT_SIMD:
if (cmayHaveNaNOrInf_simd(x, NRX*ncx) ||
cmayHaveNaNOrInf_simd(y, NRY*ncy)) {
simple_cmatprod(x, nrx, ncx, y, nry, ncy, z);
return;
}
break; /* use blas */
}
char *transa = "N", *transb = "N";
Rcomplex one, zero;
one.r = 1.0; one.i = zero.r = zero.i = 0.0;
F77_CALL(zgemm)(transa, transb, &nrx, &ncy, &ncx, &one,
x, &nrx, y, &nry, &zero, z, &nrx FCONE FCONE);
#endif
}
static void symcrossprod(double *x, int nr, int nc, double *z)
{
R_xlen_t NR = nr, NC = nc;
if (nr == 0 || nc == 0) {
/* zero-extent operations should return zeroes */
for(R_xlen_t i = 0; i < NC*NC; i++) z[i] = 0;
return;
}
switch(R_Matprod) {
case MATPROD_DEFAULT:
/* see matprod for more details */
if (mayHaveNaNOrInf(x, NR*nc)) {
simple_crossprod(x, nr, nc, x, nr, nc, z);
return;
}
break; /* use blas */
case MATPROD_INTERNAL:
internal_crossprod(x, nr, nc, x, nr, nc, z);