Hi, Does anybody know, what is going on here?> diag(sqrt(1))[,1] [1,] 1> diag(sqrt(0.3333))<0 x 0 matrix>> sqrt(1)[1] 1> sqrt(0.3333)[1] 0.5773214 BR, Markku Karhunen researcher University of Helsinki
On 09/04/2009 12:25 PM, Markku Karhunen wrote:> > Hi, > > Does anybody know, what is going on here? > >> diag(sqrt(1)) > [,1] > [1,] 1 >> diag(sqrt(0.3333)) > <0 x 0 matrix> >> sqrt(1) > [1] 1 >> sqrt(0.3333) > [1] 0.5773214 > > BR, Markku Karhunen > researcher > University of HelsinkiTry this instead; > diag( sqrt(.3333), 1, 1 ) [,1] [1,] 0.5773214 Romain -- Romain Francois Professional R Enthusiast +33(0) 6 28 91 30 30 http://romainfrancois.blog.free.fr |- http://tr.im/xMdt : update on the ant package |- http://tr.im/xHLs : R capable version of ant `- http://tr.im/xHiZ : Tip: get java home from R with rJava
Markku Karhunen wrote:> Hi, > > Does anybody know, what is going on here? >diag( x ) produces a round(x) x round(x) identity matrix when x is length 1. (This is the third case listed on the man page ?diag). See the note there about a safer form if you wanted a matrix with x on the diagonal. Duncan Murdoch> >> diag(sqrt(1)) >> > [,1] > [1,] 1 > >> diag(sqrt(0.3333)) >> > <0 x 0 matrix> > >> sqrt(1) >> > [1] 1 > >> sqrt(0.3333) >> > [1] 0.5773214 > > BR, Markku Karhunen > researcher > University of Helsinki > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
On Fri, Sep 4, 2009 at 11:25 AM, Markku Karhunen<markku.karhunen at helsinki.fi> wrote:> Hi, > > Does anybody know, what is going on here? > >> diag(sqrt(1)) > > ? ? [,1] > [1,] ? ?1 >> >> diag(sqrt(0.3333)) > > <0 x 0 matrix> >> >> sqrt(1) > > [1] 1 >> >> sqrt(0.3333) > > [1] 0.5773214 >Read the help for diag yet? 'diag' has four distinct usages: ... 3. 'x' is a scalar (length-one vector) and the only argument it a square identity matrix of size given by the scalar. ... So diag(0.1) becomes diag(0) which is a 0-size matrix. Try diag(2.4) Barry
it's documented as "unexpected" ?diag Note Using diag(x) can have unexpected effects if x is a vector that could be of length one. Use diag(x, nrow = length(x)) for consistent behaviour. And the result follows from this part, else if (length(x) == 1L && nargs() == 1L) { n <- as.integer(x) x <- 1 } baptiste 2009/9/4 Markku Karhunen <markku.karhunen@helsinki.fi>> Hi, > > Does anybody know, what is going on here? > > diag(sqrt(1)) >> > [,1] > [1,] 1 > >> diag(sqrt(0.3333)) >> > <0 x 0 matrix> > >> sqrt(1) >> > [1] 1 > >> sqrt(0.3333) >> > [1] 0.5773214 > > BR, Markku Karhunen > researcher > University of Helsinki > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- _____________________________ Baptiste AuguiƩ School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK http://newton.ex.ac.uk/research/emag ______________________________ [[alternative HTML version deleted]]
> On 04-Sep-09 10:45:27, Markku Karhunen wrote: >> True. Should have read ?diag. >> >> However, this provokes a more general question: Is there some way I >> can declare some scalar and _all its functions_ as matrices? >> >> For instance, I would like to >> >> A = as.matrix(0.98) >> B = function(A) >> C = diag(sqrt(B)) >> >> so that all scalars are explicitly [1,1] matrices. >> BR, Markku > > Hmmm, it might be a good idea to explain why you want to do this. > For instance: > > M <- matrix(c(1,2,3,4),nrow=2) > c <- matrix(2,nrow=1) > c%*%M > # Error in c %*% M : non-conformable arguments > c*M > # Error in c * M : non-conformable arrays > c+M > # Error in c + M : non-conformable arrays > > So what would you want to use the [1,1]-matrix scalars for, that > cannot be done just using them as numbers? > > Ted.Broadly speaking, I would like to use the same code for multivariate and univariate cases. For instance, I use the inverse Wishart densities of MCMCpack. If I take diwish(x) of a scalar x, the programme crashes, because diwish() by default checks ncol(x)==nrow(x). However, I would like to have an inverse gamma density. Best, Markku