# Kappa condition number in R

I have read that the kappa function in R does not always explicitly calculate the condition number of a matrix, but rather, estimates the 2 norm of a matrix or a QR decomposition (see here). I was just wondering if anyone knows what the approximation used here is and has any idea where I can read more about the math of the underlying algorithm behind kappa?

Update: As user yarnabrina has pointed out, the functions in the R source code I am particularly interested in are .kappa_tri and kappa.qr. Thanks again to yarnabrina and also any future answers!

• There's no function Kappa in base R. Did you mean kappa? – yarnabrina May 13 '19 at 3:57
• I have corrected this now yes. Apologies. – JDoe2 May 13 '19 at 11:56
• thanks for awarding the bounty, but I'm surprised. I didn't really answer your question. – yarnabrina May 23 '19 at 9:22
• Yeah but it was the best anyone did and the bounty would have expired! Cheers again. – JDoe2 May 23 '19 at 10:41

From the documentation of kappa, you can use exact = TRUE, and it'll use SVD for the exact calculation.

For kappa(), if exact = FALSE (the default) the 2-norm condition number is estimated by a cheap approximation. However, the exact calculation (via svd) is also likely to be quick enough.

For details of the method, you can check the source code, available at here. Here's a part from that:

kappa.default <- function(z, exact = FALSE,
norm = NULL, method = c("qr", "direct"), ...)
{
method <- match.arg(method)
z <- as.matrix(z)
norm <- if(!is.null(norm)) match.arg(norm, c("2", "1","O", "I")) else "2"
if(exact && norm == "2") {
s <- svd(z, nu = 0, nv = 0)\$d
max(s)/min(s[s > 0])
}
else { ## exact = FALSE or norm in "1", "O", "I"
if(exact)
warning(gettextf("norm '%s' currently always uses exact = FALSE",
norm))
d <- dim(z)
if(method == "qr" || d[1L] != d[2L])
kappa.qr(qr(if(d[1L] < d[2L]) t(z) else z),
exact = FALSE, norm = norm, ...)
else .kappa_tri(z, exact = FALSE, norm = norm, ...)
}
}

• Ah okay I see thank you... apologies I was really hoping to understand the approximations kappa_qr and kappa_tri? I guess the only way to see is to look at the source code? I find it hard to tell what is going on here as they call to fortran in this code it seems. – JDoe2 May 13 '19 at 11:53
• @JDoe2 I don't know Fortran myself, sorry. What I understand from the link is that kappa.qr calls .kappa_tri, and hence I don't follow why do you want to find their difference. – yarnabrina May 14 '19 at 16:36
• No worries, thank you for your time though! Do you know what .kappa_tri does though? I only wish to know what the methods are doing - at the moment I'm not really sure on either! Thanks again though! – JDoe2 May 15 '19 at 11:19