File tree Expand file tree Collapse file tree 1 file changed +37
-7
lines changed Expand file tree Collapse file tree 1 file changed +37
-7
lines changed Original file line number Diff line number Diff line change 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.
5
3
4
+ # # makeCacheMatrix returns a list of functions to set and get the result of a
5
+ # # calculation perfomed on a given matrix
6
6
makeCacheMatrix <- function (x = matrix ()) {
7
+ s <- NULL
7
8
8
- }
9
+ # Function to set the data and clear the previous cache
10
+ set <- function (y ) {
11
+ x <<- y
12
+ s <<- NULL
13
+ }
9
14
15
+ # Function to get the data
16
+ get <- function () x
10
17
11
- # # Write a short comment describing this function
18
+ # Function to set the cache
19
+ setsolve <- function (solve ) s <<- solve
12
20
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
13
32
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
15
45
}
You can’t perform that action at this time.
0 commit comments