Skip to content

Commit 7be1629

Browse files
committed
Complete assignement
1 parent 7f657dd commit 7be1629

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

cachematrix.R

Lines changed: 27 additions & 5 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+
## These functions enable the caching of the invert of a matrix by using getters and setters by function
2+
## on a matrix capsule (caching the computed invert matrix)
33

4-
## Write a short comment describing this function
4+
## This function create a cacheable matrix with the default empty matrix
5+
## and return a list of function to modify the matrix or the cached invert of the matrix
56

67
makeCacheMatrix <- function(x = matrix()) {
7-
8+
m <- NULL
9+
set <- function(y) {
10+
x <<- y
11+
m <<- NULL
12+
}
13+
get <- function() x
14+
setinv <- function(inv) m <<- inv
15+
getinv <- function() m
16+
list(set = set, get = get,
17+
setinv = setinv,
18+
getinv = getinv)
819
}
920

1021

11-
## Write a short comment describing this function
22+
## This function solves the invert of the 'x' cacheable matrix. 'x' must have been created
23+
## with makeCacheMatrix() and set using x$set and returns a matrix that is the inverse of 'x'
24+
## the '...' allow to pass others arguments to the solve method
1225

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

0 commit comments

Comments
 (0)