From bates at stat.wisc.edu  Mon Oct 12 14:42:33 2009
From: bates at stat.wisc.edu (Douglas Bates)
Date: Mon, 12 Oct 2009 07:42:33 -0500
Subject: [Rd] unexpected behaviour of isSymmetric() (PR#14000)
In-Reply-To: <4AD315DF.9050702@dbmail.com>
References: <20091012000511.C97B2282EF50@mail.pubhealth.ku.dk>
	<4AD315DF.9050702@dbmail.com>
Message-ID: <40e66e0b0910120542j41bd8a4ahb86fee7f623ae8b2@mail.gmail.com>

On Mon, Oct 12, 2009 at 6:41 AM, Romain Francois
<romain.francois at dbmail.com> wrote:
> On 10/12/2009 02:05 AM, mike at stat.ubc.ca wrote:
>>
>> Full_Name: Mike Danilov
>> Version: 2.9.0
>> OS: Fedora Core 9
>> Submission from: (NULL) (142.103.121.198)
>>
>>
>> When checking for the symmetry of a matrix, function isSymmetric.matrix()
>> gets
>> confused by the discrepancy of colnames/rownames if its argument. See the
>> code
>> snippet below. Perhaps it's a problem of the matrix product which copies
>> colnames of the first argument but not the rownames of the second to its
>> value.
>> Not sure which one should be fixed but the way it is now it seems
>> illogical that
>> X'X is deemed to be non-symmetric.
>>
>> x<- c(1,2,3)
>> names(x)<- c("v1","v2","v3")
>> isSymmetric(x%*%t(x)) ## returns FALSE instead of TRUE
>
> It seems to be concerned with the names
>
>> y <- x %*% t(x)
>> y
> ? ? v1 v2 v3
> [1,] ?1 ?2 ?3
> [2,] ?2 ?4 ?6
> [3,] ?3 ?6 ?9
>
>> isSymmetric( y )
> [1] FALSE
>
> # dropping the dimnames
>> isSymmetric( structure( y, dimnames = NULL ) )
> [1] TRUE
>
> # pass the ... along this path : isSymmetric > all.equal > all.equal.numeric
>> isSymmetric( y, check.attributes = F )
> [1] TRUE
>
> # set the dimnames equal
>> dimnames( y ) <- rep( list( names(x) ), 2 )
>> isSymmetric( y )
> [1] TRUE
>
> Not sure this is expected behaviour
>
> Romain

I think the problem is more with the propagation of the column names
in the construction x %*% t(x).  If you use the tcrossprod function to
create x %*% t(x) more carefully then the results are sensible


> x<- c(1,2,3); names(x)<- c("v1","v2","v3")
> isSymmetric(tcrossprod(x))
[1] TRUE
> tcrossprod(x)
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    2    4    6
[3,]    3    6    9



[Rd] unexpected behaviour of isSymmetric() (PR#14000)
Douglas Bates