From 765504d79e82abcfa048b4f28f66b16818ba3aa3 Mon Sep 17 00:00:00 2001 From: AlexLeger Date: Thu, 19 Feb 2015 22:40:27 +0100 Subject: [PATCH 1/4] first commit --- cachematrix.R | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..22644103b66 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -4,12 +4,30 @@ ## Write a short comment describing this function makeCacheMatrix <- function(x = matrix()) { - + inv <- NULL + set <- function(y) { + x <<- y + inv <<- NULL + } + get <- function() x + setinv <- function(inverse) inv <<- inverse + getinv <- function() inv + list(set = set, get = get, + setinv = setinv, + getinv = getinv) } ## Write a short comment describing this function cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + inv <- x$getinv() + if(!is.null(inv)) { + message("getting cached data") + return(inv) + } + data <- x$get() + inv <- solve(data, ...) + x$setinv(inv) + inv } From 8d3e23d031f714565f1199d30bac1175508159f2 Mon Sep 17 00:00:00 2001 From: AlexLeger Date: Thu, 19 Feb 2015 22:47:03 +0100 Subject: [PATCH 2/4] comments --- cachematrix.R | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 22644103b66..325c2308aef 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,7 +1,11 @@ -## Put comments here that give an overall description of what your -## functions do +## The goal of these two functions is to provide a way to cache the +## inverse of a matrix once it has been computed for the first time. +## Then, if the inverse of this matrix is called again, it will be +## retrieved from the cache rather than computed again, which can be +## very time consuming. -## Write a short comment describing this function +## This function will take a matrix and return a list of four +## functions. makeCacheMatrix <- function(x = matrix()) { inv <- NULL From 38a8bf2d7f862df493ff18353a043e589a7fb98c Mon Sep 17 00:00:00 2001 From: AlexLeger Date: Thu, 19 Feb 2015 22:54:28 +0100 Subject: [PATCH 3/4] comments --- cachematrix.R | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 325c2308aef..6261b595048 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -5,13 +5,16 @@ ## very time consuming. ## This function will take a matrix and return a list of four -## functions. +## functions, allowing to access both the original matrix and +## its inverse. + + makeCacheMatrix <- function(x = matrix()) { inv <- NULL - set <- function(y) { + set <- function(y) { ## The set function allows the original matrix to be modified x <<- y - inv <<- NULL + inv <<- NULL ## Since the original matrix has been modified, the inverse that had been computed is no longer valid } get <- function() x setinv <- function(inverse) inv <<- inverse @@ -22,12 +25,16 @@ makeCacheMatrix <- function(x = matrix()) { } -## Write a short comment describing this function +## This function takes a special "list" such as the one created by +## the makeCacheMatrix function, and does the following : +## 1) Checks whether the inverse of the matrix has already been computed +## 2) If so, returns this inverse with the message "getting cached matrix" +## 3) If not, it computes the inverse of the matrix, stores it using the setinv function, then returns it. cacheSolve <- function(x, ...) { inv <- x$getinv() if(!is.null(inv)) { - message("getting cached data") + message("getting cached matrix") return(inv) } data <- x$get() From 5ebb4cd10613287076e45afa4e7c6628ff6a9a59 Mon Sep 17 00:00:00 2001 From: AlexLeger Date: Thu, 19 Feb 2015 23:02:57 +0100 Subject: [PATCH 4/4] comments --- cachematrix.R | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 6261b595048..c7bb0e5ba80 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -32,13 +32,13 @@ makeCacheMatrix <- function(x = matrix()) { ## 3) If not, it computes the inverse of the matrix, stores it using the setinv function, then returns it. cacheSolve <- function(x, ...) { - inv <- x$getinv() - if(!is.null(inv)) { + inv <- x$getinv() # Retrieve the inverse that was "cached" in the x list passed as parameter + if(!is.null(inv)) { # If it isn't null, it means it has been computed before, so we can return the cached value message("getting cached matrix") return(inv) } data <- x$get() - inv <- solve(data, ...) - x$setinv(inv) + inv <- solve(data, ...) # Compute the inverse using the solve function + x$setinv(inv) inv }