Program 7: Write a R program that includes linear algebra operations on vectors and matrices.
Next Program
# Create sample vectors and matrices
vector_a <- c(1, 2, 3)
vector_b <- c(4, 5, 6)
matrix_A <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2)
matrix_B <- matrix(c(7, 8, 9, 10, 11, 12), nrow = 2)
# Vector addition
vector_sum <- vector_a + vector_b
cat("Vector A + Vector B:", vector_sum, "\n")
# Vector subtraction
vector_diff <- vector_a - vector_b
cat("Vector A - Vector B:", vector_diff, "In")
# Scalar multiplication
scalar <- 2
vector_scaled <- scalar * vector_a
cat("Scalar Multiplication:", vector_scaled, "In")
# Dot product (inner product) of vectors
dot_product <- sum(vector_a * vector_b)
cat("Dot Product of Vector A and Vector B:", dot_product, "Vn")
# Matrix addition
matrix_sum <- matrix_A+ matrix_B
cat("Matrix A + Matrix Biln")
print(matrix_sum)
# Matrix subtraction
matrix_diff <- matrix_A - matrix_B
cat("Matrix A - Matrix B:in")
print(matrix_diff)
# Matrix multiplication
matrix_product <- matrix_A %*% matrix_B
cat("Matrix A * Matrix B:ln")
print(matrix_product)
# Matrix transpose
matrix_transposed <- t(matrix_A)
cat("Transpose of Matrix Ailn")
print(matrix_transposed)
# Matrix determinant
matrix_det <- det(matrix_A)
cat("Determinant of Matrix A:", matrix_det, "In")
# Matrix inverse
matrix_inverse <- solve(matrix_A)
cat("Inverse of Matrix A:in")
print(matrix_inverse)