1
1
/*
2
2
* R : A Computer Language for Statistical Data Analysis
3
- * Copyright (C) 1998-2020 The R Core Team
3
+ * Copyright (C) 1998-2021 The R Core Team
4
4
* Copyright (C) 2002-2015 The R Foundation
5
5
* Copyright (C) 1995, 1996 Robert Gentleman and Ross Ihaka
6
6
*
@@ -63,6 +63,7 @@ SEXP GetColNames(SEXP dimnames)
63
63
return R_NilValue ;
64
64
}
65
65
66
+ // .Internal(matrix(data, nrow, ncol, byrow, dimnames, missing(nrow), missing(ncol)))
66
67
SEXP attribute_hidden do_matrix (SEXP call , SEXP op , SEXP args , SEXP rho )
67
68
{
68
69
SEXP vals , ans , snr , snc , dimnames ;
@@ -116,36 +117,33 @@ SEXP attribute_hidden do_matrix(SEXP call, SEXP op, SEXP args, SEXP rho)
116
117
if (lendat > INT_MAX ) error ("data is too long" );
117
118
nr = (int ) lendat ;
118
119
} else if (miss_nr ) {
119
- if (lendat > (double ) nc * INT_MAX ) error ("data is too long" );
120
- // avoid division by zero
121
- if (nc == 0 ) {
122
- if (lendat ) error (_ ("nc = 0 for non-null data" ));
123
- else nr = 0 ;
124
- } else
120
+ if (lendat > (double ) nc * INT_MAX ) error ("data is too long" ); // incl lendat > nc == 0
121
+ if (nc == 0 ) // as lendat <= nc, have lendat == 0
122
+ nr = 0 ;
123
+ else
125
124
nr = (int ) ceil ((double ) lendat / (double ) nc );
126
125
} else if (miss_nc ) {
127
- if (lendat > (double ) nr * INT_MAX ) error ("data is too long" );
128
- // avoid division by zero
129
- if (nr == 0 ) {
130
- if (lendat ) error (_ ("nr = 0 for non-null data" ));
131
- else nc = 0 ;
132
- } else
126
+ if (lendat > (double ) nr * INT_MAX ) error ("data is too long" ); // incl lendat > nr == 0
127
+ if (nr == 0 ) // then lendat == 0
128
+ nc = 0 ;
129
+ else
133
130
nc = (int ) ceil ((double ) lendat / (double ) nr );
134
131
}
135
132
136
- if (lendat > 0 ) {
133
+ if (lendat > 0 ) {
137
134
R_xlen_t nrc = (R_xlen_t ) nr * nc ;
138
- if (lendat > 1 && nrc % lendat != 0 ) {
135
+ if (lendat > 1 && ( nrc % lendat ) != 0 ) { // ==> nrc > 0
139
136
if (((lendat > nr ) && (lendat / nr ) * nr != lendat ) ||
140
137
((lendat < nr ) && (nr / lendat ) * lendat != nr ))
141
138
warning (_ ("data length [%d] is not a sub-multiple or multiple of the number of rows [%d]" ), lendat , nr );
142
139
else if (((lendat > nc ) && (lendat / nc ) * nc != lendat ) ||
143
140
((lendat < nc ) && (nc / lendat ) * lendat != nc ))
144
141
warning (_ ("data length [%d] is not a sub-multiple or multiple of the number of columns [%d]" ), lendat , nc );
142
+ else if (nrc != lendat )
143
+ warning (_ ("data length differs from size of matrix: [%d != %d x %d]" ), lendat , nr , nc );
145
144
}
146
- else if ((lendat > 1 ) && (nrc == 0 )){
147
- warning (_ ("data length exceeds size of matrix" ));
148
- }
145
+ else if (lendat > 1 && nrc == 0 ) // for now *not* warning for e.g., matrix(NA, 0, 4)
146
+ warning (_ ("non-empty data for zero-extent matrix" ));
149
147
}
150
148
151
149
#ifndef LONG_VECTOR_SUPPORT
@@ -289,11 +287,8 @@ SEXP allocArray(SEXPTYPE mode, SEXP dims)
289
287
290
288
SEXP DropDims (SEXP x )
291
289
{
292
- SEXP dims , dimnames , newnames = R_NilValue ;
293
- int i , n , ndims ;
294
-
295
290
PROTECT (x );
296
- dims = getAttrib (x , R_DimSymbol );
291
+ SEXP dims = getAttrib (x , R_DimSymbol );
297
292
298
293
/* Check that dropping will actually do something. */
299
294
/* (1) Check that there is a "dim" attribute. */
@@ -302,19 +297,21 @@ SEXP DropDims(SEXP x)
302
297
UNPROTECT (1 ); /* x */
303
298
return x ;
304
299
}
305
- ndims = LENGTH (dims );
300
+
301
+ int ndims = LENGTH (dims );
306
302
int * dim = INTEGER (dims ); // used several times
307
303
308
304
/* (2) Check whether there are redundant extents */
309
- n = 0 ;
305
+ int i , n = 0 ;
310
306
for (i = 0 ; i < ndims ; i ++ )
311
307
if (dim [i ] != 1 ) n ++ ;
312
308
if (n == ndims ) {
313
309
UNPROTECT (1 ); /* x */
314
310
return x ;
315
311
}
316
312
317
- PROTECT (dimnames = getAttrib (x , R_DimNamesSymbol ));
313
+ SEXP dimnames = PROTECT (getAttrib (x , R_DimNamesSymbol )),
314
+ newnames = R_NilValue ;
318
315
if (n <= 1 ) {
319
316
/* We have reduced to a vector result.
320
317
If that has length one, it is ambiguous which dimnames to use,
@@ -811,7 +808,7 @@ static void matprod(double *x, int nrx, int ncx,
811
808
F77_CALL (dgemv )(transT , & nry , & ncy , & one , y ,
812
809
& nry , x , & ione , & zero , z , & ione FCONE );
813
810
else /* matrix-matrix or outer product */
814
- F77_CALL (dgemm )(transN , transN , & nrx , & ncy , & ncx , & one , x ,
811
+ F77_CALL (dgemm )(transN , transN , & nrx , & ncy , & ncx , & one , x ,
815
812
& nrx , y , & nry , & zero , z , & nrx FCONE FCONE );
816
813
}
817
814
0 commit comments