R Markdown

decompose_and_identify_transformations <- function(A, x) {
  # Function to print a matrix in a nice format
  print_matrix <- function(M) {
    cat("⎡", sprintf("%7.2f", M[1,1]), sprintf("%7.2f", M[1,2]), "⎤\n")
    cat("⎣", sprintf("%7.2f", M[2,1]), sprintf("%7.2f", M[2,2]), "⎦\n")
  }

  # Helper function to identify specific transformations
  identify_transformation <- function(A) {
    # Simple Scaling
    if (A[1,2] == 0 && A[2,1] == 0 && A[1,1] == A[2,2] && A[1,1] > 0) {
      return(list("Simple Scaling", A[1,1]))
    }
    # Scaling and Reflection about the x-axis
    if (A[1,2] == 0 && A[2,1] == 0 && abs(A[1,1]) == abs(A[2,2]) && A[1,1] > 0 && A[2,2] < 0) {
      return(list("Scaling and Reflection about the x-axis", A[1,1]))
    }
    # Non-uniform, indivisible scaling
    if (A[1,2] == 0 && A[2,1] == 0 && A[1,1] > 0 && A[2,2] > 0 && A[1,1] != A[2,2] && (A[1,1] %% A[2,2] != 0 && A[2,2] %% A[1,1] != 0)) {
      return(list("Non-uniform, indivisible scaling", NA))
    }
    # Non-uniform, divisible scaling
    if (A[1,2] == 0 && A[2,1] == 0 && A[1,1] > 0 && A[2,2] > 0 && A[1,1] != A[2,2] && (A[1,1] %% A[2,2] == 0 || A[2,2] %% A[1,1] == 0)) {
      return(list("Non-uniform, divisible scaling", NA))
    }
    # Non-uniform indivisible scaling with reflection about the y-axis
    if (A[1,2] == 0 && A[2,1] == 0 && A[1,1] < 0 && A[2,2] > 0 && A[1,1] != A[2,2] && (A[1,1] %% A[2,2] != 0 && A[2,2] %% A[1,1] != 0)) {
      return(list("Non-uniform indivisible scaling with reflection about the y-axis", NA))
    }
    # Non-uniform divisible scaling with reflection about the y-axis
    if (A[1,2] == 0 && A[2,1] == 0 && A[1,1] < 0 && A[2,2] > 0 && A[1,1] != A[2,2] && (A[1,1] %% A[2,2] == 0 || A[2,2] %% A[1,1] == 0)) {
      return(list("Non-uniform divisible scaling with reflection about the y-axis", NA))
    }
    # Non-uniform indivisible scaling with reflection about the x-axis
    if (A[1,2] == 0 && A[2,1] == 0 && A[1,1] > 0 && A[2,2] < 0 && A[1,1] != A[2,2] && (A[1,1] %% A[2,2] != 0 && A[2,2] %% A[1,1] != 0)) {
      return(list("Non-uniform indivisible scaling with reflection about the x-axis", NA))
    }
    # Zero Matrix
    if (all(A == 0)) {
      return(list("Zero Matrix", NA))
    }
    # Horizontal Shearing
    if (A[1,1] == 1 && A[2,2] == 1 && A[1,2] != 0 && A[2,1] == 0) {
      return(list("Horizontal Shearing", A[1,2]))
    }
    # Horizontal Shearing with Scaling
    if (A[1,1] != 1 && A[2,2] == A[1,1] && A[1,2] != 0 && A[2,1] == 0) {
      return(list("Horizontal Shearing with Scaling", A[1,2]))
    }
    # Vertical Shearing
    if (A[1,1] == 1 && A[2,2] == 1 && A[1,2] == 0 && A[2,1] != 0) {
      return(list("Vertical Shearing", A[2,1]))
    }
    # Vertical Shearing with Scaling
    if (A[1,1] != 1 && A[2,2] == A[1,1] && A[1,2] == 0 && A[2,1] != 0) {
      return(list("Vertical Shearing with Scaling", A[2,1]))
    }
    # If none of the above, return uncovered test case
    return(list("uncovered test case", NA))
  }
  
  # Detect transformation type
  transformation_result <- identify_transformation(A)

  # Compute the transformed vector b
  b <- A %*% x

  # Convert b and x to polar coordinates
  r_b <- sqrt(b[1]^2 + b[2]^2)
  theta_b <- atan2(b[2], b[1]) * (180 / pi)  # Convert to degrees
  
  r_x <- sqrt(x[1]^2 + x[2]^2)
  theta_x <- atan2(x[2], x[1]) * (180 / pi)  # Convert to degrees
  
  # Calculate the differences in radius and angle
  delta_r <- r_b - r_x
  delta_theta <- theta_b - theta_x
  
  # Output vector x and vector b
  vec_x_str <- paste("<", paste(x, collapse = " , "), ">")
  vec_b_str <- paste("<", paste(b, collapse = " , "), ">")
  cat("Vector x: vec_x =", vec_x_str, "\n")
  cat("Vector b: vec_b =", vec_b_str, "\n\n")

  # Handle the different cases with general forms
  if (transformation_result[[1]] == "Simple Scaling") {
    cat("Original Matrix A =\n")
    print_matrix(A)
    cat("General form of Simple Scaling:\n")
    cat("⎡  a  0 ⎤\n⎣  0  a ⎦\n")
    cat("This transformation is an example of scaling by a factor of", transformation_result[[2]], ".\n\n")
  } else if (transformation_result[[1]] == "Scaling and Reflection about the x-axis") {
    cat("Original Matrix A =\n")
    print_matrix(A)
    cat("General form of Scaling and Reflecting:\n")
    cat("⎡  a  0 ⎤\n⎣  0 -a ⎦\n")
    cat("This transformation is an example of a reflection about the x-axis and scaling by a factor of", transformation_result[[2]], ".\n\n")
  } else if (transformation_result[[1]] == "Non-uniform, indivisible scaling") {
    cat("Original Matrix A =\n")
    print_matrix(A)
    cat("General form of Non-uniform, indivisible scaling:\n")
    cat("⎡  a  0 ⎤\n⎣  0  b ⎦\n")
    cat("This transformation is an example of Non-uniform, indivisible scaling. Scaling x by a factor of", A[1,1], "and y by a factor", A[2,2], ".\n\n")
  } else if (transformation_result[[1]] == "Non-uniform, divisible scaling") {
    cat("Original Matrix A =\n")
    print_matrix(A)
    cat("General form of Non-uniform, divisible scaling:\n")
    cat("⎡  a  0 ⎤\n⎣  0  b ⎦\n")
    cat("This transformation is an example of Non-uniform, divisible scaling. Scaling x by a factor of", A[1,1], "and y by a factor", A[2,2], ".\n\n")
  } else if (transformation_result[[1]] == "Non-uniform indivisible scaling with reflection about the y-axis") {
    cat("Original Matrix A =\n")
    print_matrix(A)
    cat("General form of Non-uniform indivisible scaling with reflection about the y-axis:\n")
    cat("⎡ -a  0 ⎤\n⎣  0  b ⎦\n")
    cat("This transformation is an example of Non-uniform indivisible scaling with a reflection about the y-axis. Reflect about the y-axis then scale x by", abs(A[1,1]), "and y by", A[2,2], ".\n\n")
  } else if (transformation_result[[1]] == "Non-uniform divisible scaling with reflection about the y-axis") {
    cat("Original Matrix A =\n")
    print_matrix(A)
    cat("General form of Non-uniform divisible scaling with reflection about the y-axis:\n")
    cat("⎡ -a  0 ⎤\n⎣  0  b ⎦\n")
    cat("This transformation is an example of Non-uniform divisible scaling with a reflection about the y-axis. Reflect about the y-axis then scale x by", abs(A[1,1]), "and y by", A[2,2], ".\n\n")
  } else if (transformation_result[[1]] == "Non-uniform indivisible scaling with reflection about the x-axis") {
    cat("Original Matrix A =\n")
    print_matrix(A)
    cat("General form of Non-uniform indivisible scaling with reflection about the x-axis:\n")
    cat("⎡  a  0 ⎤\n⎣  0 -b ⎦\n")
    cat("This transformation is an example of Non-uniform indivisible scaling with reflection about the x-axis. Reflect about the x-axis then scale x by", A[1,1], "and y by", abs(A[2,2]), ".\n\n")
  } else if (transformation_result[[1]] == "Zero Matrix") {
    cat("Original Matrix A =\n")
    print_matrix(A)
    cat("This transformation is an example of a Zero Matrix, where all entries are 0. It results in a vector with magnitude 0 and undefined angle.\n\n")
  } else if (transformation_result[[1]] == "Horizontal Shearing") {
    cat("Original Matrix A =\n")
    print_matrix(A)
    cat("General form of Horizontal Shearing:\n")
    cat("⎡  1  b ⎤\n⎣  0  1 ⎦\n")
    cat("This transformation is an example of Horizontal Shearing. Keep the y-axis constant and stretch x-direction by", transformation_result[[2]] + 1, ".\n\n")
  } else if (transformation_result[[1]] == "Horizontal Shearing with Scaling") {
    cat("Original Matrix A =\n")
    print_matrix(A)
    cat("General form of Horizontal Shearing with Scaling:\n")
    cat("⎡  a  b ⎤\n⎣  0  a ⎦\n")
    cat("This transformation is an example of Horizontal Shearing with Scaling. Scale the vector x by", A[1,1], "then horizontally shear the vector to x = ax_1 + bx_2.\n\n")
  } else if (transformation_result[[1]] == "Vertical Shearing") {
    cat("Original Matrix A =\n")
    print_matrix(A)
    cat("General form of Vertical Shearing:\n")
    cat("⎡  1  0 ⎤\n⎣  c  1 ⎦\n")
    cat("This transformation is an example of Vertical Shearing. Keep the x-axis constant and stretch y-direction by", transformation_result[[2]] + 1, ".\n\n")
  } else if (transformation_result[[1]] == "Vertical Shearing with Scaling") {
    cat("Original Matrix A =\n")
    print_matrix(A)
    cat("General form of Vertical Shearing with Scaling:\n")
    cat("⎡  a  0 ⎤\n⎣  c  a ⎦\n")
    cat("This transformation is an example of Vertical Shearing with Scaling. Scale the vector x by", A[1,1], "then vertically shear the vector to y = cx_1 + ax_2.\n\n")
  } else {
    cat("uncovered test case\n")
  }
  
  # Output the polar coordinates and differences
  cat("Polar coordinates for b (r, θ):\n")
  cat("r_b:", r_b, "\n")
  cat("θ_b:", theta_b, "degrees\n\n")
  
  cat("Polar coordinates for x (r, θ):\n")
  cat("r_x:", r_x, "\n")
  cat("θ_x:", theta_x, "degrees\n\n")
  
  cat("Differences (Δr, Δθ):\n")
  cat("Δr:", delta_r, "\n")
  cat("Δθ:", delta_theta, "degrees\n")
}

# Test the function with your specified matrices, including the new shearing cases
x <- c(1, 1)

cat("Test Case 1: Simple Scaling\n")
## Test Case 1: Simple Scaling
A <- matrix(c(2, 0, 0, 2), nrow = 2, byrow = TRUE)
decompose_and_identify_transformations(A, x)
## Vector x: vec_x = < 1 , 1 > 
## Vector b: vec_b = < 2 , 2 > 
## 
## Original Matrix A =
## ⎡    2.00    0.00 ⎤
## ⎣    0.00    2.00 ⎦
## General form of Simple Scaling:
## ⎡  a  0 ⎤
## ⎣  0  a ⎦
## This transformation is an example of scaling by a factor of 2 .
## 
## Polar coordinates for b (r, θ):
## r_b: 2.828427 
## θ_b: 45 degrees
## 
## Polar coordinates for x (r, θ):
## r_x: 1.414214 
## θ_x: 45 degrees
## 
## Differences (Δr, Δθ):
## Δr: 1.414214 
## Δθ: 0 degrees
cat("\nTest Case 2: Scaling and Reflection about the x-axis\n")
## 
## Test Case 2: Scaling and Reflection about the x-axis
B <- matrix(c(2, 0, 0, -2), nrow = 2, byrow = TRUE)
decompose_and_identify_transformations(B, x)
## Vector x: vec_x = < 1 , 1 > 
## Vector b: vec_b = < 2 , -2 > 
## 
## Original Matrix A =
## ⎡    2.00    0.00 ⎤
## ⎣    0.00   -2.00 ⎦
## General form of Scaling and Reflecting:
## ⎡  a  0 ⎤
## ⎣  0 -a ⎦
## This transformation is an example of a reflection about the x-axis and scaling by a factor of 2 .
## 
## Polar coordinates for b (r, θ):
## r_b: 2.828427 
## θ_b: -45 degrees
## 
## Polar coordinates for x (r, θ):
## r_x: 1.414214 
## θ_x: 45 degrees
## 
## Differences (Δr, Δθ):
## Δr: 1.414214 
## Δθ: -90 degrees
cat("\nTest Case 3: Non-uniform, indivisible scaling\n")
## 
## Test Case 3: Non-uniform, indivisible scaling
C <- matrix(c(3, 0, 0, 5), nrow = 2, byrow = TRUE)
decompose_and_identify_transformations(C, x)
## Vector x: vec_x = < 1 , 1 > 
## Vector b: vec_b = < 3 , 5 > 
## 
## Original Matrix A =
## ⎡    3.00    0.00 ⎤
## ⎣    0.00    5.00 ⎦
## General form of Non-uniform, indivisible scaling:
## ⎡  a  0 ⎤
## ⎣  0  b ⎦
## This transformation is an example of Non-uniform, indivisible scaling. Scaling x by a factor of 3 and y by a factor 5 .
## 
## Polar coordinates for b (r, θ):
## r_b: 5.830952 
## θ_b: 59.03624 degrees
## 
## Polar coordinates for x (r, θ):
## r_x: 1.414214 
## θ_x: 45 degrees
## 
## Differences (Δr, Δθ):
## Δr: 4.416738 
## Δθ: 14.03624 degrees
cat("\nTest Case 4: Non-uniform, divisible scaling\n")
## 
## Test Case 4: Non-uniform, divisible scaling
D <- matrix(c(4, 0, 0, 2), nrow = 2, byrow = TRUE)
decompose_and_identify_transformations(D, x)
## Vector x: vec_x = < 1 , 1 > 
## Vector b: vec_b = < 4 , 2 > 
## 
## Original Matrix A =
## ⎡    4.00    0.00 ⎤
## ⎣    0.00    2.00 ⎦
## General form of Non-uniform, divisible scaling:
## ⎡  a  0 ⎤
## ⎣  0  b ⎦
## This transformation is an example of Non-uniform, divisible scaling. Scaling x by a factor of 4 and y by a factor 2 .
## 
## Polar coordinates for b (r, θ):
## r_b: 4.472136 
## θ_b: 26.56505 degrees
## 
## Polar coordinates for x (r, θ):
## r_x: 1.414214 
## θ_x: 45 degrees
## 
## Differences (Δr, Δθ):
## Δr: 3.057922 
## Δθ: -18.43495 degrees
cat("\nTest Case 5: Non-uniform indivisible scaling with reflection about the y-axis\n")
## 
## Test Case 5: Non-uniform indivisible scaling with reflection about the y-axis
E <- matrix(c(-3, 0, 0, 5), nrow = 2, byrow = TRUE)
decompose_and_identify_transformations(E, x)
## Vector x: vec_x = < 1 , 1 > 
## Vector b: vec_b = < -3 , 5 > 
## 
## Original Matrix A =
## ⎡   -3.00    0.00 ⎤
## ⎣    0.00    5.00 ⎦
## General form of Non-uniform indivisible scaling with reflection about the y-axis:
## ⎡ -a  0 ⎤
## ⎣  0  b ⎦
## This transformation is an example of Non-uniform indivisible scaling with a reflection about the y-axis. Reflect about the y-axis then scale x by 3 and y by 5 .
## 
## Polar coordinates for b (r, θ):
## r_b: 5.830952 
## θ_b: 120.9638 degrees
## 
## Polar coordinates for x (r, θ):
## r_x: 1.414214 
## θ_x: 45 degrees
## 
## Differences (Δr, Δθ):
## Δr: 4.416738 
## Δθ: 75.96376 degrees
cat("\nTest Case 6: Non-uniform divisible scaling with reflection about the y-axis\n")
## 
## Test Case 6: Non-uniform divisible scaling with reflection about the y-axis
F <- matrix(c(-4, 0, 0, 2), nrow = 2, byrow = TRUE)
decompose_and_identify_transformations(F, x)
## Vector x: vec_x = < 1 , 1 > 
## Vector b: vec_b = < -4 , 2 > 
## 
## Original Matrix A =
## ⎡   -4.00    0.00 ⎤
## ⎣    0.00    2.00 ⎦
## General form of Non-uniform divisible scaling with reflection about the y-axis:
## ⎡ -a  0 ⎤
## ⎣  0  b ⎦
## This transformation is an example of Non-uniform divisible scaling with a reflection about the y-axis. Reflect about the y-axis then scale x by 4 and y by 2 .
## 
## Polar coordinates for b (r, θ):
## r_b: 4.472136 
## θ_b: 153.4349 degrees
## 
## Polar coordinates for x (r, θ):
## r_x: 1.414214 
## θ_x: 45 degrees
## 
## Differences (Δr, Δθ):
## Δr: 3.057922 
## Δθ: 108.4349 degrees
cat("\nTest Case 7: Non-uniform indivisible scaling with reflection about the x-axis\n")
## 
## Test Case 7: Non-uniform indivisible scaling with reflection about the x-axis
G <- matrix(c(3, 0, 0, -5), nrow = 2, byrow = TRUE)
decompose_and_identify_transformations(G, x)
## Vector x: vec_x = < 1 , 1 > 
## Vector b: vec_b = < 3 , -5 > 
## 
## Original Matrix A =
## ⎡    3.00    0.00 ⎤
## ⎣    0.00   -5.00 ⎦
## General form of Non-uniform indivisible scaling with reflection about the x-axis:
## ⎡  a  0 ⎤
## ⎣  0 -b ⎦
## This transformation is an example of Non-uniform indivisible scaling with reflection about the x-axis. Reflect about the x-axis then scale x by 3 and y by 5 .
## 
## Polar coordinates for b (r, θ):
## r_b: 5.830952 
## θ_b: -59.03624 degrees
## 
## Polar coordinates for x (r, θ):
## r_x: 1.414214 
## θ_x: 45 degrees
## 
## Differences (Δr, Δθ):
## Δr: 4.416738 
## Δθ: -104.0362 degrees
cat("\nTest Case 8: Zero Matrix\n")
## 
## Test Case 8: Zero Matrix
H <- matrix(c(0, 0, 0, 0), nrow = 2, byrow = TRUE)
decompose_and_identify_transformations(H, x)
## Vector x: vec_x = < 1 , 1 > 
## Vector b: vec_b = < 0 , 0 > 
## 
## Original Matrix A =
## ⎡    0.00    0.00 ⎤
## ⎣    0.00    0.00 ⎦
## This transformation is an example of a Zero Matrix, where all entries are 0. It results in a vector with magnitude 0 and undefined angle.
## 
## Polar coordinates for b (r, θ):
## r_b: 0 
## θ_b: 0 degrees
## 
## Polar coordinates for x (r, θ):
## r_x: 1.414214 
## θ_x: 45 degrees
## 
## Differences (Δr, Δθ):
## Δr: -1.414214 
## Δθ: -45 degrees
cat("\nTest Case 9: Horizontal Shearing\n")
## 
## Test Case 9: Horizontal Shearing
I <- matrix(c(1, 2, 0, 1), nrow = 2, byrow = TRUE)
decompose_and_identify_transformations(I, x)
## Vector x: vec_x = < 1 , 1 > 
## Vector b: vec_b = < 3 , 1 > 
## 
## Original Matrix A =
## ⎡    1.00    2.00 ⎤
## ⎣    0.00    1.00 ⎦
## General form of Horizontal Shearing:
## ⎡  1  b ⎤
## ⎣  0  1 ⎦
## This transformation is an example of Horizontal Shearing. Keep the y-axis constant and stretch x-direction by 3 .
## 
## Polar coordinates for b (r, θ):
## r_b: 3.162278 
## θ_b: 18.43495 degrees
## 
## Polar coordinates for x (r, θ):
## r_x: 1.414214 
## θ_x: 45 degrees
## 
## Differences (Δr, Δθ):
## Δr: 1.748064 
## Δθ: -26.56505 degrees
cat("\nTest Case 10: Horizontal Shearing with Scaling\n")
## 
## Test Case 10: Horizontal Shearing with Scaling
J <- matrix(c(2, 3, 0, 2), nrow = 2, byrow = TRUE)
decompose_and_identify_transformations(J, x)
## Vector x: vec_x = < 1 , 1 > 
## Vector b: vec_b = < 5 , 2 > 
## 
## Original Matrix A =
## ⎡    2.00    3.00 ⎤
## ⎣    0.00    2.00 ⎦
## General form of Horizontal Shearing with Scaling:
## ⎡  a  b ⎤
## ⎣  0  a ⎦
## This transformation is an example of Horizontal Shearing with Scaling. Scale the vector x by 2 then horizontally shear the vector to x = ax_1 + bx_2.
## 
## Polar coordinates for b (r, θ):
## r_b: 5.385165 
## θ_b: 21.80141 degrees
## 
## Polar coordinates for x (r, θ):
## r_x: 1.414214 
## θ_x: 45 degrees
## 
## Differences (Δr, Δθ):
## Δr: 3.970951 
## Δθ: -23.19859 degrees
cat("\nTest Case 11: Vertical Shearing\n")
## 
## Test Case 11: Vertical Shearing
K <- matrix(c(1, 0, 2, 1), nrow = 2, byrow = TRUE)
decompose_and_identify_transformations(K, x)
## Vector x: vec_x = < 1 , 1 > 
## Vector b: vec_b = < 1 , 3 > 
## 
## Original Matrix A =
## ⎡    1.00    0.00 ⎤
## ⎣    2.00    1.00 ⎦
## General form of Vertical Shearing:
## ⎡  1  0 ⎤
## ⎣  c  1 ⎦
## This transformation is an example of Vertical Shearing. Keep the x-axis constant and stretch y-direction by 3 .
## 
## Polar coordinates for b (r, θ):
## r_b: 3.162278 
## θ_b: 71.56505 degrees
## 
## Polar coordinates for x (r, θ):
## r_x: 1.414214 
## θ_x: 45 degrees
## 
## Differences (Δr, Δθ):
## Δr: 1.748064 
## Δθ: 26.56505 degrees
cat("\nTest Case 12: Vertical Shearing with Scaling\n")
## 
## Test Case 12: Vertical Shearing with Scaling
L <- matrix(c(2, 0, 3, 2), nrow = 2, byrow = TRUE)
decompose_and_identify_transformations(L, x)
## Vector x: vec_x = < 1 , 1 > 
## Vector b: vec_b = < 2 , 5 > 
## 
## Original Matrix A =
## ⎡    2.00    0.00 ⎤
## ⎣    3.00    2.00 ⎦
## General form of Vertical Shearing with Scaling:
## ⎡  a  0 ⎤
## ⎣  c  a ⎦
## This transformation is an example of Vertical Shearing with Scaling. Scale the vector x by 2 then vertically shear the vector to y = cx_1 + ax_2.
## 
## Polar coordinates for b (r, θ):
## r_b: 5.385165 
## θ_b: 68.19859 degrees
## 
## Polar coordinates for x (r, θ):
## r_x: 1.414214 
## θ_x: 45 degrees
## 
## Differences (Δr, Δθ):
## Δr: 3.970951 
## Δθ: 23.19859 degrees