Dear R-users, I found a strange problem working with products of two matrices, say: a <- A[i, ] ; crossprod(a) where i is a set of integers selecting rows. When i is empty the result is in a sense random. After some trials the right answer (a matrix of zeros) appears. --------------- Illustration -------------------- R : Copyright 2003, The R Development Core Team Version 1.8.0 (2003-10-08)> A <-matrix(0, 5, 5) > i <- c() > a <- A[i, ] ; crossprod(a)[,1] [,2] [,3] [,4] [,5] [1,] 6.578187e-313 NaN NaN NaN NaN [2,] NaN 1.273197e-313 NaN 1.485397e-313 NaN [3,] NaN 4.243992e-313 2.121996e-314 NaN NaN [4,] NaN 1.697597e-313 NaN 4.880590e-313 NaN [5,] 5.941588e-313 NaN NaN 1.697597e-313 NaN> a <- A[i, ] ; crossprod(a)[,1] [,2] [,3] [,4] [,5] [1,] 2.121996e-314 5.729389e-313 NaN NaN NaN [2,] NaN NaN NaN NaN 1.909796e-313 [3,] 2.970794e-313 NaN NaN NaN NaN [4,] NaN NaN NaN 8.487983e-314 NaN [5,] NaN 6.365987e-313 2.546395e-313 NaN NaN> a <- A[i, ] ; crossprod(a)[,1] [,2] [,3] [,4] [,5] [1,] NaN 1.485397e-313 NaN NaN 2.970794e-313 [2,] 3.182994e-313 NaN NaN 1.060998e-313 NaN [3,] NaN NaN NaN 1.697597e-313 2.737375e-312 [4,] NaN NaN NaN NaN 2.048394e+10 [5,] NaN NaN NaN NaN 2.970794e-313> a <- A[i, ] ; crossprod(a)[,1] [,2] [,3] [,4] [,5] [1,] 1.591383e-266 20489834629 0 0 0 [2,] 5.031994e-266 0 0 0 0 [3,] 1.591205e-266 0 0 0 0 [4,] 1.264128e-267 0 0 0 0 [5,] 1.037656e-311 0 0 0 0> a <- A[i, ] ; crossprod(a)[,1] [,2] [,3] [,4] [,5] [1,] 0 0 0 0 0 [2,] 0 0 0 0 0 [3,] 0 0 0 0 0 [4,] 0 0 0 0 0 [5,] 0 0 0 0 0 --------------- End of illustration------------ The same problem does not appear using the matrix product:> a <- A[i, ] ; t(a) %*% a[,1] [,2] [,3] [,4] [,5] [1,] 0 0 0 0 0 [2,] 0 0 0 0 0 [3,] 0 0 0 0 0 [4,] 0 0 0 0 0 [5,] 0 0 0 0 0 Note that Splus 6 returns an error message:> a <- A[i, ] ; crossprod(a)Problem in .Fortran.ok.Internal(if(cmplx) "zcrossp1"..: subroutine dcrossp1: Argument 1 has zero length Thank you, Giovanni
I still have R 1.7.1, and the problem appears there as well: > a <- A[i, ] ; crossprod(a) [,1] [,2] [,3] [,4] [,5] [1,] 1.195616e-301 7.042305e-302 9.563047e-302 2.281448e-302 2.198017e-302 [2,] 6.905419e-302 1.204915e-301 3.382433e-302 2.398701e-302 2.358828e-302 [3,] 1.194968e-301 7.039991e-302 1.384628e-302 2.446584e-302 4.507199e-302 [4,] 7.046400e-302 1.204416e-301 2.444003e-302 2.357136e-302 2.446228e-302 [5,] 1.205363e-301 1.204367e-301 2.445605e-302 3.979963e-302 7.861951e-302 > a <- A[i, ] ; crossprod(a) [,1] [,2] [,3] [,4] [,5] [1,] 0 0 0 0 0 [2,] 0 0 0 0 0 [3,] 0 0 0 0 0 [4,] 0 0 0 0 0 [5,] 0 0 0 0 0 > a <- A[i, ] ; crossprod(a) [,1] [,2] [,3] [,4] [,5] [1,] 0 0 0 0 0 [2,] 0 0 0 0 0 [3,] 0 0 0 0 0 [4,] 0 0 0 0 0 [5,] 0 0 0 0 0 > a <- A[i, ] ; crossprod(a) [,1] [,2] [,3] [,4] [,5] [1,] 0 0 0 0 0 [2,] 0 0 0 0 0 [3,] 0 0 0 0 0 [4,] 0 0 0 0 0 [5,] 0 0 0 0 0 > a <- A[i, ] ; crossprod(a) [,1] [,2] [,3] [,4] [,5] [1,] 0 0 5.092790e-313 4.372522e-111 0 [2,] 0 0 5.708624e-307 0.000000e+00 0 [3,] NaN 0 4.516565e-300 0.000000e+00 0 [4,] 0 0 8.997262e-312 0.000000e+00 0 [5,] 0 0 1.086462e-311 0.000000e+00 0 > hope this helps. spencer graves Giovanni Marchetti wrote:>Dear R-users, > >I found a strange problem >working with products of two matrices, say: > >a <- A[i, ] ; crossprod(a) > >where i is a set of integers selecting rows. When i is empty >the result is in a sense random. > >After some trials the right answer >(a matrix of zeros) appears. > >--------------- Illustration -------------------- >R : Copyright 2003, The R Development Core Team >Version 1.8.0 (2003-10-08) > > > >>A <-matrix(0, 5, 5) >>i <- c() >>a <- A[i, ] ; crossprod(a) >> >> > [,1] [,2] [,3] [,4] [,5] >[1,] 6.578187e-313 NaN NaN NaN NaN >[2,] NaN 1.273197e-313 NaN 1.485397e-313 NaN >[3,] NaN 4.243992e-313 2.121996e-314 NaN NaN >[4,] NaN 1.697597e-313 NaN 4.880590e-313 NaN >[5,] 5.941588e-313 NaN NaN 1.697597e-313 NaN > > >>a <- A[i, ] ; crossprod(a) >> >> > [,1] [,2] [,3] [,4] [,5] >[1,] 2.121996e-314 5.729389e-313 NaN NaN NaN >[2,] NaN NaN NaN NaN 1.909796e-313 >[3,] 2.970794e-313 NaN NaN NaN NaN >[4,] NaN NaN NaN 8.487983e-314 NaN >[5,] NaN 6.365987e-313 2.546395e-313 NaN NaN > > >>a <- A[i, ] ; crossprod(a) >> >> > [,1] [,2] [,3] [,4] [,5] >[1,] NaN 1.485397e-313 NaN NaN 2.970794e-313 >[2,] 3.182994e-313 NaN NaN 1.060998e-313 NaN >[3,] NaN NaN NaN 1.697597e-313 2.737375e-312 >[4,] NaN NaN NaN NaN 2.048394e+10 >[5,] NaN NaN NaN NaN 2.970794e-313 > > >>a <- A[i, ] ; crossprod(a) >> >> > [,1] [,2] [,3] [,4] [,5] >[1,] 1.591383e-266 20489834629 0 0 0 >[2,] 5.031994e-266 0 0 0 0 >[3,] 1.591205e-266 0 0 0 0 >[4,] 1.264128e-267 0 0 0 0 >[5,] 1.037656e-311 0 0 0 0 > > >>a <- A[i, ] ; crossprod(a) >> >> > [,1] [,2] [,3] [,4] [,5] >[1,] 0 0 0 0 0 >[2,] 0 0 0 0 0 >[3,] 0 0 0 0 0 >[4,] 0 0 0 0 0 >[5,] 0 0 0 0 0 >--------------- End of illustration------------ > >The same problem does not appear using the matrix product: > > > >>a <- A[i, ] ; t(a) %*% a >> >> > [,1] [,2] [,3] [,4] [,5] >[1,] 0 0 0 0 0 >[2,] 0 0 0 0 0 >[3,] 0 0 0 0 0 >[4,] 0 0 0 0 0 >[5,] 0 0 0 0 0 > >Note that Splus 6 returns an error message: > > > >>a <- A[i, ] ; crossprod(a) >> >> > >Problem in .Fortran.ok.Internal(if(cmplx) "zcrossp1"..: subroutine dcrossp1: >Argument 1 has zero length > > >Thank you, > >Giovanni > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help > >
Somehow R creates `a' as a matrix with 0 rows and 5 columns. I don't know how crossprod() or other linear algebra functions deals with such a degenerate matrix. I'd suggest R Core to add checks for strictly positive dimensions in such functions. (Also, I find it strange that A[1,] is a vector, but A[numeric(0),] is a 0x5 matrix...) Andy> From: Giovanni Marchetti [mailto:gmm at ds.unifi.it] > > Dear R-users, > > I found a strange problem > working with products of two matrices, say: > > a <- A[i, ] ; crossprod(a) > > where i is a set of integers selecting rows. When i is empty > the result is in a sense random. > > After some trials the right answer > (a matrix of zeros) appears. > > --------------- Illustration -------------------- > R : Copyright 2003, The R Development Core Team > Version 1.8.0 (2003-10-08) > > > A <-matrix(0, 5, 5) > > i <- c() > > a <- A[i, ] ; crossprod(a) > [,1] [,2] [,3] [,4] [,5] > [1,] 6.578187e-313 NaN NaN NaN NaN > [2,] NaN 1.273197e-313 NaN 1.485397e-313 NaN > [3,] NaN 4.243992e-313 2.121996e-314 NaN NaN > [4,] NaN 1.697597e-313 NaN 4.880590e-313 NaN > [5,] 5.941588e-313 NaN NaN 1.697597e-313 NaN > > a <- A[i, ] ; crossprod(a) > [,1] [,2] [,3] [,4] > [,5] > [1,] 2.121996e-314 5.729389e-313 NaN NaN > NaN > [2,] NaN NaN NaN NaN > 1.909796e-313 > [3,] 2.970794e-313 NaN NaN NaN > NaN > [4,] NaN NaN NaN 8.487983e-314 > NaN > [5,] NaN 6.365987e-313 2.546395e-313 NaN > NaN > > a <- A[i, ] ; crossprod(a) > [,1] [,2] [,3] [,4] [,5] > [1,] NaN 1.485397e-313 NaN NaN 2.970794e-313 > [2,] 3.182994e-313 NaN NaN 1.060998e-313 NaN > [3,] NaN NaN NaN 1.697597e-313 2.737375e-312 > [4,] NaN NaN NaN NaN 2.048394e+10 > [5,] NaN NaN NaN NaN 2.970794e-313 > > a <- A[i, ] ; crossprod(a) > [,1] [,2] [,3] [,4] [,5] > [1,] 1.591383e-266 20489834629 0 0 0 > [2,] 5.031994e-266 0 0 0 0 > [3,] 1.591205e-266 0 0 0 0 > [4,] 1.264128e-267 0 0 0 0 > [5,] 1.037656e-311 0 0 0 0 > > a <- A[i, ] ; crossprod(a) > [,1] [,2] [,3] [,4] [,5] > [1,] 0 0 0 0 0 > [2,] 0 0 0 0 0 > [3,] 0 0 0 0 0 > [4,] 0 0 0 0 0 > [5,] 0 0 0 0 0 > --------------- End of illustration------------ > > The same problem does not appear using the matrix product: > > > a <- A[i, ] ; t(a) %*% a > [,1] [,2] [,3] [,4] [,5] > [1,] 0 0 0 0 0 > [2,] 0 0 0 0 0 > [3,] 0 0 0 0 0 > [4,] 0 0 0 0 0 > [5,] 0 0 0 0 0 > > Note that Splus 6 returns an error message: > > > a <- A[i, ] ; crossprod(a) > > Problem in .Fortran.ok.Internal(if(cmplx) "zcrossp1"..: > subroutine dcrossp1: > Argument 1 has zero length > > > Thank you, > > Giovanni > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo> /r-help >