R/matrix_inverse_plot.R

Defines functions plot_matrix_det plot_matrix_inverse

Documented in plot_matrix_inverse

#' compstatslib plot_matrix_inverse() function
#' 
#' Non-interactive plotting function that helps visualize an inverse.
#' 
#' @param x1 The first row (or column) vector of matrix A.
#'
#' @param y1 The second row (or column) vector of matrix A.
#' 
#' @param x2 The first row (or column) vector of the inverse matrix A^(-1).
#' 
#' @param y2 The second row (or column) vector of the inverse matrix A^(-1).
#' 
#' @usage 
#' plot_matrix_inverse(x1, y1, x2, y2)
#' 
#' The user can choose the magnitude of the vectors, which will be graphically represented by the function.
#'
#' @seealso \code{\link{interactive_matrix_inverse}}
#'
#' @export
plot_matrix_inverse <- function(x1, y1, x2, y2) {
  A <- matrix(c(x1, y1, x2, y2), nrow = 2)
  Ainv <- solve(A)
  
  A_col <- rgb(1, 0, 0, 0.1)
  Ainv_col <- rgb(0, 0, 1, 0.1)
  
  plot(NA, xlim=c(-3, 3), ylim=c(-3,3), frame.plot = FALSE)
  plot_matrix_det(A, A_col)
  plot_matrix_det(Ainv, Ainv_col)
}

plot_matrix_det <- function(M, col) {
  polygon(c(0,M[1,1], M[1,1]+M[1,2], M[1,2]), 
          c(0,M[2,1], M[2,1]+M[2,2], M[2,2]), 
          col=col, border=rgb(0,0,0,0.3))
  
  arrows(0, 0, M[1,1], M[2,1])
  arrows(0, 0, M[1,2], M[2,2])
}
soumyaray/compstatslib documentation built on March 11, 2023, 8:39 p.m.