|
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!!! |
3 | 2 |
|
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 |
5 | 6 |
|
6 | | -makeCacheMatrix <- function(x = matrix()) { |
| 7 | +# set matrix value |
| 8 | +# get matrix value |
| 9 | +# set inverse matrix value |
| 10 | +# get inverse matrix value |
7 | 11 |
|
| 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) |
8 | 22 | } |
9 | 23 |
|
10 | 24 |
|
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. |
13 | 29 | 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 |
15 | 39 | } |
0 commit comments