diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..d97c6d15dc1 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,38 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function - +## this function creates a "matirx" that can cache its inverse. makeCacheMatrix <- function(x = matrix()) { - + s <- NULL + ## setter funcdtion for matrix + set <- function(y) { + x <<- y + s <<- NULL + } + ## getter funcdtion for matrix + get <- function() x + ## setter function for the inverse of a matrix + ## by using this function, cacing the inverse of a matrix + setsolve <- function(solve) s <<- solve + ## return cached the inverse of a matrix + getsolve <- function() s + ## make and return CacheMatrix List Object + list(set = set, get = get, + setsolve = setsolve, + getsolve = getsolve) } -## Write a short comment describing this function - +## this function computes the inverse of "matrix". +## if the inverse has already been calculated, this function return the +## inverse the cache. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' -} + + s <- x$getsolve() + if(!is.null(s)) { + message("getting cached data") + return(s) + } + data <- x$get() + s <- solve(data, ...) + x$setsolve(s) + ## Return a matrix that is the inverse of 'x' + s +} \ No newline at end of file