diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..69893d1b6ff 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,51 @@ -## Put comments here that give an overall description of what your -## functions do +## Programming Assignment 2: Lexical Scoping +## +## In this assignment we use the <<- operator which can be used to assign +## a value to an object in an environment that is different from the current +## environment. makeCacheMatrix and cacheSolve are two functions that +## create a special object that stores a numeric matrix +## and cache's its inverse. -## Write a short comment describing this function - -makeCacheMatrix <- function(x = matrix()) { +## makeCacheMatrix: creates a special "matrix" object +## that can cache its inverse. +makeCacheMatrix <- function(m = matrix()) { + m_inv <- NULL + set <- function(y) { + m <<- y + m_inv <<- NULL # Clear the inverse when the matrix changes. + } + get <- function() m + setinverse <- function(y) m_inv<<- y + getinverse <- function() m_inv + list(set = set, + get = get, + setinverse = setinverse, + getinverse = getinverse) } +## cacheSolve: computes the inverse of the special "matrix" +## returned by makeCacheMatrix. If the inverse has already +## been calculated (and the matrix has not changed), then +## return the cached inverse. Otherwise solve for the inverse, +## cache the solution, and return the result. +## +## For this assignment we assume that the matrix supplied is +## always invertible. -## Write a short comment describing this function - -cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' +cacheSolve <- function(m, ...) { + ## Return a matrix that is the inverse of m + m_i <- m$getinverse() + + ## If m_i is not null we have a cached solution. + if(!is.null(m_i)) { + message("Using cached result.") + return(m_i) + } + + # Otherwise m_i is null and needs to be solved. + data <- m$get() + m_i <- solve(data, ...) + m$setinverse(m_i) + m_i }