Home page for accesible maths LAB100

Style control - access keys in brackets

Font (2 3) - + Letter spacing (4 5) - + Word spacing (6 7) - + Line spacing (8 9) - +

2 Functions involving matrices

We have already seen several functions that take, or can take, matrices as inputs (e.g. det, t(), solve, diag). However, we can also write our functions involving matrices.

As a simple first example, suppose we want to write a function that returns the trace (i.e. the sum of the diagonal elements) of a square matrix. Note that we already know how to extract the diagonal elements of a matrix using the function diag.

Trace <- function(A) {

diag_e <- diag(A)

trace <- sum(diag_e)

return(trace)

}

As a more complicated example, we can consider writing a function to determine whether a supplied square matrix is nilpotent and if so, to what degree.

A matrix 𝐀 is nilpotent with degree n, where n is a positive integer, if 𝐀n= and 𝐀m for all positive integers m<n.

To help us develop the function, we can note that if a matrix is nilpotent it must be nilpotent with degree less than or equal to the dimension of the matrix. We can check whether a particular matrix is equal to the zero matrix by obtaining the maximum absolute entry of the matrix. We can also use the fact that nilpotent matrices must have determinant equal to zero. In each case, a suitable tolerance limit is required, because R will introduce some small rounding error. Here we take 1×10-8.

nilpotent <- function(A) {

dm <- dim(A)[1]

if (abs(det(A))> 1e-8) return("Matrix is not nilpotent")

if (max(abs(A))< 1e-8) return(1)

An <- A

for (n in 2:dm) {

An <- An%*%A

if (max(abs(An))< 1e-8) return(n)

}

return("Matrix is not nilpotent")

}

 

Quiz 1: System of linear equations

Find the value for x1, x2, x3, x4 and x5 that satisfies these linear equations:

x1+x2+x3+x4+x5=2.02x1-x2+x3+x4-x5=-1.5x1+3x2-x4+2x5=5.52x1-x2+x3+x4+x5=0.5x2-x3-x4+x5=2.5
  1. (a)

    (x1,x2,x3,x4,x5)=(0.5,1,-0.5,0,1).

  2. (b)

    (x1,x2,x3,x4,x5)=(-0.5,1,-0.5,0,1).

  3. (c)

    (x1,x2,x3,x4,x5)=(0.5,1,0.5,0,1).

  4. (d)

    (x1,x2,x3,x4,x5)=(0.5,1,-0.5,0,-1).

  5. (e)

    (x1,x2,x3,x4,x5)=(0.5,-1,0.5,0,1).

 

 

Workshop 1: Nilpotent matrices

Use the nilpotent function defined in Section 4 to determine whether the following matrices are nilpotent and if so the degree.

V=[-40-6-4-20100141042717621-20-4-4-2-2-31-5-20-1-1-11001]W=[46-66422-24246-4840-2000-224-2-2]
X=[-1854-58118-981438118-3412145418-1458-38]  Y=[-220002-6126-2-426-12-725-1-2410-11-4105-2-312-2-2020]
Z=[0.30.0625-0.50.15-2.4-0.504.0-1.20.060.0125-0.10.030.600.125-1.00.3]

Match the matrices to the following answers:

  1. (A)

    Nilpotent, degree 2

  2. (B)

    Nilpotent, degree 3

  3. (C)

    Nilpotent, degree 4

  4. (D)

    Nilpotent, degree 5

  5. (E)

    Matrix is not nilpotent

 

 

Workshop 2: Raising a matrix to a power

Write a function in R that takes as its inputs a matrix 𝐗 and a positive integer n and returns 𝐗n.

Hence find the (3,2) element (the entry of the third row and second column) of the 3×3 matrix 𝐖25 where

𝐖=[11-10111-10]

 

 

Workshop 3: Approximating a matrix exponential

The exponential of a square matrix 𝐀 is defined by the infinite series

exp(𝐀)=n=01n!𝐀n

where 𝐀0=𝐈.

Using your function for finding matrix powers, combined with a for loop, approximate exp(𝐖) by computing

𝐖~=n=0101n!𝐖n

where W is as defined in the previous question. Give the (3,2) entry of 𝐖~ to 3 decimal places.

[Recall that n! may be computed in R using factorial(n).]