[R] matrix conformity with matrix 1x1 and scalars

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Aug 23 13:02:14 CEST 2005


On Tue, 23 Aug 2005, Andres Legarra wrote:

> Hello,
> I am calculating this thing with vectors (b) and matrices (G,P):
> b'G/sqrt(b'Pb)
> where the denominator is a quadratic form and therefore always a scalar.
>
> In Scilab, it is quite simple:
> b'*G/sqrt(b'*P*b)
> However, in R, the denominator is an (1x1)matrix and R claims it is non
> conformable and I have to use drop() or as.numeric(). Like this:
>> b = 1:2
>> G=diag(1,2)
>> P=diag(2,2)
>> (t(b)%*%G) / drop( sqrt( t(b)%*%P%*%b ) )
>          [,1]      [,2]
> [1,] 0.3162278 0.6324555
>
> So far, so good. My problem is solved. However I found a little bit annoying
> that R is not so "clever" as to realize that b'Pb can be interpreted as a
> scalar. I wonder :
> would it be worth considering the implementation in R of
> "recycling 1x1 matrix to scalars if appropriate"?
> Just to leave the question on the ground...

This "clever"ness often leads to running code that the users did not 
intend to work that way and so give misleading answers.  In retrospect 
many of us think R/S should be less "clever" than it is, since guessing 
the minds of end-users is a dangerous pursuit.  In particular, as a recent 
posting on operator precedence shows, some users will have very different 
preconceptions from the developers, _and_ blame the developers for not 
having their perconceptions.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list