From pd@lgd @ending from gm@il@com  Tue Sep 18 15:00:29 2018
From: pd@lgd @ending from gm@il@com (peter dalgaard)
Date: Tue, 18 Sep 2018 15:00:29 +0200
Subject: [Rd] diag(-1) produces weird result
In-Reply-To: <CANVKczMh3VTThqh_oaO_RysaW+yoq4d+EUNX4EN4qFSOAHd13w@mail.gmail.com>
References: <C9E6B4F9-DA68-457F-A99A-1D371E4BDE93@revelle.net>
 <8afc0f794d614405957feb241d04765f@LNXP265MB1290.GBRP265.PROD.OUTLOOK.COM>
 <CANVKczMh3VTThqh_oaO_RysaW+yoq4d+EUNX4EN4qFSOAHd13w@mail.gmail.com>
Message-ID: <CD2E0BDE-66DD-4374-B4A2-028B47B7ED07@gmail.com>

Yes, both are rooted in age-old design infelicities (in which, basically, interactive expedience has taken precedence over consistency and generality). 

Unfortunately, they are quite difficult to rectify, because there are bound to be countless uses of, say, diag(5) as a 5x5 identity matrix which would break if it suddenly meant the 1x1 matrix(5) instead.

We'd need a very carefully orchestrated warn-deprecate-defunct-newBehaviour sequence, with a time scale of years, most likely. It is, in principle, doable (I think), but we don't really have the mechanisms to follow through on it.  Almost all developers have main job responsibilities, and it is very easy to get sidetracked at the wrong moment, so most changes get done on a now-or-never basis. 

I have toyed with the idea of setting up for version-dependent code where code sections could be coded up front and then activated when the relevant version is reached. Then I got swamped again...

-pd

> On 17 Sep 2018, at 20:08 , Barry Rowlingson <b.rowlingson at lancaster.ac.uk> wrote:
> 
> On Mon, Sep 17, 2018 at 5:22 PM, G?bor Cs?rdi <csardi.gabor at gmail.com>
> wrote:
> 
>> I would say it is a mis-feature. If the 'x' argument of diag() is a
>> vector of length 1, then it creates an identity matrix of that size,
>> instead of creating a 1x1 matrix with the given value:
>> 
>> ? diag(3)
>>     [,1] [,2] [,3]
>> [1,]    1    0    0
>> [2,]    0    1    0
>> [3,]    0    0    1
>> 
>> Of course this makes it cumbersome to use diag() in a package, when
>> you are not sure if the input vector is longer than 1. This seems to
>> be a good workaround:
>> 
>> ? diag(-1, nrow = 1)
>>     [,1]
>> [1,]   -1
>> 
>> Or, in general if you have vector v:
>> 
>> ? v <- -1
>> ? diag(v, nrow = length(v))
>>     [,1]
>> [1,]   -1
>>> 
>> 
> 
> Anyone else getting deja-vu with the `sample` function?
> 
>> sample(5:3)
> [1] 3 5 4
> 
> ok...
> 
>> sample(5:4)
> [1] 4 5
> 
> fine...
> 
>> sample(5:5)
> [1] 3 1 5 2 4
> 
> uh oh. Documented, of course.
> 
> B
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



[Rd] diag(-1) produces weird result
peter dalgaard