[R] Error in the inverse of a diagonal matrix?

Göran Broström gb at stat.umu.se
Mon Feb 14 10:35:21 CET 2000


On Mon, 14 Feb 2000, Manuel Castejon Limas wrote:

> I´m new to R so maybe this issue has been asked before and I still could not read the complete set of past messages sent to the list.
> I found a weird behabiour that I will explain with a simple example. Lets consider the following block of commands:
> 
> > x <- diag(c(1,4,10))
> > x
>      [,1] [,2] [,3]
> [1,]    1    0    0
> [2,]    0    4    0
> [3,]    0    0   10
> 
> > invx <- x^-1
> > invx
>      [,1] [,2] [,3]
> [1,]    1  Inf  Inf
> [2,]  Inf 0.25  Inf
> [3,]  Inf  Inf  0.1
> 
> I would really appreciate if the result would have been
>      [,1] [,2] [,3]
> [1,]  1    0    0
> [2,]  0  0.25  0
> [3,]  0    0  0.1
> 
> most of all because
> > x %*% invx
>      [,1] [,2] [,3]
> [1,]  NaN  NaN  NaN
> [2,]  NaN  NaN  NaN
> [3,]  NaN  NaN  NaN
> 
> does not really satisfies me :-)
> 
> I´m using version 0.90.1.
> 
> Is this what it was meant to be?
> Is this a known bug?
> Am I doing something wrong?
> Anybody knows a workaround (for a general matrix, not for a diagonal one)?
> 
> I would really appreciate your help and support.
> Thanks in advance.
> 
> 
> -------------------------------------------------------------------------------------------------------------------------------------------------------
>                           Manuel Castejon Limas.
>                      Manuel.Castejon at dim.unirioja.es 
> Departamento de Ingenieria Mecanica.   | Mechanical Engineering Department.
> Area de Proyectos de Ingenieria.       | Project Management Area.
> Universidad de La Rioja.               | University of La Rioja.
> España.                                | Spain.
> -------------------------------------------------------------------------------------------------------------------------------------------------------

You should use  solve(x) instead of x^-1, which inverts element-wise.

Göran
----------------------------------------------------------
 Göran Broström                      tel: +46 90 786-5223
 Department of Statistics            fax: +46 90 786-6614
 Umeå University                                         
 SE-90187 Umeå, Sweden              email: gb at stat.umu.se
                                                        
 http://www.stat.umu.se/egna/gb    ftp://capa.stat.umu.se
----------------------------------------------------------

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list