Skip to content

Commit a3edf85

Browse files
author
Ankur Chauhan
committed
Implement cacheSolve and makeCacheMatrix
1 parent 7f657dd commit a3edf85

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

cachematrix.R

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,39 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
3-
4-
## Write a short comment describing this function
1+
# The functions in this file are inteded to produce a special "matrix" that has
2+
# the ability to cache it's response. (if the matrix is invertible).
3+
#
4+
# 'makeCacheMatrix' - given a regular matrix, produces a special cache-enabled
5+
# matrix version (as a list with get, set, get)
6+
# 'cacheSolve' - computes the inverse (with caching)
57

8+
## This function creates a special "matrix" object that can cache its inverse.
69
makeCacheMatrix <- function(x = matrix()) {
10+
s <- NULL
711

8-
}
12+
get <- function() x
13+
set <- function(y) {
14+
x <<- y
15+
s <<- NULL
16+
}
917

18+
getsolve <- function() s
19+
setsolve <- function(solve) s <<- solve
20+
21+
list(set=set, get=get, setsolve=setsolve, getsolve=getsolve)
22+
}
1023

11-
## Write a short comment describing this function
1224

25+
# This function computes the inverse of the special "matrix" returned by
26+
# `makeCacheMatrix` above. If the inverse has already been calculated (and
27+
# the matrix has not changed), then `cacheSolve` should retrieve the inverse
28+
# from the cache.
1329
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
30+
m <- x$getsolve()
31+
if(!is.null(m)) {
32+
message("getting cached data")
33+
return(m)
34+
}
35+
data <- x$get()
36+
m <- solve(data, ...)
37+
x$setsolve(m)
38+
m
1539
}

0 commit comments

Comments
 (0)