From a3c410891325ab3a04d121f14fded89fcd2a2980 Mon Sep 17 00:00:00 2001 From: Leonard Studer Date: Wed, 21 Jan 2015 19:18:12 +0100 Subject: [PATCH 1/2] initial commit of cachematrix.R --- cachematrix.R | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..86de4474fca 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,60 @@ ## Put comments here that give an overall description of what your ## functions do +# Usage: +# > M <- matrix(c(1,2,3,4),2,2) +# > NN <- makeCacheMatrix() +# > NN$set(M) +# > NN$get() +# [,1] [,2] +# [1,] 1 3 +# [2,] 2 4 +# > NN$getinverse() +# NULL +# > cacheSolve(NN) +# [,1] [,2] +# [1,] -2 1.5 +# [2,] 1 -0.5 +# > NN$getinverse() +# [,1] [,2] +# [1,] -2 1.5 +# [2,] 1 -0.5 +# > cacheSolve(NN) +# getting cached data +# [,1] [,2] +# [1,] -2 1.5 +# [2,] 1 -0.5 + ## Write a short comment describing this function makeCacheMatrix <- function(x = matrix()) { + inverse_matrix <- NULL + set <- function(y) { + a_matrix <<- y + inverse_matrix <<- NULL + } + get <- function() a_matrix + setinverse <- function(the_inverse_matrix) inverse_matrix <<- the_inverse_matrix + getinverse <- function() inverse_matrix + list(set = set, get = get, + setinverse = setinverse, + getinverse = getinverse) } ## Write a short comment describing this function cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' -} + ## Return a matrix that is the inverse of 'x' + ## to be precise, 'x' is a composite object built up by a call to makeCacheMatrix() + inverse_matrix <- x$getinverse() + if(!is.null(inverse_matrix)) { + message("getting cached data") + return(inverse_matrix) + } + the_matrix_elements <- x$get() + inverse_matrix <- solve(the_matrix_elements, ...) + x$setinverse(inverse_matrix) + inverse_matrix +} \ No newline at end of file From 2c9dd6245f1c6da7ee951ef1faf55af56a778002 Mon Sep 17 00:00:00 2001 From: Leonard Studer Date: Sun, 25 Jan 2015 11:48:19 +0100 Subject: [PATCH 2/2] commented cachematrix.R --- cachematrix.R | 71 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 45 insertions(+), 26 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 86de4474fca..c4a87a385f3 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,60 +1,79 @@ ## Put comments here that give an overall description of what your ## functions do +## makeCacheMatrix and cacheSolve is a pair of functions that could save +## to compute again the inverse of a matrix which inverse has already been computed. +## Computing an inverse of a matrix can be time consuming. Hence it ia advantageous +## to save the result. +## The function makeCacheMatrix stores and retrives the matrix and its inverse. +## The function cacheSolve computes the inverse of the matrix. +## +## # Usage: -# > M <- matrix(c(1,2,3,4),2,2) -# > NN <- makeCacheMatrix() -# > NN$set(M) -# > NN$get() +# > M <- matrix(c(1,2,3,4),2,2) # Create a matrix +# > NN <- makeCacheMatrix() # Create an object where to store the matrix and its inverse +# > NN$set(M) # Store the matrix in the object +# > NN$get() # Retrieve the matrix # [,1] [,2] # [1,] 1 3 # [2,] 2 4 -# > NN$getinverse() -# NULL -# > cacheSolve(NN) -# [,1] [,2] +# > NN$getinverse() # Retrieving the inverse of a matrix not yet stored +# NULL # returns a NULL +# > cacheSolve(NN) # Compute the inverse of the matrix, +# [,1] [,2] # it automatically sotores it in the object # [1,] -2 1.5 # [2,] 1 -0.5 -# > NN$getinverse() -# [,1] [,2] +# > NN$getinverse() # Retrieving the inverse matrix form the object +# [,1] [,2] # now returns something meaningfull # [1,] -2 1.5 # [2,] 1 -0.5 -# > cacheSolve(NN) -# getting cached data -# [,1] [,2] +# > cacheSolve(NN) # when computing, for the second time, the inverse of the +# getting cached data # same matrix, we get it straight form the cache. +# [,1] [,2] # [1,] -2 1.5 # [2,] 1 -0.5 -## Write a short comment describing this function - +## The function makeCacheMatrix stores and retrieves the matrix and its inverse makeCacheMatrix <- function(x = matrix()) { inverse_matrix <- NULL + set <- function(y) { + # the makeCacheMatrix$set sets a matrix in cache a_matrix <<- y inverse_matrix <<- NULL } + get <- function() a_matrix + # the makeCacheMatrix$get function returns a matrix from cache + setinverse <- function(the_inverse_matrix) inverse_matrix <<- the_inverse_matrix + # the makeCacheMatrix$setinverse sets in cache the inverse of a matrix + getinverse <- function() inverse_matrix + # the makeCacheMatrix$getinverse returns from cache the inverse of a matrix + + # the makeCacheMatrix function returns a list of 4 (sub)functions list(set = set, get = get, setinverse = setinverse, getinverse = getinverse) } -## Write a short comment describing this function - +## The function cacheSolve computes the inverse of the matrix. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' - ## to be precise, 'x' is a composite object built up by a call to makeCacheMatrix() - inverse_matrix <- x$getinverse() - if(!is.null(inverse_matrix)) { + # Return a matrix that is the inverse of 'x' + # 'x' is a composite object built up by a call to makeCacheMatrix() + + inverse_matrix <- x$getinverse() # try to get the inverse of the matrix + if(!is.null(inverse_matrix)) { # the inverse of the matrix is known and stored... message("getting cached data") - return(inverse_matrix) + return(inverse_matrix) # returned it ! } - the_matrix_elements <- x$get() - inverse_matrix <- solve(the_matrix_elements, ...) - x$setinverse(inverse_matrix) - inverse_matrix + + # if no inverse matrix is stored in x + the_matrix_elements <- x$get() # get the matrix elements + inverse_matrix <- solve(the_matrix_elements, ...) # compute its inverse + x$setinverse(inverse_matrix) # stores this computed inverse matrix + inverse_matrix # and returned it } \ No newline at end of file