From ae5eefe9ebbfe461efe49f6cd0ec35f7cb6d1b0c Mon Sep 17 00:00:00 2001 From: Tristan Woerth Date: Sat, 26 Apr 2014 20:02:27 +0200 Subject: [PATCH 1/3] cache matrix inversion --- cachematrix.R | 53 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 7 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..821e3d5c53f 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,54 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function +## Special wrapper of a matrix in cases where we need cheaper repeat access to the inverse. +## Build the matrix wrapper makeCacheMatrix <- function(x = matrix()) { + ## by default we do not have an inverse. + inv <- NULL + ## Allow the caller to set a new matrix in place of the use used at initialisation. + set <- function( y ) { + x <<- y + ## make sure an eventual cached inverse is cleared + inv <<- NULL + } + + ## Return the matrix + get <- function() { + x + } + + ## cache the inverse + setinverse <- function( inverse ) { + inv <<- inverse + } + + ## return the inverse + getinverse <- function() { + inv + } } -## Write a short comment describing this function - +## Given an object returned by makeCacheMatrix, calculate the inverse and cache it for further calls. +## If a cached inverse has already been calculated on the wrapped matrix, the cached version is returned. cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' -} + + inv <- x$getinverse + + if( ! is.null( inv ) ) { + ## The wrapped matrix has a non NULL inverse + message( "cache hit" ) + return inv + } + + ## Calculate the inverse using solve + ## TODO: handle special cases more carefully, not all matrices can be inverted. + inv <- solve( x$get() ) + + ## Cache the result of solve() + x$setinverse( inv ) + + ## return the inverse + inv +} \ No newline at end of file From 9a6bab82f6781cd81566abb3637cb8c746137ff4 Mon Sep 17 00:00:00 2001 From: Tristan Woerth Date: Sat, 26 Apr 2014 20:03:33 +0200 Subject: [PATCH 2/3] better indentation --- cachematrix.R | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 821e3d5c53f..b6b1710a4c2 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -32,23 +32,23 @@ makeCacheMatrix <- function(x = matrix()) { ## Given an object returned by makeCacheMatrix, calculate the inverse and cache it for further calls. ## If a cached inverse has already been calculated on the wrapped matrix, the cached version is returned. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' - - inv <- x$getinverse - - if( ! is.null( inv ) ) { - ## The wrapped matrix has a non NULL inverse - message( "cache hit" ) - return inv - } - - ## Calculate the inverse using solve - ## TODO: handle special cases more carefully, not all matrices can be inverted. - inv <- solve( x$get() ) - - ## Cache the result of solve() - x$setinverse( inv ) - - ## return the inverse - inv + ## Return a matrix that is the inverse of 'x' + + inv <- x$getinverse + + if( ! is.null( inv ) ) { + ## The wrapped matrix has a non NULL inverse + message( "cache hit" ) + return inv + } + + ## Calculate the inverse using solve + ## TODO: handle special cases more carefully, not all matrices can be inverted. + inv <- solve( x$get() ) + + ## Cache the result of solve() + x$setinverse( inv ) + + ## return the inverse + inv } \ No newline at end of file From db9a4f922a6b26dd4581b8a7c3e140afa4960e79 Mon Sep 17 00:00:00 2001 From: Tristan Woerth Date: Sat, 26 Apr 2014 20:14:50 +0200 Subject: [PATCH 3/3] tested and fixed --- cachematrix.R | 105 +++++++++++++++++++++++++++++--------------------- 1 file changed, 62 insertions(+), 43 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index b6b1710a4c2..b4589ccf209 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -2,53 +2,72 @@ ## Build the matrix wrapper makeCacheMatrix <- function(x = matrix()) { - ## by default we do not have an inverse. - inv <- NULL - - ## Allow the caller to set a new matrix in place of the use used at initialisation. - set <- function( y ) { - x <<- y - ## make sure an eventual cached inverse is cleared - inv <<- NULL - } - - ## Return the matrix - get <- function() { - x - } - - ## cache the inverse - setinverse <- function( inverse ) { - inv <<- inverse - } - - ## return the inverse - getinverse <- function() { - inv - } + ## by default we do not have an inverse. + inv <- NULL + + ## Allow the caller to set a new matrix in place of the use used at initialisation. + set <- function( y ) { + x <<- y + ## make sure an eventual cached inverse is cleared + inv <<- NULL + } + + ## Return the matrix + get <- function() { + x + } + + ## cache the inverse + setinverse <- function( inverse ) { + inv <<- inverse + } + + ## return the inverse + getinverse <- function() { + inv + } + list( set = set, get = get, setinverse = setinverse, getinverse = getinverse ) } ## Given an object returned by makeCacheMatrix, calculate the inverse and cache it for further calls. ## If a cached inverse has already been calculated on the wrapped matrix, the cached version is returned. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' - - inv <- x$getinverse - - if( ! is.null( inv ) ) { - ## The wrapped matrix has a non NULL inverse - message( "cache hit" ) - return inv - } - - ## Calculate the inverse using solve - ## TODO: handle special cases more carefully, not all matrices can be inverted. - inv <- solve( x$get() ) - - ## Cache the result of solve() - x$setinverse( inv ) + ## Return a matrix that is the inverse of 'x' + + inv <- x$getinverse() + + if( ! is.null( inv ) ) { + ## The wrapped matrix has a non NULL inverse + message( "cache hit" ) + return( inv ) + } + + ## Calculate the inverse using solve + ## TODO: handle special cases more carefully, not all matrices can be inverted. + inv <- solve( x$get() ) + + ## Cache the result of solve() + x$setinverse( inv ) + + ## return the inverse + inv +} - ## return the inverse - inv -} \ No newline at end of file +## Tested using: +## +## > m <- matrix(rnorm( 9 ), 3, 3) +## > +## > +## > cm <- makeCacheMatrix( m ) +## > cacheSolve( cm ) +## [,1] [,2] [,3] +## [1,] 2.371877 0.8502974 2.338579 +## [2,] 0.960316 -1.2965751 1.011746 +## [3,] -3.353513 -1.6878236 -5.258276 +## > cacheSolve( cm ) +## cache hit +## [,1] [,2] [,3] +## [1,] 2.371877 0.8502974 2.338579 +## [2,] 0.960316 -1.2965751 1.011746 +## [3,] -3.353513 -1.6878236 -5.258276 \ No newline at end of file