tensor_predictors/tensorPredictors/R/dist_projection.R

28 lines
761 B
R

#' Porjection Distance of two matrices
#'
#' Defined as sine of the maximum principal angle between the column spaces
#' of the matrices
#' max{ sin theta_i, i = 1, ..., min(d1, d2) }
#'
#' @param A,B matrices of size \eqn{p\times d_1} and \eqn{p\times d_2}.
#'
#' @export
dist.projection <- function(A, B, is.ortho = FALSE,
tol = sqrt(.Machine$double.eps)
) {
if (!is.ortho) {
qrA <- qr(A, tol)
A <- qr.Q(qrA)[, seq_len(qrA$rank), drop = FALSE]
qrB <- qr(B, tol)
B <- qr.Q(qrB)[, seq_len(qrB$rank), drop = FALSE]
}
if (ncol(A) == 0L && ncol(B) == 0L) {
0
} else if (ncol(A) == 0L || ncol(B) == 0L) {
1
} else {
sin(acos(min(c(La.svd(crossprod(A, B), 0, 0)$d, 1))))
}
}