diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..8ac7e1396df 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,84 @@ -## Put comments here that give an overall description of what your -## functions do +################################################################ +########## Caching the inverse of a matrix ############ +################################################################ -## Write a short comment describing this function -makeCacheMatrix <- function(x = matrix()) { +## Programming Assignment from the Coursera Course: R Programming. + +## Matrix inversion is usually a costly computation and their may be some benefit to caching +## the inverse of a matrix rather than compute it repeatedly (there are also alternatives +## to matrix inversion that we will not discuss here). # +## The following pair functions cache the inverse of a matrix. +## N.B. For this we assume that the matrix supplied is always invertible. + +## This function creates a special "matrix" object that can cache its inverse. +makeCacheMatrix <- function(x = matrix()) { + # x is the matrix need be inversed. + # invMatrix is the cache result of matrix x. + invMatrix <- NULL + ## set add new matrix data. + set <- function(y) { + # Search in parent environments instead of going directly to global environment. + x <<- y + invMatrix <<- NULL + } + # get return the matrix given as argument, the current matrix data. + get <- function() x + # setInvMatrix stores the inverse matrix. + setInvMatrix <- function(solve) invMatrix <<- solve + # getInvMatrix return the cached inverse matrix. + getInvMatrix <- function() invMatrix + list(set = set, get = get, + setInvMatrix = setInvMatrix, + getInvMatrix = getInvMatrix) } -## Write a short comment describing this function +## This function computes the inverse of the special "matrix" returned by makeCacheMatrix above. +## If the inverse has already been calculated (and the matrix has not changed), +## then the cachesolve will retrieve the inverse from the cache. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + # Check if the inverse has been already calculated. + # If TRUE it will be returned the cached result. + invMatrix <- x$getInvMatrix() + if(!is.null(invMatrix)) { + message("getting cached data") + return(invMatrix) + } + # If FALSE (No cache) will call 'solve' to calculate inverse. + data <- x$get() + invMatrix <- solve(data, ...) + # Keep the inverse value for the next use. + x$setInvMatrix(invMatrix) + return(invMatrix) } + + +############################################### +##### Test functionality of the functions ##### +############################################### + +### Hilbert matrices are often used in numerical linear algebra because +### they are easy to construct and have many interesting properties. +### H3 <- matrix(c(1, 1/2, 1/3, 1/2, 1/3, 1/4, 1/3, 1/4, 1/5), nrow=3) +### H3 + +### x <- makeCacheMatrix(H3) + +### cacheSolve(x) +### cacheSolve(x) + + +# > cacheSolve(x) +# [,1] [,2] [,3] +# [1,] 9 -36 30 +# [2,] -36 192 -180 +# [3,] 30 -180 180 +# > cacheSolve(x) +# getting cached inverse matrix +# [,1] [,2] [,3] +# [1,] 9 -36 30 +# [2,] -36 192 -180 +# [3,] 30 -180 180