1. Geometric Transformation of Shapes Using Matrix Multiplication

# Define the square with 4 points
square <- matrix(c(0, 0, 1, 1, 0, 1, 1, 0), nrow = 2, byrow = TRUE)

# Function to plot the shape
plot_shape <- function(points, title) {
  df <- data.frame(x = c(points[1,], points[1, 1]), y = c(points[2,], points[2, 1]))
  ggplot(df, aes(x = x, y = y)) +
    geom_polygon(fill = "lightblue", color = "black") +
    coord_fixed() +
    ggtitle(title) +
    xlim(-3, 3) + ylim(-3, 3)
}

plot_shape(square, "Original Square")

Scale

# Function to apply a transformation matrix to the points
transform_shape <- function(shape, matrix) {
  return(matrix %*% shape)
}

# Create a sequence of scaling factors for animation
scaling_factors <- seq(0.5, 2, length.out = 20)  # scale from 0.5 to 2

# Create a data frame to store the transformed squares over time for animation
animate_data <- data.frame()

for (i in seq_along(scaling_factors)) {
  # Create a scaling matrix for each time step
  scaling_matrix <- matrix(c(scaling_factors[i], 0, 0, scaling_factors[i]), nrow = 2)
  
  # Apply scaling to the square
  scaled_square <- transform_shape(square, scaling_matrix)
  
  # Store the scaled square in a data frame for animation
  temp_df <- data.frame(
    x = c(scaled_square[1, ], scaled_square[1, 1]),
    y = c(scaled_square[2, ], scaled_square[2, 1]),
    time = i
  )
  animate_data <- rbind(animate_data, temp_df)
}

# Create animated plot using gganimate
p <- ggplot(animate_data, aes(x = x, y = y, group = time)) +
  geom_polygon(fill = "lightblue", color = "black") +
  coord_fixed() +
  labs(title = 'Scaling Animation: Frame {frame}') +
  xlim(-3, 3) + ylim(-3, 3) +
  transition_states(time, transition_length = 1, state_length = 1)

# Render the animation
animate(p)

Reflected

# Gram-Schmidt orthogonalization function (similar to `gsBasis` in Python)
gs_basis <- function(vectors) {
  u1 <- vectors[, 1]
  u2 <- vectors[, 2] - (sum(vectors[, 2] * u1) / sum(u1 * u1)) * u1
  E <- cbind(u1 / sqrt(sum(u1 * u1)), u2 / sqrt(sum(u2 * u2)))
  return(E)
}

# Function to build the reflection matrix
build_reflection_matrix <- function(rf_basis) {
  E <- gs_basis(rf_basis)
  
  # Transformation matrix for reflection across y-axis
  TE <- matrix(c(1, 0, 0, -1), nrow = 2)
  
  # Calculate the transformation matrix T
  T <- E %*% TE %*% solve(E)
  return(T)
}

# Custom basis to reflect the square
rf_basis <- matrix(c(1, -1, 1.5, 2), nrow = 2)

# Function to apply a transformation matrix to the points
transform_shape <- function(shape, matrix) {
  return(matrix %*% shape)
}

# Build reflection matrix based on custom basis
T <- build_reflection_matrix(rf_basis)

# Apply reflection transformation to the square
reflected_square <- transform_shape(square, T)

# Function to plot the original and reflected shapes on the same graph
plot_shape_combined <- function(original_points, reflected_points, title) {
  df_original <- data.frame(x = c(original_points[1,], original_points[1, 1]), 
                            y = c(original_points[2,], original_points[2, 1]), 
                            shape = "Original")
  
  df_reflected <- data.frame(x = c(reflected_points[1,], reflected_points[1, 1]), 
                             y = c(reflected_points[2,], reflected_points[2, 1]), 
                             shape = "Reflected")
  
  df_combined <- rbind(df_original, df_reflected)
  
  ggplot(df_combined, aes(x = x, y = y, color = shape, fill = shape)) +
    geom_polygon(alpha = 0.5) +
    coord_fixed() +
    ggtitle(title) +
    xlim(-3, 3) + ylim(-3, 3)
}

# Plot the original and reflected squares together
plot_shape_combined(square, reflected_square, "Original and Reflected Square")

Rotate

# Load necessary libraries
library(ggplot2)
library(gganimate)

# Define the square with 4 points (each column is a point (x, y))
square <- matrix(c(0, 0, 1, 1, 0, 1, 1, 0), nrow = 2, byrow = TRUE)

# Function to apply a transformation matrix to the points
transform_shape <- function(shape, matrix) {
  return(matrix %*% shape)
}

# Create a sequence of rotation angles for the animation (from 0 to 360 degrees)
rotation_angles <- seq(0, 2 * pi, length.out = 50)

# Create a data frame to store the transformed squares over time for animation
animate_data <- data.frame()

# Loop through each rotation angle and calculate the rotated square
for (i in seq_along(rotation_angles)) {
  # Create rotation matrix for the current angle
  rotation_matrix <- matrix(c(cos(rotation_angles[i]), -sin(rotation_angles[i]),
                              sin(rotation_angles[i]), cos(rotation_angles[i])), nrow = 2)
  
  # Apply rotation to the square
  rotated_square <- transform_shape(square, rotation_matrix)
  
  # Store the rotated square in a data frame for animation
  temp_df <- data.frame(
    x = c(rotated_square[1, ], rotated_square[1, 1]),
    y = c(rotated_square[2, ], rotated_square[2, 1]),
    time = i
  )
  animate_data <- rbind(animate_data, temp_df)
}

# Create animated plot using gganimate
p <- ggplot(animate_data, aes(x = x, y = y, group = time)) +
  geom_polygon(fill = "lightblue", color = "black") +
  coord_fixed() +
  labs(title = 'Rotating Square: Frame {frame}') +
  xlim(-2, 2) + ylim(-2, 2) +
  transition_states(time, transition_length = 1, state_length = 1)

# Render the animation
animate(p)

2. Matrix Properties and Decomposition

## [1] 512 512

https://blog.naver.com/juhy9212/220914485227

grey_svd <- svd(grey_img)

u <- grey_svd $u
d <- diag(grey_svd $d)
v <- grey_svd $v

image <- u %*% d %*% t(v)
image(image, col = grey(seq(0, 1, length = 256)), main = "512 * 512")

for (i in c(5, 20, 50)) {
k <- 50  # k singular values
u <- u[, 1:k]
d <- d[1:k, 1:k]
v <- v[, 1:k]

image.comp <- u %*% d %*% t(v)
image(image.comp, col = grey(seq(0, 1, length = 256)), main = paste("SDV_", "K:", i))
}

3. Matrix Rank, Properties, and Eigenspace

𝐴

Task: Determine the Rank of the Given Matrix 𝐴:

The rank of matrix 𝐴 refers to the dimension of its row space and column space. As can be confirmed below, the rank of matrix 𝐴 is β€œ4.”. So the maximum rank is 4 for a matrix of size 4Γ—4, and the minimum rank is 1 as long as there is at least one independent row or column.

When matrix 𝐴 is an π‘š Γ— 𝑛 matrix, its row space is the subspace of 𝑅 𝑛 formed by linear combinations of its rows. Similarly, the column space is the subspace of 𝑅 π‘š formed by linear combinations of its columns. The rank of matrix 𝐴 represents the dimension of these spaces, indicating the number of linearly independent rows and columns.”

The rank indicates the linear independence of the given matrix. A rank of 4 means that both the rows and columns of matrix 𝐴 are linearly independent. In other words, none of the rows or columns of this matrix can be expressed as a linear combination of the others.

# A
A <- matrix(c(2,  4,  1,  3,
              -2, -3, 4,  1,
               5,  6, 2,  8,
              -1, -2, 3,  7), 
            byrow = TRUE, nrow = 4)

# Rank Calculation
rank_A <- rankMatrix(A)
print(rank_A)
## [1] 4
## attr(,"method")
## [1] "tolNorm2"
## attr(,"useGrad")
## [1] FALSE
## attr(,"tol")
## [1] 8.881784e-16
# properties
attr(rank_A, "method")
## [1] "tolNorm2"
attr(rank_A, "useGrad")
## [1] FALSE
attr(rank_A, "tol")
## [1] 8.881784e-16

Task: Prove that the rank of a matrix equals the dimension of its row space (or column space). Provide an example to illustrate the concept.

  • The rank of a matrix is ​​determined by the linear independence of rows or columns.
  • After transforming a matrix into a trapezoidal form, the rows or columns where the pivots exist represent linearly independent vectors. The number of these pivots is equal to the rank of the matrix.
  • At this time, the subspace generated by the rows where the pivots exist is the row space, and the subspace generated by the columns where the pivots exist is the column space. Therefore, the rank becomes equal to the dimensions of the row space and the column space.

The rank of a matrix is determined by the rows and columns in which the pivots exist, which is the same as the dimension of the row space and the column space. The example below verify that the rank and the dimension of the space are the same by using the echelon matrix transformation.

rref_A <- rref(A)

# Print the result
print(rref_A)
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0    0
## [2,]    0    1    0    0
## [3,]    0    0    1    0
## [4,]    0    0    0    1
# Get the rank of the matrix
rank_A <- rankMatrix(A)
print(rank_A)
## [1] 4
## attr(,"method")
## [1] "tolNorm2"
## attr(,"useGrad")
## [1] FALSE
## attr(,"tol")
## [1] 8.881784e-16

𝐡

Task: Determine the Rank of the Given Matrix 𝐡:

Able to verify that the rank of matrix 𝐡 is 1. This means that only one row of the matrix 𝐡is linearly independent.

# B
B <- matrix(c(2, 5, 7,
              4, 10, 14,
              1, 2.5, 3.5), 
            byrow = TRUE, nrow = 3)

# Rank Calculation
rank_B <- rankMatrix(B)
print(rank_B)
## [1] 1
## attr(,"method")
## [1] "tolNorm2"
## attr(,"useGrad")
## [1] FALSE
## attr(,"tol")
## [1] 6.661338e-16
attr(rank_B, "method")
## [1] "tolNorm2"
attr(rank_B, "useGrad")
## [1] FALSE
attr(rank_B, "tol")
## [1] 6.661338e-16

Task: Perform a row reduction on matrix 𝐡 and describe how it helps in finding the rank. Discuss any special properties of matrix (e.g., is it a rank-deficient matrix?).

Applied Gaussian elimination (reduced row echelon form or RREF). The first row is [1,2.5,3.5]. Since the matrix has been reduced to RREF form, this row indicates that it is a pivot row, contributing to the matrix’s rank. However, the second and third rows consist entirely of zeros, confirming that these rows are linearly dependent on the first row. Therefore, the rank of matrix 𝐡 is 1, meaning that only the first row is linearly independent

Matrix 𝐡 is a rank-deficient matrix, which means it has rank 1. As a result, it has linearly dependent rows and no inverse.

This indicates that the matrix does not have full rank, and therefore, it cannot be used in certain areas, such as computing an inverse or solving a system of linear equations uniquely.

# Reduce the matrix using Gaussian elimination
rref_B <- pracma::rref(B)  
print(rref_B)
##      [,1] [,2] [,3]
## [1,]    1  2.5  3.5
## [2,]    0  0.0  0.0
## [3,]    0  0.0  0.0

Eigenvalues & Eigenvectors

Task: Find the eigenvalues and eigenvectors of the matrix A:

The eigenvalues are 5,3,2. The det(eig_vectors) value is not 0. The output is -0.4982729, which is not 0, meaning that the eigenvectors are linearly independent.

https://blog.naver.com/pmw9440/221535698941

# A
A <- matrix(c(3, 1, 2,
              0, 5, 4,
              0, 0, 2), nrow = 3, byrow = TRUE)

values <- eigen(A)$values  
vectors <- eigen(A)$vectors  

print(values)
## [1] 5 3 2
print(vectors)
##           [,1] [,2]       [,3]
## [1,] 0.4472136    1 -0.3713907
## [2,] 0.8944272    0 -0.7427814
## [3,] 0.0000000    0  0.5570860
eig_vectors <- eigen(A)$vectors

det(eig_vectors)
## [1] -0.4982729

Task: Determine if matrix A can be diagonalized

For a matrix to be diagonalizable, its eigenvectors must be linearly independent. As we saw in the previous step, A can be diagonalized because its eigenvectors are linearly independent.

# This process shows that the matrix 𝐴 is diagonalizable, which means that it can be decomposed into eigenvalues and eigenvectors and reconstructed into the original matrix.
D <- diag(values)
P <- vectors

# (A = P %*% D %*% solve(P))
A_reconstructed <- P %*% D %*% solve(P)
print(A_reconstructed)
##      [,1] [,2] [,3]
## [1,]    3    1    2
## [2,]    0    5    4
## [3,]    0    0    2

https://darkpgmr.tistory.com/105

Task: Discuss the geometric interpretation

When matrix A is viewed as a linear transformation, the non-zero vector whose result of transformation by linear transformation A becomes its own constant multiple is called an eigenvector, and this constant multiple value is called an eigenvalue.

Geometrically, the eigenvector of matrix (linear transformation) A represents a direction vector whose direction is preserved and only its scale is changed by linear transformation A, and the eigenvalue is a value that represents the degree of change in the scale of the eigenvector.

For example, when considering a three-dimensional rotational transformation such as the rotation of the Earth, the eigenvector that does not change due to this rotational transformation will be the rotation axis vector, and its eigenvalue will be 1.