#' @include multivariate.data.frame.R
#' @include multivariate.R
#' @include covv.R
#'
NULL

..trace = function(X) sum(diag(X))

..covls = function(...,tcp) {

  Xs <- list(...)
  if (length(Xs)==1) {
    x = Xs[[1]]
    if (is.multivariate.data.frame(x))
      Xs=x
    else if (is.pm(x))
      return(x$cov)
    else
      Xs=multivariate.data.frame(x)
  }
  else
      Xs=as.multivariate.data.frame(Xs)

    Xnames=names(Xs)

    Xs <- project(Xs)
    Xs <- as.multivariate.data.frame(mapply(tcp,
                 Xs,
                 SIMPLIFY = FALSE))

    nX <- length(Xs)

    Xx <- rep(1:nX,nX)
    Xy <- rep(1:nX,rep(nX,nX))

    n  <- nrow(Xs)-1

    CovXXs <- mapply(function(x,y) ..trace(sqrtm(Xs[[x]] %*% Xs[y])),
                  Xx,Xy,
                  SIMPLIFY = TRUE) / n


    dim(CovXXs)=c(nX,nX)
    colnames(CovXXs)=Xnames
    rownames(CovXXs)=Xnames

    return(CovXXs)
}

..rls = function(...,cov) {
  COV = cov(...)
  s = sqrt(diag(COV))
  VV= outer(s,s)
  return(COV/VV)
}

..rls.partial = function(...,r) {
  C = solve(r(...))
  D = sqrt(diag(C) %o% diag(C))
  return(C/D)
}


#' Compute the variance, covariance matrix of K coordinate matrices.
#'
#' Covariance between two matrices is defined as the sum of the
#' sigular values of the X'Y matrix. All the matrices must have
#' the same number of rows.
#'
#' @param ... the set of matrices
#'
#' @examples
#'    # Build Three matrices of 3 rows.
#'    A <- matrix(1:9,nrow=3)
#'    B <- matrix(10:15,nrow=3)
#'    C <- matrix(20:31,nrow=3)
#'    # compute the variance covariance matrix
#'    covls(A,B,C)
#'    covls(A=A,B=B,C=C)
#'
#' @author Eric Coissac & Christelle Gonindard-Melodelima
#' @export
covls = function(...) {
  ..covls(...,tcp = ..tcrossprod)
}


#' Compute the variance, covariance matrix of K coordinate matrices.
#'
#' Covariance between two matrices is defined as the sum of the
#' sigular values of the X'Y matrix. All the matrices must have
#' the same number of rows.
#'
#' @param ... the set of matrices
#'
#' @examples
#'    # Build Three matrices of 3 rows.
#'    A <- matrix(1:9,nrow=3)
#'    B <- matrix(10:15,nrow=3)
#'    C <- matrix(20:31,nrow=3)
#'    # compute the variance covariance matrix
#'    covls2(A,B,C)
#'    covls2(A=A,B=B,C=C)
#'
#' @author Eric Coissac & Christelle Gonindard-Melodelima
#' @export
covls2 = function(...) {
  ..covls(...,tcp = ..tcrossprod_diag0)
}

#' Compute RV coefficient matrix of K coordinate matrices.
#'
#'
#'
#' @author Eric Coissac
#' @author Christelle Gonindard-Melodelima
#' @export
rls = function(...) {
  ..rls(...,cov=covls)
}

#' Compute RV2 coefficient matrix of K coordinate matrices.
#'
#'
#'
#' @author Eric Coissac
#' @author Christelle Gonindard-Melodelima
#' @export
rls2 = function(...) {
  ..rls(...,cov=covls2)
}


#' Compute the RV2 partial correlation matrix of K coordinate matrices
#'
#' @author Eric Coissac
#' @author Christelle Gonindard-Melodelima
#' @export
rls.partial = function(...) {
  ..rls.partial(...,r=rls)
}

#' Compute the RV2 partial correlation matrix of K coordinate matrices
#'
#' @author Eric Coissac
#' @author Christelle Gonindard-Melodelima
#' @export
rls2.partial = function(...) {
  ..rls.partial(...,r=rls2)
}


