From 5388602095d69e5f04e6d4f5e4d24aa42cfe1b19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Michel?= Date: Sat, 19 Jul 2014 15:23:05 +0200 Subject: [PATCH 1/2] Update cachematrix.R programming assignment 2 --- cachematrix.R | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..dd8626059f5 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,36 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function +## Creates a special "matrix" object that can cache its inverse. +## x : the matrix to encapsulate +## set(y) : set the matrix +## get : get the matrix +## setinverse(inverse) : set the inverse of the current matrix +## getinverse : retrieve the cached inverse matrix or NULL if no inverse matrix was set makeCacheMatrix <- function(x = matrix()) { - + inv <- NULL + set <- function(y) { + x <<- y + inv <<- NULL + } + get <- function() x + setinverse <- function(inverse) inv <<- inverse + getinverse <- function() inv + list(set = set, get = get, + setinverse = setinverse, + getinverse = getinverse) } -## Write a short comment describing this function +## Compute the inverse matrix of the CacheMatrix x. +## This function returns the cached inverse matrix of x if it exists. +## If not it computes the inverse matrix of the encapsulated matrix of x and caches the result cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + ## Return a matrix that is the inverse of 'x' + inv <- x$getinverse() + if (!is.null(inv)) { + return(inv) + } + inv <- solve(x$get()) + x$setinverse(inv) + inv } From bcd1055b38a426ab9fef8d8e89f97c95a20542bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Michel?= Date: Fri, 25 Jul 2014 02:25:56 +0200 Subject: [PATCH 2/2] Update cachematrix.R pass arguments to solve --- cachematrix.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cachematrix.R b/cachematrix.R index dd8626059f5..76bd25bc938 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -30,7 +30,7 @@ cacheSolve <- function(x, ...) { if (!is.null(inv)) { return(inv) } - inv <- solve(x$get()) + inv <- solve(x$get(), ...) x$setinverse(inv) inv }