From 07026f8fd75995178dcd9d9d2cbe2883cc827946 Mon Sep 17 00:00:00 2001 From: domangi Date: Sat, 20 Dec 2014 14:20:16 +0100 Subject: [PATCH 1/2] added function body to makeCacheMatrix and cacheSolve --- cachematrix.R | 47 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..a1cc04b3574 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,48 @@ -## Put comments here that give an overall description of what your -## functions do +## makeCacheMatrix(X = matrix()) returns a list of functions that can be uses to set or get the matrix and to set and get the cached matrix inverse +## if X is passed as parameter, then the matrix is initialized with it, else an empty matrix is used +## cacheSolve(x) returns the inverse of the matrix actually setted on x +## if inverse cached, then just returns cached value, else calculates and caches the inverse first +## x is the list of functions return by makeCacheMatrix -## Write a short comment describing this function +## params +## -> (optional) x should be a squared and invertible matrix. Sets the actual matrix. +## +## returns a list of functions +## -> set(y): sets the matrix to y. Resets inverse cache. +## -> get: returns the actual matrix +## -> setinverse(inverse): caches the inverse of the matrix +## -> getinverse: returns the cached matrix inverse makeCacheMatrix <- function(x = matrix()) { - + i <- NULL + set <- function(y) { + x <<- y + i <<- NULL + } + get <- function() x + setinverse <- function(inverse) i <<- inverse + getinverse <- function() i + list(set = set, get = get, + setinverse = setinverse, + getinverse = getinverse) } -## Write a short comment describing this function - +## params +## -> x is the list of functions returned by makeCacheMatrix +## -> ... are arbitrary parameters that could be passed to "solve" function +## +## returns the inverse of the matrix. +## If inverse was already calculated, then it returns the cached copy, and prints a notice message "getting cached data" cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' -} + i <- x$getinverse() + if(!is.null(i)) { + message("getting cached data") + return(i) + } + data <- x$get() + i <- solve(data, ...) + x$setinverse(i) + i +} \ No newline at end of file From ed27278f57964fbb553c818f1d81e501a23ee7cf Mon Sep 17 00:00:00 2001 From: domangi Date: Sat, 20 Dec 2014 14:52:30 +0100 Subject: [PATCH 2/2] added test code to comments --- cachematrix.R | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/cachematrix.R b/cachematrix.R index a1cc04b3574..4c100ef83b1 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -3,6 +3,28 @@ ## cacheSolve(x) returns the inverse of the matrix actually setted on x ## if inverse cached, then just returns cached value, else calculates and caches the inverse first ## x is the list of functions return by makeCacheMatrix +## +## Test Code +## 1) create a 2x2 invertible matrix +## > A <- matrix(c(1,2,2,3), 2, 2) +## [,1] [,2] +## [1,] 1 2 +## [2,] 2 3 +## +## 2) call makeCacheMatrix to get the special matrix +## > X <- makeCacheMatrix(A) +## +## 3) call cacheSolve to get the inverse of the matrix +## > inverse <- cacheSolve(X) +## [,1] [,2] +## [1,] -3 2 +## [2,] 2 -1 +## +## 4) Test that the inverse is correct: A * inverse = I +## > A %*% inverse +## +## 5) Call cacheSolve again should print the message "getting cached data" + ## params @@ -13,6 +35,9 @@ ## -> get: returns the actual matrix ## -> setinverse(inverse): caches the inverse of the matrix ## -> getinverse: returns the cached matrix inverse +## +## example +## > X <- makeCacheMatrix(matrix(c(1,2,2,3), 2, 2)) makeCacheMatrix <- function(x = matrix()) { i <- NULL set <- function(y) { @@ -34,6 +59,11 @@ makeCacheMatrix <- function(x = matrix()) { ## ## returns the inverse of the matrix. ## If inverse was already calculated, then it returns the cached copy, and prints a notice message "getting cached data" +## +## example +## > X <- makeCacheMatrix(matrix(c(1,2,2,3), 2, 2)) +## > inverse <- cacheSolve(X) +## > matrix(c(1,2,2,3), 2, 2) %*% inverse # should return the 2x2 identity matrix cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' i <- x$getinverse()