Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 78b020d

Browse files
committed
Implementation of inverse matrix caching
1 parent 7f657dd commit 78b020d

File tree

1 file changed

+40
-7
lines changed

1 file changed

+40
-7
lines changed

cachematrix.R

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,48 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## Matrix inversion is usually a costly computation and there
2+
## may be some benefit to caching the inverse of a matrix rather
3+
## than compute it repeatedly. These functions compute and cache
4+
## the matrix inverse.
5+
##
6+
## These functions assume that the matrix supplied is invertible.
37

4-
## Write a short comment describing this function
58

9+
## makeCacheMatrix:
10+
## This function creates a special "matrix" with functions to
11+
## get, set, getsolve, and setsolve.
12+
##
13+
## 'x' is a matrix that is wrapped with additional functionality.
14+
##
15+
## Return a "special" matrix that is able to cache its inverse.
616
makeCacheMatrix <- function(x = matrix()) {
7-
17+
m <- NULL
18+
set <- function(y) {
19+
x <<- y
20+
m <<- NULL
21+
}
22+
get <- function() x
23+
setsolve <- function(solve) m <<- solve
24+
getsolve <- function() m
25+
list(set = set, get = get,
26+
setsolve = setsolve,
27+
getsolve = getsolve)
828
}
929

1030

11-
## Write a short comment describing this function
12-
31+
## cacheSolve:
32+
## This function computes the inverse of the special "matrix"
33+
## returned by makeCacheMatrix above.
34+
##
35+
## 'x' is a special "matrix" created by calling makeCacheMatrix
36+
##
37+
## Return the inverse of the special "matrix"
1338
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
39+
m <- x$getsolve()
40+
if(!is.null(m)) {
41+
message("getting cached data")
42+
return(m)
43+
}
44+
data <- x$get()
45+
m <- solve(data, ...)
46+
x$setsolve(m)
47+
m
1548
}

0 commit comments

Comments
 (0)