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

peter dalgaard pdalgd at gmail.com
Tue May 5 00:24:08 CEST 2015

> 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.


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

More information about the R-devel mailing list