det() {was "RE: [R] using MANOVA in R"}

Martin Maechler maechler at stat.math.ethz.ch
Thu May 11 11:56:43 CEST 2000


>>>>> Manuel Castejon Limas <manuel.castejon at dim.unirioja.es> writes:

      ...

    Manuel> # Let´s define some kind of determinant function

      >> det <- function (x)   { prod(eigen(x)$values) }

      ....


We have been there before (a year ago on R-devel, see "eigenvalue"),
and since then had the following in   ?qr :

>>    Examples:
>>
>>	 ## The determinant of a matrix  -- if you really must have it
>>	 det2 <- function(x) prod(diag(qr(x)$qr))*(-1)^(ncol(x)-1)


But since people seem to re-invent that teeny part of the wheel,
something like the following will be in R 1.1.x :

  ### From Doug Bates' 20 Apr 1999 post to R-devel;
  ### "method" idea from J.K.Lindsey's rmutil

  det <- function(x, method = c("qr","eigenvalues"))
  {
      if(!is.matrix(x))
	  stop("x must be a matrix")
      method <- match.arg(method) # ensures a method from above
      if(method == "qr")
	  prod(diag(qr(x)$qr)) *(-1)^(ncol(x)-1)
      else ## if(method == "eigenvalues")
	  Re(prod(eigen(x, only.values=TRUE)$values))
  }


    det                   package:base                   R Documentation

    Calculate the Determinant of a Matrix

    Description:

	 `det' calculates the determinant of a matrix either by `qr'
	 decomposition or from the `eigen'values.

    Usage:

	 det(x, method = c("qr","eigenvalues"))

    Arguments:

	   z: numeric matrix.

      method: `"qr"' (default) or `"eigenvalues"'

    Note:

	 Often, computing the determinant is not what you should be doing
	 to solve a given problem.

	 The `"qr"' method is much faster for large matrices.

    See Also:

	 `eigen', `qr', `svd'

    Examples:

	 (x <- matrix(1:4, ncol=2))
	 det(x)
	 det(x, method="eigenvalues")

	 det(print(cbind(1,1:3,c(2,0,1))))


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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