From radford at cs.toronto.edu  Mon Aug 11 19:48:25 2014
From: radford at cs.toronto.edu (Radford Neal)
Date: Mon, 11 Aug 2014 13:48:25 -0400
Subject: [Rd] diag(x, n) not preserving integer and logical x
Message-ID: <20140811174825.GA2543@cs.toronto.edu>

Martin Maechler wrote:
> diag(x) preserves the storage mode of x  for  'complex' and
> 'double' precision,  but converts integer and logicals to double :

Duncan Murdoch wrote:
> I think the change to preserve integer makes sense, but preserving
> logical does not.  A diagonal matrix has zeros off the diagonal, and
> they are not logical.  Having diag() sometimes return a matrix with
> FALSE off the diagonal just looks wrong.

Making diag(1:10) return an integer matrix does make sense, but it
could still be undesirable.  Often, the user will have intended to
get a real matrix, and will after another operation, but only after an
unnecessary coercion is done. For example: x<-diag(1:10); x[1,2]<-2.0

I personally think keeping diag(1:10) integer is best, but I thought 
I'd point out the contrary argument.

Having diag(rep(TRUE,10)) return a logical matrix with FALSE off the
diagonal does not strike me as too odd.  It makes sense if you think
of (FALSE, TRUE) as forming a field under operations of sum=XOR and
product=AND.  And of course it will typically be converted to a 0/1
matrix later if that's what was actually intended.

   Radford Neal



[Rd] diag(x, n) not preserving integer and logical x
Radford Neal