## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.align = "center")
library(hlrhotrix)
library(ggplot2)

## -----------------------------------------------------------------------------
R <- make_R2(a11 = 5, a21 = 3, a12 = 6, a22 = 2)
print(R)

## -----------------------------------------------------------------------------
# Example 3.2 of the manuscript
A <- make_R4(
  a11 = -2,
  a21 =  3, c11 = 4,  a12 = 1,
  a31 =  1, c21 = 2,  c12 = -1, a13 = 0,
  a32 =  1, c22 = 1,  a23 = 0,
  a33 =  3
)
print(A)

## -----------------------------------------------------------------------------
R6 <- make_R6(
  a11=1, a21=2, c11=3, a12=4,
  a31=5, c21=6, a22=7, c12=8, a13=9,
  a41=2, c31=3, a32=4, a23=5, c13=6, a14=7,
  a42=1, c32=2, a33=3, c23=4, a24=5,
  a43=6, c33=7, a34=8, a44=9
)
print(R6)

## -----------------------------------------------------------------------------
det_hl(R)    # -8   (Example 3.3)
det_hl(A)    # -36  (Example 3.2)
det_hl(R6)   # 15360

## -----------------------------------------------------------------------------
B <- make_R4(
  a11=-2, a21=1, c11=4, a12=3,
  a31=0, c21=-1, c12=2, a13=1,
  a32=0, c22=1, a23=1, a33=3
)
det_hl(B) == -det_hl(A)

## -----------------------------------------------------------------------------
adj_hl(R)
adj_hl(A)

## -----------------------------------------------------------------------------
inv_hl(R)
inv_hl(A)

## -----------------------------------------------------------------------------
Ai <- inv_hl(A)
for (k in seq_along(A$A)) {
  Ak <- matrix(c(A$A[[k]]["diag","col1"], A$A[[k]]["sym","col1"],
                 A$A[[k]]["sym","col2"],  A$A[[k]]["diag","col2"]), nrow=2)
  Ik <- matrix(c(Ai$A[[k]]["diag","col1"], Ai$A[[k]]["sym","col1"],
                 Ai$A[[k]]["sym","col2"],  Ai$A[[k]]["diag","col2"]), nrow=2)
  cat(sprintf("Block A%d * inv(A)%d:\n", k, k))
  print(round(Ak %*% Ik, 8))
}

## -----------------------------------------------------------------------------
# Example 3.3
trace_hl(R)          # 7
char_poly_hl(R)      # t^2 + 7t - 8
eigenvalues_hl(R)    # -8 and 1

## -----------------------------------------------------------------------------
eigenvalues_hl_high(A)
eigenvalues_hl_high(R6)

## -----------------------------------------------------------------------------
summary_hl(R)

## ----fig.width=4, fig.height=4.5----------------------------------------------
plot_rhombus_gg(2, title = "hl-Rhotrix  (dim = 2)")

## ----fig.width=5, fig.height=5.5----------------------------------------------
plot_rhombus_gg(4,
  title    = "hl-Rhotrix  (dim = 4)",
  subtitle = "Minors A21, A22 (R2) and M21 (M2)")

## ----fig.width=7, fig.height=7.5----------------------------------------------
plot_rhombus_gg(6,
  title    = "hl-Rhotrix  (dim = 6)",
  subtitle = "Minors A21, A22, A23 (R2) and M21, M22, M23 (M2)")

## ----eval=FALSE---------------------------------------------------------------
#  # Export for the manuscript (PDF, 300 dpi)
#  ggsave("fig_rhombus_dim4.pdf", plot_rhombus_gg(4),
#         width = 10, height = 10, units = "cm", dpi = 300)
#  
#  # Three-panel figure with patchwork
#  library(patchwork)
#  plot_rhombus_gg(2) + plot_rhombus_gg(4) + plot_rhombus_gg(6)
#  
#  # Further customisation
#  plot_rhombus_gg(4) +
#    theme(plot.background = element_rect(fill = "grey98", colour = NA))

