File tree Expand file tree Collapse file tree 1 file changed +15
-13
lines changed Expand file tree Collapse file tree 1 file changed +15
-13
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
+ # # Functions for manipulating matrices that allow for their inverses to be
2
+ # # cached, saving computation time if the inverse of a matrix is queried more
3
+ # # often than the matrix is modified. The makeCacheMatrix function builds such a
4
+ # # matrix and the cacheSolve function returns its inverse.
5
5
6
+ # # Converts a standard matrix x to a matrix that allows for its inverse to be
7
+ # # cached. The original matrix x is unmodified, the converted matrix is returned
8
+ # # by the function.
6
9
makeCacheMatrix <- function (x = matrix ()) {
7
10
m <- NULL
8
11
set <- function (y ) {
9
12
x <<- y
10
13
m <<- NULL
11
14
}
12
15
get <- function () x
13
- setmean <- function (mean ) m <<- mean
14
- getmean <- function () m
16
+ setinverse <- function (inverse ) m <<- inverse
17
+ getinverse <- function () m
15
18
list (set = set , get = get ,
16
- setmean = setmean ,
17
- getmean = getmean )
19
+ setinverse = setinverse ,
20
+ getinverse = getinverse )
18
21
}
19
22
20
-
21
- # # Write a short comment describing this function
22
-
23
+ # # Takes a CacheMatrix x and returns its inverse. If the inverse is cached, no
24
+ # # recomputation will be required.
23
25
cacheSolve <- function (x , ... ) {
24
26
# # Return a matrix that is the inverse of 'x'
25
- m <- x $ getmean ()
27
+ m <- x $ getinverse ()
26
28
if (! is.null(m )) {
27
29
message(" getting cached data" )
28
30
return (m )
29
31
}
30
32
data <- x $ get()
31
33
m <- solve(data , ... )
32
- x $ setmean (m )
34
+ x $ setinverse (m )
33
35
m
34
36
}
You can’t perform that action at this time.
0 commit comments