Problem set 1
1. Rank of matrix A
# According to the lecture,the rank of a matrix refers to the number of its pivots, i.e., the number of linearly rows (or columns) it contains.
# However, to accurately determine a matrix rank, one needs to first calculate its determinant.
# Henceforth, for matrix A, if the determinant is non zero, the rank will be 4 as all its 4 rows are linearly independent. If the determinant is zero we will know that the matrix is not invertible.
# Defining the matrix
mat <- matrix(c(1, 2, 3, 4,
-1, 0, 1, 3,
0, 1, -2, 1,
5, 4, -2, -3), nrow = 4, byrow = TRUE)
# Calculating the determinant
determinant_mat <- det(mat)
cat("Determinant of the matrix:", determinant_mat, "\n")
## Determinant of the matrix: -9
# Calculating the rank using qr decomposition
rank_mat <- qr(mat)$rank
cat("Rank of the matrix:", rank_mat, "\n")
## Rank of the matrix: 4
2. Finding the maximum and the minimum rank of a non - zero mxn
matrix
# The maximum rank of a non-zero mxn matrix cannot exceed the number of columns in the matrix and the minimum rank of such a matrix must be at least one linearly independent row or column and cannot be zero.
# Theoretically, we should get the following results: Maximum Rank = (n) and Minimum Rank = (1)
# Setting seed for reproducibility
set.seed(127)
# Defining matrix dimensions
m <- 5
n <- 3
# Generating a random matrix
mat <- matrix(rnorm(m * n), nrow = m)
# Printing the original matrix
cat("Original Matrix:\n", mat, "\n\n")
## Original Matrix:
## -0.5677337 -0.8147606 -0.4939396 0.001818846 0.8197849 0.9967579 0.7517822 -0.1255472 0.5646199 0.1335086 -0.1059632 0.6059296 0.01325097 -0.2787887 -0.1356745
# Step 1: Calculating the rank
rank_mat <- qr(mat)$rank
cat("Rank of the matrix:", rank_mat, "\n")
## Rank of the matrix: 3
# Step 2: Calculating the maximum rank (minimum of the two dimensions)
max_rank <- min(m, n)
cat("Maximum Rank of the matrix:", max_rank, "\n")
## Maximum Rank of the matrix: 3
# Step 3: Calculating the minimum rank (assuming the matrix is non-zero)
min_rank <- ifelse(any(mat != 0), 1, 0)
cat("Minimum Rank of the matrix:", min_rank, "\n")
## Minimum Rank of the matrix: 1
3. Rank of matrix B
# The rank of a matrix refers to the number of linearly independent rows (or columns) it contains. Matrix B is a 3*3 matrix with 3 rows and 3 columns. We will first need to find its determinant to then calculate its rank.
# Defining the matrix
mat <- matrix(c(1, 2, 1, 3, 6, 3, 2, 4, 2), nrow = 3, byrow = TRUE)
# Calculating the determinant
determinant_mat <- det(mat)
# Calculating the rank
rank_mat <- qr(mat)$rank
# Printing the determinant and the rank
cat("Determinant of the matrix:", determinant_mat, "\n")
## Determinant of the matrix: 0
cat("Rank of the matrix:", rank_mat, "\n")
## Rank of the matrix: 1
Problem set 2
Compute the eigenvalues and eigenvectors of a 3 by 3 matrix A
corresponding to c(1, 2, 3, 0, 4, 5, 0, 0, 6)
library(polynom)
# Defining the matrix
A <- matrix(c(1, 2, 3, 0, 4, 5, 0, 0, 6), nrow = 3, byrow = TRUE)
# Computing eigenvalues and eigenvectors
eigen_results <- eigen(A)
# Eigenvalues
eigenvalues <- eigen_results$values
# Printing the eigenvalues
cat("Eigenvalues:\n")
## Eigenvalues:
print(eigen_results$values)
## [1] 6 4 1
# Eigenvectors
eigenvectors <- eigen_results$vectors
cat("\nEigenvectors:\n")
##
## Eigenvectors:
print(eigen_results$vectors)
## [,1] [,2] [,3]
## [1,] 0.5108407 0.5547002 1
## [2,] 0.7981886 0.8320503 0
## [3,] 0.3192754 0.0000000 0
# Calculating the characteristic polynomial
characteristic_poly <- polyroot(c(-1, rev(eigen_results$values)))
# Printing the characteristic polynomial
cat("\nCharacteristic Polynomial Coefficients:\n")
##
## Characteristic Polynomial Coefficients:
print(characteristic_poly)
## [1] 0.3333333+0.0i -0.5000000+0.5i -0.5000000-0.5i