Skip to content

Commit 998c583

Browse files
author
Yvan Wibaux
committed
assignement inverse matrix cached
1 parent 7f657dd commit 998c583

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

cachematrix.R

Lines changed: 28 additions & 6 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
1+
## Matrix inversion is usually a costly computation and there may be some benefit
2+
## to caching the inverse of a matrix rather than compute it repeatedly
33

4-
## Write a short comment describing this function
4+
## This function creates a special "matrix" object that can cache its inverse.
55

66
makeCacheMatrix <- function(x = matrix()) {
7-
7+
inverse <- NULL
8+
set <- function(y) {
9+
x <<- y
10+
inverse <<- NULL
11+
}
12+
get <- function() x
13+
setInverse <- function(i) inverse <<- i
14+
getInverse <- function() inverse
15+
list(set = set, get = get,
16+
setInverse = setInverse,
17+
getInverse = getInverse)
818
}
919

1020

11-
## Write a short comment describing this function
21+
## This function computes the inverse of the special "matrix"
22+
## returned by makeCacheMatrix above.
23+
## If the inverse has already been calculated (and the matrix has not changed),
24+
## then the cachesolve should retrieve the inverse from the cache.
1225

1326
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
27+
## Return a matrix that is the inverse of 'x'
28+
inverse <- x$getInverse()
29+
if(!is.null(inverse)) {
30+
message("getting cached data")
31+
return(inverse)
32+
}
33+
data <- x$get()
34+
inverse <- solve(x$get(), ...)
35+
x$setInverse(inverse)
36+
inverse
1537
}

0 commit comments

Comments
 (0)