diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..d8b845e37be 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,50 @@ -## Put comments here that give an overall description of what your -## functions do -## Write a short comment describing this function +## The aim of the folowing functions is to cache the +## inverse matrix calculation so that we can avoid +## multiple useless computations -makeCacheMatrix <- function(x = matrix()) { +## This function create a matrix that can have its +## inverse cached +makeCacheMatrix <- function(x = matrix()) { + + ## initialise the inverse matrix + inverse <- NULL + + ## define the getter and setter functions + get <- function() x + set <- function(y) { + x <<- y + inverse <<- NULL + } + + ## define the functions for the inverse + setInverse <- function(Inv) inverse <<- Inv + getInverse <- function() inverse + + list(set = set, get = get, + setInverse = setInverse, + getInverse = getInverse) } -## Write a short comment describing this function +## This function returns the inverse of a matrix by first checking +## if it has not been computed before ! cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + + ## initialise the inverse matrix + inverse <- x$getInverse() + + ## check the existence of a cached value + if(!is.null(inverse)) { + message("getting cached data") + return(inverse) + } + else { + data <- x$get() + inverse <- solve(data) + x$setInverse(inverse) + return(inverse) + } }