diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..86a8a6b80b2 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,31 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function +## The matrix inversion is usualy a costly operation. +## The set of functions below intended to cache matrix inverse +## to save computational time on inverse calculartion. +## This function creates a special "matrix" object that can cache its inverse. makeCacheMatrix <- function(x = matrix()) { - + i <- NULL + set <- function(y) { + x <<- y + i <<- NULL + } + get <- function() x + setinv <- function(inverse) i <<- inverse + getinv <- function() i + list(set = set, get = get, setinv = setinv, getinv = getinv) } -## Write a short comment describing this function - +## This function computes the inverse of the special "matrix" returned by makeCacheMatrix above. +## If the inverse has already been calculated, then the cachesolve should retrieve the inverse from the cache cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + ## Return a matrix that is the inverse of 'x' + i <- x$getinv() + if (!is.null(i)) { + return(i) + } + data <- x$get() + inverse <- solve(data, ...) + x$setinv(inverse) + inverse }