[Rd] Why is the diag function so slow (for extraction)?

luke-tierney at uiowa.edu luke-tierney at uiowa.edu
Tue May 5 15:46:45 CEST 2015


Looks like the c(x)[...] bit used to be as.matrix(x)[...]. Not sure
why the change was made many years ago, but this was before names were
handled explicitly. It would definitely be better to not force the
duplicate, at least in the case where we are sure c() and [ would not
dispatch.

Best,

luke

On Mon, 4 May 2015, peter dalgaard wrote:

>
>> On 04 May 2015, at 19:59 , franknarf <by.hook.or at gmail.com> wrote:
>>
>> But I'm still wondering why diag() uses c()...? With it being so slow, I'd
>> be inclined to write a qdiag() without the c() and just use that the next
>> time I need matrix algebra. Any insight would be appreciated; thanks!
>
> Well, there are two possibilities: Either it is deliberate or it isn't.
>
> The latter isn't too unlikely, given that the effect is seen for large matrices. I would appear to be a matter of O(n) (picking out n items) vs. O(n^2) (copying an n x n matrix), but this might drown out in a context involving matrix multiplication and/or inversion, both of which are O(n^3).
>
> If it is deliberate, the question is why. There could be devils in the details; notice in particular that c() strips off non-name attributes. However, I'm not aware of a situation where such attributes could cause trouble.
>
> -pd
>
>

-- 
Luke Tierney
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa                  Phone:             319-335-3386
Department of Statistics and        Fax:               319-335-3017
    Actuarial Science
241 Schaeffer Hall                  email:   luke-tierney at uiowa.edu
Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu



More information about the R-devel mailing list