Skip to content

Commit bb324ac

Browse files
committed
Implement cachematrix.R
1 parent 7f657dd commit bb324ac

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

cachematrix.R

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,45 @@
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+
## These functions will allow users to cache the result of calculating the
2+
## inverse of a matrix.
53

4+
## makeCacheMatrix returns a list of functions to set and get the result of a
5+
## calculation perfomed on a given matrix
66
makeCacheMatrix <- function(x = matrix()) {
7+
s <- NULL
78

8-
}
9+
# Function to set the data and clear the previous cache
10+
set <- function(y) {
11+
x <<- y
12+
s <<- NULL
13+
}
914

15+
# Function to get the data
16+
get <- function() x
1017

11-
## Write a short comment describing this function
18+
# Function to set the cache
19+
setsolve <- function(solve) s <<- solve
1220

21+
# Function to get the cache
22+
getsolve <- function() s
23+
24+
list(set = set, get = get,
25+
setsolve = setsolve,
26+
getsolve = getsolve)
27+
}
28+
29+
30+
## cacheSolve calculates the matrix's inverse (if it hasn't been already
31+
## calculated) and caches the result
1332
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
33+
# Retrieve calculation from cache and return it if present
34+
s <- x$getsolve()
35+
if(!is.null(s)) {
36+
return(s)
37+
}
38+
39+
# Perform calculation on data, store the result in the cache for later use
40+
# and return the result
41+
data <- x$get()
42+
s <- solve(data, ...)
43+
x$setsolve(s)
44+
s
1545
}

0 commit comments

Comments
 (0)