Stat 33B, Fall 2021 HW5: Functions
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
HW5: Functions
Stat 33B, Fall 2021
Introduction
The purpose of this assignment is to write some functions in R.
General Instructions
• Write your narrative and code in an Rmd (R markdown) file.
• Name this file as hw5-first-last.Rmd, where first and last are your first and last names (e.g. hw5-gaston-sanchez.Rmd).
• Please do not use code chunk options such as: echo = FALSE, eval = FALSE, results = 'hide '. All chunks must be visible and evaluated.
1) Vector Norms
Consider a vector X = (x1, x2, . . . , xn), and suppose you are interested in calculating the two most common types of vector norms (in the linear algebra sense). Simply put, a vector-norm is just a way to measure the “size” of a vector.
L1-norm
L2-norm
1.1) Computing vector norms without using sum()
Write a function that takes an R numeric vector, and returns a numeric vector contain- ing both the L-1 norm and the L-2 norm. The elements in this vector must have names (i.e. named elements).
• Assume the input is always a vector, possibly containing missing values.
• If the input vector is not numeric, then the function must stop() execution returning a meaningful error message.
• Include a logical argument na.rm that allows the user to decide whether missing values should be removed before computing the norms.
• You are not allowed to use functions that compute vector norms from R, or from external packages.
• Also, you are not allowed to use sum(). Instead use any control-flow structures (e.g. conditionals, loops, etc).
• Give meaningful names to the function and its arguments.
• Test your function with the vectors:
– v = 1:10
– u = c(2, 4, 6, NA, 10); use na.rm = FALSE as well as na.rm = TRUE
# your code
1.2) Computing vector norms using sum()
Write another function that takes an R numeric vector, and returns a numeric vector with named element containing both the L-1 norm and the L-2 norm. Use the same specifications given in the previous section 1.1) but this time you are allowed to use sum()
# your code
2) Shape of a Matrix
Consider the following three matrices:
mat1 = matrix(1 :9 , nrow = 3 , ncol = 3)
mat1
mat2 = matrix(1 :12 , nrow = 3 , ncol = 4)
mat2
mat3 = matrix(1 :12 , nrow = 4 , ncol = 3)
mat3
• mat1 is a square matrix because it has the same number of rows as columns (number of rows = number of columns)
• mat2 is a wide matrix because it has more columns than rows (number of columns > number of rows)
• mat3 is a tall matrix because it has more rows than columns (number of rows > number of columns)
2.1) Testing a the shape of a matrix
Write a function shape() to test whether a certain matrix (e.g. mat1, mat2, mat3) is square, wide or tall. The output should be a character string: "square" for square matrices, "wide" for wide matrices, or "tall" for tall matrices.
• Assume the input is an R object, not necessarily a matrix.
• If the input object is not a matrix, then the function must stop() execution returning a meaningful error message.
• The following functions are your friends:
– is.matrix(): whether an object is a matrix
– nrow(): number of rows of a matrix
– ncol(): number of columns of a matrix
• You can use any control-flow structures (e.g. conditionals, loops, etc).
• Test your function with mat1, mat2, mat3, and 1:5
# your code
3) Diagonal Matrix
Among square matrices, there are special types of matrices known as diagonal matrices. We will say that a matrix is diagonal if it meets all of the following criteria:
• it has to be square (same number of rows and columns)
• elements outside diagonal must be zero
• at least one element in the diagonal must be different from zero
For illustration purposes, the code below contains one diagonal matrix, and two non-diagonal matrices
# this is diagonal
d1 = diag(3 :1 , nrow = 3 , ncol = 3)
d1
# this is not diagonal
nd1 = matrix(9 :1 , nrow = 3 , ncol = 3)
nd1
# this is not diagonal
nd2 = matrix(c (1 ,0 ,0 ,2 ,0 ,0 ,0 ,0 ,0), nrow = 3 , ncol = 3)
nd2
3.1) Testing if a matrix is diagonal
Write a function to test whether a certain matrix (e.g. d1, nd2, nd3) is diagonal or non- diagonal. The output must be a logical vector, that is TRUE for diagonal matrix, FALSE otherwise.
• Assume the input is an R object, not necessarily a matrix.
• If the input is not a matrix, then the function must provide a warning() message indicating that the input is not a matrix, and return FALSE.
• You can use any control-flow structures (e.g. conditionals, loops, etc).
• Give a meaningful name to the function.
• Test your function with d1, nd1, nd2, and 1:5
• The following functions can be your friends:
– nrow(): number of rows of a matrix
– ncol(): number of columns of a matrix
– upper.tri(): matrix of logicals indicating upper triangular part of a matrix
– upper.tri(): matrix of logicals indicating upper triangular part of a matrix
– diag(): extracts elements in the diagonal of a matrix
– any(): are some values true? e.g. any(1:3 == 3)
– all(): are all values true? e.g. all(1:3 > -1)
# your code
4) Trace of a Matrix
The trace of a square matrix is the sum of the elements in its diagonal. For example, consider the following matrix A
The trace of A is: 1 + 5 + 9 = 15
4.1) Trace of matrix without using diag()
Write an R function that computes the trace of a square matrix.
• Assume the input is always a numeric matrix (not necessarily square).
• If the input matrix is not square, then the function should stop execution returning a meaningful error message.
• You are NOT allowed to use the function diag() or any other function from external packages.
• However, you are allowed to use functions such as nrow(), ncol(), and dim(), as well as any control-flow structures (e.g. conditionals, loops, etc).
• Include a logical argument na.rm that allows the user to decide whether missing values on the diagonal should be removed to compute the trace.
• Give meaningful names to the function and its arguments.
• Test your function with mat1, d1, mat2, and with the following matrix mat4 using the argument na.rm = TRUE as well as na.rm = FALSE
mat4 = mat1
mat4[2 ,2] = NA
4.2) Trace of matrix using diag()
Rewrite a function to compute the trace of a square matrix, using the same specifications given in the previous section 4.1) but this time you are allowed to use diag().
# your code
2021-12-15