From p.dalgaard at biostat.ku.dk  Thu May 14 22:42:11 2009
From: p.dalgaard at biostat.ku.dk (Peter Dalgaard)
Date: Thu, 14 May 2009 22:42:11 +0200
Subject: [Rd] diag() has a bug (PR#13702)
In-Reply-To: <CBE46617-5AFB-4EA5-AA67-C1D07FFB0052@jhsph.edu>
References: <20090514134509.58502282BE82@mail.pubhealth.ku.dk>	<47C259F5-83A3-4476-8E23-D67018FE91A8@jhsph.edu>	<1901f69e0905141230l79d71494ra985e767b08b828d@mail.gmail.com>
	<CBE46617-5AFB-4EA5-AA67-C1D07FFB0052@jhsph.edu>
Message-ID: <4A0C8223.2000604@biostat.ku.dk>

Benilton Carvalho wrote:
> in that case, wouldn't
> 
> diag(diag(foo))
> 
> suffice?

Yes (beware the length 1 case, though).

However, don't delete the bug report. That error message is just wrong:

     if (is.array(x) && length(dim(x)) != 1L)
         stop("first argument is array, but not matrix.")


> 
> b
> 
> On May 14, 2009, at 4:30 PM, Michael Spiegel wrote:
> 
>> The intent of diag(foo, 2, 2) was to return a matrix that is a 2 x 2 
>> matrix which contains only the diagonal entries of the matrix foo.  I 
>> can't do diag(foo) because this returns a vector.  I could reach my 
>> goal with matrix(diag(foo), nrow = nrow(foo), ncol = ncol(foo)).  If 
>> this is not a reasonable thing for diag(<matrix>, <numeric>, 
>> <numeric>) to return, then at the very least the error message should 
>> be changed, as the first argument is in fact a matrix.
>>
>> On Thu, May 14, 2009 at 3:24 PM, Benilton Carvalho 
>> <bcarvalh at jhsph.edu> wrote:
>> My understanding is that providing nrow and ncol, you want to create a 
>> diagonal matrix with those dimensions.
>>
>> diag(pi, 6, 6)
>>
>> and that by
>>
>> diag(foo, 2, 2)
>>
>> you really meant
>>
>> diag(foo)[2]
>>
>> Apologies if I misunderstood.
>>
>> b
>>
>> On May 14, 2009, at 10:45 AM, michael.m.spiegel at gmail.com wrote:
>>
>> Full_Name: Michael Spiegel
>> Version: 2.9.0
>> OS: linux
>> Submission from: (NULL) (204.111.252.142)
>>
>>
>> The diag() function appears to reject the first argument when it is a 
>> matrix,
>> and nrow and ncol arguments are also provided.
>>
>> foo <- matrix(c(1:4),2,2)
>> foo
>>    [,1] [,2]
>> [1,]    1    3
>> [2,]    2    4
>> diag(foo)
>> [1] 1 4
>> diag(foo, 2, 2)
>> Error in diag(foo, 2, 2) : first argument is array, but not matrix.
>> is.matrix(foo)
>> [1] TRUE
>>
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>>
> 
> 
> ------------------------------------------------------------------------
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel


-- 
    O__  ---- Peter Dalgaard             ?ster Farimagsgade 5, Entr.B
   c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
  (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)              FAX: (+45) 35327907



[Rd] diag() has a bug (PR#13702)
Peter Dalgaard