Skip to content

Commit c376d10

Browse files
authored
Update cachematrix.R
1 parent 7f657dd commit c376d10

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

cachematrix.R

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,39 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
# Excuse me for my English. I'm french student!!!
32

4-
## Write a short comment describing this function
3+
# Matrix inversion is expensive in computational time. For better performance,
4+
# the inverse of the matrix is cached (no need to recalculate it each time)
5+
# The finctions makeCacheMatrix.R and cacheResolve.R are used to do this
56

6-
makeCacheMatrix <- function(x = matrix()) {
7+
# set matrix value
8+
# get matrix value
9+
# set inverse matrix value
10+
# get inverse matrix value
711

12+
makeCacheMatrix <- function(x = matrix()) {
13+
xInv <- NULL
14+
fixer <- function(y) {
15+
x <<- y
16+
xInv <<- NULL
17+
}
18+
obtenir <- function() x
19+
fixerInverse <- function(inverse) xInv <<- inverse
20+
obtenirInverse <- function() xInv
21+
list(fixer=fixer, obtenir=obtenir, fixerInverse=fixerInverse, obtenirInverse=obtenirInverse)
822
}
923

1024

11-
## Write a short comment describing this function
12-
25+
# cacheSolve.R return matrix inverse value
26+
# It first checks if the inverse of the matrix has already been computed and is in the
27+
# cache. If so, it retrieves the value and skips the calculation.
28+
# Otherwise, it calculates the inverse of the matrix and stores the value in the cache.
1329
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
30+
inv <- x$obtenirInverse()
31+
if(!is.null(inv)) {
32+
message("Obtenir les donnees en cache.")
33+
return(inv)
34+
}
35+
donnee <- x$obtenir()
36+
inv <- solve(donnee)
37+
x$fixerInverse(inv)
38+
inv
1539
}

0 commit comments

Comments
 (0)