diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..6d0edfc667e 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,37 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function - -makeCacheMatrix <- function(x = matrix()) { - -} - - -## Write a short comment describing this function - -cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' -} +## The solution implements an approach to speedup such time-consuming operation +## as matrix inverse (using "solve") +## Assumption: input matrix is invertible + +## Function makeCacheMatrix creates a special "matrix" object that can cache +## its inverse. + +makeCacheMatrix <- function(x = matrix()) { + mi <- NULL + set <- function(y) { + x <<- y + mi <<- NULL + } + get <- function() x + setInverse <- function(inversed) mi <<- inversed + getInverse <- function() mi + list(set = set, get = get, + setInverse = setInverse, + getInverse = getInverse) +} + + +## Function "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 cacheSolve retrieves the inverse from the cache. + +cacheSolve <- function(x, ...) { + mi <- x$getInverse() + if(!is.null(mi)) { + message("getting cached data") + return(mi) + } + data <- x$get() + mi <- solve(data, ...) + x$setInverse(mi) + mi +}