R/pdet.R

Defines functions pdet

Documented in pdet

#' pdet
#'
#' Compute the determinant of a pauli or pauli_d object.
#'
#' @param pauli A pauli object, a pauli_d object, or a 2 x 2 data frame with numeric values
#'
#' @return A list of determinants (on for each data frame in pauli object) with
#' class attribute set to pdet
#' @export
#'
#' @examples
#' pf <- pauli()
#' pdet(pf)
pdet <- function(pauli){

  #Convert data frames to matrices
  mat <- lapply(pauli, as.matrix)

  #Write function to get det of one matrix
  get_det <- function(x){
    if (is.complex(x[1, 1]) || is.complex(x[1, 2]) ||
        is.complex(x[2, 1]) || is.complex(x[2, 1])){
      det0 <- unname(x[1, 1]*x[2, 2] - x[1, 2]*x[2, 1])

      #Coerce det to real if complex with 0i
      if (is.complex(det0) && Im(det0) == 0){
        det0 <- Re(det0)
      }
    } else {
      det0 <- det(x)
    }

    return(det0)
  }

  #Get determinant across list
  det_frame <- lapply(mat, get_det)

  #Set attributes and return pdet object
  structure(det_frame,
            class = "pdet"
  )
}
mncube/pauliframes documentation built on Dec. 21, 2021, 8:07 p.m.