Skip to content

Commit 65c9bac

Browse files
committed
Implemented cacheSolve
1 parent 7f657dd commit 65c9bac

File tree

1 file changed

+37
-15
lines changed

1 file changed

+37
-15
lines changed

cachematrix.R

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,37 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
3-
4-
## Write a short comment describing this function
5-
6-
makeCacheMatrix <- function(x = matrix()) {
7-
8-
}
9-
10-
11-
## Write a short comment describing this function
12-
13-
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
15-
}
1+
## The solution implements an approach to speedup such time-consuming operation
2+
## as matrix inverse (using "solve")
3+
## Assumption: input matrix is invertible
4+
5+
## Function makeCacheMatrix creates a special "matrix" object that can cache
6+
## its inverse.
7+
8+
makeCacheMatrix <- function(x = matrix()) {
9+
mi <- NULL
10+
set <- function(y) {
11+
x <<- y
12+
mi <<- NULL
13+
}
14+
get <- function() x
15+
setInverse <- function(inversed) mi <<- inversed
16+
getInverse <- function() mi
17+
list(set = set, get = get,
18+
setInverse = setInverse,
19+
getInverse = getInverse)
20+
}
21+
22+
23+
## Function "cacheSolve" computes the inverse of the special "matrix" returned by
24+
## "makeCacheMatrix". If the inverse has already been calculated and the matrix
25+
## has not changed, then cacheSolve retrieves the inverse from the cache.
26+
27+
cacheSolve <- function(x, ...) {
28+
mi <- x$getInverse()
29+
if(!is.null(mi)) {
30+
message("getting cached data")
31+
return(mi)
32+
}
33+
data <- x$get()
34+
mi <- solve(data, ...)
35+
x$setInverse(mi)
36+
mi
37+
}

0 commit comments

Comments
 (0)