Re: [R] 0 x 0 matrix
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 __ 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.
Re: [R] 0 x 0 matrix
On 05-Sep-09 10:00:26, Markku Karhunen wrote: 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 I see. In such a case, it might be worth wrapping diwish() inside a function of your own, which tests for 'x' being a scalar and, if it is, converting it to a 1x1 matrix within the function. For example: diWish - function(x){ if( all.equal(dim(x),c(1,1)) ) {X - x} else if( (is.vector(x))(length(x)==1) ) X - as.matrix(x) diwish(X) } (This may not be optimal, but it gives the idea). Hoping this helps, Ted. E-Mail: (Ted Harding) ted.hard...@manchester.ac.uk Fax-to-email: +44 (0)870 094 0861 Date: 05-Sep-09 Time: 21:26:29 -- XFMail -- __ 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.
Re: [R] 0 x 0 matrix
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.)) 0 x 0 matrix sqrt(1) [1] 1 sqrt(0.) [1] 0.5773214 BR, Markku Karhunen researcher University of Helsinki Try this instead; diag( sqrt(.), 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 __ 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.
Re: [R] 0 x 0 matrix
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.)) 0 x 0 matrix sqrt(1) [1] 1 sqrt(0.) [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. __ 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.
Re: [R] 0 x 0 matrix
On Fri, Sep 4, 2009 at 11:25 AM, Markku Karhunenmarkku.karhu...@helsinki.fi wrote: Hi, Does anybody know, what is going on here? diag(sqrt(1)) [,1] [1,] 1 diag(sqrt(0.)) 0 x 0 matrix sqrt(1) [1] 1 sqrt(0.) [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 __ 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.
Re: [R] 0 x 0 matrix
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.karhu...@helsinki.fi Hi, Does anybody know, what is going on here? diag(sqrt(1)) [,1] [1,]1 diag(sqrt(0.)) 0 x 0 matrix sqrt(1) [1] 1 sqrt(0.) [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]] __ 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.
Re: [R] 0 x 0 matrix
baptiste auguie wrote: 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 } Looks like the man page is wrong: it says diag(0.9) should produce a 1x1 matrix, but as the code shows, it produces a 0x0 one. I'll fix it. Duncan Murdoch baptiste 2009/9/4 Markku Karhunen markku.karhu...@helsinki.fi Hi, Does anybody know, what is going on here? diag(sqrt(1)) [,1] [1,]1 diag(sqrt(0.)) 0 x 0 matrix sqrt(1) [1] 1 sqrt(0.) [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. __ 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. __ 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.
Re: [R] 0 x 0 matrix
Duncan Murdoch wrote: baptiste auguie wrote: 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 } Looks like the man page is wrong: it says diag(0.9) should produce a 1x1 matrix, but as the code shows, it produces a 0x0 one. I'll fix it. Oops, this has already been fixed. I was looking at an old version of R on this laptop. Duncan Murdoch Duncan Murdoch baptiste 2009/9/4 Markku Karhunen markku.karhu...@helsinki.fi Hi, Does anybody know, what is going on here? diag(sqrt(1)) [,1] [1,]1 diag(sqrt(0.)) 0 x 0 matrix sqrt(1) [1] 1 sqrt(0.) [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. __ 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. __ 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. __ 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.
Re: [R] 0 x 0 matrix
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 On Fri, Sep 4, 2009 at 11:25 AM, Markku Karhunenmarkku.karhu...@helsinki.fi wrote: Hi, Does anybody know, what is going on here? diag(sqrt(1)) [,1] [1,] 1 diag(sqrt(0.)) 0 x 0 matrix sqrt(1) [1] 1 sqrt(0.) [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 __ 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.
Re: [R] 0 x 0 matrix
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. E-Mail: (Ted Harding) ted.hard...@manchester.ac.uk Fax-to-email: +44 (0)870 094 0861 Date: 04-Sep-09 Time: 14:51:52 -- XFMail -- __ 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.