Rules

No rules. This is practice.

Grades

No grades. This is practice.

Disclaimer

These practice problems are supplied without any guarantee that they will help you do the quiz problems. However, they were written after the quiz problems were written and with the intention that they would help.

These practice problems are also supplied without any guarantee that they are exactly or even nearly like the quiz problems. However, they are like at least some quiz problems in at least some respects.

Problem 1

Write an R function that, given a numeric matrix A and a numeric vector x, calculates xT A x. Note that this only makes sense when A is a square matrix and the dimension of x is the same as the dimensions of the row and column dimensions of A.

Follow GIEMO (garbage in, error messages out). Make sure your function gives an error when the dimensions are wrong or when either argument is not numeric. When either argument contains NA, NaN, Inf, -Inf, your function can give an error, give a warning, or produce one of these results (your choice).

Try to write your function without loops.

Do enough testing to be sure that your function works correctly on at least one set of arguments of the required dimensions so that a numeric result is returned.

Problem 2

Write a new binary operator (see the section titled Special Binary Operators of the handout on matrices, arrays, and data frames) that does the following. Its operands are two numeric vectors that must be of the same length. If they have length zero, the result is zero. Otherwise, if they have length d and the components of one are xi and the components of the other are yi, then the result is

i = 1d xi yd + 1 − i
(This is of no mathematical or statistical use. It is just a practice problem.)

Try to write your function without using a loop.

Hint: The R function rev reverses a vector.

Do enough testing to be sure that your function works correctly on at least one set of arguments of the required dimensions so that a numeric result is returned.

Problem 3

Write a function that takes a numeric matrix and symmetrizes it.

If the matrix argument has components ai j, then the result has components

[ ai j + aj i ] ⁄ 2

This makes no sense unless the matrix argument is square. Your function should give an error when the argument is not square or not numeric.

Try to write your function without using loops.

Hint: The R function t transposes a matrix.

Do enough testing to be sure that your function works correctly on at least one square matrix.

Problem 4

Write a function that takes a symmetric numeric matrix and returns a matrix that is the same except for having mean free rows and columns.

More precisely, if the matrix argument has components ai j, and the vector of column means (or row means, same thing because of symmetry) has components μi, and the grand mean (the mean of the column means, or the mean of all the components of the matrix argument) is ν, then the result has components

ai j − μi − μj + ν

This makes no sense unless the matrix argument is square. Your function should give an error when the argument is not square or not numeric.

Try to write your function without using loops.

Hints:

Do enough testing to be sure that your function works correctly on at least one symmetric matrix.