Linear Algebra Operations on Vectors and Matrices in R
Vectors
Vector Addition and Subtraction
Example:
# Define vectors v1 <- c(1, 2, 3) v2 <- c(4, 5, 6) # Addition v_sum <- v1 + v2 print(v_sum) # [1] 5 7 9 # Subtraction v_diff <- v1 - v2 print(v_diff) # [1] -3 -3 -3
Scalar Multiplication
Example:
# Scalar scalar <- 2 # Scalar multiplication v_scaled <- scalar * v1 print(v_scaled) # [1] 2 4 6
Dot Product
The dot product (or inner product) of two vectors.
Example:
# Dot product dot_product <- sum(v1 * v2) print(dot_product) # [1] 32
Vector Norm
The Euclidean norm (or magnitude) of a vector.
Example:
# Vector norm norm_v1 <- sqrt(sum(v1^2)) print(norm_v1) # [1] 3.741657
Matrices
Matrix Creation
Example:
# Create a matrix m <- matrix(1:6, nrow = 2, ncol = 3) print(m) # [,1] [,2] [,3] # [1,] 1 3 5 # [2,] 2 4 6
Matrix Addition and Subtraction
Example:
# Define matrices m1 <- matrix(1:4, nrow = 2, ncol = 2) m2 <- matrix(5:8, nrow = 2, ncol = 2) # Addition m_sum <- m1 + m2 print(m_sum) # [,1] [,2] # [1,] 6 8 # [2,] 8 10 # Subtraction m_diff <- m1 - m2 print(m_diff) # [,1] [,2] # [1,] -4 -4 # [2,] -4 -4
Scalar Multiplication
Example:
# Scalar scalar <- 3 # Scalar multiplication m_scaled <- scalar * m1 print(m_scaled) # [,1] [,2] # [1,] 3 6 # [2,] 9 12
Matrix Multiplication
Example:
# Define matrices m1 <- matrix(1:4, nrow = 2, ncol = 2) m2 <- matrix(5:8, nrow = 2, ncol = 2) # Matrix multiplication m_product <- m1 %*% m2 print(m_product) # [,1] [,2] # [1,] 19 22 # [2,] 43 50
Transposition
Example:
# Matrix transposition m_transposed <- t(m1) print(m_transposed) # [,1] [,2] # [1,] 1 3 # [2,] 2 4
Determinant
The determinant of a square matrix.
Example:
# Define a square matrix m <- matrix(c(1, 2, 3, 4), nrow = 2, ncol = 2) # Calculate the determinant determinant <- det(m) print(determinant) # [1] -2
Inverse
The inverse of a square matrix.
Example:
# Define a square matrix m <- matrix(c(1, 2, 3, 4), nrow = 2, ncol = 2) # Calculate the inverse m_inverse <- solve(m) print(m_inverse) # [,1] [,2] # [1,] -2.0 1.0 # [2,] 1.5 -0.5
Eigenvalues and Eigenvectors
Eigenvalues and Eigenvectors
Example:
# Define a matrix m <- matrix(c(1, 2, 3, 4), nrow = 2, ncol = 2) # Compute eigenvalues and eigenvectors eigen_result <- eigen(m) print(eigen_result$values) # [1] 5.372281 -0.372281 print(eigen_result$vectors) # [,1] [,2] # [1,] -0.8245648 0.4159736 # [2,] -0.5657675 -0.9093769
Solving Linear Systems
Solving a Linear System
To solve Ax=bAx = bAx=b, where AAA is a matrix and bbb is a vector.
Example:
# Define matrix A and vector b A <- matrix(c(2, 1, -1, 1, 3, 2, 1, 1, 4), nrow = 3, byrow = TRUE) b <- c(8, 13, 11) # Solve for x x <- solve(A, b) print(x) # [1] 2 3 1
Singular Value Decomposition (SVD)
Performing SVD
Example:
# Define a matrix m <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3) # Perform Singular Value Decomposition svd_result <- svd(m) print(svd_result$u) # [,1] [,2] # [1,] -0.3879274 -0.9223658 # [2,] -0.9223658 0.3879274 print(svd_result$d) # [1] 9.508032 0.772869 print(svd_result$v) # [,1] [,2] [,3] # [1,] -0.4286671 0.5663065 -0.7034620 # [2,] -0.5663065 0.8259630 0.0542291
Matrix Decomposition
LU Decomposition
Example:
# Load the Matrix package install.packages("Matrix") library(Matrix) # Define a matrix m <- matrix(c(4, 3, 6, 3, 4, 5, 2, 2, 3), nrow = 3, byrow = TRUE) # Perform LU Decomposition lu_decomposition <- lu(m) print(lu_decomposition$L) # [,1] [,2] [,3] # [1,] 1.0 0.0 0.0 # [2,] 0.5 1.0 0.0 # [3,] 0.5 0.5 1.0 print(lu_decomposition$U) # [,1] [,2] [,3] # [1,] 4.0 3.0 6.0 # [2,] 0.0 1.5 0.5 # [3,] 0.0 0.0 1.0
Summary
- Vectors:
- Addition, subtraction, scalar multiplication
- Dot product, vector norm
- Matrices:
- Creation, addition, subtraction, scalar multiplication
- Matrix multiplication, transposition
- Determinant, inverse
- Eigenvalues and Eigenvectors:
- Calculation of eigenvalues and eigenvectors
- Solving Linear Systems:
- Solving equations of the form Ax=bAx = bAx=b
- Singular Value Decomposition (SVD):
- Performing SVD on matrices
- Matrix Decomposition:
- LU Decomposition